-- 1.0.268：S5/S6/S7 列表双源（dopdemorq SQL Server）源/实体登记（默认 disabled）
--
-- 用途：注册第二源 DOPDEMORQ_SQLSERVER + S5 采购收货 / S5 生产收货 / S6 IPQC Head 的 _SQLSERVER 入站实体。
-- 说明：全部 source/entity status=0 就位不启用，合入后不改变现有 MYSQL/API 运行行为；经 P0/Phase1/Phase2 验证。
--
-- 硬约束落地：
--   1) 全部 source / entity status=0（就位不启用）：合入后不改变现有 MYSQL/API 运行行为。
--   2) SQLSERVER 实体一律 sync_mode='FULL'：Phase 1 只走 PullAll + fullRefresh=true bootstrap，
--      规避 MdpDbPullExecutor 严格 `UpdateTime > @cursor` 的跨批同值静默丢行风险（P0 已确认，不改核心）。
--   3) 密码不入 git：db_password_enc 留 NULL；正式启用前由运维用 CryptogramUtil.Encrypt 密文经带外 UPDATE 注入；
--      dopsa 为 db_owner（非只读），正式启用前须替换为 SELECT-only 账号（Phase 2 门禁）。
--   4) source_table_name 与本库版一致（dopdemorq 表名字段名与本库一致；tenant_id 由 DOP 入站上下文赋值，源无此列）。
-- 幂等：WHERE NOT EXISTS 守卫，可重复执行。

-- ========== (1) 第二源：dopdemorq SQL Server（disabled）==========
INSERT INTO mdp_source
  (tenant_id, source_code, source_name, source_type, status,
   db_type, db_host, db_port, db_name, db_user, db_password_enc, remark, create_time, update_time)
SELECT 0, 'DOPDEMORQ_SQLSERVER', 'dopdemorq 源库(SQL Server) S5-S7列表第二源', 'DB', 0,
       'SQLSERVER', '123.60.180.165', 1433, 'dopdemorq', 'dopsa', NULL,
       'Phase1双源：默认disabled；密码带外注入；正式启用前替换SELECT-only账号', NOW(), NOW()
WHERE NOT EXISTS (SELECT 1 FROM mdp_source WHERE source_code='DOPDEMORQ_SQLSERVER');

-- ========== (2) S5 采购收货 SQLSERVER 入站实体（disabled, FULL）==========
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, (SELECT id FROM mdp_source WHERE source_code='DOPDEMORQ_SQLSERVER'),
       'S5_PURCHASE_RECEIPT_MASTER_SQLSERVER', 'S5采购收货主入站(SQLServer)', 'TABLE',
       'PurOrdRctMaster', 'mdp_stg_purchase_receipt',
       'FULL', 5000, 'Domain,Receiver', 0, 'Phase1双源 disabled', NOW(), NOW()
WHERE NOT EXISTS (SELECT 1 FROM mdp_entity WHERE entity_code='S5_PURCHASE_RECEIPT_MASTER_SQLSERVER');

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, (SELECT id FROM mdp_source WHERE source_code='DOPDEMORQ_SQLSERVER'),
       'S5_PURCHASE_RECEIPT_DETAIL_SQLSERVER', 'S5采购收货明细入站(SQLServer)', 'TABLE',
       'PurOrdRctDetail', 'mdp_stg_purchase_receipt',
       'FULL', 5000, 'Domain,Receiver,Line', 0, 'Phase1双源 disabled（FULL 规避增量边界）', NOW(), NOW()
WHERE NOT EXISTS (SELECT 1 FROM mdp_entity WHERE entity_code='S5_PURCHASE_RECEIPT_DETAIL_SQLSERVER');

-- ========== (3) S5 生产收货 SQLSERVER 入站实体（沿用 S7_NBR_* 命名，disabled, FULL）==========
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, (SELECT id FROM mdp_source WHERE source_code='DOPDEMORQ_SQLSERVER'),
       'S7_NBR_MASTER_SQLSERVER', 'S5生产入库主入站(SQLServer)', 'TABLE',
       'NbrMaster', 'mdp_stg_production_receipt',
       'FULL', 5000, 'Domain,Nbr', 0, 'Phase1双源 disabled（FULL 规避增量边界）', NOW(), NOW()
WHERE NOT EXISTS (SELECT 1 FROM mdp_entity WHERE entity_code='S7_NBR_MASTER_SQLSERVER');

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, (SELECT id FROM mdp_source WHERE source_code='DOPDEMORQ_SQLSERVER'),
       'S7_NBR_DETAIL_SQLSERVER', 'S5生产入库明细入站(SQLServer)', 'TABLE',
       'NbrDetail', 'mdp_stg_production_receipt',
       'FULL', 5000, 'Domain,Nbr,Line', 0, 'Phase1双源 disabled', NOW(), NOW()
WHERE NOT EXISTS (SELECT 1 FROM mdp_entity WHERE entity_code='S7_NBR_DETAIL_SQLSERVER');

-- ========== (4) S6 IPQC Head SQLSERVER 入站实体（disabled, FULL；qms_gcjyd 无 UpdateTime）==========
-- 注：仅 Head。IPQC Detail(qms_gcjydzb) 属 bespoke，Phase 2 处理，本文件不登记其实体。
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, (SELECT id FROM mdp_source WHERE source_code='DOPDEMORQ_SQLSERVER'),
       'S6_IPQC_INSPECTION_HEAD_SQLSERVER', 'S6过程检验头入站(SQLServer)', 'TABLE',
       'qms_gcjyd', 'mdp_stg_ipqc_pull',
       'FULL', 1000, 'djbh', 0, 'Phase1双源 disabled（仅Head）', NOW(), NOW()
WHERE NOT EXISTS (SELECT 1 FROM mdp_entity WHERE entity_code='S6_IPQC_INSPECTION_HEAD_SQLSERVER');
