USE game;

-- 后台权限管理增量表设计
-- 兼容现有 admin 表，不重建 admin，仅通过 admin.id 建立关联。
-- 保留 admin.group / admin.role 旧字段，便于平滑迁移；新权限体系以 RBAC 关系表为准。
CREATE TABLE IF NOT EXISTS `admin` (
    `id` int NOT NULL AUTO_INCREMENT,
    `username` varchar(100) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci DEFAULT NULL COMMENT '用户名',
    `password` varchar(100) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci DEFAULT NULL COMMENT '密码',
    `real_name` varchar(100) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci DEFAULT NULL COMMENT '真实姓名',
    `nickname` varchar(100) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci DEFAULT NULL COMMENT '显示名称',
    `phone` varchar(100) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci DEFAULT NULL COMMENT '手机号',
    `email` varchar(100) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci DEFAULT NULL COMMENT '邮箱',
    `token` varchar(100) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci DEFAULT NULL COMMENT '令牌',
    `group` varchar(100) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci DEFAULT NULL COMMENT '旧用户组字段，兼容历史逻辑',
    `status` tinyint NOT NULL DEFAULT 1 COMMENT '状态：1启用 0停用',
    `expires` int DEFAULT 0 COMMENT '过期时间',
    `role` int DEFAULT NULL COMMENT '旧角色字段，兼容历史逻辑',
    `remark` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci DEFAULT NULL COMMENT '备注',
    `lastLoginTime` int NOT NULL DEFAULT 0 COMMENT '最近登录时间',
    `lastLoginIp` varchar(64) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci DEFAULT NULL COMMENT '最近登录IP',
    `createTime` int NOT NULL DEFAULT 0 COMMENT '创建时间',
    `updateTime` int NOT NULL DEFAULT 0 COMMENT '更新时间',
    PRIMARY KEY (`id`),
    UNIQUE KEY `user_unique` (`username`),
    KEY `idx_admin_status` (`status`),
    KEY `idx_admin_phone` (`phone`),
    KEY `idx_admin_email` (`email`)
) ENGINE = InnoDB AUTO_INCREMENT = 16 DEFAULT CHARSET = utf8mb4;

-- 对已有 admin 表做增量补齐，避免与现网库冲突。
ALTER TABLE `admin`
    ADD COLUMN IF NOT EXISTS `real_name` varchar(100) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci DEFAULT NULL COMMENT '真实姓名' AFTER `password`,
    ADD COLUMN IF NOT EXISTS `nickname` varchar(100) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci DEFAULT NULL COMMENT '显示名称' AFTER `real_name`,
    ADD COLUMN IF NOT EXISTS `remark` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci DEFAULT NULL COMMENT '备注' AFTER `role`,
    ADD COLUMN IF NOT EXISTS `lastLoginTime` int NOT NULL DEFAULT 0 COMMENT '最近登录时间' AFTER `remark`,
    ADD COLUMN IF NOT EXISTS `lastLoginIp` varchar(64) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci DEFAULT NULL COMMENT '最近登录IP' AFTER `lastLoginTime`;

