-- 1.0.259: Phase 1 统一入站底座
-- 1) 3 张通用 stg 升到 canonical 契约
-- 2) mdp_entity.biz_key_expr + 全量回填
-- 3) 补登记 S5_PURCHASE_RECEIPT_MASTER / S7_NBR_DETAIL
-- 幂等：information_schema 守卫，可重复执行

-- ========== mdp_stg_purchase_receipt ==========
SET @ddl := (SELECT IF(
  EXISTS(SELECT 1 FROM information_schema.COLUMNS WHERE TABLE_SCHEMA=DATABASE() AND TABLE_NAME='mdp_stg_purchase_receipt' AND COLUMN_NAME='source_system'),
  'SELECT 1',
  'ALTER TABLE mdp_stg_purchase_receipt ADD COLUMN source_system varchar(50) NULL AFTER tenant_id'));
PREPARE s FROM @ddl; EXECUTE s; DEALLOCATE PREPARE s;

SET @ddl := (SELECT IF(
  EXISTS(SELECT 1 FROM information_schema.COLUMNS WHERE TABLE_SCHEMA=DATABASE() AND TABLE_NAME='mdp_stg_purchase_receipt' AND COLUMN_NAME='source_biz_key'),
  'SELECT 1',
  'ALTER TABLE mdp_stg_purchase_receipt ADD COLUMN source_biz_key varchar(300) NULL AFTER source_row_id'));
PREPARE s FROM @ddl; EXECUTE s; DEALLOCATE PREPARE s;

SET @ddl := (SELECT IF(
  EXISTS(SELECT 1 FROM information_schema.COLUMNS WHERE TABLE_SCHEMA=DATABASE() AND TABLE_NAME='mdp_stg_purchase_receipt' AND COLUMN_NAME='sync_time'),
  'SELECT 1',
  'ALTER TABLE mdp_stg_purchase_receipt ADD COLUMN sync_time datetime NULL DEFAULT CURRENT_TIMESTAMP'));
PREPARE s FROM @ddl; EXECUTE s; DEALLOCATE PREPARE s;

SET @ddl := (SELECT IF(
  EXISTS(SELECT 1 FROM information_schema.COLUMNS WHERE TABLE_SCHEMA=DATABASE() AND TABLE_NAME='mdp_stg_purchase_receipt' AND COLUMN_NAME='process_status'),
  'SELECT 1',
  'ALTER TABLE mdp_stg_purchase_receipt ADD COLUMN process_status varchar(20) NOT NULL DEFAULT ''PENDING'''));
PREPARE s FROM @ddl; EXECUTE s; DEALLOCATE PREPARE s;

SET @ddl := (SELECT IF(
  EXISTS(SELECT 1 FROM information_schema.COLUMNS WHERE TABLE_SCHEMA=DATABASE() AND TABLE_NAME='mdp_stg_purchase_receipt' AND COLUMN_NAME='process_message'),
  'SELECT 1',
  'ALTER TABLE mdp_stg_purchase_receipt ADD COLUMN process_message varchar(500) NULL'));
PREPARE s FROM @ddl; EXECUTE s; DEALLOCATE PREPARE s;

SET @ddl := (SELECT IF(
  EXISTS(SELECT 1 FROM information_schema.COLUMNS WHERE TABLE_SCHEMA=DATABASE() AND TABLE_NAME='mdp_stg_purchase_receipt' AND COLUMN_NAME='update_time'),
  'SELECT 1',
  'ALTER TABLE mdp_stg_purchase_receipt ADD COLUMN update_time datetime NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP'));
PREPARE s FROM @ddl; EXECUTE s; DEALLOCATE PREPARE s;

-- ========== mdp_stg_production_receipt ==========
SET @ddl := (SELECT IF(
  EXISTS(SELECT 1 FROM information_schema.COLUMNS WHERE TABLE_SCHEMA=DATABASE() AND TABLE_NAME='mdp_stg_production_receipt' AND COLUMN_NAME='source_system'),
  'SELECT 1',
  'ALTER TABLE mdp_stg_production_receipt ADD COLUMN source_system varchar(50) NULL AFTER tenant_id'));
PREPARE s FROM @ddl; EXECUTE s; DEALLOCATE PREPARE s;

SET @ddl := (SELECT IF(
  EXISTS(SELECT 1 FROM information_schema.COLUMNS WHERE TABLE_SCHEMA=DATABASE() AND TABLE_NAME='mdp_stg_production_receipt' AND COLUMN_NAME='source_biz_key'),
  'SELECT 1',
  'ALTER TABLE mdp_stg_production_receipt ADD COLUMN source_biz_key varchar(300) NULL AFTER source_row_id'));
