-- 为科研计划与国际合作功能添加菜单权限
USE ry-vue;

-- 先检查是否已存在该菜单
SET @existing_menu = (SELECT id FROM sys_menu WHERE menu_name = '科研计划与国际合作' AND parent_id = 1);

-- 如果不存在，插入新菜单
IF @existing_menu IS NULL THEN
    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, update_by, update_time, remark)
    VALUES ('科研计划与国际合作', 1, 100, 'sciencePlan', 'system/sciencePlan/index', 1, 0, 'C', '0', '0', 'system:sciencePlan:view', 'education', 'admin', NOW(), 'admin', NOW(), '科研计划与国际合作管理');
    
    -- 获取新添加的菜单ID
    SET @menu_id = LAST_INSERT_ID();
ELSE
    -- 如果已存在，使用现有ID
    SET @menu_id = @existing_menu;
END IF;

-- 检查是否已存在编辑权限
SET @existing_edit_perm = (SELECT id FROM sys_menu WHERE menu_name = '编辑' AND parent_id = @menu_id);

-- 如果不存在，插入编辑权限
IF @existing_edit_perm IS NULL THEN
    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, update_by, update_time, remark)
    VALUES ('编辑', @menu_id, 1, '#', '', 1, 0, 'F', '0', '0', 'system:sciencePlan:edit', '', 'admin', NOW(), 'admin', NOW(), '科研计划与国际合作编辑权限');
    
    SET @edit_perm_id = LAST_INSERT_ID();
ELSE
    SET @edit_perm_id = @existing_edit_perm;
END IF;

-- 为超级管理员角色分配菜单权限
INSERT IGNORE INTO sys_role_menu (role_id, menu_id)
SELECT role_id, @menu_id FROM sys_role WHERE role_key = 'admin';

-- 为超级管理员角色分配编辑权限
INSERT IGNORE INTO sys_role_menu (role_id, menu_id)
SELECT role_id, @edit_perm_id FROM sys_role WHERE role_key = 'admin';

-- 输出结果
SELECT '菜单ID:', @menu_id, '编辑权限ID:', @edit_perm_id;