-- ============================================================
-- 1.0.163.sql
-- S5-S6-S7-MENU-SKELETON-FIRST-1
--
-- 业务目标：菜单先行，页面后补。
--   A) 12 个 S5/S6/S7 二级菜单从叶子（Type=Menu）转为目录（Type=Dir），
--      Component 由 /aidop/planning/index 改为 Layout，Path/Name 改为语义化。
--   B) 31 个 S5/S6/S7 三级菜单骨架新增，
--      Component 统一挂既有白名单页面 /aidop/planning/index（AidopDemoShell 占位）。
--      真实业务页面交付后，后续批次再逐项替换 Component。
--   C) S7 第 4 个二级菜单标题修正：成品库存管理 → 成品仓储看板。
--   D) 31 个三级菜单同步分配到「父二级菜单已分配」的所有租户。
--
-- 安全保证：
--   - UPDATE 带 Id + 旧 Type=2 守卫，幂等：已升级行不再命中。
--   - INSERT SysMenu 用 ON DUPLICATE KEY UPDATE，按本批次预期值覆盖结构字段。
--   - INSERT SysTenantMenu 用 NOT EXISTS 守卫 (TenantId, MenuId)，防重复分配。
--   - 不 DELETE / DROP / TRUNCATE。
--   - 不动 SysRoleMenu / SysOrg / SysUser / SysRole / SysTenant 等权限/租户主表。
--   - 不动 S0/S1/S2/S3/S4/S8 任何菜单。
-- ============================================================

-- A. 12 行 SysMenu UPDATE：二级菜单转目录 + 语义化 Path/Name
UPDATE SysMenu SET Type=1, Component='Layout', Path='/aidop/s5/iqc',                       Name='aidopS5Iqc',                       UpdateTime=NOW() WHERE Id=1322000000015 AND Type=2;
UPDATE SysMenu SET Type=1, Component='Layout', Path='/aidop/s5/warehouse',                 Name='aidopS5Warehouse',                 UpdateTime=NOW() WHERE Id=1322000000016 AND Type=2;
UPDATE SysMenu SET Type=1, Component='Layout', Path='/aidop/s5/inventory',                 Name='aidopS5Inventory',                 UpdateTime=NOW() WHERE Id=1322000000017 AND Type=2;
UPDATE SysMenu SET Type=1, Component='Layout', Path='/aidop/s5/warehouse-kanban',          Name='aidopS5WarehouseKanban',           UpdateTime=NOW() WHERE Id=1322000000018 AND Type=2;
UPDATE SysMenu SET Type=1, Component='Layout', Path='/aidop/s6/production-record',         Name='aidopS6ProductionRecord',          UpdateTime=NOW() WHERE Id=1322000000019 AND Type=2;
UPDATE SysMenu SET Type=1, Component='Layout', Path='/aidop/s6/process-quality',           Name='aidopS6ProcessQuality',            UpdateTime=NOW() WHERE Id=1322000000020 AND Type=2;
UPDATE SysMenu SET Type=1, Component='Layout', Path='/aidop/s6/equipment-tooling',         Name='aidopS6EquipmentTooling',          UpdateTime=NOW() WHERE Id=1322000000021 AND Type=2;
UPDATE SysMenu SET Type=1, Component='Layout', Path='/aidop/s6/execution-kanban',          Name='aidopS6ExecutionKanban',           UpdateTime=NOW() WHERE Id=1322000000022 AND Type=2;
UPDATE SysMenu SET Type=1, Component='Layout', Path='/aidop/s7/fqc',                       Name='aidopS7Fqc',                       UpdateTime=NOW() WHERE Id=1322000000023 AND Type=2;
UPDATE SysMenu SET Type=1, Component='Layout', Path='/aidop/s7/production-receipt',        Name='aidopS7ProductionReceipt',         UpdateTime=NOW() WHERE Id=1322000000024 AND Type=2;
UPDATE SysMenu SET Type=1, Component='Layout', Path='/aidop/s7/finished-outbound',         Name='aidopS7FinishedOutbound',          UpdateTime=NOW() WHERE Id=1322000000025 AND Type=2;
UPDATE SysMenu SET Type=1, Component='Layout', Path='/aidop/s7/finished-warehouse-kanban', Name='aidopS7FinishedWarehouseKanban',   Title='成品仓储看板', UpdateTime=NOW() WHERE Id=1322000000026 AND Type=2;

