-- ========================================
-- 清理重复菜单SQL脚本
-- 用途：删除数据库中重复的心理学相关菜单
-- ========================================
USE ry_news;
SET NAMES utf8mb4;

-- ========================================
-- 1. 查找重复的菜单（基于menu_name和path组合）
-- ========================================
-- 先查看重复的菜单
SELECT menu_name, path, component, COUNT(*) as count
FROM sys_menu
WHERE menu_name LIKE '%心理%' 
   OR menu_name LIKE '%量表%' 
   OR menu_name LIKE '%题目%'
   OR menu_name LIKE '%因子%'
   OR menu_name LIKE '%测评%'
   OR menu_name LIKE '%网站%'
   OR menu_name LIKE '%栏目%'
   OR menu_name LIKE '%评论%'
   OR menu_name LIKE '%预警%'
   OR menu_name LIKE '%问卷%'
   OR menu_name LIKE '%档案%'
GROUP BY menu_name, path, component
HAVING count > 1;

-- ========================================
-- 2. 删除重复的菜单（保留menu_id最小的那个）
-- ========================================
-- 删除"心理测评管理"目录的重复项（保留第一个）
DELETE t1 FROM sys_menu t1
INNER JOIN sys_menu t2 
WHERE t1.menu_name = '心理测评管理' 
  AND t1.parent_id = 0
  AND t2.menu_name = '心理测评管理'
  AND t2.parent_id = 0
  AND t1.menu_id > t2.menu_id;

-- 删除"心理网站管理"目录的重复项
DELETE t1 FROM sys_menu t1
INNER JOIN sys_menu t2 
WHERE t1.menu_name = '心理网站管理' 
  AND t1.parent_id = 0
  AND t2.menu_name = '心理网站管理'
  AND t2.parent_id = 0
  AND t1.menu_id > t2.menu_id;

-- 删除基于menu_name和parent_id的重复菜单（优先处理）
DELETE t1 FROM sys_menu t1
INNER JOIN sys_menu t2 
WHERE t1.menu_name = t2.menu_name
  AND t1.parent_id = t2.parent_id
  AND t1.menu_id > t2.menu_id
  AND (t1.menu_name LIKE '%心理%' 
       OR t1.menu_name LIKE '%量表%' 
       OR t1.menu_name LIKE '%题目%'
       OR t1.menu_name LIKE '%因子%'
       OR t1.menu_name LIKE '%测评%'
       OR t1.menu_name LIKE '%报告%'
       OR t1.menu_name LIKE '%解释%'
       OR t1.menu_name LIKE '%档案%'
       OR t1.menu_name LIKE '%问卷%'
       OR t1.menu_name LIKE '%网站%'
       OR t1.menu_name LIKE '%栏目%'
       OR t1.menu_name LIKE '%评论%'
       OR t1.menu_name LIKE '%预警%'
       OR t1.menu_name LIKE '%规则%');

-- 删除其他重复菜单（基于path和component）
DELETE t1 FROM sys_menu t1
INNER JOIN sys_menu t2 
WHERE t1.path = t2.path 
  AND (t1.component = t2.component OR (t1.component IS NULL AND t2.component IS NULL))
  AND t1.menu_name = t2.menu_name
  AND t1.parent_id = t2.parent_id
  AND t1.menu_id > t2.menu_id
  AND (t1.menu_name LIKE '%心理%' 
       OR t1.menu_name LIKE '%量表%' 
       OR t1.menu_name LIKE '%题目%'
       OR t1.menu_name LIKE '%因子%'
       OR t1.menu_name LIKE '%测评%'
       OR t1.menu_name LIKE '%报告%'
       OR t1.menu_name LIKE '%解释%'
       OR t1.menu_name LIKE '%档案%'
       OR t1.menu_name LIKE '%问卷%'
       OR t1.menu_name LIKE '%网站%'
       OR t1.menu_name LIKE '%栏目%'
       OR t1.menu_name LIKE '%评论%'
       OR t1.menu_name LIKE '%预警%'
       OR t1.menu_name LIKE '%规则%');

-- ========================================
-- 3. 清理孤立的子菜单（父菜单已被删除）
-- ========================================
-- 删除那些父菜单ID不存在于sys_menu表中的子菜单
DELETE FROM sys_menu
WHERE parent_id > 0
  AND parent_id NOT IN (SELECT menu_id FROM (SELECT menu_id FROM sys_menu) AS temp)
  AND (menu_name LIKE '%心理%' 
       OR menu_name LIKE '%量表%' 
       OR menu_name LIKE '%题目%'
       OR menu_name LIKE '%因子%'
       OR menu_name LIKE '%测评%'
       OR menu_name LIKE '%报告%'
       OR menu_name LIKE '%解释%'
       OR menu_name LIKE '%档案%'
       OR menu_name LIKE '%问卷%'
       OR menu_name LIKE '%网站%'
       OR menu_name LIKE '%栏目%'
       OR menu_name LIKE '%评论%'
       OR menu_name LIKE '%预警%'
       OR menu_name LIKE '%规则%');

-- ========================================
-- 4. 清理角色菜单关联表中的孤立记录
-- ========================================
-- 删除指向已删除菜单的角色菜单关联
DELETE FROM sys_role_menu
WHERE menu_id NOT IN (SELECT menu_id FROM (SELECT menu_id FROM sys_menu) AS temp2);

-- ========================================
-- 5. 验证清理结果
-- ========================================
SELECT '清理完成！' AS result;

-- 检查是否还有重复菜单
SELECT 
    menu_name AS '菜单名称',
    path AS '路由路径',
    component AS '组件路径',
    parent_id AS '父菜单ID',
    COUNT(*) AS '剩余数量'
FROM sys_menu
WHERE menu_name LIKE '%心理%' 
   OR menu_name LIKE '%量表%' 
   OR menu_name LIKE '%题目%'
   OR menu_name LIKE '%因子%'
   OR menu_name LIKE '%测评%'
   OR menu_name LIKE '%报告%'
   OR menu_name LIKE '%解释%'
   OR menu_name LIKE '%档案%'
   OR menu_name LIKE '%问卷%'
   OR menu_name LIKE '%网站%'
   OR menu_name LIKE '%栏目%'
   OR menu_name LIKE '%评论%'
   OR menu_name LIKE '%预警%'
   OR menu_name LIKE '%规则%'
GROUP BY menu_name, path, component, parent_id
HAVING COUNT(*) > 1;

-- 如果没有输出，说明没有重复菜单了
SELECT '如果上面的查询没有返回结果，说明所有重复菜单已清理完成！' AS message;
