-- =============================================
-- 内容与辅助模块菜单初始化脚本
-- 用于在后台管理系统中添加内容管理相关菜单
-- 包含：单页管理、悬浮球配置、福利屋入口
-- 
-- 注意：此脚本需要在 Admin 数据库中执行
-- 表名使用小写（menus, roles, permissions, role_menus, role_permissions）
-- =============================================

-- 注意：执行此脚本前请确保：
-- 1. 已存在超级管理员角色 (Code = 'super_admin')
-- 2. 数据库中已有基础菜单数据

-- 声明变量
DECLARE @ContentMenuId BIGINT;
DECLARE @SuperAdminRoleId BIGINT;

-- 获取超级管理员角色ID
SELECT @SuperAdminRoleId = Id FROM roles WHERE Code = 'super_admin';

-- =============================================
-- 1. 创建内容管理目录（顶级菜单）
-- =============================================
IF NOT EXISTS (SELECT 1 FROM menus WHERE Path = '/business/content')
BEGIN
    INSERT INTO menus (ParentId, Name, Path, Component, Icon, MenuType, Permission, SortOrder, Status, IsExternal, IsCache, CreatedAt)
    VALUES (0, N'内容管理', '/business/content', 'Layout', 'Document', 1, NULL, 70, 1, 0, 0, GETDATE());
    
    SET @ContentMenuId = SCOPE_IDENTITY();
    PRINT N'创建内容管理目录，ID: ' + CAST(@ContentMenuId AS VARCHAR);
END
ELSE
BEGIN
    SELECT @ContentMenuId = Id FROM menus WHERE Path = '/business/content';
    UPDATE menus SET ParentId = 0, Component = 'Layout' WHERE Id = @ContentMenuId AND ParentId <> 0;
    PRINT N'内容管理目录已存在，ID: ' + CAST(@ContentMenuId AS VARCHAR);
END

-- =============================================
-- 2. 创建单页管理子菜单
-- =============================================
IF NOT EXISTS (SELECT 1 FROM menus WHERE Path = '/business/danye/list')
BEGIN
    INSERT INTO menus (ParentId, Name, Path, Component, Icon, MenuType, Permission, SortOrder, Status, IsExternal, IsCache, CreatedAt)
    VALUES (@ContentMenuId, N'单页管理', '/business/danye/list', 'business/danye/list', 'Notebook', 2, 'danye:list', 1, 1, 0, 1, GETDATE());
    PRINT N'创建单页管理菜单';
END

-- =============================================
-- 3. 创建悬浮球配置子菜单
-- =============================================
IF NOT EXISTS (SELECT 1 FROM menus WHERE Path = '/business/floatball/list')
BEGIN
    INSERT INTO menus (ParentId, Name, Path, Component, Icon, MenuType, Permission, SortOrder, Status, IsExternal, IsCache, CreatedAt)
    VALUES (@ContentMenuId, N'悬浮球配置', '/business/floatball/list', 'business/floatball/list', 'Pointer', 2, 'floatball:list', 2, 1, 0, 1, GETDATE());
    PRINT N'创建悬浮球配置菜单';
END

-- =============================================
-- 4. 创建福利屋入口子菜单
-- =============================================
IF NOT EXISTS (SELECT 1 FROM menus WHERE Path = '/business/welfarehouse/list')
BEGIN
    INSERT INTO menus (ParentId, Name, Path, Component, Icon, MenuType, Permission, SortOrder, Status, IsExternal, IsCache, CreatedAt)
    VALUES (@ContentMenuId, N'福利屋入口', '/business/welfarehouse/list', 'business/welfarehouse/list', 'Present', 2, 'welfarehouse:list', 3, 1, 0, 1, GETDATE());
    PRINT N'创建福利屋入口菜单';
END

-- =============================================
-- 5. 添加单页管理相关权限
-- =============================================
IF NOT EXISTS (SELECT 1 FROM permissions WHERE Code = 'danye:list')
    INSERT INTO permissions (Name, Code, Module, CreatedAt) VALUES (N'单页列表', 'danye:list', N'内容管理', GETDATE());
IF NOT EXISTS (SELECT 1 FROM permissions WHERE Code = 'danye:edit')
    INSERT INTO permissions (Name, Code, Module, CreatedAt) VALUES (N'编辑单页', 'danye:edit', N'内容管理', GETDATE());

-- =============================================
-- 6. 添加悬浮球配置相关权限
-- =============================================
IF NOT EXISTS (SELECT 1 FROM permissions WHERE Code = 'floatball:list')
    INSERT INTO permissions (Name, Code, Module, CreatedAt) VALUES (N'悬浮球列表', 'floatball:list', N'内容管理', GETDATE());
IF NOT EXISTS (SELECT 1 FROM permissions WHERE Code = 'floatball:add')
    INSERT INTO permissions (Name, Code, Module, CreatedAt) VALUES (N'新增悬浮球', 'floatball:add', N'内容管理', GETDATE());
IF NOT EXISTS (SELECT 1 FROM permissions WHERE Code = 'floatball:edit')
    INSERT INTO permissions (Name, Code, Module, CreatedAt) VALUES (N'编辑悬浮球', 'floatball:edit', N'内容管理', GETDATE());
