/* 修改人:Ld 修改时间:2026-07-30 09:05:58; 新增库房管理测试库菜单，仅给已有库位管理权限的角色授权 */
SET XACT_ABORT ON;
BEGIN TRAN;

DECLARE @ModuleId INT;
DECLARE @ParentId INT = 42;

SELECT @ModuleId = id
FROM dbo.基础表_模块信息
WHERE [path] = N'WarehouseManagement/WarehouseRoomManagement'
   OR component = N'/WarehouseManagement/WarehouseRoomManagement'
   OR title = N'库房管理';

IF @ModuleId IS NULL
BEGIN
    INSERT INTO dbo.基础表_模块信息
    (
        [path],
        component,
        redirect,
        name,
        title,
        icon,
        是否通知,
        消息通知来源,
        通知条件,
        dashboardIcon,
        是否禁用
    )
    VALUES
    (
        N'WarehouseManagement/WarehouseRoomManagement',
        N'/WarehouseManagement/WarehouseRoomManagement',
        NULL,
        N'WarehouseRoomManagement',
        N'库房管理',
        N'MaterialArrivalManagement',
        NULL,
        NULL,
        NULL,
        NULL,
        0
    );

    SET @ModuleId = SCOPE_IDENTITY();
END
ELSE
BEGIN
    UPDATE dbo.基础表_模块信息
    SET [path] = N'WarehouseManagement/WarehouseRoomManagement',
        component = N'/WarehouseManagement/WarehouseRoomManagement',
        redirect = NULL,
        name = N'WarehouseRoomManagement',
        title = N'库房管理',
        icon = N'MaterialArrivalManagement',
        是否禁用 = 0
    WHERE id = @ModuleId;
END;

;WITH RoleList AS
(
    SELECT DISTINCT 角色编号
    FROM dbo.基础表_角色模块信息
    WHERE id = 92
      AND pid = @ParentId
      AND ISNULL(是否启用, 1) = 1
)
INSERT INTO dbo.基础表_角色模块信息
(
    id,
    pid,
    模块顺序,
    角色编号,
    是否启用,
    角色模块顺序
)
SELECT
    @ModuleId,
    @ParentId,
    @ModuleId,
    RoleList.角色编号,
    1,
    ISNULL(RoleOrder.MaxOrder, 0) + 1
FROM RoleList
OUTER APPLY
(
    SELECT MAX(角色模块顺序) AS MaxOrder
    FROM dbo.基础表_角色模块信息
    WHERE 角色编号 = RoleList.角色编号
      AND pid = @ParentId
) RoleOrder
WHERE NOT EXISTS
(
    SELECT 1
    FROM dbo.基础表_角色模块信息
    WHERE id = @ModuleId
      AND pid = @ParentId
      AND 角色编号 = RoleList.角色编号
);

SELECT @ModuleId AS 库房管理模块ID;
SELECT id, [path], component, name, title, icon, 是否禁用
FROM dbo.基础表_模块信息
WHERE id = @ModuleId;
SELECT id, pid, 角色编号, 是否启用, 角色模块顺序
FROM dbo.基础表_角色模块信息
WHERE id = @ModuleId
ORDER BY 角色编号;

COMMIT TRAN;
