USE [YL_MESDB]
GO

SET NOCOUNT ON;

DECLARE @TargetAccount int = 1;
DECLARE @ParentTitle varchar(100) = '生产管理';
DECLARE @ParentId int;

SELECT TOP (1)
    @ParentId = [id]
FROM [dbo].[登录基础数据_二级菜单]
WHERE [账号代码] = @TargetAccount
  AND [是否启用] = 1
  AND [pid] = 0
  AND [title] = @ParentTitle
ORDER BY [id];

IF @ParentId IS NULL
BEGIN
    RAISERROR('未找到账号 1 的生产管理父菜单。', 16, 1);
    RETURN;
END;

DECLARE @Menus TABLE
(
    rn int IDENTITY(1,1) PRIMARY KEY,
    [path] varchar(300) NOT NULL,
    [componet] varchar(300) NOT NULL,
    [name] varchar(100) NOT NULL,
    [title] varchar(100) NOT NULL,
    [icon] varchar(100) NULL
);

INSERT INTO @Menus ([path], [componet], [name], [title], [icon])
VALUES
    ('DesignReportTask/Task/index', '/ProductionManagement/DesignReportTask/Task/index', 'DesignReportTask', '设计任务', 'DocumentImport'),
    ('DesignReportTask/Report/index', '/ProductionManagement/DesignReportTask/Report/index', 'DesignReportWork', '设计报工', 'DocumentImport'),
    ('DesignReportTask/Check/index', '/ProductionManagement/DesignReportTask/Check/index', 'DesignReportCheck', '设计工时校验', 'PayStateSearch'),
    ('DesignReportTask/HoursReport/index', '/ProductionManagement/DesignReportTask/HoursReport/index', 'DesignReportHoursReport', '设计工时报表', 'DimensionsSummaryReport');

DECLARE @BaseId int;

SELECT @BaseId = ISNULL(MAX([id]), 0)
FROM [dbo].[登录基础数据_二级菜单];

;WITH MissingMenus AS
(
    SELECT
        ROW_NUMBER() OVER (ORDER BY m.rn) AS new_rn,
        m.[path],
        m.[componet],
        m.[name],
        m.[title],
        m.[icon]
    FROM @Menus AS m
    WHERE NOT EXISTS
    (
        SELECT 1
        FROM [dbo].[登录基础数据_二级菜单] AS existing
        WHERE existing.[账号代码] = @TargetAccount
          AND
          (
              existing.[path] = m.[path]
              OR existing.[componet] = m.[componet]
              OR existing.[title] = m.[title]
          )
    )
)
INSERT INTO [dbo].[登录基础数据_二级菜单]
    ([id], [pid], [path], [componet], [redirect], [name], [title], [icon], [账号代码], [模块代码], [是否启用], [操作时间])
SELECT
    @BaseId + new_rn,
    @ParentId,
    [path],
    [componet],
    NULL,
    [name],
    [title],
    [icon],
    @TargetAccount,
    @BaseId + new_rn,
    1,
    GETDATE()
FROM MissingMenus;

UPDATE target
SET
    target.[pid] = @ParentId,
    target.[path] = source.[path],
    target.[componet] = source.[componet],
    target.[name] = source.[name],
    target.[title] = source.[title],
    target.[icon] = source.[icon],
    target.[模块代码] = target.[id],
    target.[是否启用] = 1,
    target.[操作时间] = GETDATE()
FROM [dbo].[登录基础数据_二级菜单] AS target
INNER JOIN @Menus AS source
    ON target.[账号代码] = @TargetAccount
   AND
   (
       target.[path] = source.[path]
       OR target.[componet] = source.[componet]
       OR target.[title] = source.[title]
   );

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