-- ============================================
-- 改名方案模块权限配置
-- ============================================
-- 此脚本为改名方案模块创建菜单和权限配置
-- 执行前请确保 sys_menu 表存在

-- ============================================
-- 第一步：查找或创建易凡模块的父菜单
-- ============================================

-- 查找易凡模块的菜单ID（假设已存在）
SET @yifan_parent_id = (SELECT id FROM sys_menu WHERE name = '易凡模块' AND type = 1 LIMIT 1);

-- 如果不存在，创建易凡模块目录
INSERT INTO sys_menu (name, type, path, component, icon, sort, status, visible, parent_id, perms, created_time, updated_time)
SELECT '易凡模块', 1, '/module_yifan', 'Layout', 'el-icon-star-filled', 100, 0, 1, NULL, NULL, NOW(), NOW()
WHERE NOT EXISTS (SELECT 1 FROM sys_menu WHERE name = '易凡模块' AND type = 1);

-- 重新获取父菜单ID
SET @yifan_parent_id = (SELECT id FROM sys_menu WHERE name = '易凡模块' AND type = 1 LIMIT 1);

-- ============================================
-- 第二步：创建改名方案菜单
-- ============================================

-- 删除旧的改名方案菜单（如果存在）
DELETE FROM sys_menu WHERE name = '改名方案' AND type = 2;

-- 创建改名方案菜单
INSERT INTO sys_menu (name, type, path, component, icon, sort, status, visible, parent_id, perms, created_time, updated_time)
VALUES ('改名方案', 2, '/module_yifan/yifan_naming_solutions', 'module_yifan/yifan_naming_solutions/index', 'el-icon-document', 3, 0, 1, @yifan_parent_id, 'module_yifan:yifan_naming_solutions:query', NOW(), NOW());

-- 获取改名方案菜单ID
SET @naming_solutions_menu_id = LAST_INSERT_ID();

-- ============================================
-- 第三步：创建改名方案的按钮权限
-- ============================================

-- 查询权限
INSERT INTO sys_menu (name, type, path, component, icon, sort, status, visible, parent_id, perms, created_time, updated_time)
VALUES ('查询', 3, NULL, NULL, NULL, 1, 0, 1, @naming_solutions_menu_id, 'module_yifan:yifan_naming_solutions:query', NOW(), NOW());

-- 详情权限
INSERT INTO sys_menu (name, type, path, component, icon, sort, status, visible, parent_id, perms, created_time, updated_time)
VALUES ('详情', 3, NULL, NULL, NULL, 2, 0, 1, @naming_solutions_menu_id, 'module_yifan:yifan_naming_solutions:detail', NOW(), NOW());

-- 新增权限
INSERT INTO sys_menu (name, type, path, component, icon, sort, status, visible, parent_id, perms, created_time, updated_time)
VALUES ('新增', 3, NULL, NULL, NULL, 3, 0, 1, @naming_solutions_menu_id, 'module_yifan:yifan_naming_solutions:create', NOW(), NOW());

-- 修改权限
INSERT INTO sys_menu (name, type, path, component, icon, sort, status, visible, parent_id, perms, created_time, updated_time)
VALUES ('修改', 3, NULL, NULL, NULL, 4, 0, 1, @naming_solutions_menu_id, 'module_yifan:yifan_naming_solutions:update', NOW(), NOW());

-- 删除权限
INSERT INTO sys_menu (name, type, path, component, icon, sort, status, visible, parent_id, perms, created_time, updated_time)
VALUES ('删除', 3, NULL, NULL, NULL, 5, 0, 1, @naming_solutions_menu_id, 'module_yifan:yifan_naming_solutions:delete', NOW(), NOW());

-- 批量操作权限
INSERT INTO sys_menu (name, type, path, component, icon, sort, status, visible, parent_id, perms, created_time, updated_time)
VALUES ('批量操作', 3, NULL, NULL, NULL, 6, 0, 1, @naming_solutions_menu_id, 'module_yifan:yifan_naming_solutions:batch', NOW(), NOW());

