-- ========================================
-- 心理测评系统完整菜单配置
-- 创建时间: 2025-11-01
-- 说明：包含清理、插入和权限分配，一键执行
-- ========================================

SET NAMES utf8mb4;
SET FOREIGN_KEY_CHECKS = 0;

-- ========================================
-- 第一步：清理旧数据
-- ========================================

-- 删除角色菜单关联
DELETE rm.* FROM sys_role_menu rm
INNER JOIN sys_menu m ON rm.menu_id = m.menu_id
WHERE m.menu_name LIKE '%心理%' 
   OR m.menu_name LIKE '%量表%' 
   OR m.menu_name LIKE '%题目%' 
   OR m.menu_name LIKE '%因子%' 
   OR m.menu_name LIKE '%测评%'
   OR m.menu_name LIKE '%报告%'
   OR m.menu_name LIKE '%解释%'
   OR m.menu_name LIKE '%档案%'
   OR m.menu_name LIKE '%问卷%'
   OR m.menu_name LIKE '%二维码%';

-- 删除心理测评相关菜单
DELETE 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 '%二维码%';

-- ========================================
-- 第二步：插入新菜单
-- ========================================

-- 1. 心理测评管理目录（父菜单）
INSERT INTO sys_menu VALUES 
(NULL, '心理测评管理', 0, 4, 'psychology', NULL, '', '', 1, 0, 'M', '0', '0', '', 'form', 'admin', NOW(), '', NULL, '心理测评管理目录');

SET @parent_id = LAST_INSERT_ID();

-- 2. 量表管理菜单
INSERT INTO sys_menu VALUES 
(NULL, '量表管理', @parent_id, 1, 'scale', 'psychology/scale/index', '', '', 1, 0, 'C', '0', '0', 'psychology:scale:list', 'list', 'admin', NOW(), '', NULL, '量表管理菜单');

SET @scale_id = LAST_INSERT_ID();

-- 3. 量表按钮
INSERT INTO sys_menu VALUES 
(NULL, '量表查询', @scale_id, 1, '', '', '', '', 1, 0, 'F', '0', '0', 'psychology:scale:query', '#', 'admin', NOW(), '', NULL, ''),
(NULL, '量表新增', @scale_id, 2, '', '', '', '', 1, 0, 'F', '0', '0', 'psychology:scale:add', '#', 'admin', NOW(), '', NULL, ''),
(NULL, '量表修改', @scale_id, 3, '', '', '', '', 1, 0, 'F', '0', '0', 'psychology:scale:edit', '#', 'admin', NOW(), '', NULL, ''),
(NULL, '量表删除', @scale_id, 4, '', '', '', '', 1, 0, 'F', '0', '0', 'psychology:scale:remove', '#', 'admin', NOW(), '', NULL, '');

-- 4. 题目管理菜单（隐藏）
INSERT INTO sys_menu VALUES 
(NULL, '题目管理', @scale_id, 2, 'item', 'psychology/scale/item', '', '', 1, 0, 'C', '1', '0', 'psychology:item:list', 'edit', 'admin', NOW(), '', NULL, '题目管理菜单（隐藏）');

SET @item_id = LAST_INSERT_ID();

-- 5. 题目按钮
INSERT INTO sys_menu VALUES 
(NULL, '题目查询', @item_id, 1, '', '', '', '', 1, 0, 'F', '1', '0', 'psychology:item:query', '#', 'admin', NOW(), '', NULL, ''),
(NULL, '题目新增', @item_id, 2, '', '', '', '', 1, 0, 'F', '1', '0', 'psychology:item:add', '#', 'admin', NOW(), '', NULL, ''),
(NULL, '题目修改', @item_id, 3, '', '', '', '', 1, 0, 'F', '1', '0', 'psychology:item:edit', '#', 'admin', NOW(), '', NULL, ''),
(NULL, '题目删除', @item_id, 4, '', '', '', '', 1, 0, 'F', '1', '0', 'psychology:item:remove', '#', 'admin', NOW(), '', NULL, ''),
(NULL, '选项查询', @item_id, 5, '', '', '', '', 1, 0, 'F', '1', '0', 'psychology:option:query', '#', 'admin', NOW(), '', NULL, ''),
(NULL, '选项新增', @item_id, 6, '', '', '', '', 1, 0, 'F', '1', '0', 'psychology:option:add', '#', 'admin', NOW(), '', NULL, ''),
(NULL, '选项删除', @item_id, 7, '', '', '', '', 1, 0, 'F', '1', '0', 'psychology:option:remove', '#', 'admin', NOW(), '', NULL, '');

