-- ==============================================================
--  用户-项目绑定 菜单权限种子
--  feat/user-project-api  |  2026-04-23
-- --------------------------------------------------------------
--  新增两条按钮权限：
--   1) system:user:assign-project  —— 用户管理页"分配项目"按钮
--   2) system:project:assign-user  —— 项目管理页"管理成员"按钮
--
--  parent_id 用子查询动态定位，避免不同环境 menu id 不同
-- ==============================================================

-- 1) 用户分配项目（挂在 用户管理 菜单下）
INSERT INTO system_menu
  (name, permission, type, sort, parent_id,
   path, icon, component, status,
   creator, create_time, updater, update_time, deleted)
SELECT
  '用户分配项目',
  'system:user:assign-project',
  3,                                   -- 3 = 按钮
  10,                                  -- 排序值，靠后
  m.id,                                -- 父菜单 = 用户管理
  '', '', '', 0,                       -- 按钮不需要 path/icon/component；status=0 启用
  '1', NOW(), '1', NOW(), 0
FROM system_menu m
WHERE m.permission = 'system:user:list' AND m.deleted = 0
  AND NOT EXISTS (
    SELECT 1 FROM system_menu x
    WHERE x.permission = 'system:user:assign-project' AND x.deleted = 0
  )
LIMIT 1;

-- 2) 项目管理成员（挂在 项目管理 菜单下）
INSERT INTO system_menu
  (name, permission, type, sort, parent_id,
   path, icon, component, status,
   creator, create_time, updater, update_time, deleted)
SELECT
  '项目管理成员',
  'system:project:assign-user',
  3,
  10,
  m.id,
  '', '', '', 0,
  '1', NOW(), '1', NOW(), 0
FROM system_menu m
WHERE m.permission = 'system:project:query' AND m.deleted = 0
  AND NOT EXISTS (
    SELECT 1 FROM system_menu x
    WHERE x.permission = 'system:project:assign-user' AND x.deleted = 0
  )
LIMIT 1;

-- --------------------------------------------------------------
-- 部署后核对：
--   SELECT id, name, permission, parent_id FROM system_menu
--   WHERE permission IN ('system:user:assign-project','system:project:assign-user');
-- 期望：2 行，parent_id 非 NULL 且分别指向"用户管理"和"项目管理"菜单
-- --------------------------------------------------------------
