-- =============================================
-- 统计报表模块菜单初始化脚本
-- 用于在后台管理系统中添加统计报表相关菜单
-- 包含：数据看板
-- 
-- 注意：此脚本需要在 Admin 数据库中执行
-- 表名使用小写（menus, roles, permissions, role_menus, role_permissions）
-- =============================================

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

-- 声明变量
DECLARE @StatisticsMenuId 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/statistics')
BEGIN
    INSERT INTO menus (ParentId, Name, Path, Component, Icon, MenuType, Permission, SortOrder, Status, IsExternal, IsCache, CreatedAt)
    VALUES (0, N'统计报表', '/business/statistics', 'Layout', 'DataAnalysis', 1, NULL, 5, 1, 0, 0, GETDATE());
    
    SET @StatisticsMenuId = SCOPE_IDENTITY();
    PRINT N'创建统计报表目录，ID: ' + CAST(@StatisticsMenuId AS VARCHAR);
END
ELSE
BEGIN
    SELECT @StatisticsMenuId = Id FROM menus WHERE Path = '/business/statistics';
    UPDATE menus SET ParentId = 0, Component = 'Layout' WHERE Id = @StatisticsMenuId AND ParentId <> 0;
    PRINT N'统计报表目录已存在，ID: ' + CAST(@StatisticsMenuId AS VARCHAR);
END

-- =============================================
-- 2. 创建数据看板子菜单
-- =============================================
IF NOT EXISTS (SELECT 1 FROM menus WHERE Path = '/business/statistics/data-stand')
BEGIN
    INSERT INTO menus (ParentId, Name, Path, Component, Icon, MenuType, Permission, SortOrder, Status, IsExternal, IsCache, CreatedAt)
    VALUES (@StatisticsMenuId, N'数据看板', '/business/statistics/data-stand', 'business/statistics/data-stand', 'DataBoard', 2, 'statistics:data-stand', 1, 1, 0, 1, GETDATE());
    PRINT N'创建数据看板菜单';
END

-- =============================================
-- 3. 添加统计报表相关权限
-- =============================================
IF NOT EXISTS (SELECT 1 FROM permissions WHERE Code = 'statistics:data-stand')
    INSERT INTO permissions (Name, Code, Module, CreatedAt) VALUES (N'数据看板', 'statistics:data-stand', N'统计报表', GETDATE());

-- =============================================
-- 4. 为超级管理员角色分配新菜单
-- =============================================
IF @SuperAdminRoleId IS NOT NULL
BEGIN
    -- 分配统计报表目录
    IF NOT EXISTS (SELECT 1 FROM role_menus WHERE RoleId = @SuperAdminRoleId AND MenuId = @StatisticsMenuId)
    BEGIN
        INSERT INTO role_menus (RoleId, MenuId) VALUES (@SuperAdminRoleId, @StatisticsMenuId);
        PRINT N'为超级管理员分配统计报表目录';
    END
    
    -- 分配所有新创建的子菜单
    INSERT INTO role_menus (RoleId, MenuId)
    SELECT @SuperAdminRoleId, m.Id
    FROM menus m
    WHERE m.ParentId = @StatisticsMenuId
    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 ('statistics:data-stand')
    AND NOT EXISTS (
        SELECT 1 FROM role_permissions rp 
        WHERE rp.RoleId = @SuperAdminRoleId AND rp.PermissionId = p.Id
    );
    
    PRINT N'为超级管理员分配统计报表权限';
END

-- =============================================
-- 5. 验证结果
-- =============================================
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/statistics%'
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 'statistics:%'
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/statistics%'
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 'statistics:%'
ORDER BY r.Name, p.Code;

PRINT N'';
PRINT N'统计报表模块菜单初始化完成';
