-- =============================================
-- FIIH质检数据导入导出权限配置
-- 功能：为FIIH质检配置页面添加数据导入和导出按钮权限
-- =============================================

-- 获取fiihupgrade质检配置菜单的ID
SET @parent_menu_id = (SELECT menu_id FROM sys_menu WHERE perms = 'fiihupgrade:config:list' LIMIT 1);

-- 如果找不到，尝试通过菜单名称查找
SET @parent_menu_id = IFNULL(@parent_menu_id, (SELECT menu_id FROM sys_menu WHERE menu_name = '质检配置' AND path = 'mes/fiihupgrade/index' LIMIT 1));

-- 添加数据导入权限按钮
INSERT INTO sys_menu (menu_name, parent_id, order_num, path, component, is_frame, is_cache, menu_type, visible, status, perms, icon, create_by, create_time, remark)
SELECT '数据导入', @parent_menu_id, 10, '', NULL, 1, 0, 'F', '0', '0', 'fiihupgrade:data:import', '#', 'admin', NOW(), 'FIIH质检数据导入'
WHERE @parent_menu_id IS NOT NULL
  AND NOT EXISTS (SELECT 1 FROM sys_menu WHERE perms = 'fiihupgrade:data:import');

-- 添加数据导出权限按钮
INSERT INTO sys_menu (menu_name, parent_id, order_num, path, component, is_frame, is_cache, menu_type, visible, status, perms, icon, create_by, create_time, remark)
SELECT '数据导出', @parent_menu_id, 11, '', NULL, 1, 0, 'F', '0', '0', 'fiihupgrade:data:export', '#', 'admin', NOW(), 'FIIH质检数据导出'
WHERE @parent_menu_id IS NOT NULL
  AND NOT EXISTS (SELECT 1 FROM sys_menu WHERE perms = 'fiihupgrade:data:export');

-- 为管理员角色分配新权限（假设管理员角色ID为1）
INSERT INTO sys_role_menu (role_id, menu_id)
SELECT 1, menu_id FROM sys_menu WHERE perms = 'fiihupgrade:data:import'
  AND NOT EXISTS (SELECT 1 FROM sys_role_menu WHERE role_id = 1 AND menu_id = (SELECT menu_id FROM sys_menu WHERE perms = 'fiihupgrade:data:import'));

INSERT INTO sys_role_menu (role_id, menu_id)
SELECT 1, menu_id FROM sys_menu WHERE perms = 'fiihupgrade:data:export'
  AND NOT EXISTS (SELECT 1 FROM sys_role_menu WHERE role_id = 1 AND menu_id = (SELECT menu_id FROM sys_menu WHERE perms = 'fiihupgrade:data:export'));