CREATE TABLE IF NOT EXISTS `admin_user_group` (
    `id` int NOT NULL AUTO_INCREMENT,
    `group_code` varchar(64) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NOT NULL COMMENT '用户组编码，例如 G_OP',
    `group_name` varchar(100) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NOT NULL COMMENT '用户组名称',
    `status` tinyint NOT NULL DEFAULT 1 COMMENT '状态：1启用 0停用',
    `remark` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci DEFAULT NULL COMMENT '备注',
    `create_admin` varchar(100) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci DEFAULT NULL COMMENT '创建人',
    `update_admin` varchar(100) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci DEFAULT NULL COMMENT '更新人',
    `createTime` int NOT NULL DEFAULT 0 COMMENT '创建时间，秒级时间戳',
    `updateTime` int NOT NULL DEFAULT 0 COMMENT '更新时间，秒级时间戳',
    PRIMARY KEY (`id`),
    UNIQUE KEY `uk_admin_user_group_code` (`group_code`),
    KEY `idx_admin_user_group_status` (`status`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='后台用户组表';

CREATE TABLE IF NOT EXISTS `admin_role` (
    `id` int NOT NULL AUTO_INCREMENT,
    `role_code` varchar(64) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NOT NULL COMMENT '角色编码，例如 R_SUPER',
    `role_name` varchar(100) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NOT NULL COMMENT '角色名称',
    `status` tinyint NOT NULL DEFAULT 1 COMMENT '状态：1启用 0停用',
    `is_system` tinyint NOT NULL DEFAULT 0 COMMENT '是否系统内置：1是 0否',
    `remark` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci DEFAULT NULL COMMENT '备注',
    `create_admin` varchar(100) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci DEFAULT NULL COMMENT '创建人',
    `update_admin` varchar(100) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci DEFAULT NULL COMMENT '更新人',
    `createTime` int NOT NULL DEFAULT 0 COMMENT '创建时间，秒级时间戳',
    `updateTime` int NOT NULL DEFAULT 0 COMMENT '更新时间，秒级时间戳',
    PRIMARY KEY (`id`),
    UNIQUE KEY `uk_admin_role_code` (`role_code`),
    KEY `idx_admin_role_status` (`status`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='后台角色表';

CREATE TABLE IF NOT EXISTS `admin_permission` (
    `id` int NOT NULL AUTO_INCREMENT,
    `permission_code` varchar(32) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NOT NULL COMMENT '单点权限编码，例如 AC0004',
    `permission_name` varchar(100) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NOT NULL COMMENT '权限名称',
    `permission_group` varchar(64) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci DEFAULT NULL COMMENT '权限分组，例如 activity/config/user',
    `api_path` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci DEFAULT NULL COMMENT '接口路径，例如 /api/activity/edit',
    `http_method` varchar(16) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci DEFAULT NULL COMMENT '请求方法，例如 GET/POST/PUT/DELETE',
    `status` tinyint NOT NULL DEFAULT 1 COMMENT '状态：1启用 0停用',
    `remark` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci DEFAULT NULL COMMENT '备注',
    `create_admin` varchar(100) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci DEFAULT NULL COMMENT '创建人',
    `update_admin` varchar(100) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci DEFAULT NULL COMMENT '更新人',
    `createTime` int NOT NULL DEFAULT 0 COMMENT '创建时间，秒级时间戳',
    `updateTime` int NOT NULL DEFAULT 0 COMMENT '更新时间，秒级时间戳',
    PRIMARY KEY (`id`),
    UNIQUE KEY `uk_admin_permission_code` (`permission_code`),
    KEY `idx_admin_permission_group` (`permission_group`),
    KEY `idx_admin_permission_path_method` (`api_path`, `http_method`),
    KEY `idx_admin_permission_status` (`status`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='后台单点权限表';

CREATE TABLE IF NOT EXISTS `admin_user_group_rel` (
    `id` int NOT NULL AUTO_INCREMENT,
    `admin_id` int NOT NULL COMMENT 'admin.id',
    `group_id` int NOT NULL COMMENT 'admin_user_group.id',
    `create_admin` varchar(100) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci DEFAULT NULL COMMENT '创建人',
    `createTime` int NOT NULL DEFAULT 0 COMMENT '创建时间，秒级时间戳',
    PRIMARY KEY (`id`),
    UNIQUE KEY `uk_admin_user_group_rel` (`admin_id`, `group_id`),
    KEY `idx_admin_user_group_rel_group_id` (`group_id`),
    CONSTRAINT `fk_admin_user_group_rel_admin_id` FOREIGN KEY (`admin_id`) REFERENCES `admin` (`id`) ON DELETE CASCADE ON UPDATE CASCADE,
    CONSTRAINT `fk_admin_user_group_rel_group_id` FOREIGN KEY (`group_id`) REFERENCES `admin_user_group` (`id`) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='管理员与用户组关联表';

CREATE TABLE IF NOT EXISTS `admin_group_role_rel` (
    `id` int NOT NULL AUTO_INCREMENT,
    `group_id` int NOT NULL COMMENT 'admin_user_group.id',
    `role_id` int NOT NULL COMMENT 'admin_role.id',
    `create_admin` varchar(100) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci DEFAULT NULL COMMENT '创建人',
    `createTime` int NOT NULL DEFAULT 0 COMMENT '创建时间，秒级时间戳',
    PRIMARY KEY (`id`),
    UNIQUE KEY `uk_admin_group_role_rel` (`group_id`, `role_id`),
    KEY `idx_admin_group_role_rel_role_id` (`role_id`),
    CONSTRAINT `fk_admin_group_role_rel_group_id` FOREIGN KEY (`group_id`) REFERENCES `admin_user_group` (`id`) ON DELETE CASCADE ON UPDATE CASCADE,
    CONSTRAINT `fk_admin_group_role_rel_role_id` FOREIGN KEY (`role_id`) REFERENCES `admin_role` (`id`) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='用户组与角色关联表';

CREATE TABLE IF NOT EXISTS `admin_role_permission_rel` (
    `id` int NOT NULL AUTO_INCREMENT,
    `role_id` int NOT NULL COMMENT 'admin_role.id',
    `permission_id` int NOT NULL COMMENT 'admin_permission.id',
    `create_admin` varchar(100) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci DEFAULT NULL COMMENT '创建人',
    `createTime` int NOT NULL DEFAULT 0 COMMENT '创建时间，秒级时间戳',
    PRIMARY KEY (`id`),
    UNIQUE KEY `uk_admin_role_permission_rel` (`role_id`, `permission_id`),
    KEY `idx_admin_role_permission_rel_permission_id` (`permission_id`),
    CONSTRAINT `fk_admin_role_permission_rel_role_id` FOREIGN KEY (`role_id`) REFERENCES `admin_role` (`id`) ON DELETE CASCADE ON UPDATE CASCADE,
    CONSTRAINT `fk_admin_role_permission_rel_permission_id` FOREIGN KEY (`permission_id`) REFERENCES `admin_permission` (`id`) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='角色与单点权限关联表';

CREATE TABLE IF NOT EXISTS `admin_user_permission_rel` (
    `id` int NOT NULL AUTO_INCREMENT,
    `admin_id` int NOT NULL COMMENT 'admin.id',
    `permission_id` int NOT NULL COMMENT 'admin_permission.id',
    `grant_type` tinyint NOT NULL DEFAULT 1 COMMENT '授权类型：1允许 2拒绝，支持单点覆盖角色权限',
    `create_admin` varchar(100) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci DEFAULT NULL COMMENT '创建人',
    `createTime` int NOT NULL DEFAULT 0 COMMENT '创建时间，秒级时间戳',
    PRIMARY KEY (`id`),
    UNIQUE KEY `uk_admin_user_permission_rel` (`admin_id`, `permission_id`),
    KEY `idx_admin_user_permission_rel_permission_id` (`permission_id`),
    KEY `idx_admin_user_permission_rel_grant_type` (`grant_type`),
    CONSTRAINT `fk_admin_user_permission_rel_admin_id` FOREIGN KEY (`admin_id`) REFERENCES `admin` (`id`) ON DELETE CASCADE ON UPDATE CASCADE,
    CONSTRAINT `fk_admin_user_permission_rel_permission_id` FOREIGN KEY (`permission_id`) REFERENCES `admin_permission` (`id`) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='管理员单点权限关联表';

-- 初始化示例数据
INSERT INTO `admin_user_group` (`group_code`, `group_name`, `status`, `remark`, `create_admin`, `update_admin`, `createTime`, `updateTime`)
VALUES
    ('G_SUPER', '超级管理员组', 1, '系统初始化用户组', 'system', 'system', UNIX_TIMESTAMP(), UNIX_TIMESTAMP()),
    ('G_OP', '运营组', 1, '运营后台使用', 'system', 'system', UNIX_TIMESTAMP(), UNIX_TIMESTAMP()),
    ('G_CS', '客服组', 1, '客服后台使用', 'system', 'system', UNIX_TIMESTAMP(), UNIX_TIMESTAMP())
ON DUPLICATE KEY UPDATE
    `group_name` = VALUES(`group_name`),
    `status` = VALUES(`status`),
    `remark` = VALUES(`remark`),
    `update_admin` = VALUES(`update_admin`),
    `updateTime` = VALUES(`updateTime`);

INSERT INTO `admin_role` (`role_code`, `role_name`, `status`, `is_system`, `remark`, `create_admin`, `update_admin`, `createTime`, `updateTime`)
VALUES
    ('R_SUPER', '超级管理员', 1, 1, '默认拥有全量权限', 'system', 'system', UNIX_TIMESTAMP(), UNIX_TIMESTAMP()),
    ('R_ACTIVITY', '活动管理员', 1, 1, '管理活动与配置', 'system', 'system', UNIX_TIMESTAMP(), UNIX_TIMESTAMP()),
    ('R_NOTIFICATION', '通知管理员', 1, 1, '管理客户端通知配置', 'system', 'system', UNIX_TIMESTAMP(), UNIX_TIMESTAMP())
ON DUPLICATE KEY UPDATE
    `role_name` = VALUES(`role_name`),
    `status` = VALUES(`status`),
    `remark` = VALUES(`remark`),
    `update_admin` = VALUES(`update_admin`),
    `updateTime` = VALUES(`updateTime`);

INSERT INTO `admin_permission` (`permission_code`, `permission_name`, `permission_group`, `api_path`, `http_method`, `status`, `remark`, `create_admin`, `update_admin`, `createTime`, `updateTime`)
VALUES
    -- ── 管理员管理 ──────────────────────────────────────────────
    ('AC1001', '管理员列表',         'admin', '/api/admin/list',           'POST', 1, '',  'system', 'system', UNIX_TIMESTAMP(), UNIX_TIMESTAMP()),
    ('AC1002', '管理员新增',         'admin', '/api/admin/add',            'POST', 1, '',  'system', 'system', UNIX_TIMESTAMP(), UNIX_TIMESTAMP()),
    ('AC1003', '管理员操作日志',     'admin', '/api/admin/log/list',       'POST', 1, '',  'system', 'system', UNIX_TIMESTAMP(), UNIX_TIMESTAMP()),
    ('AC1004', '管理员配置列表',     'admin', '/api/admin/config/list',    'POST', 1, '',  'system', 'system', UNIX_TIMESTAMP(), UNIX_TIMESTAMP()),
    ('AC1005', '管理员配置新增',     'admin', '/api/admin/config/add',     'POST', 1, '',  'system', 'system', UNIX_TIMESTAMP(), UNIX_TIMESTAMP()),
    ('AC1006', '管理员配置编辑',     'admin', '/api/admin/config/edit',    'POST', 1, '',  'system', 'system', UNIX_TIMESTAMP(), UNIX_TIMESTAMP()),
    -- ── 权限管理 ────────────────────────────────────────────────
    ('AC1101', '用户组列表',         'permission', '/api/admin/usergroup/list',          'POST', 1, '', 'system', 'system', UNIX_TIMESTAMP(), UNIX_TIMESTAMP()),
    ('AC1102', '用户组新增',         'permission', '/api/admin/usergroup/add',           'POST', 1, '', 'system', 'system', UNIX_TIMESTAMP(), UNIX_TIMESTAMP()),
    ('AC1103', '用户组编辑',         'permission', '/api/admin/usergroup/edit',          'POST', 1, '', 'system', 'system', UNIX_TIMESTAMP(), UNIX_TIMESTAMP()),
    ('AC1104', '用户组删除',         'permission', '/api/admin/usergroup/delete',        'POST', 1, '', 'system', 'system', UNIX_TIMESTAMP(), UNIX_TIMESTAMP()),
    ('AC1105', '用户组绑定角色',     'permission', '/api/admin/usergroup/role/set',      'POST', 1, '', 'system', 'system', UNIX_TIMESTAMP(), UNIX_TIMESTAMP()),
    ('AC1106', '用户组角色查询',     'permission', '/api/admin/usergroup/role/list',     'POST', 1, '', 'system', 'system', UNIX_TIMESTAMP(), UNIX_TIMESTAMP()),
    ('AC1107', '角色列表',           'permission', '/api/admin/role/list',               'POST', 1, '', 'system', 'system', UNIX_TIMESTAMP(), UNIX_TIMESTAMP()),
    ('AC1108', '角色新增',           'permission', '/api/admin/role/add',                'POST', 1, '', 'system', 'system', UNIX_TIMESTAMP(), UNIX_TIMESTAMP()),
    ('AC1109', '角色编辑',           'permission', '/api/admin/role/edit',               'POST', 1, '', 'system', 'system', UNIX_TIMESTAMP(), UNIX_TIMESTAMP()),
    ('AC1110', '角色删除',           'permission', '/api/admin/role/delete',             'POST', 1, '', 'system', 'system', UNIX_TIMESTAMP(), UNIX_TIMESTAMP()),
    ('AC1111', '角色绑定权限',       'permission', '/api/admin/role/permission/set',     'POST', 1, '', 'system', 'system', UNIX_TIMESTAMP(), UNIX_TIMESTAMP()),
    ('AC1112', '角色权限查询',       'permission', '/api/admin/role/permission/list',    'POST', 1, '', 'system', 'system', UNIX_TIMESTAMP(), UNIX_TIMESTAMP()),
    ('AC1113', '单点权限列表',       'permission', '/api/admin/permission/list',         'POST', 1, '', 'system', 'system', UNIX_TIMESTAMP(), UNIX_TIMESTAMP()),
    ('AC1114', '单点权限新增',       'permission', '/api/admin/permission/add',          'POST', 1, '', 'system', 'system', UNIX_TIMESTAMP(), UNIX_TIMESTAMP()),
    ('AC1115', '单点权限编辑',       'permission', '/api/admin/permission/edit',         'POST', 1, '', 'system', 'system', UNIX_TIMESTAMP(), UNIX_TIMESTAMP()),
    ('AC1116', '单点权限删除',       'permission', '/api/admin/permission/delete',       'POST', 1, '', 'system', 'system', UNIX_TIMESTAMP(), UNIX_TIMESTAMP()),
    ('AC1117', '用户组分配查询',     'permission', '/api/admin/user/group/list',         'POST', 1, '', 'system', 'system', UNIX_TIMESTAMP(), UNIX_TIMESTAMP()),
    ('AC1118', '用户组分配设置',     'permission', '/api/admin/user/group/set',          'POST', 1, '', 'system', 'system', UNIX_TIMESTAMP(), UNIX_TIMESTAMP()),
    ('AC1119', '用户单点权限查询',   'permission', '/api/admin/user/permission/list',    'POST', 1, '', 'system', 'system', UNIX_TIMESTAMP(), UNIX_TIMESTAMP()),
    ('AC1120', '用户单点权限设置',   'permission', '/api/admin/user/permission/set',     'POST', 1, '', 'system', 'system', UNIX_TIMESTAMP(), UNIX_TIMESTAMP()),
    ('AC1121', '用户角色查询',       'permission', '/api/admin/user/role/list',          'POST', 1, '', 'system', 'system', UNIX_TIMESTAMP(), UNIX_TIMESTAMP()),
    -- ── 玩家日志 ────────────────────────────────────────────────
    ('AC2001', '玩家日志查询',       'log', '/api/log/user',               'POST', 1, '', 'system', 'system', UNIX_TIMESTAMP(), UNIX_TIMESTAMP()),
    ('AC2002', '资产日志查询',       'log', '/api/log/asset',              'POST', 1, '', 'system', 'system', UNIX_TIMESTAMP(), UNIX_TIMESTAMP()),
    ('AC2003', '事件日志查询',       'log', '/api/log/event',              'POST', 1, '', 'system', 'system', UNIX_TIMESTAMP(), UNIX_TIMESTAMP()),
    ('AC2004', '月登录次数统计',     'log', '/api/log/loginCountByMonth',  'POST', 1, '', 'system', 'system', UNIX_TIMESTAMP(), UNIX_TIMESTAMP()),
    ('AC2005', '订单日志查询',       'log', '/api/log/order',              'POST', 1, '', 'system', 'system', UNIX_TIMESTAMP(), UNIX_TIMESTAMP()),
    ('AC2006', '提交补单审核',       'log', '/api/log/order/reissue/apply',   'POST', 1, '', 'system', 'system', UNIX_TIMESTAMP(), UNIX_TIMESTAMP()),
    ('AC2007', '通过补单审核',       'log', '/api/log/order/reissue/approve', 'POST', 1, '', 'system', 'system', UNIX_TIMESTAMP(), UNIX_TIMESTAMP()),
    ('AC2008', '驳回补单审核',       'log', '/api/log/order/reissue/reject',  'POST', 1, '', 'system', 'system', UNIX_TIMESTAMP(), UNIX_TIMESTAMP()),
    -- ── 玩家数据 ────────────────────────────────────────────────
    ('AC3001', '玩家列表',           'user', '/api/user/list',             'POST', 1, '', 'system', 'system', UNIX_TIMESTAMP(), UNIX_TIMESTAMP()),
    ('AC3002', '当前用户信息',       'user', '/api/user/info',             'GET',  1, '', 'system', 'system', UNIX_TIMESTAMP(), UNIX_TIMESTAMP()),
    ('AC3003', 'GM操作',             'user', '/api/user/gm',               'POST', 1, '', 'system', 'system', UNIX_TIMESTAMP(), UNIX_TIMESTAMP()),
    ('AC3004', '封禁玩家',           'user', '/api/user/ban',              'POST', 1, '', 'system', 'system', UNIX_TIMESTAMP(), UNIX_TIMESTAMP()),
    -- ── 服务器管理 ──────────────────────────────────────────────
    ('AC4001', '应用列表',           'server', '/api/server/list',             'POST', 1, '', 'system', 'system', UNIX_TIMESTAMP(), UNIX_TIMESTAMP()),
    ('AC4002', '服务器列表',         'server', '/api/server/serverList',        'POST', 1, '', 'system', 'system', UNIX_TIMESTAMP(), UNIX_TIMESTAMP()),
    ('AC4003', '新增服务器',         'server', '/api/server/addServer',         'POST', 1, '', 'system', 'system', UNIX_TIMESTAMP(), UNIX_TIMESTAMP()),
    ('AC4004', '节点列表',           'server', '/api/server/nodeList',          'POST', 1, '', 'system', 'system', UNIX_TIMESTAMP(), UNIX_TIMESTAMP()),
    ('AC4005', '新增节点',           'server', '/api/server/addNode',           'POST', 1, '', 'system', 'system', UNIX_TIMESTAMP(), UNIX_TIMESTAMP()),
    ('AC4006', '编辑服务器',         'server', '/api/server/editServer',        'POST', 1, '', 'system', 'system', UNIX_TIMESTAMP(), UNIX_TIMESTAMP()),
    ('AC4007', '更新应用',           'server', '/api/server/updateApp',         'POST', 1, '', 'system', 'system', UNIX_TIMESTAMP(), UNIX_TIMESTAMP()),
    ('AC4008', '更新应用(审核)',      'server', '/api/server/updateAppReview',   'POST', 1, '', 'system', 'system', UNIX_TIMESTAMP(), UNIX_TIMESTAMP()),
    ('AC4009', '更新应用(飞书)',      'server', '/api/server/updateAppFeishu',   'POST', 1, '', 'system', 'system', UNIX_TIMESTAMP(), UNIX_TIMESTAMP()),
    ('AC4010', '重启服务器',         'server', '/api/server/restart',           'POST', 1, '', 'system', 'system', UNIX_TIMESTAMP(), UNIX_TIMESTAMP()),
    ('AC4011', '重载服务器',         'server', '/api/server/reload',            'POST', 1, '', 'system', 'system', UNIX_TIMESTAMP(), UNIX_TIMESTAMP()),
    -- ── 统计 ────────────────────────────────────────────────────
    ('AC5001', '等级统计',           'statistics', '/api/statistics/level',     'POST', 1, '', 'system', 'system', UNIX_TIMESTAMP(), UNIX_TIMESTAMP()),
    ('AC5002', '订单统计',           'statistics', '/api/statistics/order',     'POST', 1, '', 'system', 'system', UNIX_TIMESTAMP(), UNIX_TIMESTAMP()),
    ('AC5003', '基础统计信息',       'statistics', '/api/statistics/info',      'POST', 1, '', 'system', 'system', UNIX_TIMESTAMP(), UNIX_TIMESTAMP()),
    ('AC5004', '热力图统计',         'statistics', '/api/statistics/heat',      'POST', 1, '', 'system', 'system', UNIX_TIMESTAMP(), UNIX_TIMESTAMP()),
    -- ── 活动管理 ────────────────────────────────────────────────
    ('AC6001', '活动列表',           'activity', '/api/activity/list',          'POST', 1, '', 'system', 'system', UNIX_TIMESTAMP(), UNIX_TIMESTAMP()),
    ('AC6002', '活动编辑',           'activity', '/api/activity/edit',          'POST', 1, '', 'system', 'system', UNIX_TIMESTAMP(), UNIX_TIMESTAMP()),
    ('AC6003', '活动新增',           'activity', '/api/activity/add',           'POST', 1, '', 'system', 'system', UNIX_TIMESTAMP(), UNIX_TIMESTAMP()),
    ('AC6004', '活动删除',           'activity', '/api/activity/delete',        'POST', 1, '', 'system', 'system', UNIX_TIMESTAMP(), UNIX_TIMESTAMP()),
    ('AC6005', '活动同步',           'activity', '/api/activity/sync',          'POST', 1, '', 'system', 'system', UNIX_TIMESTAMP(), UNIX_TIMESTAMP()),
    -- ── 邮件 ────────────────────────────────────────────────────
    ('AC7001', '发送邮件',           'mail', '/api/mail/send',                  'POST', 1, '', 'system', 'system', UNIX_TIMESTAMP(), UNIX_TIMESTAMP()),
    ('AC7002', '邮件列表',           'mail', '/api/mail/list',                  'POST', 1, '', 'system', 'system', UNIX_TIMESTAMP(), UNIX_TIMESTAMP()),
    ('AC7003', '删除邮件',           'mail', '/api/mail/delete',                'POST', 1, '', 'system', 'system', UNIX_TIMESTAMP(), UNIX_TIMESTAMP()),
    ('AC7004', '审核通过邮件',       'mail', '/api/mail/approve',               'POST', 1, '', 'system', 'system', UNIX_TIMESTAMP(), UNIX_TIMESTAMP()),
    ('AC7005', '驳回邮件审核',       'mail', '/api/mail/reject',                'POST', 1, '', 'system', 'system', UNIX_TIMESTAMP(), UNIX_TIMESTAMP()),
    -- ── 运营功能 ────────────────────────────────────────────────
    ('AC8001', '复制用户数据',       'operation', '/api/operation/copyUser',    'POST', 1, '', 'system', 'system', UNIX_TIMESTAMP(), UNIX_TIMESTAMP()),
    -- ── 翻译管理 ────────────────────────────────────────────────
    ('AC9001', '语言列表',           'language', '/api/language/list',          'POST', 1, '', 'system', 'system', UNIX_TIMESTAMP(), UNIX_TIMESTAMP()),
    ('AC9002', '语言导出',           'language', '/api/language/export',        'POST', 1, '', 'system', 'system', UNIX_TIMESTAMP(), UNIX_TIMESTAMP()),
    ('AC9003', '语言保存',           'language', '/api/language/save',          'POST', 1, '', 'system', 'system', UNIX_TIMESTAMP(), UNIX_TIMESTAMP()),
    ('AC9004', '语言新增',           'language', '/api/language/add',           'POST', 1, '', 'system', 'system', UNIX_TIMESTAMP(), UNIX_TIMESTAMP()),
    ('AC9005', '语言删除',           'language', '/api/language/delete',        'POST', 1, '', 'system', 'system', UNIX_TIMESTAMP(), UNIX_TIMESTAMP()),
    -- ── 通知配置 ────────────────────────────────────────────────
    ('AC9101', '通知配置查询',       'notification', '/api/config/notification',        'GET', 1, '', 'system', 'system', UNIX_TIMESTAMP(), UNIX_TIMESTAMP()),
    ('AC9102', '通知配置更新',       'notification', '/api/config/notification/update', 'PUT', 1, '', 'system', 'system', UNIX_TIMESTAMP(), UNIX_TIMESTAMP()),
    -- ── AB 实验 ─────────────────────────────────────────────────
    ('AC9201', '实验列表',                 'experiment', '/api/v1/experiments',                            'GET',    1, '', 'system', 'system', UNIX_TIMESTAMP(), UNIX_TIMESTAMP()),
    ('AC9202', '实验新增',                 'experiment', '/api/v1/experiments',                            'POST',   1, '', 'system', 'system', UNIX_TIMESTAMP(), UNIX_TIMESTAMP()),
    ('AC9203', '实验更新',                 'experiment', '/api/v1/experiments/:id',                        'PUT',    1, '', 'system', 'system', UNIX_TIMESTAMP(), UNIX_TIMESTAMP()),
    ('AC9204', '实验删除',                 'experiment', '/api/v1/experiments/:id',                        'DELETE', 1, '', 'system', 'system', UNIX_TIMESTAMP(), UNIX_TIMESTAMP()),
    ('AC9205', '实验变体列表',             'experiment', '/api/v1/experiments/:id/variants',               'GET',    1, '', 'system', 'system', UNIX_TIMESTAMP(), UNIX_TIMESTAMP()),
    ('AC9206', '实验变体新增',             'experiment', '/api/v1/experiments/:id/variants',               'POST',   1, '', 'system', 'system', UNIX_TIMESTAMP(), UNIX_TIMESTAMP()),
    ('AC9207', '实验变体更新',             'experiment', '/api/v1/experiments/:id/variants/:variantId',    'PUT',    1, '', 'system', 'system', UNIX_TIMESTAMP(), UNIX_TIMESTAMP()),
    ('AC9208', '实验变体删除',             'experiment', '/api/v1/experiments/:id/variants/:variantId',    'DELETE', 1, '', 'system', 'system', UNIX_TIMESTAMP(), UNIX_TIMESTAMP()),
    ('AC9209', '实验白名单列表',           'experiment', '/api/v1/experiments/:id/whitelist',              'GET',    1, '', 'system', 'system', UNIX_TIMESTAMP(), UNIX_TIMESTAMP()),
    ('AC9210', '实验白名单新增',           'experiment', '/api/v1/experiments/:id/whitelist',              'POST',   1, '', 'system', 'system', UNIX_TIMESTAMP(), UNIX_TIMESTAMP()),
    ('AC9211', '实验白名单批量新增',       'experiment', '/api/v1/experiments/:id/whitelist/batch',        'POST',   1, '', 'system', 'system', UNIX_TIMESTAMP(), UNIX_TIMESTAMP()),
    ('AC9212', '实验白名单删除',           'experiment', '/api/v1/experiments/:id/whitelist/:userId',      'DELETE', 1, '', 'system', 'system', UNIX_TIMESTAMP(), UNIX_TIMESTAMP()),
    ('AC9213', '实验结果查询',             'experiment', '/api/v1/experiments/:id/results',                'GET',    1, '', 'system', 'system', UNIX_TIMESTAMP(), UNIX_TIMESTAMP()),
    ('AC9214', '用户实验分组查询',         'experiment', '/api/v1/users/:userId/groups',                   'GET',    1, '', 'system', 'system', UNIX_TIMESTAMP(), UNIX_TIMESTAMP()),
    -- ── 自动化脚本 ──────────────────────────────────────────────
    ('AC9301', '下载文案文件',       'scripts', '/api/scripts/copywriting',           'POST', 1, '', 'system', 'system', UNIX_TIMESTAMP(), UNIX_TIMESTAMP()),
    ('AC9302', '复制线上环境到QA',   'scripts', '/api/scripts/copyonline',            'POST', 1, '', 'system', 'system', UNIX_TIMESTAMP(), UNIX_TIMESTAMP()),
    ('AC9303', '复制线下环境到QA',   'scripts', '/api/scripts/clientImageGitPull',   'POST', 1, '', 'system', 'system', UNIX_TIMESTAMP(), UNIX_TIMESTAMP())
ON DUPLICATE KEY UPDATE
    `permission_name`  = VALUES(`permission_name`),
    `permission_group` = VALUES(`permission_group`),
    `api_path`         = VALUES(`api_path`),
    `http_method`      = VALUES(`http_method`),
    `status`           = VALUES(`status`),
    `remark`           = VALUES(`remark`),
    `update_admin`     = VALUES(`update_admin`),
    `updateTime`       = VALUES(`updateTime`);

-- 初始化关联关系示例
INSERT INTO `admin_group_role_rel` (`group_id`, `role_id`, `create_admin`, `createTime`)
SELECT g.id, r.id, 'system', UNIX_TIMESTAMP()
FROM `admin_user_group` g
JOIN `admin_role` r
WHERE (g.group_code = 'G_SUPER' AND r.role_code = 'R_SUPER')
   OR (g.group_code = 'G_OP' AND r.role_code = 'R_ACTIVITY')
   OR (g.group_code = 'G_OP' AND r.role_code = 'R_NOTIFICATION')
ON DUPLICATE KEY UPDATE
    `create_admin` = VALUES(`create_admin`);

INSERT INTO `admin_role_permission_rel` (`role_id`, `permission_id`, `create_admin`, `createTime`)
SELECT r.id, p.id, 'system', UNIX_TIMESTAMP()
FROM `admin_role` r
JOIN `admin_permission` p
WHERE (r.role_code = 'R_SUPER')
   OR (r.role_code = 'R_ACTIVITY'     AND p.permission_code IN ('AC6001','AC6002','AC6003','AC6004','AC6005'))
   OR (r.role_code = 'R_NOTIFICATION' AND p.permission_code IN ('AC9101','AC9102'))
ON DUPLICATE KEY UPDATE
    `create_admin` = VALUES(`create_admin`);

-- 可选：把现有管理员挂到默认用户组，请按实际用户名替换。
-- INSERT INTO `admin_user_group_rel` (`admin_id`, `group_id`, `create_admin`, `createTime`)
-- SELECT a.id, g.id, 'system', UNIX_TIMESTAMP()
-- FROM `admin` a
-- JOIN `admin_user_group` g ON g.group_code = 'G_SUPER'
-- WHERE a.username IN ('admin');