-- 6. 因子管理菜单（隐藏）
INSERT INTO sys_menu VALUES 
(NULL, '因子管理', @scale_id, 3, 'factor', 'psychology/scale/factor', '', '', 1, 0, 'C', '1', '0', 'psychology:factor:list', 'tree', 'admin', NOW(), '', NULL, '因子管理菜单（隐藏）');

SET @factor_id = LAST_INSERT_ID();

-- 7. 因子按钮
INSERT INTO sys_menu VALUES 
(NULL, '因子查询', @factor_id, 1, '', '', '', '', 1, 0, 'F', '1', '0', 'psychology:factor:query', '#', 'admin', NOW(), '', NULL, ''),
(NULL, '因子新增', @factor_id, 2, '', '', '', '', 1, 0, 'F', '1', '0', 'psychology:factor:add', '#', 'admin', NOW(), '', NULL, ''),
(NULL, '因子修改', @factor_id, 3, '', '', '', '', 1, 0, 'F', '1', '0', 'psychology:factor:edit', '#', 'admin', NOW(), '', NULL, ''),
(NULL, '因子删除', @factor_id, 4, '', '', '', '', 1, 0, 'F', '1', '0', 'psychology:factor:remove', '#', 'admin', NOW(), '', NULL, ''),
(NULL, '规则查询', @factor_id, 5, '', '', '', '', 1, 0, 'F', '1', '0', 'psychology:factorrule:query', '#', 'admin', NOW(), '', NULL, ''),
(NULL, '规则新增', @factor_id, 6, '', '', '', '', 1, 0, 'F', '1', '0', 'psychology:factorrule:add', '#', 'admin', NOW(), '', NULL, ''),
(NULL, '规则删除', @factor_id, 7, '', '', '', '', 1, 0, 'F', '1', '0', 'psychology:factorrule:remove', '#', 'admin', NOW(), '', NULL, '');

-- 8. 测评管理菜单
INSERT INTO sys_menu VALUES 
(NULL, '测评管理', @parent_id, 2, 'assessment', 'psychology/assessment/index', '', '', 1, 0, 'C', '0', '0', 'psychology:assessment:list', 'documentation', 'admin', NOW(), '', NULL, '测评管理菜单');

SET @assessment_id = LAST_INSERT_ID();

-- 9. 测评按钮
INSERT INTO sys_menu VALUES 
(NULL, '测评查询', @assessment_id, 1, '', '', '', '', 1, 0, 'F', '0', '0', 'psychology:assessment:query', '#', 'admin', NOW(), '', NULL, ''),
(NULL, '测评新增', @assessment_id, 2, '', '', '', '', 1, 0, 'F', '0', '0', 'psychology:assessment:add', '#', 'admin', NOW(), '', NULL, ''),
(NULL, '测评修改', @assessment_id, 3, '', '', '', '', 1, 0, 'F', '0', '0', 'psychology:assessment:edit', '#', 'admin', NOW(), '', NULL, ''),
(NULL, '测评删除', @assessment_id, 4, '', '', '', '', 1, 0, 'F', '0', '0', 'psychology:assessment:remove', '#', 'admin', NOW(), '', NULL, '');

