-- S5 双模式样板种子（幂等）
-- 实体：本库 PurOrdRctDetail → mdp_stg_purchase_receipt
-- 任务：S5/S6/S7 KPI + S5 采购收货入站
-- 出站占位源：QMS_API（status=0，配置后启用）

CREATE TABLE IF NOT EXISTS mdp_stg_purchase_receipt (
  id BIGINT PRIMARY KEY AUTO_INCREMENT,
  tenant_id BIGINT NOT NULL,
  source_table VARCHAR(200),
  source_row_id VARCHAR(200),
  raw_data JSON,
  sync_batch_id VARCHAR(100),
  create_time DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
  KEY idx_batch (sync_batch_id),
  KEY idx_src (source_table, source_row_id),
  KEY idx_tenant (tenant_id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='S5采购收货贴源层';

INSERT INTO mdp_entity (
  tenant_id, source_id, entity_code, entity_name, entity_type,
  source_table_name, target_table_name, sync_mode, incr_column, batch_size, status, remark, create_time, update_time
)
SELECT 0, 1, 'S5_PURCHASE_RECEIPT_DETAIL', 'S5采购收货明细入站', 'TABLE',
       'PurOrdRctDetail', 'mdp_stg_purchase_receipt', 'INCREMENTAL', 'UpdateTime', 1000, 1,
       '双模式样板：本库PurOrdRctDetail→stg', NOW(), NOW()
FROM DUAL
WHERE NOT EXISTS (SELECT 1 FROM mdp_entity WHERE entity_code='S5_PURCHASE_RECEIPT_DETAIL');

INSERT INTO mdp_source (
  tenant_id, source_code, source_name, source_type, status,
  api_base_url, api_auth_type, api_auth_config, remark, create_time, update_time
)
SELECT 0, 'QMS_API', 'QMS 质量系统 API（出站占位）', 'API', 0,
       'https://qms.example.local/api', 'TOKEN', '{"token":"REPLACE_ME"}',
       'P-017 出站占位：配置真实地址与鉴权后将 status 置 1', NOW(), NOW()
FROM DUAL
WHERE NOT EXISTS (SELECT 1 FROM mdp_source WHERE source_code='QMS_API');

INSERT INTO mdp_sync_task (
  tenant_id, task_code, task_name, task_type, business_domain_code, business_domain_name,
  consumer_modules, source_system_code, service_key, job_code, status, config_version, description, create_time, update_time
)
SELECT * FROM (
  SELECT 0,'S5_MDP_SYNC_TRANSFORM','S5物料仓储T8 KPI同步','SERVICE_SYNC','material_warehouse','物料仓储','S5','T8_V5_SQLSERVER','S5MdpSyncTransformService','S5_MDP_SYNC_TRANSFORM',1,1,'S5 KPI（T8只读）',NOW(),NOW()
  UNION ALL SELECT 0,'S5_PURCHASE_RECEIPT_INBOUND','S5采购收货入站(源→stg→std)','DB_SYNC','material_warehouse','物料仓储','S5','AIDOPDEV_MYSQL','PurchaseReceiptMdpSyncService','S5_PURCHASE_RECEIPT_MDP_SYNC',1,1,'双模式样板入站',NOW(),NOW()
  UNION ALL SELECT 0,'S6_MDP_SYNC_TRANSFORM','S6生产执行T8 KPI同步','SERVICE_SYNC','manufacturing','生产执行','S6','T8_V5_SQLSERVER','S6MdpSyncTransformService','S6_MDP_SYNC_TRANSFORM',1,1,'S6 KPI',NOW(),NOW()
  UNION ALL SELECT 0,'S6_IPQC_INSPECTION_INBOUND','S6 IPQC入站(源→stg→std)','DB_SYNC','manufacturing','生产执行','S6','AIDOPDEV_MYSQL','IpqcInspectionMdpSyncService','S6_IPQC_INSPECTION_MDP_SYNC',1,1,'双模式样板入站',NOW(),NOW()
  UNION ALL SELECT 0,'S7_MDP_SYNC_TRANSFORM','S7成品仓储T8 KPI同步','SERVICE_SYNC','finished_warehouse','成品仓储','S7','T8_V5_SQLSERVER','S7MdpSyncTransformService','S7_MDP_SYNC_TRANSFORM',1,1,'S7 KPI',NOW(),NOW()
  UNION ALL SELECT 0,'S7_PRODUCTION_RECEIPT_INBOUND','S7成品入库入站(源→stg→std)','DB_SYNC','finished_warehouse','成品仓储','S7','AIDOPDEV_MYSQL','ProductionReceiptMdpSyncService','S5_PRODUCTION_RECEIPT_MDP_SYNC',1,1,'双模式样板入站(WOI)',NOW(),NOW()
) x
WHERE NOT EXISTS (SELECT 1 FROM mdp_sync_task t WHERE t.task_code=x.task_code);
