-- ============================================
-- 模块白名单 - 后台菜单插入 SQL
-- 数据库：ycc
-- 父菜单：产品管理 (48d0e129-a141-4a74-b519-7adc38d22d27)
-- 超级管理员：741b7a39-a95d-4b9d-8dc0-84ee664d5fef
-- ============================================

-- 1. 插入「模块白名单」菜单（已存在则跳过）
INSERT INTO `admin_menu` (
  `id`, `pid`, `name`, `path`, `component`, `redirect`, `meta`,
  `status`, `type`, `sort`, `del_state`, `version`
)
SELECT
  'c01e094d-6af8-11f1-bd31-dec53e82fe74',
  '48d0e129-a141-4a74-b519-7adc38d22d27',
  'whitelist',
  '/product-manage/whitelist/list',
  '/product-manage/whitelist/list',
  NULL,
  JSON_OBJECT('icon', 'lucide:shield-check', 'title', '模块白名单'),
  1,
  1,
  0,
  0,
  0
FROM DUAL
WHERE NOT EXISTS (
  SELECT 1 FROM `admin_menu`
  WHERE `path` = '/product-manage/whitelist/list' AND `del_state` = 0
);

-- 2. 给「超级管理员」授权（关键：没有这一步菜单不会显示）
INSERT INTO `admin_role_menu` (`id`, `role_id`, `menu_id`, `del_state`, `version`)
SELECT
  UUID(),
  '741b7a39-a95d-4b9d-8dc0-84ee664d5fef',
  m.`id`,
  0,
  0
FROM `admin_menu` m
WHERE m.`path` = '/product-manage/whitelist/list'
  AND m.`del_state` = 0
  AND NOT EXISTS (
    SELECT 1 FROM `admin_role_menu` rm
    WHERE rm.`role_id` = '741b7a39-a95d-4b9d-8dc0-84ee664d5fef'
      AND rm.`menu_id` = m.`id`
      AND rm.`del_state` = 0
  );

-- 3. 给所有已拥有「模块列表」权限的角色同步授权
INSERT INTO `admin_role_menu` (`id`, `role_id`, `menu_id`, `del_state`, `version`)
SELECT
  UUID(),
  arm.`role_id`,
  m.`id`,
  0,
  0
FROM `admin_role_menu` arm
JOIN `admin_menu` m ON m.`path` = '/product-manage/whitelist/list' AND m.`del_state` = 0
WHERE arm.`menu_id` = '7ef2b1d3-4578-4884-a735-915f60c87e13'
  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
  );

-- 执行后请重新登录管理后台
