-- PROD 数据修复: 清理 UUID 迁移残留 + 同步 QA 结构
USE mokeegateway;

-- 1. 清理孤儿关联记录(user_id 还是旧 BIGINT 值, 已无对应真实用户)
DELETE FROM sys_user_system WHERE user_id NOT IN (SELECT id FROM sys_user);
DELETE FROM sys_user_role WHERE user_id NOT IN (SELECT id FROM sys_user);
DELETE FROM sys_role_menu WHERE role_id NOT IN (SELECT id FROM sys_role);
DELETE FROM sys_role_menu WHERE menu_id NOT IN (SELECT id FROM sys_menu);

-- 2. 清理多余的孤儿角色(不属于任何现有系统)
DELETE FROM sys_role WHERE system_id NOT IN (SELECT id FROM sys_system);

-- 3. 补充 admin → gateway 系统绑定(如果缺失)
INSERT IGNORE INTO sys_user_system (id, user_id, system_id)
SELECT REPLACE(UUID(),'-',''), u.id, s.id
FROM sys_user u, sys_system s
WHERE u.username='admin' AND s.system_code='gateway';

-- 4. 补充 admin → gateway super_admin 角色(如果缺失)
INSERT IGNORE INTO sys_user_role (id, user_id, role_id)
SELECT REPLACE(UUID(),'-',''), u.id, r.id
FROM sys_user u, sys_role r, sys_system s
WHERE u.username='admin' AND r.role_code='super_admin' AND r.system_id=s.id AND s.system_code='gateway';

