/* 修改人:Ld 修改时间:2026-07-29 15:27:40; 新增采购合同生成入库单移动端页面测试库菜单 */
SET XACT_ABORT ON;
BEGIN TRAN;

DECLARE @ModuleId INT;

SELECT @ModuleId = id
FROM dbo.基础表_模块信息
WHERE [path] = N'PurchasingManagement/PurchaseInboundCreateMobile';

IF @ModuleId IS NULL
BEGIN
    INSERT INTO dbo.基础表_模块信息
    (
        [path],
        component,
        redirect,
        name,
        title,
        icon,
        是否通知,
        消息通知来源,
        通知条件,
        dashboardIcon,
        是否禁用
    )
    VALUES
    (
        N'PurchasingManagement/PurchaseInboundCreateMobile',
        N'/PurchasingManagement/PurchaseInboundCreateMobile',
        NULL,
        N'PurchaseInboundCreateMobile',
        N'生成入库单(移动端)',
        N'PurchasePartsStorage',
        NULL,
        NULL,
        NULL,
        NULL,
        0
    );

    SET @ModuleId = SCOPE_IDENTITY();
END
ELSE
BEGIN
    UPDATE dbo.基础表_模块信息
    SET component = N'/PurchasingManagement/PurchaseInboundCreateMobile',
        redirect = NULL,
        name = N'PurchaseInboundCreateMobile',
        title = N'生成入库单(移动端)',
        icon = N'PurchasePartsStorage',
        是否禁用 = 0
    WHERE id = @ModuleId;
END;

/* 修改人:Ld 修改时间:2026-07-29 15:27:40; 给已拥有采购合同查询的采购管理角色追加新菜单权限 */
;WITH RoleList AS
(
    SELECT DISTINCT 角色编号
    FROM dbo.基础表_角色模块信息
    WHERE id = 36
      AND pid = 34
      AND ISNULL(是否启用, 1) = 1
)
INSERT INTO dbo.基础表_角色模块信息
(
    id,
    pid,
    模块顺序,
    角色编号,
    是否启用,
    角色模块顺序
)
SELECT
    @ModuleId,
    34,
    @ModuleId,
    RoleList.角色编号,
    1,
    ISNULL(RoleOrder.MaxOrder, 0) + 1
FROM RoleList
OUTER APPLY
(
    SELECT MAX(角色模块顺序) AS MaxOrder
    FROM dbo.基础表_角色模块信息
    WHERE 角色编号 = RoleList.角色编号
      AND pid = 34
) RoleOrder
WHERE NOT EXISTS
(
    SELECT 1
    FROM dbo.基础表_角色模块信息
    WHERE id = @ModuleId
      AND pid = 34
      AND 角色编号 = RoleList.角色编号
);

SELECT @ModuleId AS 新增模块ID;
SELECT *
FROM dbo.基础表_模块信息
WHERE id = @ModuleId;
SELECT *
FROM dbo.基础表_角色模块信息
WHERE id = @ModuleId
ORDER BY 角色编号;

COMMIT TRAN;
