-- ============================================
-- 食品安全宣传菜单权限 SQL 脚本
-- 创建时间: 2026-07-08
-- 依赖: 风控平台菜单 risk_menu.sql 已执行,@risk_parent_id 为风控平台目录 ID
-- 关联脚本: risk_food_safety_campaign.sql(表结构+字典)
-- ============================================

-- 设置变量(指向已有风控平台目录)
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, 15, 'foodSafety', 'risk/foodSafety/index', 1, 0, 'C', '0', '0', 'risk:foodSafety:list', '#', 103, 1, NOW(), '食品安全宣传管理菜单');

SET @food_safety_id = LAST_INSERT_ID();

-- 2. 食品安全宣传按钮权限(列表/新增/修改/删除/导出/活动开始/活动结束/上传资料)
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
('食品安全宣传查询', @food_safety_id, 1, '#', '', 1, 0, 'F', '0', '0', 'risk:foodSafety:list', '#', 103, 1, NOW(), ''),
('食品安全宣传新增', @food_safety_id, 2, '#', '', 1, 0, 'F', '0', '0', 'risk:foodSafety:add', '#', 103, 1, NOW(), ''),
('食品安全宣传修改', @food_safety_id, 3, '#', '', 1, 0, 'F', '0', '0', 'risk:foodSafety:edit', '#', 103, 1, NOW(), ''),
('食品安全宣传删除', @food_safety_id, 4, '#', '', 1, 0, 'F', '0', '0', 'risk:foodSafety:remove', '#', 103, 1, NOW(), ''),
('食品安全宣传导出', @food_safety_id, 5, '#', '', 1, 0, 'F', '0', '0', 'risk:foodSafety:export', '#', 103, 1, NOW(), ''),
('食品安全宣传活动开始', @food_safety_id, 6, '#', '', 1, 0, 'F', '0', '0', 'risk:foodSafety:start', '#', 103, 1, NOW(), ''),
('食品安全宣传活动结束', @food_safety_id, 7, '#', '', 1, 0, 'F', '0', '0', 'risk:foodSafety:finish', '#', 103, 1, NOW(), ''),
('食品安全宣传上传资料', @food_safety_id, 8, '#', '', 1, 0, 'F', '0', '0', 'risk:foodSafety:uploadMaterial', '#', 103, 1, NOW(), '');

-- 3. 给超级管理员角色授权所有食品安全宣传权限
INSERT INTO sys_role_menu (role_id, menu_id)
SELECT 1, menu_id FROM sys_menu
WHERE perms LIKE 'risk:foodSafety:%'
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:foodSafety:%'
ON DUPLICATE KEY UPDATE role_id = role_id;

-- 完成提示
SELECT '食品安全宣传菜单权限初始化完成!' AS message,
       COUNT(*) AS total_menus
FROM sys_menu
WHERE perms LIKE 'risk:foodSafety:%';