-- 5. 补充按钮权限(如果缺失)
INSERT IGNORE INTO sys_menu (id, system_id, parent_id, menu_name, menu_type, sort_order, status, permission)
SELECT REPLACE(UUID(),'-',''), s.id, m.id, '新增系统', 3, 1, 1, 'system:system:add'
FROM sys_system s, sys_menu m WHERE s.system_code='gateway' AND m.system_id=s.id AND m.menu_name='系统列表';
INSERT IGNORE INTO sys_menu (id, system_id, parent_id, menu_name, menu_type, sort_order, status, permission)
SELECT REPLACE(UUID(),'-',''), s.id, m.id, '编辑系统', 3, 2, 1, 'system:system:edit'
FROM sys_system s, sys_menu m WHERE s.system_code='gateway' AND m.system_id=s.id AND m.menu_name='系统列表';
INSERT IGNORE INTO sys_menu (id, system_id, parent_id, menu_name, menu_type, sort_order, status, permission)
SELECT REPLACE(UUID(),'-',''), s.id, m.id, '删除系统', 3, 3, 1, 'system:system:delete'
FROM sys_system s, sys_menu m WHERE s.system_code='gateway' AND m.system_id=s.id AND m.menu_name='系统列表';
INSERT IGNORE INTO sys_menu (id, system_id, parent_id, menu_name, menu_type, sort_order, status, permission)
SELECT REPLACE(UUID(),'-',''), s.id, m.id, '新增用户', 3, 1, 1, 'system:user:add'
FROM sys_system s, sys_menu m WHERE s.system_code='gateway' AND m.system_id=s.id AND m.menu_name='用户管理';
INSERT IGNORE INTO sys_menu (id, system_id, parent_id, menu_name, menu_type, sort_order, status, permission)
SELECT REPLACE(UUID(),'-',''), s.id, m.id, '编辑用户', 3, 2, 1, 'system:user:edit'
FROM sys_system s, sys_menu m WHERE s.system_code='gateway' AND m.system_id=s.id AND m.menu_name='用户管理';
INSERT IGNORE INTO sys_menu (id, system_id, parent_id, menu_name, menu_type, sort_order, status, permission)
SELECT REPLACE(UUID(),'-',''), s.id, m.id, '删除用户', 3, 3, 1, 'system:user:delete'
FROM sys_system s, sys_menu m WHERE s.system_code='gateway' AND m.system_id=s.id AND m.menu_name='用户管理';
INSERT IGNORE INTO sys_menu (id, system_id, parent_id, menu_name, menu_type, sort_order, status, permission)
SELECT REPLACE(UUID(),'-',''), s.id, m.id, '新增角色', 3, 1, 1, 'system:role:add'
FROM sys_system s, sys_menu m WHERE s.system_code='gateway' AND m.system_id=s.id AND m.menu_name='角色管理';
INSERT IGNORE INTO sys_menu (id, system_id, parent_id, menu_name, menu_type, sort_order, status, permission)
SELECT REPLACE(UUID(),'-',''), s.id, m.id, '编辑角色', 3, 2, 1, 'system:role:edit'
FROM sys_system s, sys_menu m WHERE s.system_code='gateway' AND m.system_id=s.id AND m.menu_name='角色管理';
INSERT IGNORE INTO sys_menu (id, system_id, parent_id, menu_name, menu_type, sort_order, status, permission)
SELECT REPLACE(UUID(),'-',''), s.id, m.id, '删除角色', 3, 3, 1, 'system:role:delete'
FROM sys_system s, sys_menu m WHERE s.system_code='gateway' AND m.system_id=s.id AND m.menu_name='角色管理';
INSERT IGNORE INTO sys_menu (id, system_id, parent_id, menu_name, menu_type, sort_order, status, permission)
SELECT REPLACE(UUID(),'-',''), s.id, m.id, '分配菜单', 3, 4, 1, 'system:role:assignMenu'
FROM sys_system s, sys_menu m WHERE s.system_code='gateway' AND m.system_id=s.id AND m.menu_name='角色管理';
INSERT IGNORE INTO sys_menu (id, system_id, parent_id, menu_name, menu_type, sort_order, status, permission)
SELECT REPLACE(UUID(),'-',''), s.id, m.id, '新增菜单', 3, 1, 1, 'system:menu:add'
FROM sys_system s, sys_menu m WHERE s.system_code='gateway' AND m.system_id=s.id AND m.menu_name='菜单管理';
INSERT IGNORE INTO sys_menu (id, system_id, parent_id, menu_name, menu_type, sort_order, status, permission)
SELECT REPLACE(UUID(),'-',''), s.id, m.id, '编辑菜单', 3, 2, 1, 'system:menu:edit'
FROM sys_system s, sys_menu m WHERE s.system_code='gateway' AND m.system_id=s.id AND m.menu_name='菜单管理';
INSERT IGNORE INTO sys_menu (id, system_id, parent_id, menu_name, menu_type, sort_order, status, permission)
SELECT REPLACE(UUID(),'-',''), s.id, m.id, '删除菜单', 3, 3, 1, 'system:menu:delete'
FROM sys_system s, sys_menu m WHERE s.system_code='gateway' AND m.system_id=s.id AND m.menu_name='菜单管理';
INSERT IGNORE INTO sys_menu (id, system_id, parent_id, menu_name, menu_type, sort_order, status, permission)
SELECT REPLACE(UUID(),'-',''), s.id, m.id, '新增接口', 3, 1, 1, 'system:api:add'
FROM sys_system s, sys_menu m WHERE s.system_code='gateway' AND m.system_id=s.id AND m.menu_name='接口管理';
INSERT IGNORE INTO sys_menu (id, system_id, parent_id, menu_name, menu_type, sort_order, status, permission)
SELECT REPLACE(UUID(),'-',''), s.id, m.id, '编辑接口', 3, 2, 1, 'system:api:edit'
FROM sys_system s, sys_menu m WHERE s.system_code='gateway' AND m.system_id=s.id AND m.menu_name='接口管理';
INSERT IGNORE INTO sys_menu (id, system_id, parent_id, menu_name, menu_type, sort_order, status, permission)
SELECT REPLACE(UUID(),'-',''), s.id, m.id, '删除接口', 3, 3, 1, 'system:api:delete'
FROM sys_system s, sys_menu m WHERE s.system_code='gateway' AND m.system_id=s.id AND m.menu_name='接口管理';
INSERT IGNORE INTO sys_menu (id, system_id, parent_id, menu_name, menu_type, sort_order, status, permission)
SELECT REPLACE(UUID(),'-',''), s.id, m.id, '新增字典', 3, 1, 1, 'system:dict:add'
FROM sys_system s, sys_menu m WHERE s.system_code='gateway' AND m.system_id=s.id AND m.menu_name='数据字典';
INSERT IGNORE INTO sys_menu (id, system_id, parent_id, menu_name, menu_type, sort_order, status, permission)
SELECT REPLACE(UUID(),'-',''), s.id, m.id, '编辑字典', 3, 2, 1, 'system:dict:edit'
FROM sys_system s, sys_menu m WHERE s.system_code='gateway' AND m.system_id=s.id AND m.menu_name='数据字典';
INSERT IGNORE INTO sys_menu (id, system_id, parent_id, menu_name, menu_type, sort_order, status, permission)
SELECT REPLACE(UUID(),'-',''), s.id, m.id, '删除字典', 3, 3, 1, 'system:dict:delete'
FROM sys_system s, sys_menu m WHERE s.system_code='gateway' AND m.system_id=s.id AND m.menu_name='数据字典';

-- 6. 给 super_admin 角色补全所有菜单绑定
INSERT IGNORE INTO sys_role_menu (id, role_id, menu_id)
SELECT REPLACE(UUID(),'-',''), r.id, m.id
FROM sys_role r, sys_menu m, sys_system s
WHERE r.role_code='super_admin' AND r.system_id=s.id AND m.system_id=s.id AND s.system_code='gateway';