-- 10. 测评执行菜单（隐藏）
INSERT INTO sys_menu VALUES 
(NULL, '开始测评', @assessment_id, 5, 'start', 'psychology/assessment/start', '', '', 1, 0, 'C', '1', '0', 'psychology:assessment:add', 'edit', 'admin', NOW(), '', NULL, '开始测评页面（隐藏）'),
(NULL, '答题中', @assessment_id, 6, 'taking', 'psychology/assessment/taking', '', '', 1, 0, 'C', '1', '0', '', 'edit', 'admin', NOW(), '', NULL, '答题页面（隐藏）');

-- 11. 报告管理菜单
INSERT INTO sys_menu VALUES 
(NULL, '报告管理', @parent_id, 3, 'report', 'psychology/report/index', '', '', 1, 0, 'C', '0', '0', 'psychology:report:list', 'documentation', 'admin', NOW(), '', NULL, '报告管理菜单');

SET @report_id = LAST_INSERT_ID();

-- 12. 报告按钮
INSERT INTO sys_menu VALUES 
(NULL, '报告查询', @report_id, 1, '', '', '', '', 1, 0, 'F', '0', '0', 'psychology:report:query', '#', 'admin', NOW(), '', NULL, ''),
(NULL, '报告新增', @report_id, 2, '', '', '', '', 1, 0, 'F', '0', '0', 'psychology:report:add', '#', 'admin', NOW(), '', NULL, ''),
(NULL, '报告修改', @report_id, 3, '', '', '', '', 1, 0, 'F', '0', '0', 'psychology:report:edit', '#', 'admin', NOW(), '', NULL, ''),
(NULL, '报告删除', @report_id, 4, '', '', '', '', 1, 0, 'F', '0', '0', 'psychology:report:remove', '#', 'admin', NOW(), '', NULL, '');

-- 13. 报告详情页面（隐藏）
INSERT INTO sys_menu VALUES 
(NULL, '报告详情', @report_id, 5, 'detail', 'psychology/report/detail', '', '', 1, 0, 'C', '1', '0', 'psychology:report:query', 'eye', 'admin', NOW(), '', NULL, '报告详情页面（隐藏）');

-- 14. 结果解释配置菜单
INSERT INTO sys_menu VALUES 
(NULL, '解释配置', @parent_id, 4, 'interpretation', 'psychology/interpretation/index', '', '', 1, 0, 'C', '0', '0', 'psychology:interpretation:list', 'edit', 'admin', NOW(), '', NULL, '结果解释配置菜单');

SET @interpretation_id = LAST_INSERT_ID();

-- 15. 解释配置按钮
INSERT INTO sys_menu VALUES 
(NULL, '解释查询', @interpretation_id, 1, '', '', '', '', 1, 0, 'F', '0', '0', 'psychology:interpretation:query', '#', 'admin', NOW(), '', NULL, ''),
(NULL, '解释新增', @interpretation_id, 2, '', '', '', '', 1, 0, 'F', '0', '0', 'psychology:interpretation:add', '#', 'admin', NOW(), '', NULL, ''),
(NULL, '解释修改', @interpretation_id, 3, '', '', '', '', 1, 0, 'F', '0', '0', 'psychology:interpretation:edit', '#', 'admin', NOW(), '', NULL, ''),
(NULL, '解释删除', @interpretation_id, 4, '', '', '', '', 1, 0, 'F', '0', '0', 'psychology:interpretation:remove', '#', 'admin', NOW(), '', NULL, '');

-- 16. 用户档案管理菜单
INSERT INTO sys_menu VALUES 
(NULL, '用户档案', @parent_id, 5, 'profile', 'psychology/profile/index', '', '', 1, 0, 'C', '0', '0', 'psychology:profile:list', 'user', 'admin', NOW(), '', NULL, '用户档案管理菜单');

SET @profile_id = LAST_INSERT_ID();

