-- =============================================
-- 中奖公告管理菜单初始化脚本
-- 用于在后台管理系统中添加中奖公告管理菜单
-- 
-- 注意：此脚本需要在 Admin 数据库 (honey_box_admin) 中执行
-- 表名使用小写（menus, roles, permissions, role_menus, role_permissions）
-- =============================================

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

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

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

-- 获取内容管理目录ID
SELECT @ContentMenuId = Id FROM menus WHERE Path = '/business/content';

-- 如果内容管理目录不存在，则创建它
IF @ContentMenuId IS NULL
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
    PRINT N'内容管理目录已存在，ID: ' + CAST(@ContentMenuId AS VARCHAR);
END

-- =============================================
-- 1. 创建中奖公告管理菜单
-- =============================================
IF NOT EXISTS (SELECT 1 FROM menus WHERE Path = '/business/announcement/list')
BEGIN
    INSERT INTO menus (ParentId, Name, Path, Component, Icon, MenuType, Permission, SortOrder, Status, IsExternal, IsCache, CreatedAt)
    VALUES (@ContentMenuId, N'中奖公告管理', '/business/announcement/list', 'business/announcement/index', 'Bell', 2, 'announcement:list', 4, 1, 0, 1, GETDATE());
    
    SET @AnnouncementMenuId = SCOPE_IDENTITY();
    PRINT N'创建中奖公告管理菜单，ID: ' + CAST(@AnnouncementMenuId AS VARCHAR);
END
ELSE
BEGIN
    SELECT @AnnouncementMenuId = Id FROM menus WHERE Path = '/business/announcement/list';
    PRINT N'中奖公告管理菜单已存在，ID: ' + CAST(@AnnouncementMenuId AS VARCHAR);
END

-- =============================================
-- 2. 添加中奖公告管理相关权限
-- =============================================

IF NOT EXISTS (SELECT 1 FROM permissions WHERE Code = 'announcement:list')
    INSERT INTO permissions (Name, Code, Module, CreatedAt) VALUES (N'公告列表', 'announcement:list', N'内容管理', GETDATE());
IF NOT EXISTS (SELECT 1 FROM permissions WHERE Code = 'announcement:add')
    INSERT INTO permissions (Name, Code, Module, CreatedAt) VALUES (N'新增公告', 'announcement:add', N'内容管理', GETDATE());
IF NOT EXISTS (SELECT 1 FROM permissions WHERE Code = 'announcement:edit')
    INSERT INTO permissions (Name, Code, Module, CreatedAt) VALUES (N'编辑公告', 'announcement:edit', N'内容管理', GETDATE());
IF NOT EXISTS (SELECT 1 FROM permissions WHERE Code = 'announcement:delete')
    INSERT INTO permissions (Name, Code, Module, CreatedAt) VALUES (N'删除公告', 'announcement:delete', N'内容管理', GETDATE());
IF NOT EXISTS (SELECT 1 FROM permissions WHERE Code = 'announcement:status')
    INSERT INTO permissions (Name, Code, Module, CreatedAt) VALUES (N'切换公告状态', 'announcement:status', N'内容管理', GETDATE());

PRINT N'中奖公告管理权限创建完成';

-- =============================================
-- 3. 为超级管理员角色分配新菜单和权限
-- =============================================
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
    
    -- 分配中奖公告管理菜单
    IF @AnnouncementMenuId IS NOT NULL AND NOT EXISTS (SELECT 1 FROM role_menus WHERE RoleId = @SuperAdminRoleId AND MenuId = @AnnouncementMenuId)
    BEGIN
        INSERT INTO role_menus (RoleId, MenuId) VALUES (@SuperAdminRoleId, @AnnouncementMenuId);
        PRINT N'为超级管理员分配中奖公告管理菜单';
    END
    
    -- 分配新增的权限
    INSERT INTO role_permissions (RoleId, PermissionId)
    SELECT @SuperAdminRoleId, p.Id
    FROM permissions p
    WHERE p.Code IN (
        'announcement:list', 'announcement:add', 'announcement:edit', 
        'announcement:delete', 'announcement:status'
    )
    AND NOT EXISTS (
        SELECT 1 FROM role_permissions rp 
        WHERE rp.RoleId = @SuperAdminRoleId AND rp.PermissionId = p.Id
    );
    
    PRINT N'为超级管理员分配中奖公告管理权限';
END
ELSE
BEGIN
    PRINT N'警告：未找到超级管理员角色，请手动分配菜单和权限';
END

-- =============================================
-- 4. 验证结果
-- =============================================
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/announcement%'
   OR m.Path = '/business/content'
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 'announcement:%'
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/announcement%'
   OR m.Path = '/business/content'
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 'announcement:%'
ORDER BY r.Name, p.Code;

PRINT N'';
PRINT N'中奖公告管理菜单初始化完成';
