-- 最终完整修复所有路由问题
USE soybean_admin;

-- ============================================
-- 第一步：修复所有路由的 name 字段
-- 必须与前端 routeMap 完全一致
-- ============================================

-- 设备管理
UPDATE sys_route SET name = 'device' WHERE route_id = 'device';
UPDATE sys_route SET name = 'device_list' WHERE route_id = 'device_list';
UPDATE sys_route SET name = 'device_online' WHERE route_id = 'device_online';
UPDATE sys_route SET name = 'device_power' WHERE route_id = 'device_power';
UPDATE sys_route SET name = 'device_monitor' WHERE route_id = 'device_monitor';
UPDATE sys_route SET name = 'device_group' WHERE route_id = 'device_group';

-- 屏幕监控
UPDATE sys_route SET name = 'screen' WHERE route_id = 'screen';
UPDATE sys_route SET name = 'screen_wall' WHERE route_id = 'screen_wall';
UPDATE sys_route SET name = 'screen_control' WHERE route_id = 'screen_control';
UPDATE sys_route SET name = 'screen_record' WHERE route_id = 'screen_record';

-- 用户管理（关键！）
UPDATE sys_route SET name = 'user-manage' WHERE route_id = 'user_manage';
UPDATE sys_route SET name = 'user-manage_list' WHERE route_id = 'user_list';
UPDATE sys_route SET name = 'user-manage_role' WHERE route_id = 'user_role';
UPDATE sys_route SET name = 'user-manage_permission' WHERE route_id = 'user_permission';

-- 申请管理
UPDATE sys_route SET name = 'application' WHERE route_id = 'application';
UPDATE sys_route SET name = 'application_approval' WHERE route_id = 'application_approval';
UPDATE sys_route SET name = 'application_history' WHERE route_id = 'application_history';

-- 系统设置
UPDATE sys_route SET name = 'system' WHERE route_id = 'system';
UPDATE sys_route SET name = 'system_amt' WHERE route_id = 'system_amt';
UPDATE sys_route SET name = 'system_agent' WHERE route_id = 'system_agent';
UPDATE sys_route SET name = 'system_log' WHERE route_id = 'system_log';

-- 我的设备
UPDATE sys_route SET name = 'my-device' WHERE route_id = 'my_device';
UPDATE sys_route SET name = 'my-device_status' WHERE route_id = 'my_device_status';
UPDATE sys_route SET name = 'my-device_power-control' WHERE route_id = 'my_device_power';
UPDATE sys_route SET name = 'my-device_remote-control' WHERE route_id = 'my_device_remote';

-- 设备申请
UPDATE sys_route SET name = 'my-application' WHERE route_id = 'my_application';
UPDATE sys_route SET name = 'my-application_apply' WHERE route_id = 'my_application_apply';
UPDATE sys_route SET name = 'my-application_my-list' WHERE route_id = 'my_application_list';

-- ============================================
-- 第二步：修复所有组件路径
-- 父路由用 layout.base，子路由用 view.xxx
-- ============================================

-- 父路由
UPDATE sys_route SET component = 'layout.base' WHERE route_id IN ('device', 'screen', 'user_manage', 'application', 'system', 'my_device', 'my_application');

-- 设备管理子路由
UPDATE sys_route SET component = 'view.device_list' WHERE route_id = 'device_list';
UPDATE sys_route SET component = 'view.device_online' WHERE route_id = 'device_online';
UPDATE sys_route SET component = 'view.device_power' WHERE route_id = 'device_power';
UPDATE sys_route SET component = 'view.device_monitor' WHERE route_id = 'device_monitor';
UPDATE sys_route SET component = 'view.device_group' WHERE route_id = 'device_group';

-- 屏幕监控子路由
UPDATE sys_route SET component = 'view.screen_wall' WHERE route_id = 'screen_wall';
UPDATE sys_route SET component = 'view.screen_control' WHERE route_id = 'screen_control';
UPDATE sys_route SET component = 'view.screen_record' WHERE route_id = 'screen_record';

-- 用户管理子路由
UPDATE sys_route SET component = 'view.user-manage_list' WHERE route_id = 'user_list';
UPDATE sys_route SET component = 'view.user-manage_role' WHERE route_id = 'user_role';
UPDATE sys_route SET component = 'view.user-manage_permission' WHERE route_id = 'user_permission';

-- 申请管理子路由
UPDATE sys_route SET component = 'view.application_approval' WHERE route_id = 'application_approval';
UPDATE sys_route SET component = 'view.application_history' WHERE route_id = 'application_history';

-- 系统设置子路由
UPDATE sys_route SET component = 'view.system_amt' WHERE route_id = 'system_amt';
UPDATE sys_route SET component = 'view.system_agent' WHERE route_id = 'system_agent';
UPDATE sys_route SET component = 'view.system_log' WHERE route_id = 'system_log';

-- 我的设备子路由
UPDATE sys_route SET component = 'view.my-device_status' WHERE route_id = 'my_device_status';
UPDATE sys_route SET component = 'view.my-device_power-control' WHERE route_id = 'my_device_power';
UPDATE sys_route SET component = 'view.my-device_remote-control' WHERE route_id = 'my_device_remote';

-- 设备申请子路由
UPDATE sys_route SET component = 'view.my-application_apply' WHERE route_id = 'my_application_apply';
UPDATE sys_route SET component = 'view.my-application_my-list' WHERE route_id = 'my_application_list';

-- ============================================
-- 第三步：更新角色菜单配置（最关键！）
-- 必须包含所有父路由和子路由的 name
-- ============================================

UPDATE sys_role 
SET menus = 'device,device_list,device_group,device_monitor,device_online,device_power,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_agent,system_amt,system_log,my-device,my-device_status,my-device_remote-control,my-device_power-control,my-application,my-application_apply,my-application_my-list'
WHERE role_code = 'R_SUPER';

UPDATE sys_role 
SET menus = 'device,device_list,device_group,device_monitor,device_online,device_power,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 = 'my-device,my-device_status,my-device_remote-control,my-device_power-control,my-application,my-application_apply,my-application_my-list'
WHERE role_code = 'R_USER';

-- ============================================
-- 第四步：验证修复结果
-- ============================================

SELECT '========================================' AS '';
SELECT '用户管理路由配置' AS '';
SELECT '========================================' AS '';
SELECT route_id, name, path, component 
FROM sys_route 
WHERE route_id IN ('user_manage', 'user_list', 'user_role', 'user_permission')
ORDER BY order_num;

SELECT '' AS '';
SELECT '========================================' AS '';
SELECT 'R_SUPER 角色菜单配置（前200字符）' AS '';
SELECT '========================================' AS '';
SELECT role_code, role_name, SUBSTRING(menus, 1, 200) AS menus_preview
FROM sys_role 
WHERE role_code = 'R_SUPER';

SELECT '' AS '';
SELECT '========================================' AS '';
SELECT '检查子路由是否在角色配置中' AS '';
SELECT '========================================' AS '';
SELECT 
    r.name AS route_name,
    CASE 
        WHEN role.menus LIKE CONCAT('%', r.name, '%') THEN '✓ YES'
        ELSE '✗ NO - 这会导致路由被过滤！'
    END AS in_role_config
FROM sys_route r
CROSS JOIN sys_role role
WHERE role.role_code = 'R_SUPER'
  AND r.route_id IN ('user_manage', 'user_list', 'user_role', 'user_permission')
ORDER BY r.order_num;
