-- WP-SD6: 改善计划台账菜单（短名，FUNC-S9-017 由前端映射表注入）
INSERT INTO SysMenu (Id, Pid, Type, Name, Path, Component, Title, Icon, IsIframe, IsHide, IsKeepAlive, IsAffix, OrderNo, Status, Remark, CreateTime)
VALUES
    (1320990000314, 1320990000200, 2, 'aidopSmartOpsImprovementPlans', '/aidop/smart-ops/improvement-plans', '/aidop/improvement/index', '改善计划台账', 'ele-Document', 0, 0, 1, 0, 195, 1, '智慧诊断改善计划台账', NOW())
ON DUPLICATE KEY UPDATE
    Pid        = VALUES(Pid),
    Type       = VALUES(Type),
    Name       = VALUES(Name),
    Path       = VALUES(Path),
    Component  = VALUES(Component),
    Title      = VALUES(Title),
    OrderNo    = VALUES(OrderNo),
    Remark     = VALUES(Remark),
    UpdateTime = NOW();

INSERT INTO SysTenantMenu (Id, TenantId, MenuId)
SELECT
    CAST(CONCAT(SUBSTRING(CAST(m.MenuId AS CHAR), -7), RIGHT(CAST(t.TenantId AS CHAR), 8)) AS UNSIGNED) AS Id,
    t.TenantId,
    m.MenuId
FROM (SELECT 1320990000314 AS MenuId) m
JOIN (
    SELECT DISTINCT TenantId
    FROM SysTenantMenu
    WHERE MenuId = 1320990000200
) t
WHERE NOT EXISTS (
    SELECT 1 FROM SysTenantMenu x WHERE x.TenantId = t.TenantId AND x.MenuId = m.MenuId
);
