-- 诊断和修复403权限问题
USE soybean_admin;

-- 1. 检查当前用户和角色配置
SELECT '=== 当前用户配置 ===' AS info;
SELECT user_id, user_name, roles, status FROM sys_user;

SELECT '=== 当前角色配置 ===' AS info;
SELECT role_code, role_name, menus, status FROM sys_role;

-- 2. 检查路由表中的路由名称
SELECT '=== 路由表中的路由 ===' AS info;
SELECT name, path, component, status FROM sys_route WHERE status = 1 ORDER BY order_num;

-- 3. 修复：确保角色表存在且有正确的菜单配置
-- 如果角色的menus字段为空或NULL，更新它们

-- 为超级管理员配置所有菜单（包括home和device相关）
UPDATE sys_role 
SET menus = 'home,device,device_list,device_online,device_power,device_monitor,device_group,screen,screen_wall,screen_control,screen_record,user-manage,user-manage_list,user-manage_role,user-manage_permission,application,application_approval,application_history,system,system_amt,system_agent,system_log'
WHERE role_code = 'R_SUPER';

-- 为管理员配置大部分菜单（除了系统设置和权限管理）
UPDATE sys_role 
SET menus = 'home,device,device_list,device_online,device_power,device_monitor,device_group,screen,screen_wall,screen_control,screen_record,user-manage,user-manage_list,user-manage_role,application,application_approval,application_history'
WHERE role_code = 'R_ADMIN';

-- 为普通用户配置基本菜单
UPDATE sys_role 
SET menus = 'home,my-device,my-device_status,my-device_power-control,my-device_remote-control,my-application,my-application_apply,my-application_my-list'
WHERE role_code = 'R_USER';

-- 4. 验证修复结果
SELECT '=== 修复后的角色配置 ===' AS info;
SELECT role_code, role_name, menus, status FROM sys_role;

-- 5. 检查路由名称是否匹配
SELECT '=== 检查路由名称匹配情况 ===' AS info;
SELECT 
    r.role_code,
    r.menus AS configured_menus,
    GROUP_CONCAT(rt.name ORDER BY rt.name) AS available_routes
FROM sys_role r
CROSS JOIN sys_route rt
WHERE rt.status = 1
GROUP BY r.role_code, r.menus;
