-- ============================================
-- 品控平台菜单权限 SQL 脚本
-- 创建时间: 2026-06-21
-- 依赖: 风控平台菜单 risk_menu.sql 已执行,@risk_parent_id 为风控平台目录 ID
-- ============================================

-- 设置变量(指向已有风控平台目录,品控作为同级子菜单挂在风控平台目录下)
SET @risk_parent_id = (SELECT menu_id FROM sys_menu WHERE menu_name = '风控平台' AND menu_type = 'M' LIMIT 1);

-- 1. 品控巡检
INSERT IGNORE INTO sys_menu (menu_name, parent_id, order_num, path, component, is_frame, is_cache, menu_type, visible, status, perms, icon, create_dept, create_by, create_time, remark)
VALUES ('品控巡检', @risk_parent_id, 13, 'qualityInspectionTask', 'risk/qualityInspectionTask/index', 1, 0, 'C', '0', '0', 'risk:qualityInspectionTask:list', '#', 103, 1, NOW(), '品控巡检任务菜单');

SET @quality_inspection_task_id = LAST_INSERT_ID();

INSERT IGNORE INTO sys_menu (menu_name, parent_id, order_num, path, component, is_frame, is_cache, menu_type, visible, status, perms, icon, create_dept, create_by, create_time, remark) VALUES
('品控巡检查询', @quality_inspection_task_id, 1, '#', '', 1, 0, 'F', '0', '0', 'risk:qualityInspectionTask:query', '#', 103, 1, NOW(), ''),
('品控巡检新增', @quality_inspection_task_id, 2, '#', '', 1, 0, 'F', '0', '0', 'risk:qualityInspectionTask:add', '#', 103, 1, NOW(), ''),
('品控巡检修改', @quality_inspection_task_id, 3, '#', '', 1, 0, 'F', '0', '0', 'risk:qualityInspectionTask:edit', '#', 103, 1, NOW(), ''),
('品控巡检删除', @quality_inspection_task_id, 4, '#', '', 1, 0, 'F', '0', '0', 'risk:qualityInspectionTask:remove', '#', 103, 1, NOW(), ''),
('品控巡检导出', @quality_inspection_task_id, 5, '#', '', 1, 0, 'F', '0', '0', 'risk:qualityInspectionTask:export', '#', 103, 1, NOW(), ''),
('品控巡检记录审核', @quality_inspection_task_id, 6, '#', '', 1, 0, 'F', '0', '0', 'risk:qualityInspectionRecord:review', '#', 103, 1, NOW(), '');

-- 2. 给超级管理员角色授权所有品控平台权限
INSERT INTO sys_role_menu (role_id, menu_id)
SELECT 1, menu_id FROM sys_menu
WHERE perms LIKE 'risk:qualityInspectionTask:%'
   OR perms = 'risk:qualityInspectionRecord:review'
ON DUPLICATE KEY UPDATE role_id = role_id;

-- 3. 给租户管理员(角色ID 2,如果存在)也授权
INSERT INTO sys_role_menu (role_id, menu_id)
SELECT 2, menu_id FROM sys_menu
WHERE perms LIKE 'risk:qualityInspectionTask:%'
   OR perms = 'risk:qualityInspectionRecord:review'
ON DUPLICATE KEY UPDATE role_id = role_id;

-- 完成提示
SELECT '品控巡检菜单权限初始化完成!' AS message,
       COUNT(*) AS total_menus
FROM sys_menu
WHERE perms LIKE 'risk:qualityInspectionTask:%'
   OR perms = 'risk:qualityInspectionRecord:review';