USE [YL_MESDB]
GO

SET NOCOUNT ON;

DECLARE @Title nvarchar(100) = N'产品合格率(工位)';
DECLARE @Path nvarchar(300) = N'ProductPassRateWorkstation/index';
DECLARE @Component nvarchar(300) = N'/ProductionManagement/ProductPassRateWorkstation/index';
DECLARE @Name nvarchar(100) = N'ProductPassRateWorkstation';
DECLARE @SourcePath nvarchar(300) = N'ProductionPlanTrack/index';
DECLARE @SourceComponent nvarchar(300) = N'/ProductionManagement/ProductionPlanTrack/index';
DECLARE @TargetRole int = NULL;

IF OBJECT_ID(N'dbo.登录基础数据_二级菜单', N'U') IS NULL
BEGIN
    RAISERROR(N'未找到表 dbo.登录基础数据_二级菜单。', 16, 1);
    RETURN;
END;

DECLARE @IdIsIdentity bit = ISNULL(COLUMNPROPERTY(OBJECT_ID(N'dbo.登录基础数据_二级菜单'), N'id', 'IsIdentity'), 0);

IF @IdIsIdentity = 1
BEGIN
    ;WITH SourceRows AS
    (
        SELECT
            src.[账号代码],
            COALESCE(prodParent.[id], src.[pid]) AS [pid],
            src.[icon],
            src.[AddData],
            src.[DeleteData],
            src.[EditData],
            src.[SearchData]
        FROM [dbo].[登录基础数据_二级菜单] AS src
        OUTER APPLY
        (
            SELECT TOP (1) rp.[id]
            FROM [dbo].[登录基础数据_二级菜单] AS rp
            WHERE rp.[账号代码] = src.[账号代码]
              AND rp.[是否启用] = 1
              AND rp.[title] = N'生产管理'
            ORDER BY rp.[id]
        ) AS prodParent
        WHERE src.[是否启用] = 1
          AND (@TargetRole IS NULL OR src.[账号代码] = @TargetRole)
          AND (src.[path] = @SourcePath OR src.[componet] = @SourceComponent OR src.[title] = N'生产计划跟踪')
          AND NOT EXISTS
          (
              SELECT 1
              FROM [dbo].[登录基础数据_二级菜单] AS existing
              WHERE existing.[账号代码] = src.[账号代码]
                AND (existing.[path] = @Path OR existing.[componet] = @Component OR existing.[title] = @Title)
          )
    )
    INSERT INTO [dbo].[登录基础数据_二级菜单]
        ([pid], [path], [componet], [redirect], [name], [title], [icon], [账号代码], [是否启用], [AddData], [DeleteData], [EditData], [SearchData])
    SELECT
        [pid],
        @Path,
        @Component,
        NULL,
        @Name,
        @Title,
        [icon],
        [账号代码],
        1,
        [AddData],
        [DeleteData],
        [EditData],
        [SearchData]
    FROM SourceRows;
END
ELSE
BEGIN
    ;WITH SourceRows AS
    (
        SELECT
            ROW_NUMBER() OVER (ORDER BY src.[账号代码], src.[id]) AS rn,
            src.[账号代码],
            COALESCE(prodParent.[id], src.[pid]) AS [pid],
            src.[icon],
            src.[AddData],
            src.[DeleteData],
            src.[EditData],
            src.[SearchData]
        FROM [dbo].[登录基础数据_二级菜单] AS src
        OUTER APPLY
        (
            SELECT TOP (1) rp.[id]
            FROM [dbo].[登录基础数据_二级菜单] AS rp
            WHERE rp.[账号代码] = src.[账号代码]
              AND rp.[是否启用] = 1
              AND rp.[title] = N'生产管理'
            ORDER BY rp.[id]
        ) AS prodParent
        WHERE src.[是否启用] = 1
          AND (@TargetRole IS NULL OR src.[账号代码] = @TargetRole)
          AND (src.[path] = @SourcePath OR src.[componet] = @SourceComponent OR src.[title] = N'生产计划跟踪')
          AND NOT EXISTS
          (
              SELECT 1
              FROM [dbo].[登录基础数据_二级菜单] AS existing
              WHERE existing.[账号代码] = src.[账号代码]
                AND (existing.[path] = @Path OR existing.[componet] = @Component OR existing.[title] = @Title)
          )
    ),
    BaseId AS
    (
        SELECT ISNULL(MAX([id]), 0) AS maxId
        FROM [dbo].[登录基础数据_二级菜单]
    )
    INSERT INTO [dbo].[登录基础数据_二级菜单]
        ([id], [pid], [path], [componet], [redirect], [name], [title], [icon], [账号代码], [是否启用], [AddData], [DeleteData], [EditData], [SearchData])
    SELECT
        BaseId.maxId + SourceRows.rn,
        SourceRows.[pid],
        @Path,
        @Component,
        NULL,
        @Name,
        @Title,
        SourceRows.[icon],
        SourceRows.[账号代码],
        1,
        SourceRows.[AddData],
        SourceRows.[DeleteData],
        SourceRows.[EditData],
        SourceRows.[SearchData]
    FROM SourceRows
    CROSS JOIN BaseId;
END;

UPDATE [dbo].[登录基础数据_二级菜单]
SET
    [path] = @Path,
    [componet] = @Component,
    [name] = @Name,
    [title] = @Title,
    [模块代码] = [id],
    [是否启用] = 1
WHERE [title] = @Title OR [componet] = @Component OR [path] = @Path;

SELECT [id], [pid], [path], [componet], [name], [title], [icon], [账号代码], [是否启用], [SearchData]
FROM [dbo].[登录基础数据_二级菜单]
WHERE [path] = @Path OR [componet] = @Component OR [title] = @Title
ORDER BY [账号代码], [id];

