-- =============================================
-- 用户管理模块菜单初始化脚本
-- 用于在后台管理系统中添加用户管理相关菜单
-- =============================================

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

-- 声明变量
DECLARE @UserMenuId INT;
DECLARE @SuperAdminRoleId INT;

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

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

-- =============================================
-- 2. 创建用户管理子菜单
-- =============================================

-- 2.1 用户列表
IF NOT EXISTS (SELECT 1 FROM Menus WHERE Path = '/business/user/list')
BEGIN
    INSERT INTO Menus (ParentId, Name, Path, Component, Icon, MenuType, Permission, SortOrder, Status, IsExternal, IsCache, CreatedAt)
    VALUES (@UserMenuId, N'用户列表', '/business/user/list', 'business/user/index', 'List', 2, 'user:list', 1, 1, 0, 1, GETDATE());
    PRINT N'创建用户列表菜单';
END

-- 2.2 用户盈亏统计
IF NOT EXISTS (SELECT 1 FROM Menus WHERE Path = '/business/user/profit-loss')
BEGIN
    INSERT INTO Menus (ParentId, Name, Path, Component, Icon, MenuType, Permission, SortOrder, Status, IsExternal, IsCache, CreatedAt)
    VALUES (@UserMenuId, N'用户盈亏统计', '/business/user/profit-loss', 'business/user/profit-loss', 'TrendCharts', 2, 'user:view', 2, 1, 0, 1, GETDATE());
    PRINT N'创建用户盈亏统计菜单';
END

-- 2.3 VIP等级管理
IF NOT EXISTS (SELECT 1 FROM Menus WHERE Path = '/business/user/vip')
BEGIN
    INSERT INTO Menus (ParentId, Name, Path, Component, Icon, MenuType, Permission, SortOrder, Status, IsExternal, IsCache, CreatedAt)
    VALUES (@UserMenuId, N'VIP等级管理', '/business/user/vip', 'business/user/vip', 'Medal', 2, 'vip:list', 3, 1, 0, 1, GETDATE());
    PRINT N'创建VIP等级管理菜单';
END

-- 2.4 用户邀请统计
IF NOT EXISTS (SELECT 1 FROM Menus WHERE Path = '/business/user/invite-stats')
BEGIN
    INSERT INTO Menus (ParentId, Name, Path, Component, Icon, MenuType, Permission, SortOrder, Status, IsExternal, IsCache, CreatedAt)
    VALUES (@UserMenuId, N'用户邀请统计', '/business/user/invite-stats', 'business/user/invite-stats', 'Share', 2, 'user:view', 4, 1, 0, 1, GETDATE());
    PRINT N'创建用户邀请统计菜单';
END

-- 2.5 用户登录统计
IF NOT EXISTS (SELECT 1 FROM Menus WHERE Path = '/business/user/login-stats')
BEGIN
    INSERT INTO Menus (ParentId, Name, Path, Component, Icon, MenuType, Permission, SortOrder, Status, IsExternal, IsCache, CreatedAt)
    VALUES (@UserMenuId, N'用户登录统计', '/business/user/login-stats', 'business/user/login-stats', 'DataLine', 2, 'user:view', 5, 1, 0, 1, GETDATE());
    PRINT N'创建用户登录统计菜单';
END

-- =============================================
-- 3. 为超级管理员角色分配新菜单
-- =============================================
IF @SuperAdminRoleId IS NOT NULL
BEGIN
    -- 分配用户管理目录
    IF NOT EXISTS (SELECT 1 FROM RoleMenus WHERE RoleId = @SuperAdminRoleId AND MenuId = @UserMenuId)
    BEGIN
        INSERT INTO RoleMenus (RoleId, MenuId) VALUES (@SuperAdminRoleId, @UserMenuId);
        PRINT N'为超级管理员分配用户管理目录';
    END
    
    -- 分配所有新创建的子菜单
    INSERT INTO RoleMenus (RoleId, MenuId)
    SELECT @SuperAdminRoleId, m.Id
    FROM Menus m
    WHERE m.ParentId = @UserMenuId
    AND NOT EXISTS (SELECT 1 FROM RoleMenus rm WHERE rm.RoleId = @SuperAdminRoleId AND rm.MenuId = m.Id);
    
    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/user%'
ORDER BY m.ParentId, m.SortOrder;

PRINT N'';
PRINT N'========== 角色菜单分配结果 ==========';
SELECT 
    r.Name AS RoleName,
    m.Name AS MenuName,
    m.Path
FROM RoleMenus rm
INNER JOIN Roles r ON rm.RoleId = r.Id
INNER JOIN Menus m ON rm.MenuId = m.Id
WHERE m.Path LIKE '/business/user%'
ORDER BY r.Name, m.Path;
