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

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

-- 声明变量
DECLARE @OrderMenuId 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/order')
BEGIN
    INSERT INTO Menus (ParentId, Name, Path, Component, Icon, MenuType, Permission, SortOrder, Status, IsExternal, IsCache, CreatedAt)
    VALUES (0, N'订单管理', '/business/order', 'Layout', 'Document', 1, NULL, 30, 1, 0, 0, GETDATE());
    
    SET @OrderMenuId = SCOPE_IDENTITY();
    PRINT N'创建订单管理目录，ID: ' + CAST(@OrderMenuId AS VARCHAR);
END
ELSE
BEGIN
    SELECT @OrderMenuId = Id FROM Menus WHERE Path = '/business/order';
    -- 确保是顶级菜单
    UPDATE Menus SET ParentId = 0, Component = 'Layout' WHERE Id = @OrderMenuId AND ParentId <> 0;
    PRINT N'订单管理目录已存在，ID: ' + CAST(@OrderMenuId AS VARCHAR);
END

-- =============================================
-- 2. 创建订单管理子菜单
-- =============================================

-- 2.1 购买订单
IF NOT EXISTS (SELECT 1 FROM Menus WHERE Path = '/business/order/buy')
BEGIN
    INSERT INTO Menus (ParentId, Name, Path, Component, Icon, MenuType, Permission, SortOrder, Status, IsExternal, IsCache, CreatedAt)
    VALUES (@OrderMenuId, N'购买订单', '/business/order/buy', 'business/order/buy', 'ShoppingCart', 2, 'order:list', 1, 1, 0, 1, GETDATE());
    PRINT N'创建购买订单菜单';
END

-- 2.2 发货订单
IF NOT EXISTS (SELECT 1 FROM Menus WHERE Path = '/business/order/shipping')
BEGIN
    INSERT INTO Menus (ParentId, Name, Path, Component, Icon, MenuType, Permission, SortOrder, Status, IsExternal, IsCache, CreatedAt)
    VALUES (@OrderMenuId, N'发货订单', '/business/order/shipping', 'business/order/shipping', 'Van', 2, 'order:list', 2, 1, 0, 1, GETDATE());
    PRINT N'创建发货订单菜单';
END

-- 2.3 回收订单
IF NOT EXISTS (SELECT 1 FROM Menus WHERE Path = '/business/order/recovery')
BEGIN
    INSERT INTO Menus (ParentId, Name, Path, Component, Icon, MenuType, Permission, SortOrder, Status, IsExternal, IsCache, CreatedAt)
    VALUES (@OrderMenuId, N'回收订单', '/business/order/recovery', 'business/order/recovery', 'Refresh', 2, 'order:list', 3, 1, 0, 1, GETDATE());
    PRINT N'创建回收订单菜单';
END

-- 2.4 卡单订单
IF NOT EXISTS (SELECT 1 FROM Menus WHERE Path = '/business/order/stuck')
BEGIN
    INSERT INTO Menus (ParentId, Name, Path, Component, Icon, MenuType, Permission, SortOrder, Status, IsExternal, IsCache, CreatedAt)
    VALUES (@OrderMenuId, N'卡单订单', '/business/order/stuck', 'business/order/stuck', 'Warning', 2, 'order:list', 4, 1, 0, 1, GETDATE());
    PRINT N'创建卡单订单菜单';
END

-- 2.5 综合订单
IF NOT EXISTS (SELECT 1 FROM Menus WHERE Path = '/business/order/list')
BEGIN
    INSERT INTO Menus (ParentId, Name, Path, Component, Icon, MenuType, Permission, SortOrder, Status, IsExternal, IsCache, CreatedAt)
    VALUES (@OrderMenuId, N'综合订单', '/business/order/list', 'business/order/list', 'List', 2, 'order:list', 5, 1, 0, 1, GETDATE());
    PRINT N'创建综合订单菜单';
END

-- =============================================
-- 3. 添加订单管理相关权限（如果不存在）
-- =============================================

-- 订单列表权限
IF NOT EXISTS (SELECT 1 FROM Permissions WHERE Code = 'order:list')
    INSERT INTO Permissions (Name, Code, Module, CreatedAt) VALUES (N'订单列表', 'order:list', N'订单管理', GETDATE());
IF NOT EXISTS (SELECT 1 FROM Permissions WHERE Code = 'order:view')
    INSERT INTO Permissions (Name, Code, Module, CreatedAt) VALUES (N'查看订单详情', 'order:view', N'订单管理', GETDATE());

-- 发货操作权限
IF NOT EXISTS (SELECT 1 FROM Permissions WHERE Code = 'order:ship')
    INSERT INTO Permissions (Name, Code, Module, CreatedAt) VALUES (N'发货操作', 'order:ship', N'订单管理', GETDATE());
IF NOT EXISTS (SELECT 1 FROM Permissions WHERE Code = 'order:cancel-ship')
    INSERT INTO Permissions (Name, Code, Module, CreatedAt) VALUES (N'取消发货', 'order:cancel-ship', N'订单管理', GETDATE());

-- 导出权限
IF NOT EXISTS (SELECT 1 FROM Permissions WHERE Code = 'order:export')
    INSERT INTO Permissions (Name, Code, Module, CreatedAt) VALUES (N'导出订单', 'order:export', N'订单管理', GETDATE());

-- =============================================
-- 4. 为超级管理员角色分配新菜单
-- =============================================
IF @SuperAdminRoleId IS NOT NULL
BEGIN
    -- 分配订单管理目录
    IF NOT EXISTS (SELECT 1 FROM RoleMenus WHERE RoleId = @SuperAdminRoleId AND MenuId = @OrderMenuId)
    BEGIN
        INSERT INTO RoleMenus (RoleId, MenuId) VALUES (@SuperAdminRoleId, @OrderMenuId);
        PRINT N'为超级管理员分配订单管理目录';
    END
    
    -- 分配所有新创建的子菜单
    INSERT INTO RoleMenus (RoleId, MenuId)
    SELECT @SuperAdminRoleId, m.Id
    FROM Menus m
    WHERE m.ParentId = @OrderMenuId
    AND NOT EXISTS (SELECT 1 FROM RoleMenus rm WHERE rm.RoleId = @SuperAdminRoleId AND rm.MenuId = m.Id);
    
    PRINT N'为超级管理员分配订单管理子菜单';
    
    -- 分配新增的权限
    INSERT INTO RolePermissions (RoleId, PermissionId)
    SELECT @SuperAdminRoleId, p.Id
    FROM Permissions p
    WHERE p.Code IN (
        'order:list', 'order:view', 'order:ship', 'order:cancel-ship', 'order:export'
    )
    AND NOT EXISTS (
        SELECT 1 FROM RolePermissions 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/order%'
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 'order:%'
ORDER BY p.Code;

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/order%'
ORDER BY r.Name, m.Path;

PRINT N'';
PRINT N'========== 角色权限分配结果 ==========';
SELECT 
    r.Name AS RoleName,
    p.Name AS PermissionName,
    p.Code
FROM RolePermissions rp
INNER JOIN Roles r ON rp.RoleId = r.Id
INNER JOIN Permissions p ON rp.PermissionId = p.Id
WHERE p.Code LIKE 'order:%'
ORDER BY r.Name, p.Code;

PRINT N'';
PRINT N'订单管理模块菜单初始化完成';
