-- 1.0.261: 补齐关键对象 *_API（B2/B3/B4 门禁收口）+ 幂等登记
-- 依赖：WMS_API 源、mdp_entity.biz_key_expr（1.0.259+）

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, 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'),
       v.code, v.name, 'API', v.src_tbl, v.path, v.stg,
       'INCREMENTAL', 1000, 'data.list', 'bizKey', v.biz,
       1, '1.0.261 API pair', NOW(), NOW()
FROM (
  SELECT 'S1_SEORDER_ENTRY_API' code, 'S1销售订单行API' name, 'crm_seorderentry' src_tbl, '/api/sales-order-entry' path, 'mdp_stg_so' stg, 'bill_no,entry_seq' biz
  UNION ALL SELECT 'S1_ASN_SHIPPER_MASTER_API','S1发货主API','ASNBOLShipperMaster','/api/asn-master','mdp_stg_ship_trans','Id'
  UNION ALL SELECT 'S2_SCHEDULE_RESULT_OP_API','S2排程结果API','ScheduleResultOpMaster','/api/schedule','mdp_stg_schedule','Domain,WorkOrd,Op,WorkDate'
  UNION ALL SELECT 'S3_PURCHASE_REQUEST_API','S3采购申请API','srm_pr_main','/api/pr','mdp_stg_supply_demand','pr_billno'
  UNION ALL SELECT 'S4_RETURN_EXEC_API','S4退货API','srm_polist_ds','/api/s4-return','mdp_stg_s4_return','dsnum'
  UNION ALL SELECT 'S4_SHORTAGE_EXEC_API','S4缺料API','dwd_material_shortage','/api/s4-shortage','mdp_stg_s4_shortage','work_order,component_item_code'
) v
WHERE NOT EXISTS (SELECT 1 FROM mdp_entity e WHERE e.entity_code=v.code);
