USE [YL_MESDB]
GO

DECLARE @NewMenuId INT;

SELECT @NewMenuId = MAX(id)
FROM [dbo].[登录基础数据_二级菜单]
WHERE title = N'结算工时'
   OR componet = '/ProductionManagement/SettlementWorkhours/index';

IF @NewMenuId IS NULL
BEGIN
    SELECT @NewMenuId = ISNULL(MAX(id), 0) + 1
    FROM [dbo].[登录基础数据_二级菜单];
END;

INSERT INTO [dbo].[登录基础数据_二级菜单]
    (id, pid, path, componet, redirect, name, title, icon, 账号代码, 模块代码, 是否启用, AddData, DeleteData, EditData, SearchData)
SELECT
    @NewMenuId AS id,
    3 AS pid,
    'SettlementWorkhours/index' AS path,
    '/ProductionManagement/SettlementWorkhours/index' AS componet,
    '' AS redirect,
    'SettlementWorkhours' AS name,
    N'结算工时' AS title,
    ISNULL(NULLIF(w.icon, ''), 'DimensionsSummaryReport') AS icon,
    w.账号代码,
    w.模块代码,
    ISNULL(w.是否启用, 1) AS 是否启用,
    w.AddData,
    w.DeleteData,
    w.EditData,
    w.SearchData
FROM [dbo].[登录基础数据_二级菜单] w
WHERE w.title = N'工时查询'
  AND NOT EXISTS (
      SELECT 1
      FROM [dbo].[登录基础数据_二级菜单] existing
      WHERE existing.title = N'结算工时'
        AND existing.账号代码 = w.账号代码
  );
GO