PREPARE s FROM @ddl; EXECUTE s; DEALLOCATE PREPARE s;

SET @ddl := (SELECT IF(
  EXISTS(SELECT 1 FROM information_schema.COLUMNS WHERE TABLE_SCHEMA=DATABASE() AND TABLE_NAME='mdp_stg_production_receipt' AND COLUMN_NAME='sync_time'),
  'SELECT 1',
  'ALTER TABLE mdp_stg_production_receipt ADD COLUMN sync_time datetime NULL DEFAULT CURRENT_TIMESTAMP'));
PREPARE s FROM @ddl; EXECUTE s; DEALLOCATE PREPARE s;

SET @ddl := (SELECT IF(
  EXISTS(SELECT 1 FROM information_schema.COLUMNS WHERE TABLE_SCHEMA=DATABASE() AND TABLE_NAME='mdp_stg_production_receipt' AND COLUMN_NAME='process_status'),
  'SELECT 1',
  'ALTER TABLE mdp_stg_production_receipt ADD COLUMN process_status varchar(20) NOT NULL DEFAULT ''PENDING'''));
PREPARE s FROM @ddl; EXECUTE s; DEALLOCATE PREPARE s;

SET @ddl := (SELECT IF(
  EXISTS(SELECT 1 FROM information_schema.COLUMNS WHERE TABLE_SCHEMA=DATABASE() AND TABLE_NAME='mdp_stg_production_receipt' AND COLUMN_NAME='process_message'),
  'SELECT 1',
  'ALTER TABLE mdp_stg_production_receipt ADD COLUMN process_message varchar(500) NULL'));
PREPARE s FROM @ddl; EXECUTE s; DEALLOCATE PREPARE s;

SET @ddl := (SELECT IF(
  EXISTS(SELECT 1 FROM information_schema.COLUMNS WHERE TABLE_SCHEMA=DATABASE() AND TABLE_NAME='mdp_stg_production_receipt' AND COLUMN_NAME='update_time'),
  'SELECT 1',
  'ALTER TABLE mdp_stg_production_receipt ADD COLUMN update_time datetime NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP'));
PREPARE s FROM @ddl; EXECUTE s; DEALLOCATE PREPARE s;

-- ========== mdp_stg_ipqc_pull ==========
SET @ddl := (SELECT IF(
  EXISTS(SELECT 1 FROM information_schema.COLUMNS WHERE TABLE_SCHEMA=DATABASE() AND TABLE_NAME='mdp_stg_ipqc_pull' AND COLUMN_NAME='source_system'),
  'SELECT 1',
  'ALTER TABLE mdp_stg_ipqc_pull ADD COLUMN source_system varchar(50) NULL AFTER tenant_id'));
PREPARE s FROM @ddl; EXECUTE s; DEALLOCATE PREPARE s;

SET @ddl := (SELECT IF(
  EXISTS(SELECT 1 FROM information_schema.COLUMNS WHERE TABLE_SCHEMA=DATABASE() AND TABLE_NAME='mdp_stg_ipqc_pull' AND COLUMN_NAME='source_biz_key'),
  'SELECT 1',
  'ALTER TABLE mdp_stg_ipqc_pull ADD COLUMN source_biz_key varchar(300) NULL AFTER source_row_id'));
PREPARE s FROM @ddl; EXECUTE s; DEALLOCATE PREPARE s;

SET @ddl := (SELECT IF(
  EXISTS(SELECT 1 FROM information_schema.COLUMNS WHERE TABLE_SCHEMA=DATABASE() AND TABLE_NAME='mdp_stg_ipqc_pull' AND COLUMN_NAME='sync_time'),
  'SELECT 1',
  'ALTER TABLE mdp_stg_ipqc_pull ADD COLUMN sync_time datetime NULL DEFAULT CURRENT_TIMESTAMP'));
PREPARE s FROM @ddl; EXECUTE s; DEALLOCATE PREPARE s;

SET @ddl := (SELECT IF(
  EXISTS(SELECT 1 FROM information_schema.COLUMNS WHERE TABLE_SCHEMA=DATABASE() AND TABLE_NAME='mdp_stg_ipqc_pull' AND COLUMN_NAME='process_status'),
  'SELECT 1',
  'ALTER TABLE mdp_stg_ipqc_pull ADD COLUMN process_status varchar(20) NOT NULL DEFAULT ''PENDING'''));
