-- ============================================
-- 品控访厂菜单权限 SQL 脚本
-- 创建时间: 2026-06-21
-- 依赖: 风控平台菜单 risk_menu.sql 已执行,@risk_parent_id 为风控平台目录 ID
-- 关联脚本: risk_quality_menu.sql(品控巡检菜单)、risk_quality_factory_visit.sql(表结构)
-- ============================================

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

-- 1. 品控访厂主菜单
INSERT 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, 14, 'factoryVisit', 'risk/factoryVisit/index', 1, 0, 'C', '0', '0', 'risk:factoryVisit:list', '#', 103, 1, NOW(), '品控访厂管理菜单');

SET @factory_visit_id = LAST_INSERT_ID();

-- 2. 品控访厂按钮权限(列表/新增/修改/删除/导出/填写报告/审核)
INSERT 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
('品控访厂查询', @factory_visit_id, 1, '#', '', 1, 0, 'F', '0', '0', 'risk:factoryVisit:list', '#', 103, 1, NOW(), ''),
('品控访厂新增', @factory_visit_id, 2, '#', '', 1, 0, 'F', '0', '0', 'risk:factoryVisit:add', '#', 103, 1, NOW(), ''),
('品控访厂修改', @factory_visit_id, 3, '#', '', 1, 0, 'F', '0', '0', 'risk:factoryVisit:edit', '#', 103, 1, NOW(), ''),
('品控访厂删除', @factory_visit_id, 4, '#', '', 1, 0, 'F', '0', '0', 'risk:factoryVisit:remove', '#', 103, 1, NOW(), ''),
('品控访厂导出', @factory_visit_id, 5, '#', '', 1, 0, 'F', '0', '0', 'risk:factoryVisit:export', '#', 103, 1, NOW(), ''),
('品控访厂填写报告', @factory_visit_id, 6, '#', '', 1, 0, 'F', '0', '0', 'risk:factoryVisit:report', '#', 103, 1, NOW(), ''),
('品控访厂审核', @factory_visit_id, 7, '#', '', 1, 0, 'F', '0', '0', 'risk:factoryVisit:audit', '#', 103, 1, NOW(), '');

-- 3. 给超级管理员角色授权所有品控访厂权限
INSERT INTO sys_role_menu (role_id, menu_id)
SELECT 1, menu_id FROM sys_menu
WHERE perms LIKE 'risk:factoryVisit:%'
ON DUPLICATE KEY UPDATE role_id = role_id;

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

-- 完成提示
SELECT '品控访厂菜单权限初始化完成!' AS message,
       COUNT(*) AS total_menus
FROM sys_menu
WHERE perms LIKE 'risk:factoryVisit:%';
