-- 完整检查权限分配和修复
-- 1. 检查超级管理员用户信息
SELECT 
    u.id,
    u.mobile,
    u.name,
    u.role_id,
    r.name as role_name
FROM sys_user u
LEFT JOIN sys_role r ON u.role_id = r.id
WHERE u.mobile = '15900000000';

-- 2. 检查 delivery:edit 权限是否存在
SELECT 
    id,
    name,
    authority,
    parent_id,
    type
FROM sys_menu 
WHERE authority = 'delivery:edit';

-- 3. 如果不存在，创建 delivery:edit 权限
INSERT INTO sys_menu (parent_id, type, name, authority, sort, is_delete, create_time)
SELECT 
    (SELECT id FROM sys_menu WHERE route_url LIKE '%delivery%' OR route_url LIKE '%loading%' LIMIT 1),
    2, 
    '运送清单编辑', 
    'delivery:edit', 
    3, 
    0, 
    NOW()
WHERE NOT EXISTS (SELECT 1 FROM sys_menu WHERE authority = 'delivery:edit');

-- 4. 确保超级管理员角色有 delivery:edit 权限
INSERT INTO sys_role_menu (role_id, menu_id)
SELECT 
    1, -- 超级管理员角色ID
    (SELECT id FROM sys_menu WHERE authority = 'delivery:edit')
WHERE NOT EXISTS (
    SELECT 1 FROM sys_role_menu rm 
    WHERE rm.role_id = 1 AND rm.menu_id = (SELECT id FROM sys_menu WHERE authority = 'delivery:edit')
);

-- 5. 检查所有 delivery 相关权限
SELECT 
    u.mobile,
    u.name,
    r.name as role_name,
    m.name as menu_name,
    m.authority
FROM sys_user u
LEFT JOIN sys_role r ON u.role_id = r.id
LEFT JOIN sys_role_menu rm ON r.id = rm.role_id
LEFT JOIN sys_menu m ON rm.menu_id = m.id
WHERE u.mobile = '15900000000'
  AND m.authority LIKE '%delivery%'
ORDER BY m.authority;

-- 6. 检查是否有通配符权限
SELECT 
    u.mobile,
    u.name,
    r.name as role_name,
    m.name as menu_name,
    m.authority
FROM sys_user u
LEFT JOIN sys_role r ON u.role_id = r.id
LEFT JOIN sys_role_menu rm ON r.id = rm.role_id
LEFT JOIN sys_menu m ON rm.menu_id = m.id
WHERE u.mobile = '15900000000'
  AND m.authority = '*:*:*'
ORDER BY m.authority;
