-- =====================================================
-- 面试列表 - 菜单权限配置 SQL
-- 在 岗位管理 菜单下增加"面试列表"子菜单及按钮权限
--
-- 执行前请确认：
-- 1. 确认 parent_id：面试列表将放在 "岗位管理" 目录下
--    如果数据库中岗位管理目录的 menu_id 不同，请修改下面子查询中的条件
-- 2. sys_menu 表使用自增主键（auto_increment=2000），
--    本脚本使用 2100+ 的固定ID，避免与已有数据冲突
-- 3. 执行后需要在"角色管理"中将新菜单分配给对应角色
-- =====================================================

-- Step 1: 新增菜单项（C=菜单页面）
-- parent_id 动态查找：path='management' 的目录菜单
INSERT INTO sys_menu (menu_id, menu_name, parent_id, order_num, path, component, query, route_name, is_frame, is_cache, menu_type, visible, status, perms, icon, create_by, create_time, update_by, update_time, remark)
SELECT 2100, '面试列表',
       (SELECT menu_id FROM sys_menu WHERE path = 'management' AND menu_type = 'M' LIMIT 1),
       10, 'interview-list', 'Management/InterviewList', '', 'InterviewList', 1, 0, 'C', '0', '0',
       'cms:interview:list', 'message', 'admin', CURRENT_TIMESTAMP, '', null, '面试列表菜单'
WHERE EXISTS (SELECT 1 FROM sys_menu WHERE path = 'management' AND menu_type = 'M');

-- Step 2: 新增按钮权限（F=按钮）
-- 面试查询
INSERT INTO sys_menu (menu_id, menu_name, parent_id, order_num, path, component, query, route_name, is_frame, is_cache, menu_type, visible, status, perms, icon, create_by, create_time, update_by, update_time, remark)
SELECT 2101, '面试查询', 2100, 1, '', '', '', '', 1, 0, 'F', '0', '0',
       'cms:interview:query', '#', 'admin', CURRENT_TIMESTAMP, '', null, ''
WHERE EXISTS (SELECT 1 FROM sys_menu WHERE menu_id = 2100);

-- 面试新增
INSERT INTO sys_menu (menu_id, menu_name, parent_id, order_num, path, component, query, route_name, is_frame, is_cache, menu_type, visible, status, perms, icon, create_by, create_time, update_by, update_time, remark)
SELECT 2102, '面试新增', 2100, 2, '', '', '', '', 1, 0, 'F', '0', '0',
       'cms:interview:add', '#', 'admin', CURRENT_TIMESTAMP, '', null, ''
WHERE EXISTS (SELECT 1 FROM sys_menu WHERE menu_id = 2100);

-- 面试修改
INSERT INTO sys_menu (menu_id, menu_name, parent_id, order_num, path, component, query, route_name, is_frame, is_cache, menu_type, visible, status, perms, icon, create_by, create_time, update_by, update_time, remark)
SELECT 2103, '面试修改', 2100, 3, '', '', '', '', 1, 0, 'F', '0', '0',
       'cms:interview:edit', '#', 'admin', CURRENT_TIMESTAMP, '', null, ''
WHERE EXISTS (SELECT 1 FROM sys_menu WHERE menu_id = 2100);

-- 面试删除
INSERT INTO sys_menu (menu_id, menu_name, parent_id, order_num, path, component, query, route_name, is_frame, is_cache, menu_type, visible, status, perms, icon, create_by, create_time, update_by, update_time, remark)
SELECT 2104, '面试删除', 2100, 4, '', '', '', '', 1, 0, 'F', '0', '0',
       'cms:interview:remove', '#', 'admin', CURRENT_TIMESTAMP, '', null, ''
WHERE EXISTS (SELECT 1 FROM sys_menu WHERE menu_id = 2100);

-- Step 3: 将新菜单分配给角色
-- 为所有非管理员角色分配面试列表权限（管理员角色无需分配，通配符 *:*:* 自动拥有全部权限）
-- 如需仅限特定角色，请修改 WHERE 条件
INSERT INTO sys_role_menu (role_id, menu_id)
SELECT r.role_id, 2100
FROM sys_role r
WHERE r.role_key != 'admin'
  AND EXISTS (SELECT 1 FROM sys_menu WHERE menu_id = 2100);

INSERT INTO sys_role_menu (role_id, menu_id)
SELECT r.role_id, 2101
FROM sys_role r
WHERE r.role_key != 'admin'
  AND EXISTS (SELECT 1 FROM sys_menu WHERE menu_id = 2101);

INSERT INTO sys_role_menu (role_id, menu_id)
SELECT r.role_id, 2102
FROM sys_role r
WHERE r.role_key != 'admin'
  AND EXISTS (SELECT 1 FROM sys_menu WHERE menu_id = 2102);

INSERT INTO sys_role_menu (role_id, menu_id)
SELECT r.role_id, 2103
FROM sys_role r
WHERE r.role_key != 'admin'
  AND EXISTS (SELECT 1 FROM sys_menu WHERE menu_id = 2103);

INSERT INTO sys_role_menu (role_id, menu_id)
SELECT r.role_id, 2104
FROM sys_role r
WHERE r.role_key != 'admin'
  AND EXISTS (SELECT 1 FROM sys_menu WHERE menu_id = 2104);

-- =====================================================
-- 验证 SQL（可选执行）
-- =====================================================
-- 检查菜单是否创建成功
-- SELECT * FROM sys_menu WHERE menu_id BETWEEN 2100 AND 2104;

-- 检查角色菜单关联
-- SELECT r.role_name, m.menu_name FROM sys_role_menu rm
-- JOIN sys_role r ON r.role_id = rm.role_id
-- JOIN sys_menu m ON m.menu_id = rm.menu_id
-- WHERE rm.menu_id BETWEEN 2100 AND 2104;
