-- 工单路由跳转问题一键修复脚本
-- 执行此脚本前请先备份数据库！
-- mysqldump -u your_username -p your_database sys_menu > sys_menu_backup.sql

-- ============================================
-- 步骤1：查看修复前的菜单配置
-- ============================================
SELECT '=== 修复前的工单管理菜单配置 ===' as info;
SELECT 
    menu_id,
    menu_name,
    parent_id,
    order_num,
    path,
    component,
    query,
    menu_type,
    visible,
    status
FROM sys_menu
WHERE menu_id = 2098  -- 工单管理父菜单
   OR parent_id = 2098  -- 工单管理的子菜单
ORDER BY parent_id, order_num;

-- ============================================
-- 步骤2：删除错误的状态子菜单
-- ============================================
SELECT '=== 开始删除错误的子菜单 ===' as info;

-- 删除所有状态相关的错误子菜单
DELETE FROM sys_menu
WHERE parent_id = 2098
  AND menu_type = 'C'
  AND menu_name IN ('进行中', '已完成', '测试中', '待领取', '一审中', '二审中', '三审中', '全部工单');

-- 删除包含错误路径的菜单
DELETE FROM sys_menu
WHERE parent_id = 2098
  AND path IN ('testOrder01', 'testWorkOrder02', 'testOrder', 'testWorkOrder');

-- 删除组件路径错误的菜单（指向test_order而不是test_work_order）
DELETE FROM sys_menu
WHERE parent_id = 2098
  AND component LIKE '%test_order%'
  AND component NOT LIKE '%test_work_order%';

SELECT '=== 错误子菜单已删除 ===' as info;

-- ============================================
-- 步骤3：修复父菜单路径
-- ============================================
SELECT '=== 开始修复父菜单路径 ===' as info;

UPDATE sys_menu
SET 
    path = 'test_work_order',
    update_time = NOW()
WHERE menu_id = 2098
  AND path != 'test_work_order';

SELECT '=== 父菜单路径已修复 ===' as info;

-- ============================================
-- 步骤4：确保测试工单子菜单配置正确
-- ============================================
SELECT '=== 检查测试工单子菜单配置 ===' as info;

UPDATE sys_menu
SET 
    path = 'test_work_order',
    component = 'system/test_work_order/index',
    update_time = NOW()
WHERE menu_id = 2092;

SELECT '=== 测试工单子菜单已修复 ===' as info;

-- ============================================
-- 步骤5：验证修复结果
-- ============================================
SELECT '=== 修复后的工单管理菜单配置 ===' as info;
SELECT 
    m1.menu_id as parent_id,
    m1.menu_name as parent_name,
    m1.path as parent_path,
    m1.menu_type as parent_type,
    m2.menu_id as child_id,
    m2.menu_name as child_name,
    m2.path as child_path,
    m2.component as child_component,
    m2.query as child_query,
    m2.menu_type as child_type,
    m2.order_num as child_order
FROM sys_menu m1
LEFT JOIN sys_menu m2 ON m1.menu_id = m2.parent_id
WHERE m1.menu_id = 2098
ORDER BY m2.order_num;

-- ============================================
-- 步骤6：检查是否还有其他错误配置
-- ============================================
SELECT '=== 检查是否还有其他错误配置 ===' as info;

-- 查找所有包含 testOrder 或 testWorkOrder 的路径
SELECT 
    menu_id,
    menu_name,
    parent_id,
    path,
    component,
    '路径包含testOrder或testWorkOrder' as issue
FROM sys_menu
WHERE (path LIKE '%testOrder%' OR path LIKE '%testWorkOrder%')
  AND path != 'test_work_order'
  AND menu_id NOT IN (2098, 2092);

-- 查找组件路径指向test_order的菜单
SELECT 
    menu_id,
    menu_name,
    parent_id,
    path,
    component,
    '组件路径错误' as issue
FROM sys_menu
WHERE component LIKE '%test_order%'
  AND component NOT LIKE '%test_work_order%'
  AND menu_name LIKE '%工单%';

-- ============================================
-- 完成提示
-- ============================================
SELECT '=== 修复完成！===' as info;
SELECT '请执行以下操作：' as info;
SELECT '1. 清除Redis缓存（如果使用）：redis-cli FLUSHDB' as step;
SELECT '2. 重启后端服务' as step;
SELECT '3. 清除浏览器缓存：Ctrl+Shift+R' as step;
SELECT '4. 退出并重新登录系统' as step;
SELECT '5. 测试URL：http://your-domain/system/test_work_order' as step;
