-- 1.0.126.sql
-- S1-MDP-MENU-1
-- 补齐统一 MDP 运行监控菜单及租户菜单范围，确保角色授权后可正常显示。

INSERT INTO SysMenu (
    Id, Pid, Type, Name, Path, Component, Title, Icon,
    IsIframe, IsHide, IsKeepAlive, IsAffix,
    OrderNo, Status, Remark, CreateTime
)
VALUES (
    1320990000406, 1320990000400, 2,
    'aidopDataPlatformMdpMonitor',
    '/aidop/data-platform/mdp-monitor',
    '/aidop/data-platform/mdpMonitor',
    'MDP运行监控',
    'ele-Monitor',
    0, 0, 1, 0,
    60, 1,
    '统一查看 S1/S3 等模块 MDP 同步与转换运行状态',
    NOW()
)
ON DUPLICATE KEY UPDATE
    Pid = VALUES(Pid),
    Type = VALUES(Type),
    Name = VALUES(Name),
    Path = VALUES(Path),
    Component = VALUES(Component),
    Title = VALUES(Title),
    Icon = VALUES(Icon),
    IsIframe = VALUES(IsIframe),
    IsHide = VALUES(IsHide),
    IsKeepAlive = VALUES(IsKeepAlive),
    IsAffix = VALUES(IsAffix),
    OrderNo = VALUES(OrderNo),
    Status = VALUES(Status),
    Remark = VALUES(Remark),
    UpdateTime = NOW();

INSERT INTO SysTenantMenu (Id, TenantId, MenuId)
SELECT CAST(CONCAT('406', RIGHT(CAST(t.TenantId AS CHAR), 10)) AS UNSIGNED) AS Id,
       t.TenantId,
       1320990000406 AS MenuId
FROM (
    SELECT DISTINCT TenantId
    FROM SysTenantMenu
    WHERE MenuId = 1320990000400
) t
WHERE NOT EXISTS (
    SELECT 1
    FROM SysTenantMenu existing
    WHERE existing.TenantId = t.TenantId
      AND existing.MenuId = 1320990000406
);

UPDATE mdp_source
SET db_host = COALESCE(NULLIF(db_host, ''), '123.60.180.165'),
    db_port = COALESCE(db_port, 3306),
    db_name = COALESCE(NULLIF(db_name, ''), 'aidopdev')
WHERE source_code = 'AIDOPDEV_MYSQL';
