-- 添加角色权限管理菜单
-- 执行时间: 2024-12-23

-- 1. 插入菜单项
INSERT INTO system_menus (
    id,
    parent_id,
    menu_name,
    menu_code,
    menu_type,
    path,
    component,
    icon,
    sort_order,
    visible,
    status,
    created_at,
    updated_at
) VALUES (
    14,
    4,
    '角色权限管理',
    'system:role_permissions',
    1,
    '/role-permissions',
    'RolePermissions',
    'SafetyOutlined',
    6,
    1,
    1,
    NOW(),
    NOW()
);

-- 2. 为超级管理员角色分配该菜单权限
INSERT INTO role_menus (role_id, menu_id, created_at)
SELECT r.id, 14, NOW()
FROM roles r
WHERE r.role_code = 'super_admin'
AND NOT EXISTS (
    SELECT 1 FROM role_menus rm
    WHERE rm.role_id = r.id AND rm.menu_id = 14
);

-- 3. 为管理员角色分配该菜单权限
INSERT INTO role_menus (role_id, menu_id, created_at)
SELECT r.id, 14, NOW()
FROM roles r
WHERE r.role_code = 'admin'
AND NOT EXISTS (
    SELECT 1 FROM role_menus rm
    WHERE rm.role_id = r.id AND rm.menu_id = 14
);

-- 验证结果
SELECT
    m.id,
    m.menu_name,
    m.menu_code,
    m.path,
    CASE WHEN m.parent_id = 0 THEN '根菜单' ELSE CONCAT('子菜单 (父ID: ', m.parent_id, ')') END as menu_level
FROM system_menus m
WHERE m.id = 14;

-- 查看哪些角色拥有此菜单权限
SELECT
    r.role_name,
    r.role_code,
    m.menu_name
FROM roles r
JOIN role_menus rm ON r.id = rm.role_id
JOIN system_menus m ON rm.menu_id = m.id
WHERE m.id = 14;