-- B. 31 行 SysMenu INSERT：三级菜单骨架，统一挂 /aidop/planning/index
INSERT INTO SysMenu (Id, Pid, Type, Name, Path, Component, Title, Icon, IsIframe, IsHide, IsKeepAlive, IsAffix, OrderNo, Status, Remark, CreateTime)
VALUES
    -- S5 来料检验 (Pid=1322000000015) 2 个
    (1329015010001, 1322000000015, 2, 'aidopS5IqcTaskList',                         '/aidop/s5/iqc/task-list',                           '/aidop/planning/index', '来料检验任务列表', 'ele-Document',     0, 0, 1, 0, 10, 1, 'S5 来料检验任务列表（真实页面待交付）', NOW()),
    (1329015010002, 1322000000015, 2, 'aidopS5IqcResultList',                       '/aidop/s5/iqc/result-list',                         '/aidop/planning/index', '来料检验结果列表', 'ele-Document',     0, 0, 1, 0, 20, 1, 'S5 来料检验结果列表（真实页面待交付）', NOW()),
    -- S5 仓储管理 (Pid=1322000000016) 5 个
    (1329015020001, 1322000000016, 2, 'aidopS5WarehouseOutsourceIssue',             '/aidop/s5/warehouse/outsource-issue',               '/aidop/planning/index', '委外发料单',       'ele-Document',     0, 0, 1, 0, 10, 1, 'S5 委外发料单（真实页面待交付）', NOW()),
    (1329015020002, 1322000000016, 2, 'aidopS5WarehousePurchaseReceipt',            '/aidop/s5/warehouse/purchase-receipt',              '/aidop/planning/index', '采购收货单',       'ele-Document',     0, 0, 1, 0, 20, 1, 'S5 采购收货单（真实页面待交付）', NOW()),
    (1329015020003, 1322000000016, 2, 'aidopS5WarehouseProductionIssue',            '/aidop/s5/warehouse/production-issue',              '/aidop/planning/index', '生产领料单',       'ele-Document',     0, 0, 1, 0, 30, 1, 'S5 生产领料单（真实页面待交付）', NOW()),
    (1329015020004, 1322000000016, 2, 'aidopS5WarehouseProductionReturn',           '/aidop/s5/warehouse/production-return',             '/aidop/planning/index', '生产退料单',       'ele-Document',     0, 0, 1, 0, 40, 1, 'S5 生产退料单（真实页面待交付）', NOW()),
    (1329015020005, 1322000000016, 2, 'aidopS5WarehouseProductionReceipt',          '/aidop/s5/warehouse/production-receipt',            '/aidop/planning/index', '生产入库单',       'ele-Document',     0, 0, 1, 0, 50, 1, 'S5 生产入库单（真实页面待交付）', NOW()),
    -- S5 库存数据 (Pid=1322000000017) 6 个
    (1329015030001, 1322000000017, 2, 'aidopS5InventoryLabelQuery',                 '/aidop/s5/inventory/label-query',                   '/aidop/planning/index', '标签查询',         'ele-Search',       0, 0, 1, 0, 10, 1, 'S5 标签查询（真实页面待交付）', NOW()),
    (1329015030002, 1322000000017, 2, 'aidopS5InventoryPendingInspection',          '/aidop/s5/inventory/pending-inspection',            '/aidop/planning/index', '暂收在检列表',     'ele-Document',     0, 0, 1, 0, 20, 1, 'S5 暂收在检列表（真实页面待交付）', NOW()),
    (1329015030003, 1322000000017, 2, 'aidopS5InventoryStockQuery',                 '/aidop/s5/inventory/stock-query',                   '/aidop/planning/index', '库存查询',         'ele-Search',       0, 0, 1, 0, 30, 1, 'S5 库存查询（真实页面待交付）', NOW()),
    (1329015030004, 1322000000017, 2, 'aidopS5InventoryInoutQuery',                 '/aidop/s5/inventory/inout-query',                   '/aidop/planning/index', '进出存查询',       'ele-Search',       0, 0, 1, 0, 40, 1, 'S5 进出存查询（真实页面待交付）', NOW()),
    (1329015030005, 1322000000017, 2, 'aidopS5InventoryStocktakeLabel',             '/aidop/s5/inventory/stocktake-label',               '/aidop/planning/index', '盘点标签确认',     'ele-Document',     0, 0, 1, 0, 50, 1, 'S5 盘点标签确认（真实页面待交付）', NOW()),
    (1329015030006, 1322000000017, 2, 'aidopS5InventoryStocktakeResult',            '/aidop/s5/inventory/stocktake-result',              '/aidop/planning/index', '盘点结果查询',     'ele-Search',       0, 0, 1, 0, 60, 1, 'S5 盘点结果查询（真实页面待交付）', NOW()),
    -- S5 物料仓储看板 (Pid=1322000000018) 4 个
    (1329015040001, 1322000000018, 2, 'aidopS5WarehouseKanbanReceivingTask',        '/aidop/s5/warehouse-kanban/receiving-task',         '/aidop/planning/index', '收料任务看板',     'ele-DataBoard',    0, 0, 1, 0, 10, 1, 'S5 收料任务看板（真实页面待交付）', NOW()),
    (1329015040002, 1322000000018, 2, 'aidopS5WarehouseKanbanIssuingTask',          '/aidop/s5/warehouse-kanban/issuing-task',           '/aidop/planning/index', '发料任务看板',     'ele-DataBoard',    0, 0, 1, 0, 20, 1, 'S5 发料任务看板（真实页面待交付）', NOW()),
    (1329015040003, 1322000000018, 2, 'aidopS5WarehouseKanbanTurnoverReport',       '/aidop/s5/warehouse-kanban/turnover-report',        '/aidop/planning/index', '库存周转报表',     'ele-DataAnalysis', 0, 0, 1, 0, 30, 1, 'S5 库存周转报表（真实页面待交付）', NOW()),
    (1329015040004, 1322000000018, 2, 'aidopS5WarehouseKanbanAgeAnalysis',          '/aidop/s5/warehouse-kanban/age-analysis',           '/aidop/planning/index', '物料库龄分析',     'ele-DataAnalysis', 0, 0, 1, 0, 40, 1, 'S5 物料库龄分析（真实页面待交付）', NOW()),
    -- S6 生产记录管理 (Pid=1322000000019) 1 个
    (1329016010001, 1322000000019, 2, 'aidopS6ProductionRecordOrderList',           '/aidop/s6/production-record/order-list',            '/aidop/planning/index', '生产指令单列表',   'ele-Document',     0, 0, 1, 0, 10, 1, 'S6 生产指令单列表（真实页面待交付）', NOW()),
    -- S6 过程质量管理 (Pid=1322000000020) 1 个
    (1329016020001, 1322000000020, 2, 'aidopS6ProcessQualityInspectionList',        '/aidop/s6/process-quality/inspection-list',         '/aidop/planning/index', '过程检验单列表',   'ele-Document',     0, 0, 1, 0, 10, 1, 'S6 过程检验单列表（真实页面待交付）', NOW()),
    -- S6 设备工装管理 (Pid=1322000000021) 2 个
    (1329016030001, 1322000000021, 2, 'aidopS6EquipmentToolingEquipmentLedger',     '/aidop/s6/equipment-tooling/equipment-ledger',      '/aidop/planning/index', '生产设备台账',     'ele-Document',     0, 0, 1, 0, 10, 1, 'S6 生产设备台账(真实页面待交付)', NOW()),
    (1329016030002, 1322000000021, 2, 'aidopS6EquipmentToolingToolingLedger',       '/aidop/s6/equipment-tooling/tooling-ledger',        '/aidop/planning/index', '模工具台账管理',   'ele-Document',     0, 0, 1, 0, 20, 1, 'S6 模工具台账管理(真实页面待交付)', NOW()),
    -- S6 生产执行看板 (Pid=1322000000022) 4 个
    (1329016040001, 1322000000022, 2, 'aidopS6ExecutionKanbanWorkOrder',            '/aidop/s6/execution-kanban/work-order',             '/aidop/planning/index', '工单执行看板',     'ele-DataBoard',    0, 0, 1, 0, 10, 1, 'S6 工单执行看板（真实页面待交付）', NOW()),
    (1329016040002, 1322000000022, 2, 'aidopS6ExecutionKanbanWorkshopEfficiency',   '/aidop/s6/execution-kanban/workshop-efficiency',    '/aidop/planning/index', '车间效率看板',     'ele-DataBoard',    0, 0, 1, 0, 20, 1, 'S6 车间效率看板（真实页面待交付）', NOW()),
    (1329016040003, 1322000000022, 2, 'aidopS6ExecutionKanbanEquipmentOee',         '/aidop/s6/execution-kanban/equipment-oee',          '/aidop/planning/index', '设备OEE看板',      'ele-DataBoard',    0, 0, 1, 0, 30, 1, 'S6 设备OEE看板（真实页面待交付）', NOW()),
    (1329016040004, 1322000000022, 2, 'aidopS6ExecutionKanbanLaborHours',           '/aidop/s6/execution-kanban/labor-hours',            '/aidop/planning/index', '员工工时看板',     'ele-DataBoard',    0, 0, 1, 0, 40, 1, 'S6 员工工时看板（真实页面待交付）', NOW()),
    -- S7 成品质量管理 (Pid=1322000000023) 2 个
    (1329017010001, 1322000000023, 2, 'aidopS7FqcTaskList',                         '/aidop/s7/fqc/task-list',                           '/aidop/planning/index', 'FQC检验任务列表',  'ele-Document',     0, 0, 1, 0, 10, 1, 'S7 FQC检验任务列表（真实页面待交付）', NOW()),
    (1329017010002, 1322000000023, 2, 'aidopS7FqcResultList',                       '/aidop/s7/fqc/result-list',                         '/aidop/planning/index', 'FQC检验结果列表',  'ele-Document',     0, 0, 1, 0, 20, 1, 'S7 FQC检验结果列表（真实页面待交付）', NOW()),
    -- S7 生产入库管理 (Pid=1322000000024) 1 个
    (1329017020001, 1322000000024, 2, 'aidopS7ProductionReceiptOrderList',          '/aidop/s7/production-receipt/order-list',           '/aidop/planning/index', '生产入库单列表',   'ele-Document',     0, 0, 1, 0, 10, 1, 'S7 生产入库单列表（真实页面待交付）', NOW()),
    -- S7 成品出库管理 (Pid=1322000000025) 1 个
    (1329017030001, 1322000000025, 2, 'aidopS7FinishedOutboundSalesShipmentNotice', '/aidop/s7/finished-outbound/sales-shipment-notice', '/aidop/planning/index', '销售发货通知',     'ele-Document',     0, 0, 1, 0, 10, 1, 'S7 销售发货通知（真实页面待交付）', NOW()),
    -- S7 成品仓储看板 (Pid=1322000000026) 2 个
    (1329017040001, 1322000000026, 2, 'aidopS7FinishedWarehouseKanbanShipmentTask', '/aidop/s7/finished-warehouse-kanban/shipment-task', '/aidop/planning/index', '发货任务看板',     'ele-DataBoard',    0, 0, 1, 0, 10, 1, 'S7 发货任务看板（真实页面待交付）', NOW()),
    (1329017040002, 1322000000026, 2, 'aidopS7FinishedWarehouseKanbanSluggish',     '/aidop/s7/finished-warehouse-kanban/sluggish',      '/aidop/planning/index', '成品滞销看板',     'ele-DataBoard',    0, 0, 1, 0, 20, 1, 'S7 成品滞销看板（真实页面待交付）', 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();

-- C. SysTenantMenu 同步：31 三级菜单 × 已分配 S5 顶级目录 (1321000006000) 的所有租户，幂等 NOT EXISTS
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 1329015010001 AS MenuId UNION ALL SELECT 1329015010002 UNION ALL
    SELECT 1329015020001 UNION ALL SELECT 1329015020002 UNION ALL SELECT 1329015020003 UNION ALL SELECT 1329015020004 UNION ALL SELECT 1329015020005 UNION ALL
    SELECT 1329015030001 UNION ALL SELECT 1329015030002 UNION ALL SELECT 1329015030003 UNION ALL SELECT 1329015030004 UNION ALL SELECT 1329015030005 UNION ALL SELECT 1329015030006 UNION ALL
    SELECT 1329015040001 UNION ALL SELECT 1329015040002 UNION ALL SELECT 1329015040003 UNION ALL SELECT 1329015040004 UNION ALL
    SELECT 1329016010001 UNION ALL
    SELECT 1329016020001 UNION ALL
    SELECT 1329016030001 UNION ALL SELECT 1329016030002 UNION ALL
    SELECT 1329016040001 UNION ALL SELECT 1329016040002 UNION ALL SELECT 1329016040003 UNION ALL SELECT 1329016040004 UNION ALL
    SELECT 1329017010001 UNION ALL SELECT 1329017010002 UNION ALL
    SELECT 1329017020001 UNION ALL
    SELECT 1329017030001 UNION ALL
    SELECT 1329017040001 UNION ALL SELECT 1329017040002
) m
CROSS JOIN (
    SELECT DISTINCT TenantId FROM SysTenantMenu WHERE MenuId = 1321000006000
) t
WHERE NOT EXISTS (
    SELECT 1 FROM SysTenantMenu existing
    WHERE existing.TenantId = t.TenantId AND existing.MenuId = m.MenuId
);
