USE [YL_MESDB]
GO

SET NOCOUNT ON;

DECLARE @Title nvarchar(100) = N'手续确认报表';
DECLARE @Path nvarchar(300) = N'FormalityConfirmReport/index';
DECLARE @Component nvarchar(300) = N'/ProductionManagement/FormalityConfirmReport/index';
DECLARE @Name nvarchar(100) = N'FormalityConfirmReport';
DECLARE @SourcePath nvarchar(300) = N'AssemblyCenter/index';
DECLARE @SourceComponent nvarchar(300) = N'/ProductionManagement/AssemblyCenter/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.[账号代码],
            src.[pid],
            src.[icon]
        FROM [dbo].[登录基础数据_二级菜单] src
        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].[登录基础数据_二级菜单] 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], [账号代码], [是否启用], [操作时间])
    SELECT
        [pid],
        @Path,
        @Component,
        NULL,
        @Name,
        @Title,
        [icon],
        [账号代码],
        1,
        GETDATE()
    FROM SourceRows;
END
ELSE
BEGIN
    ;WITH SourceRows AS (
        SELECT
            ROW_NUMBER() OVER (ORDER BY src.[账号代码], src.[id]) AS rn,
            src.[账号代码],
            src.[pid],
            src.[icon]
        FROM [dbo].[登录基础数据_二级菜单] src
        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].[登录基础数据_二级菜单] 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], [账号代码], [模块代码], [是否启用], [操作时间])
    SELECT
        BaseId.maxId + SourceRows.rn,
        SourceRows.[pid],
        @Path,
        @Component,
        NULL,
        @Name,
        @Title,
        SourceRows.[icon],
        SourceRows.[账号代码],
        BaseId.maxId + SourceRows.rn,
        1,
        GETDATE()
    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;

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