PREPARE s FROM @ddl; EXECUTE s; DEALLOCATE PREPARE s;

SET @ddl := (SELECT IF(
  EXISTS(SELECT 1 FROM information_schema.COLUMNS WHERE TABLE_SCHEMA=DATABASE() AND TABLE_NAME='mdp_stg_ipqc_pull' AND COLUMN_NAME='process_message'),
  'SELECT 1',
  'ALTER TABLE mdp_stg_ipqc_pull ADD COLUMN process_message varchar(500) NULL'));
PREPARE s FROM @ddl; EXECUTE s; DEALLOCATE PREPARE s;

SET @ddl := (SELECT IF(
  EXISTS(SELECT 1 FROM information_schema.COLUMNS WHERE TABLE_SCHEMA=DATABASE() AND TABLE_NAME='mdp_stg_ipqc_pull' AND COLUMN_NAME='update_time'),
  'SELECT 1',
  'ALTER TABLE mdp_stg_ipqc_pull ADD COLUMN update_time datetime NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP'));
PREPARE s FROM @ddl; EXECUTE s; DEALLOCATE PREPARE s;

-- backfill before unique key
UPDATE mdp_stg_purchase_receipt
SET source_system = IFNULL(NULLIF(source_system, ''), 'LEGACY'),
    source_biz_key = IFNULL(NULLIF(source_biz_key, ''), IFNULL(source_row_id, CONCAT('row-', id)))
WHERE source_system IS NULL OR source_system = '' OR source_biz_key IS NULL OR source_biz_key = '';

UPDATE mdp_stg_production_receipt
SET source_system = IFNULL(NULLIF(source_system, ''), 'LEGACY'),
    source_biz_key = IFNULL(NULLIF(source_biz_key, ''), IFNULL(source_row_id, CONCAT('row-', id)))
WHERE source_system IS NULL OR source_system = '' OR source_biz_key IS NULL OR source_biz_key = '';

UPDATE mdp_stg_ipqc_pull
SET source_system = IFNULL(NULLIF(source_system, ''), 'LEGACY'),
    source_biz_key = IFNULL(NULLIF(source_biz_key, ''), IFNULL(source_row_id, CONCAT('row-', id)))
WHERE source_system IS NULL OR source_system = '' OR source_biz_key IS NULL OR source_biz_key = '';

SET @ddl := (SELECT IF(
  EXISTS(SELECT 1 FROM information_schema.STATISTICS WHERE TABLE_SCHEMA=DATABASE() AND TABLE_NAME='mdp_stg_purchase_receipt' AND INDEX_NAME='uk_source_key'),
  'SELECT 1',
  'ALTER TABLE mdp_stg_purchase_receipt ADD UNIQUE KEY uk_source_key (source_system, source_table, source_biz_key)'));
PREPARE s FROM @ddl; EXECUTE s; DEALLOCATE PREPARE s;

SET @ddl := (SELECT IF(
  EXISTS(SELECT 1 FROM information_schema.STATISTICS WHERE TABLE_SCHEMA=DATABASE() AND TABLE_NAME='mdp_stg_production_receipt' AND INDEX_NAME='uk_source_key'),
  'SELECT 1',
  'ALTER TABLE mdp_stg_production_receipt ADD UNIQUE KEY uk_source_key (source_system, source_table, source_biz_key)'));
PREPARE s FROM @ddl; EXECUTE s; DEALLOCATE PREPARE s;

SET @ddl := (SELECT IF(
  EXISTS(SELECT 1 FROM information_schema.STATISTICS WHERE TABLE_SCHEMA=DATABASE() AND TABLE_NAME='mdp_stg_ipqc_pull' AND INDEX_NAME='uk_source_key'),
  'SELECT 1',
  'ALTER TABLE mdp_stg_ipqc_pull ADD UNIQUE KEY uk_source_key (source_system, source_table, source_biz_key)'));
PREPARE s FROM @ddl; EXECUTE s; DEALLOCATE PREPARE s;

-- ========== mdp_entity.biz_key_expr ==========
SET @ddl := (SELECT IF(
  EXISTS(SELECT 1 FROM information_schema.COLUMNS WHERE TABLE_SCHEMA=DATABASE() AND TABLE_NAME='mdp_entity' AND COLUMN_NAME='biz_key_expr'),
  'SELECT 1',
  'ALTER TABLE mdp_entity ADD COLUMN biz_key_expr varchar(300) NULL COMMENT ''业务键规格：逗号分隔有序源字段名'''));
