-- 测试403修复是否成功
USE soybean_admin;

-- 1. 检查角色配置
SELECT '=== 角色菜单配置 ===' AS info;
SELECT 
    role_code AS '角色代码',
    role_name AS '角色名称',
    CASE 
        WHEN menus LIKE '%home%' THEN '✓ 包含home'
        ELSE '✗ 缺少home'
    END AS 'home权限',
    CHAR_LENGTH(menus) - CHAR_LENGTH(REPLACE(menus, ',', '')) + 1 AS '菜单数量',
    menus AS '配置的菜单'
FROM sys_role
WHERE status = 1
ORDER BY role_code;

-- 2. 检查用户配置
SELECT '=== 用户角色配置 ===' AS info;
SELECT 
    user_id AS '用户ID',
    user_name AS '用户名',
    roles AS '角色',
    status AS '状态'
FROM sys_user
WHERE deleted = 0
ORDER BY user_id;

-- 3. 检查路由名称
SELECT '=== 路由名称检查 ===' AS info;
SELECT 
    name AS '路由名称',
    path AS '路由路径',
    CASE 
        WHEN name LIKE '%-%' AND name NOT LIKE '%_%' THEN '父路由（连字符）'
        WHEN name LIKE '%_%' THEN '子路由（下划线）'
        ELSE '单级路由'
    END AS '路由类型',
    component AS '组件'
FROM sys_route
WHERE status = 1
ORDER BY order_num;

-- 4. 验证关键路由是否存在
SELECT '=== 关键路由验证 ===' AS info;
SELECT 
    'home' AS '路由名称',
    CASE WHEN EXISTS(SELECT 1 FROM sys_route WHERE name = 'home' AND status = 1) 
        THEN '✓ 存在' ELSE '✗ 不存在' END AS '状态'
UNION ALL
SELECT 
    'device',
    CASE WHEN EXISTS(SELECT 1 FROM sys_route WHERE name = 'device' AND status = 1) 
        THEN '✓ 存在' ELSE '✗ 不存在' END
UNION ALL
SELECT 
    'device_list',
    CASE WHEN EXISTS(SELECT 1 FROM sys_route WHERE name = 'device_list' AND status = 1) 
        THEN '✓ 存在' ELSE '✗ 不存在' END
UNION ALL
SELECT 
    'my-device',
    CASE WHEN EXISTS(SELECT 1 FROM sys_route WHERE name = 'my-device' AND status = 1) 
        THEN '✓ 存在' ELSE '✗ 不存在' END
UNION ALL
SELECT 
    'my-device_status',
    CASE WHEN EXISTS(SELECT 1 FROM sys_route WHERE name = 'my-device_status' AND status = 1) 
        THEN '✓ 存在' ELSE '✗ 不存在' END;

-- 5. 检查角色菜单与路由的匹配情况
SELECT '=== R_USER 角色菜单匹配检查 ===' AS info;
SELECT 
    SUBSTRING_INDEX(SUBSTRING_INDEX(menus, ',', numbers.n), ',', -1) AS '配置的菜单',
    CASE 
        WHEN EXISTS(
            SELECT 1 FROM sys_route 
            WHERE name = SUBSTRING_INDEX(SUBSTRING_INDEX(menus, ',', numbers.n), ',', -1)
            AND status = 1
        ) THEN '✓ 路由存在'
        ELSE '✗ 路由不存在'
    END AS '匹配状态'
FROM sys_role
JOIN (
    SELECT 1 n UNION ALL SELECT 2 UNION ALL SELECT 3 UNION ALL SELECT 4 UNION ALL 
    SELECT 5 UNION ALL SELECT 6 UNION ALL SELECT 7 UNION ALL SELECT 8 UNION ALL 
    SELECT 9 UNION ALL SELECT 10 UNION ALL SELECT 11 UNION ALL SELECT 12
) numbers
WHERE role_code = 'R_USER'
AND CHAR_LENGTH(menus) - CHAR_LENGTH(REPLACE(menus, ',', '')) >= numbers.n - 1
ORDER BY numbers.n;