-- 修改状态权限
INSERT INTO sys_menu (name, type, path, component, icon, sort, status, visible, parent_id, perms, created_time, updated_time)
VALUES ('修改状态', 3, NULL, NULL, NULL, 7, 0, 1, @naming_solutions_menu_id, 'module_yifan:yifan_naming_solutions:patch', NOW(), NOW());

-- 导入权限
INSERT INTO sys_menu (name, type, path, component, icon, sort, status, visible, parent_id, perms, created_time, updated_time)
VALUES ('导入', 3, NULL, NULL, NULL, 8, 0, 1, @naming_solutions_menu_id, 'module_yifan:yifan_naming_solutions:import', NOW(), NOW());

-- 导出权限
INSERT INTO sys_menu (name, type, path, component, icon, sort, status, visible, parent_id, perms, created_time, updated_time)
VALUES ('导出', 3, NULL, NULL, NULL, 9, 0, 1, @naming_solutions_menu_id, 'module_yifan:yifan_naming_solutions:export', NOW(), NOW());

-- 下载模板权限
INSERT INTO sys_menu (name, type, path, component, icon, sort, status, visible, parent_id, perms, created_time, updated_time)
VALUES ('下载模板', 3, NULL, NULL, NULL, 10, 0, 1, @naming_solutions_menu_id, 'module_yifan:yifan_naming_solutions:download', NOW(), NOW());

-- ============================================
-- 第四步：为管理员角色分配权限
-- ============================================

-- 获取管理员角色ID（通常是1，也可能是其他值）
SET @admin_role_id = (SELECT id FROM sys_role WHERE role_code = 'admin' OR role_name = '管理员' OR id = 1 LIMIT 1);

-- 为管理员角色分配改名方案菜单权限
INSERT INTO sys_role_menus (role_id, menu_id, created_time, updated_time)
SELECT @admin_role_id, id, NOW(), NOW()
FROM sys_menu 
WHERE (id = @naming_solutions_menu_id OR parent_id = @naming_solutions_menu_id)
AND NOT EXISTS (
    SELECT 1 FROM sys_role_menus 
    WHERE role_id = @admin_role_id AND menu_id = sys_menu.id
);

-- ============================================
-- 验证结果
-- ============================================

-- 查看创建的菜单
SELECT 
    m.id,
    m.name,
    m.type,
    CASE m.type 
        WHEN 1 THEN '目录'
        WHEN 2 THEN '菜单'
        WHEN 3 THEN '按钮'
        WHEN 4 THEN '链接'
    END as type_name,
    m.path,
    m.perms,
    m.parent_id,
    p.name as parent_name
FROM sys_menu m
LEFT JOIN sys_menu p ON m.parent_id = p.id
WHERE m.name IN ('易凡模块', '改名方案') OR m.parent_id = @naming_solutions_menu_id
ORDER BY m.parent_id, m.sort;

-- 查看管理员角色的权限分配
SELECT 
    r.id as role_id,
    r.role_name,
    m.id as menu_id,
    m.name as menu_name,
    m.perms
FROM sys_role r
JOIN sys_role_menus rm ON r.id = rm.role_id
JOIN sys_menu m ON rm.menu_id = m.id
WHERE r.id = @admin_role_id 
AND (m.id = @naming_solutions_menu_id OR m.parent_id = @naming_solutions_menu_id)
ORDER BY m.sort;

-- 统计信息
SELECT 
    '改名方案菜单ID' as info_type,
    @naming_solutions_menu_id as value
UNION ALL
SELECT 
    '管理员角色ID' as info_type,
    @admin_role_id as value
UNION ALL
SELECT 
    '创建的权限数量' as info_type,
    COUNT(*) as value
FROM sys_menu 
WHERE parent_id = @naming_solutions_menu_id;

-- ============================================
-- 注意事项
-- ============================================
-- 1. status 字段：0=启用，1=停用
-- 2. visible 字段：0=显示，1=隐藏
-- 3. type 字段：1=目录，2=菜单，3=按钮/权限，4=链接
-- 4. 如果管理员角色ID不是1，需要手动调整 @admin_role_id
-- 5. 执行后需要重新登录才能看到新菜单
-- 6. 如果菜单路径或组件路径不对，需要根据实际情况调整
