-- 确保超级管理员有 delivery:edit 权限
-- 1. 检查是否存在 delivery:edit 权限点
SELECT id, name, authority FROM sys_menu WHERE authority = 'delivery:edit';

-- 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 
    (SELECT id FROM sys_role WHERE role_name = '超级管理员' OR id = 1 LIMIT 1),
    (SELECT id FROM sys_menu WHERE authority = 'delivery:edit')
WHERE NOT EXISTS (
    SELECT 1 FROM sys_role_menu rm 
    INNER JOIN sys_role r ON rm.role_id = r.id 
    INNER JOIN sys_menu m ON rm.menu_id = m.id 
    WHERE r.role_name = '超级管理员' AND m.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';
