-- 最终完整修复所有路由配置
-- 此脚本将路由名称与前端 routeMap 保持一致
USE soybean_admin;

-- ============================================
-- 第一部分：修复所有路由的 name 字段
-- 规则：与前端 src/router/elegant/transform.ts 中的 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.{folder}_{subfolder}
-- ============================================

-- 设备管理
UPDATE sys_route SET component = 'layout.base' WHERE route_id = 'device';
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 = 'layout.base' WHERE route_id = 'screen';
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 = 'layout.base' WHERE route_id = 'user_manage';
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 = 'layout.base' WHERE route_id = 'application';
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 = 'layout.base' WHERE route_id = 'system';
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 = 'layout.base' WHERE route_id = 'my_device';
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 = 'layout.base' WHERE route_id = 'my_application';
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 info;
SELECT route_id, name, path, component, order_num 
FROM sys_route 
WHERE status = 1 
ORDER BY order_num;

SELECT '=== 角色菜单配置 ===' AS info;
SELECT role_code, role_name, menus FROM sys_role;