PREPARE s FROM @ddl; EXECUTE s; DEALLOCATE PREPARE s;

UPDATE mdp_entity SET biz_key_expr='bill_no'                     WHERE entity_code='S1_SEORDER';
UPDATE mdp_entity SET biz_key_expr='bill_no,entry_seq'          WHERE entity_code='S1_SEORDER_ENTRY';
UPDATE mdp_entity SET biz_key_expr='bill_no,Id'                 WHERE entity_code='S1_SEORDER_CHANGE';
UPDATE mdp_entity SET biz_key_expr='BillNo'                     WHERE entity_code='S1_CONTRACT_REVIEW';
UPDATE mdp_entity SET biz_key_expr='ReviewBillNo,StageNo,RecID' WHERE entity_code='S1_CONTRACT_REVIEW_FLOW';
UPDATE mdp_entity SET biz_key_expr='BillNo'                     WHERE entity_code='S1_PRODUCT_DESIGN';
UPDATE mdp_entity SET biz_key_expr='ProductDesignId,Id'        WHERE entity_code='S1_PRODUCT_DESIGN_BOM';
UPDATE mdp_entity SET biz_key_expr='ProductDesignId,Id'        WHERE entity_code='S1_PRODUCT_DESIGN_ROUTING';
UPDATE mdp_entity SET biz_key_expr='bill_no,morder_no,Id'      WHERE entity_code='S1_REQUIREMENT_EXAMINE_RESULT';
UPDATE mdp_entity SET biz_key_expr='examine_id,item_number,Id' WHERE entity_code='S1_REQUIREMENT_EXAMINE_DETAIL';
UPDATE mdp_entity SET biz_key_expr='LotSerial'                 WHERE entity_code='S1_SHIPPING_PLAN';
UPDATE mdp_entity SET biz_key_expr='plan_id,OrdNbr,RecID'      WHERE entity_code='S1_SHIPPING_PLAN_DETAIL';
UPDATE mdp_entity SET biz_key_expr='Id'                        WHERE entity_code='S1_ASN_SHIPPER_MASTER';
UPDATE mdp_entity SET biz_key_expr='Id,Line'                   WHERE entity_code='S1_ASN_SHIPPER_DETAIL';
UPDATE mdp_entity SET biz_key_expr='bill_no,item_number,id'    WHERE entity_code='S1_LINKAGE_PLAN';
UPDATE mdp_entity SET biz_key_expr='Domain,WorkOrd'                WHERE entity_code='S2_WORK_ORDER_MASTER';
UPDATE mdp_entity SET biz_key_expr='Domain,WorkOrd,OP'            WHERE entity_code='S2_WORK_ORDER_ROUTING';
UPDATE mdp_entity SET biz_key_expr='Domain,WorkOrd,Op,ItemNum'   WHERE entity_code='S2_WORK_ORDER_DETAIL';
UPDATE mdp_entity SET biz_key_expr='Domain,WorkOrds,Op,Sequence' WHERE entity_code='S2_PERIOD_SEQUENCE_DET';
UPDATE mdp_entity SET biz_key_expr='Domain,WorkOrd,Op,WorkDate'  WHERE entity_code='S2_SCHEDULE_RESULT_OP';
UPDATE mdp_entity SET biz_key_expr='Domain,Supp'                         WHERE entity_code='S3_SUPPLIER';
UPDATE mdp_entity SET biz_key_expr='Domain,Address'                      WHERE entity_code='S3_CONSIGNEE_SUPPLIER';
UPDATE mdp_entity SET biz_key_expr='Domain,ItemNum'                      WHERE entity_code='S3_ITEM_ERP';
UPDATE mdp_entity SET biz_key_expr='tenant_id,number'                    WHERE entity_code='S3_ITEM_NEW';
UPDATE mdp_entity SET biz_key_expr='number,supplier_number'              WHERE entity_code='S3_SOURCE_LIST';
UPDATE mdp_entity SET biz_key_expr='Id'                                  WHERE entity_code='S3_SUPPLY_DEMAND';
UPDATE mdp_entity SET biz_key_expr='pr_billno'                           WHERE entity_code='S3_PURCHASE_REQUEST';
UPDATE mdp_entity SET biz_key_expr='Domain,PurOrd'                       WHERE entity_code='S3_PURCHASE_ORDER_MASTER';
UPDATE mdp_entity SET biz_key_expr='Domain,PurOrd,Line'                  WHERE entity_code='S3_PURCHASE_ORDER_DETAIL';
UPDATE mdp_entity SET biz_key_expr='dsnum'                               WHERE entity_code='S3_DELIVERY_PLAN';
UPDATE mdp_entity SET biz_key_expr='shddh'                               WHERE entity_code='S3_SHIPPER_MASTER';
UPDATE mdp_entity SET biz_key_expr='glid,id'                             WHERE entity_code='S3_SHIPPER_DETAIL';
UPDATE mdp_entity SET biz_key_expr='Domain,Receiver'                     WHERE entity_code='S3_RECEIPT_MASTER';
UPDATE mdp_entity SET biz_key_expr='Domain,Receiver,Line'                WHERE entity_code='S3_RECEIPT_DETAIL';
UPDATE mdp_entity SET biz_key_expr='Domain,WorkOrd'                      WHERE entity_code='S3_WORK_ORDER_MASTER';
UPDATE mdp_entity SET biz_key_expr='Domain,WorkOrd,Op,ItemNum'          WHERE entity_code='S3_WORK_ORDER_DETAIL';
UPDATE mdp_entity SET biz_key_expr='Domain,WorkOrd,OP'                   WHERE entity_code='S3_WORK_ORDER_ROUTING';
UPDATE mdp_entity SET biz_key_expr='Domain,RoutingCode,Op,SupplierCode' WHERE entity_code='S3_ROUTING_OUTSOURCE';
UPDATE mdp_entity SET biz_key_expr='Domain,ItemNum,Location'            WHERE entity_code='S3_INVENTORY';
UPDATE mdp_entity SET biz_key_expr='Domain,Receiver,Line'           WHERE entity_code='S4_IQC_RECEIPT';
UPDATE mdp_entity SET biz_key_expr='glid,id'                        WHERE entity_code='S4_SHIPMENT_EXEC';
UPDATE mdp_entity SET biz_key_expr='dsnum'                          WHERE entity_code='S4_RETURN_EXEC';
UPDATE mdp_entity SET biz_key_expr='work_order,component_item_code' WHERE entity_code='S4_SHORTAGE_EXEC';
UPDATE mdp_entity SET biz_key_expr='Domain,Receiver,Line' WHERE entity_code='S5_PURCHASE_RECEIPT_DETAIL';
UPDATE mdp_entity SET biz_key_expr='djbh'                 WHERE entity_code='S6_IPQC_INSPECTION_HEAD';
UPDATE mdp_entity SET biz_key_expr='Domain,Nbr'          WHERE entity_code='S7_NBR_MASTER';

