-- 查询白名单功能数据库迁移脚本
-- 数据库：tydata

-- ============================================
-- 1. 创建操作记录表
-- ============================================
CREATE TABLE IF NOT EXISTS `query_whitelist_op_log` (
  `id` bigint NOT NULL AUTO_INCREMENT COMMENT '主键ID',
  `create_time` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
  `update_time` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间',
  `delete_time` datetime DEFAULT NULL COMMENT '删除时间',
  `del_state` tinyint NOT NULL DEFAULT '0' COMMENT '删除状态：0=未删除，1=已删除',
  `version` bigint NOT NULL DEFAULT '0' COMMENT '版本号（乐观锁）',

  `admin_user_id` bigint NOT NULL COMMENT '操作管理员ID',
  `action` varchar(20) NOT NULL COMMENT '操作类型：create=创建规则，append=追加接口',
  `name` varchar(50) NOT NULL DEFAULT '*' COMMENT '姓名规则',
  `id_card` varchar(50) NOT NULL COMMENT '身份证号（明文，供后台审计）',
  `id_card_masked` varchar(50) DEFAULT NULL COMMENT '天远返回的脱敏身份证号',
  `api_codes` varchar(2000) NOT NULL COMMENT '本次提交的产品编码（JSON数组）',
  `remark` varchar(500) DEFAULT NULL COMMENT '备注',
  `tianyuan_code` int NOT NULL DEFAULT 0 COMMENT '天远 API 业务码',
  `tianyuan_message` varchar(500) DEFAULT NULL COMMENT '天远 API 返回描述',
  `transaction_id` varchar(64) DEFAULT NULL COMMENT '天远 API 流水号',
  `entry_id` varchar(64) DEFAULT NULL COMMENT '天远规则ID（成功时）',
  `entry_status` varchar(20) DEFAULT NULL COMMENT '规则状态（成功时）',
  `entry_api_codes` varchar(2000) DEFAULT NULL COMMENT '规则当前产品编码列表（JSON数组，成功时）',

  PRIMARY KEY (`id`),
  KEY `idx_admin_user_id` (`admin_user_id`),
  KEY `idx_id_card` (`id_card`),
  KEY `idx_action` (`action`),
  KEY `idx_tianyuan_code` (`tianyuan_code`),
  KEY `idx_create_time` (`create_time`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci COMMENT='查询白名单操作记录表';

-- ============================================
-- 2. 菜单：新增「查询白名单」
-- ============================================

-- 软删除旧「模块白名单」菜单（如存在）
UPDATE `admin_menu`
SET `del_state` = 1, `version` = `version` + 1
WHERE `path` = '/product-manage/whitelist/list' AND `del_state` = 0;

-- 软删除曾独立的「查询白名单记录」菜单（如存在）
UPDATE `admin_menu`
SET `del_state` = 1, `version` = `version` + 1
WHERE `path` = '/product-manage/query-whitelist/op-log' AND `del_state` = 0;

-- 插入「查询白名单」菜单（已存在则跳过）
INSERT INTO `admin_menu` (
  `pid`, `name`, `path`, `component`, `redirect`, `meta`,
  `status`, `type`, `sort`, `del_state`, `version`
)
SELECT
  m.`id`,
  'queryWhitelist',
  '/product-manage/query-whitelist/list',
  '/product-manage/query-whitelist/list',
  NULL,
  JSON_OBJECT('icon', 'lucide:shield-off', 'title', '查询白名单'),
  1,
  1,
  0,
  0,
  0
FROM `admin_menu` m
WHERE m.`path` = '/product-manage' AND m.`del_state` = 0
  AND NOT EXISTS (
    SELECT 1 FROM `admin_menu`
    WHERE `path` = '/product-manage/query-whitelist/list' AND `del_state` = 0
  )
LIMIT 1;

-- 给「超级管理员」授权
INSERT INTO `admin_role_menu` (`role_id`, `menu_id`, `del_state`, `version`)
SELECT
  1,
  m.`id`,
  0,
  0
FROM `admin_menu` m
WHERE m.`path` = '/product-manage/query-whitelist/list'
  AND m.`del_state` = 0
  AND NOT EXISTS (
    SELECT 1 FROM `admin_role_menu` rm
    WHERE rm.`role_id` = 1
      AND rm.`menu_id` = m.`id`
      AND rm.`del_state` = 0
  );

-- 给所有曾拥有「模块白名单」权限的角色同步授权
INSERT INTO `admin_role_menu` (`role_id`, `menu_id`, `del_state`, `version`)
SELECT
  arm.`role_id`,
  m.`id`,
  0,
  0
FROM `admin_role_menu` arm
JOIN `admin_menu` old_m ON old_m.`path` = '/product-manage/whitelist/list'
JOIN `admin_menu` m ON m.`path` = '/product-manage/query-whitelist/list' AND m.`del_state` = 0
WHERE arm.`menu_id` = old_m.`id`
  AND arm.`del_state` = 0
  AND NOT EXISTS (
    SELECT 1 FROM `admin_role_menu` x
    WHERE x.`role_id` = arm.`role_id`
      AND x.`menu_id` = m.`id`
      AND x.`del_state` = 0
  );
