-- 快速修复 delivery:edit 权限问题
-- 1. 删除可能存在的重复权限
DELETE FROM sys_menu WHERE authority = 'delivery:edit' AND id NOT IN (
    SELECT menu_id FROM sys_role_menu WHERE role_id = 1
);

-- 2. 确保 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');

-- 3. 确保超级管理员有 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')
);

-- 4. 验证结果
SELECT 
    u.mobile,
    u.name,
    r.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 = 'delivery:edit';
