-- S6/S7 双模式入站样板种子（幂等）
-- S6：qms_gcjyd → mdp_stg_ipqc_pull →（标准层仍读源表）
-- S7：NbrMaster → mdp_stg_production_receipt → mdp_std_production_receipt(WOI)

CREATE TABLE IF NOT EXISTS mdp_stg_ipqc_pull (
  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='S6 IPQC 执行器贴源层';

CREATE TABLE IF NOT EXISTS mdp_stg_production_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='S7成品入库(NbrMaster)执行器贴源层';

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, 'S6_IPQC_INSPECTION_HEAD', 'S6过程检验单头入站', 'TABLE',
       'qms_gcjyd', 'mdp_stg_ipqc_pull', 'FULL', NULL, 1000, 1,
       '双模式样板：本库qms_gcjyd→pull-stg（源无UpdateTime，FULL）', NOW(), NOW()
FROM DUAL
WHERE NOT EXISTS (SELECT 1 FROM mdp_entity WHERE entity_code='S6_IPQC_INSPECTION_HEAD');

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, 'S7_NBR_MASTER', 'S7成品入库NbrMaster入站', 'TABLE',
       'NbrMaster', 'mdp_stg_production_receipt', 'INCREMENTAL', 'UpdateTime', 1000, 1,
       '双模式样板：本库NbrMaster→stg；std仍过滤Type=WOI', NOW(), NOW()
FROM DUAL
WHERE NOT EXISTS (SELECT 1 FROM mdp_entity WHERE entity_code='S7_NBR_MASTER');

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 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()
FROM DUAL
WHERE NOT EXISTS (SELECT 1 FROM mdp_sync_task WHERE task_code='S6_IPQC_INSPECTION_INBOUND');

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 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()
FROM DUAL
WHERE NOT EXISTS (SELECT 1 FROM mdp_sync_task WHERE task_code='S7_PRODUCTION_RECEIPT_INBOUND');