-- 17. 用户档案按钮
INSERT INTO sys_menu VALUES 
(NULL, '档案查询', @profile_id, 1, '', '', '', '', 1, 0, 'F', '0', '0', 'psychology:profile:query', '#', 'admin', NOW(), '', NULL, ''),
(NULL, '档案新增', @profile_id, 2, '', '', '', '', 1, 0, 'F', '0', '0', 'psychology:profile:add', '#', 'admin', NOW(), '', NULL, ''),
(NULL, '档案修改', @profile_id, 3, '', '', '', '', 1, 0, 'F', '0', '0', 'psychology:profile:edit', '#', 'admin', NOW(), '', NULL, ''),
(NULL, '档案删除', @profile_id, 4, '', '', '', '', 1, 0, 'F', '0', '0', 'psychology:profile:remove', '#', 'admin', NOW(), '', NULL, '');

-- 18. 自定义问卷菜单
INSERT INTO sys_menu VALUES 
(NULL, '自定义问卷', @parent_id, 6, 'questionnaire', 'psychology/questionnaire/index', '', '', 1, 0, 'C', '0', '0', 'psychology:questionnaire:list', 'edit', 'admin', NOW(), '', NULL, '自定义问卷菜单');

SET @questionnaire_id = LAST_INSERT_ID();

-- 19. 自定义问卷按钮
INSERT INTO sys_menu VALUES 
(NULL, '问卷查询', @questionnaire_id, 1, '', '', '', '', 1, 0, 'F', '0', '0', 'psychology:questionnaire:query', '#', 'admin', NOW(), '', NULL, ''),
(NULL, '问卷新增', @questionnaire_id, 2, '', '', '', '', 1, 0, 'F', '0', '0', 'psychology:questionnaire:add', '#', 'admin', NOW(), '', NULL, ''),
(NULL, '问卷修改', @questionnaire_id, 3, '', '', '', '', 1, 0, 'F', '0', '0', 'psychology:questionnaire:edit', '#', 'admin', NOW(), '', NULL, ''),
(NULL, '问卷删除', @questionnaire_id, 4, '', '', '', '', 1, 0, 'F', '0', '0', 'psychology:questionnaire:remove', '#', 'admin', NOW(), '', NULL, '');

-- 20. 二维码管理菜单
INSERT INTO sys_menu VALUES 
(NULL, '二维码管理', @parent_id, 7, 'qrcode', 'psychology/qrcode/index', '', '', 1, 0, 'C', '0', '0', 'psychology:qrcode:list', 'qrcode', 'admin', NOW(), '', NULL, '二维码管理菜单');

SET @qrcode_menu_id = LAST_INSERT_ID();

-- 21. 二维码按钮
INSERT INTO sys_menu VALUES 
(NULL, '二维码查询', @qrcode_menu_id, 1, '', '', '', '', 1, 0, 'F', '0', '0', 'psychology:qrcode:query', '#', 'admin', NOW(), '', NULL, ''),
(NULL, '二维码新增', @qrcode_menu_id, 2, '', '', '', '', 1, 0, 'F', '0', '0', 'psychology:qrcode:add', '#', 'admin', NOW(), '', NULL, ''),
(NULL, '二维码修改', @qrcode_menu_id, 3, '', '', '', '', 1, 0, 'F', '0', '0', 'psychology:qrcode:edit', '#', 'admin', NOW(), '', NULL, ''),
(NULL, '二维码删除', @qrcode_menu_id, 4, '', '', '', '', 1, 0, 'F', '0', '0', 'psychology:qrcode:remove', '#', 'admin', NOW(), '', NULL, '');

SET FOREIGN_KEY_CHECKS = 1;

-- ========================================
-- 第三步：分配权限
-- ========================================

INSERT INTO sys_role_menu (role_id, menu_id)
SELECT 1, menu_id 
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 '%二维码%';

-- ========================================
-- 第四步：验证结果
-- ========================================

SELECT '配置完成！' AS result;
SELECT 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 '%二维码%';

SELECT menu_id, menu_name, parent_id, path, visible, status
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 '%二维码%'
ORDER BY parent_id, order_num;

