-- 为超级管理员（roleId=1）分配所有菜单权限
-- 这将解决操作权限管理界面中超级管理员权限未全部打开的问题

-- 1. 查询当前超级管理员已有的权限
SELECT rm.role_id, r.name as role_name, rm.menu_id, m.name as menu_name, m.authority
FROM sys_role_menu rm
LEFT JOIN sys_role r ON rm.role_id = r.id
LEFT JOIN sys_menu m ON rm.menu_id = m.id
WHERE rm.role_id = 1 AND m.is_delete = 0
ORDER BY m.sort;

-- 2. 为超级管理员角色分配所有菜单权限
-- 使用 INSERT ... ON DUPLICATE KEY UPDATE 避免重复插入
INSERT INTO sys_role_menu (role_id, menu_id)
SELECT 1, id FROM sys_menu WHERE is_delete = 0
ON DUPLICATE KEY UPDATE role_id = role_id;

-- 3. 验证分配结果
SELECT 
    rm.role_id,
    r.name as role_name,
    COUNT(rm.menu_id) as menu_count
FROM sys_role_menu rm
LEFT JOIN sys_role r ON rm.role_id = r.id
WHERE rm.role_id = 1
GROUP BY rm.role_id, r.name;

-- 4. 查询总菜单数
SELECT COUNT(*) as total_menus FROM sys_menu WHERE is_delete = 0;

-- 5. 查询超级管理员所有权限详情
SELECT 
    rm.role_id,
    r.name as role_name,
    m.id as menu_id,
    m.name as menu_name,
    m.type,
    m.authority,
    m.page_url
FROM sys_role_menu rm
LEFT JOIN sys_role r ON rm.role_id = r.id
LEFT JOIN sys_menu m ON rm.menu_id = m.id
WHERE rm.role_id = 1 AND m.is_delete = 0
ORDER BY m.parent_id, m.sort;