IF NOT EXISTS (SELECT 1 FROM permissions WHERE Code = 'floatball:delete')
    INSERT INTO permissions (Name, Code, Module, CreatedAt) VALUES (N'删除悬浮球', 'floatball:delete', N'内容管理', GETDATE());

-- =============================================
-- 7. 添加福利屋入口相关权限
-- =============================================
IF NOT EXISTS (SELECT 1 FROM permissions WHERE Code = 'welfarehouse:list')
    INSERT INTO permissions (Name, Code, Module, CreatedAt) VALUES (N'福利屋列表', 'welfarehouse:list', N'内容管理', GETDATE());
IF NOT EXISTS (SELECT 1 FROM permissions WHERE Code = 'welfarehouse:add')
    INSERT INTO permissions (Name, Code, Module, CreatedAt) VALUES (N'新增福利屋入口', 'welfarehouse:add', N'内容管理', GETDATE());
IF NOT EXISTS (SELECT 1 FROM permissions WHERE Code = 'welfarehouse:edit')
    INSERT INTO permissions (Name, Code, Module, CreatedAt) VALUES (N'编辑福利屋入口', 'welfarehouse:edit', N'内容管理', GETDATE());
IF NOT EXISTS (SELECT 1 FROM permissions WHERE Code = 'welfarehouse:delete')
    INSERT INTO permissions (Name, Code, Module, CreatedAt) VALUES (N'删除福利屋入口', 'welfarehouse:delete', N'内容管理', GETDATE());

-- =============================================
-- 8. 为超级管理员角色分配新菜单
-- =============================================
IF @SuperAdminRoleId IS NOT NULL
BEGIN
    -- 分配内容管理目录
    IF NOT EXISTS (SELECT 1 FROM role_menus WHERE RoleId = @SuperAdminRoleId AND MenuId = @ContentMenuId)
    BEGIN
        INSERT INTO role_menus (RoleId, MenuId) VALUES (@SuperAdminRoleId, @ContentMenuId);
        PRINT N'为超级管理员分配内容管理目录';
    END
    
    -- 分配所有新创建的子菜单
    INSERT INTO role_menus (RoleId, MenuId)
    SELECT @SuperAdminRoleId, m.Id
    FROM menus m
    WHERE m.ParentId = @ContentMenuId
    AND NOT EXISTS (SELECT 1 FROM role_menus rm WHERE rm.RoleId = @SuperAdminRoleId AND rm.MenuId = m.Id);
    
    PRINT N'为超级管理员分配内容管理子菜单';
    
    -- 分配新增的权限
    INSERT INTO role_permissions (RoleId, PermissionId)
    SELECT @SuperAdminRoleId, p.Id
    FROM permissions p
    WHERE p.Code IN (
        'danye:list', 'danye:edit',
        'floatball:list', 'floatball:add', 'floatball:edit', 'floatball:delete',
        'welfarehouse:list', 'welfarehouse:add', 'welfarehouse:edit', 'welfarehouse:delete'
    )
    AND NOT EXISTS (
        SELECT 1 FROM role_permissions rp 
        WHERE rp.RoleId = @SuperAdminRoleId AND rp.PermissionId = p.Id
    );
    
    PRINT N'为超级管理员分配内容管理权限';
END

-- =============================================
-- 9. 验证结果
-- =============================================
PRINT N'';
PRINT N'========== 菜单创建结果 ==========';
SELECT 
    m.Id,
    m.ParentId,
    m.Name,
    m.Path,
    m.Component,
    m.MenuType,
    m.Permission,
    m.SortOrder
FROM menus m
WHERE m.Path LIKE '/business/content%'
   OR m.Path LIKE '/business/danye%'
   OR m.Path LIKE '/business/floatball%'
   OR m.Path LIKE '/business/welfarehouse%'
ORDER BY m.ParentId, m.SortOrder;

PRINT N'';
PRINT N'========== 权限创建结果 ==========';
SELECT 
    p.Id,
    p.Name,
    p.Code,
    p.Module
FROM permissions p
WHERE p.Code LIKE 'danye:%'
   OR p.Code LIKE 'floatball:%'
   OR p.Code LIKE 'welfarehouse:%'
ORDER BY p.Code;

PRINT N'';
PRINT N'========== 角色菜单分配结果 ==========';
SELECT 
    r.Name AS RoleName,
    m.Name AS MenuName,
    m.Path
FROM role_menus rm
INNER JOIN roles r ON rm.RoleId = r.Id
INNER JOIN menus m ON rm.MenuId = m.Id
WHERE m.Path LIKE '/business/content%'
   OR m.Path LIKE '/business/danye%'
   OR m.Path LIKE '/business/floatball%'
   OR m.Path LIKE '/business/welfarehouse%'
ORDER BY r.Name, m.Path;

PRINT N'';
PRINT N'========== 角色权限分配结果 ==========';
SELECT 
    r.Name AS RoleName,
    p.Name AS PermissionName,
    p.Code
FROM role_permissions rp
INNER JOIN roles r ON rp.RoleId = r.Id
INNER JOIN permissions p ON rp.PermissionId = p.Id
WHERE p.Code LIKE 'danye:%'
   OR p.Code LIKE 'floatball:%'
   OR p.Code LIKE 'welfarehouse:%'
ORDER BY r.Name, p.Code;

PRINT N'';
PRINT N'内容与辅助模块菜单初始化完成';