INSERT INTO mdp_entity
  (tenant_id, source_id, entity_code, entity_name, entity_type, source_table_name, target_table_name,
   sync_mode, batch_size, biz_key_expr, status, remark, create_time, update_time)
SELECT 0, 1, 'S5_PURCHASE_RECEIPT_MASTER', '采购收货主', 'TABLE', 'PurOrdRctMaster', 'mdp_stg_purchase_receipt',
       'FULL', 5000, 'Domain,Receiver', 1, 'S5 采购收货主表贴源（Phase1）', NOW(), NOW()
WHERE NOT EXISTS (SELECT 1 FROM mdp_entity WHERE entity_code='S5_PURCHASE_RECEIPT_MASTER');

INSERT INTO mdp_entity
  (tenant_id, source_id, entity_code, entity_name, entity_type, source_table_name, target_table_name,
   sync_mode, batch_size, biz_key_expr, status, remark, create_time, update_time)
SELECT 0, 1, 'S7_NBR_DETAIL', '成品入库明细', 'TABLE', 'NbrDetail', 'mdp_stg_production_receipt',
       'FULL', 5000, 'Domain,Nbr,Line', 1, 'S7 成品入库明细贴源（Phase1）', NOW(), NOW()
WHERE NOT EXISTS (SELECT 1 FROM mdp_entity WHERE entity_code='S7_NBR_DETAIL');

-- ========== (4) Mock API 源 + S5/S6/S7 API 实体（Phase2 起跑）==========
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, 'WMS_API', 'WMS 收货 API（Mock）', 'API', 1,
       'http://127.0.0.1:8018', 'TOKEN', '{"token":"uat-mock-token"}',
       '双模式契约测试用', NOW(), NOW()
WHERE NOT EXISTS (SELECT 1 FROM mdp_source WHERE source_code='WMS_API');

INSERT INTO mdp_entity
  (tenant_id, source_id, entity_code, entity_name, entity_type,
   source_table_name, source_api_path, target_table_name,
   sync_mode, incr_column, batch_size, response_data_path, dedup_key_path, biz_key_expr,
   status, remark, create_time, update_time)
SELECT 0, (SELECT id FROM mdp_source WHERE source_code='WMS_API'),
       'S5_PURCHASE_RECEIPT_DETAIL_API', 'S5采购收货明细入站(API)', 'API',
       'PurOrdRctDetail', '/api/receipt', 'mdp_stg_purchase_receipt',
       'INCREMENTAL', 'updateTime', 1000, 'data.list', 'bizKey', 'Domain,Receiver,Line',
       1, 'Phase2 API', NOW(), NOW()
WHERE NOT EXISTS (SELECT 1 FROM mdp_entity WHERE entity_code='S5_PURCHASE_RECEIPT_DETAIL_API');

INSERT INTO mdp_entity
  (tenant_id, source_id, entity_code, entity_name, entity_type,
   source_table_name, source_api_path, target_table_name,
   sync_mode, incr_column, batch_size, response_data_path, dedup_key_path, biz_key_expr,
   status, remark, create_time, update_time)
SELECT 0, (SELECT id FROM mdp_source WHERE source_code='WMS_API'),
       'S5_PURCHASE_RECEIPT_MASTER_API', 'S5采购收货主入站(API)', 'API',
       'PurOrdRctMaster', '/api/receipt-master', 'mdp_stg_purchase_receipt',
       'INCREMENTAL', 'updateTime', 1000, 'data.list', 'bizKey', 'Domain,Receiver',
       1, 'Phase2 API', NOW(), NOW()
WHERE NOT EXISTS (SELECT 1 FROM mdp_entity WHERE entity_code='S5_PURCHASE_RECEIPT_MASTER_API');

INSERT INTO mdp_entity
  (tenant_id, source_id, entity_code, entity_name, entity_type,
   source_table_name, source_api_path, target_table_name,
   sync_mode, incr_column, batch_size, response_data_path, dedup_key_path, biz_key_expr,
   status, remark, create_time, update_time)
SELECT 0, (SELECT id FROM mdp_source WHERE source_code='WMS_API'),
       'S6_IPQC_INSPECTION_HEAD_API', 'S6 IPQC头入站(API)', 'API',
       'qms_gcjyd', '/api/ipqc', 'mdp_stg_ipqc_pull',
       'INCREMENTAL', 'updateTime', 1000, 'data.list', 'bizKey', 'djbh',
       1, 'Phase2 API', NOW(), NOW()
WHERE NOT EXISTS (SELECT 1 FROM mdp_entity WHERE entity_code='S6_IPQC_INSPECTION_HEAD_API');

INSERT INTO mdp_entity
  (tenant_id, source_id, entity_code, entity_name, entity_type,
   source_table_name, source_api_path, target_table_name,
   sync_mode, incr_column, batch_size, response_data_path, dedup_key_path, biz_key_expr,
   status, remark, create_time, update_time)
SELECT 0, (SELECT id FROM mdp_source WHERE source_code='WMS_API'),
       'S7_NBR_MASTER_API', 'S7成品入库主入站(API)', 'API',
       'NbrMaster', '/api/production-receipt', 'mdp_stg_production_receipt',
       'INCREMENTAL', 'updateTime', 1000, 'data.list', 'bizKey', 'Domain,Nbr',
       1, 'Phase2 API', NOW(), NOW()
WHERE NOT EXISTS (SELECT 1 FROM mdp_entity WHERE entity_code='S7_NBR_MASTER_API');

INSERT INTO mdp_entity
  (tenant_id, source_id, entity_code, entity_name, entity_type,
   source_table_name, source_api_path, target_table_name,
   sync_mode, incr_column, batch_size, response_data_path, dedup_key_path, biz_key_expr,
   status, remark, create_time, update_time)
SELECT 0, (SELECT id FROM mdp_source WHERE source_code='WMS_API'),
       'S7_NBR_DETAIL_API', 'S7成品入库明细入站(API)', 'API',
       'NbrDetail', '/api/production-receipt-detail', 'mdp_stg_production_receipt',
       'INCREMENTAL', 'updateTime', 1000, 'data.list', 'bizKey', 'Domain,Nbr,Line',
       1, 'Phase2 API', NOW(), NOW()
WHERE NOT EXISTS (SELECT 1 FROM mdp_entity WHERE entity_code='S7_NBR_DETAIL_API');
