-- ===========================================================================
-- STEP 2B VERIFY —— 全部为 SELECT 真值标量断言（AutoVersionUpdate 的 IsTruthy 契约）
-- 禁用 SET / 用户变量：租户与组织一律内联子查询
-- 文件以最后一条 SQL 的 ; 结束
-- ===========================================================================

-- ---------- §1 行数（21 张表，合计 110） ----------
SELECT COUNT(*) = 5 FROM PersonSkill
  WHERE tenant_id = (SELECT t.Id FROM SysTenant t JOIN SysOrg o ON o.Id=t.OrgId WHERE o.Code='UATTEST_CHL' AND t.Status=1 LIMIT 1);
SELECT COUNT(*) = 10 FROM EmpSkills
  WHERE tenant_id = (SELECT t.Id FROM SysTenant t JOIN SysOrg o ON o.Id=t.OrgId WHERE o.Code='UATTEST_CHL' AND t.Status=1 LIMIT 1);
SELECT COUNT(*) = 4 FROM ImageType
  WHERE tenant_id = (SELECT t.Id FROM SysTenant t JOIN SysOrg o ON o.Id=t.OrgId WHERE o.Code='UATTEST_CHL' AND t.Status=1 LIMIT 1);
SELECT COUNT(*) = 3 FROM QualitySegmentImage
  WHERE tenant_id = (SELECT t.Id FROM SysTenant t JOIN SysOrg o ON o.Id=t.OrgId WHERE o.Code='UATTEST_CHL' AND t.Status=1 LIMIT 1);
SELECT COUNT(*) = 5 FROM PreprocessElementParam
  WHERE tenant_id = (SELECT t.Id FROM SysTenant t JOIN SysOrg o ON o.Id=t.OrgId WHERE o.Code='UATTEST_CHL' AND t.Status=1 LIMIT 1);
SELECT COUNT(*) = 4 FROM PreprocessElement
  WHERE tenant_id = (SELECT t.Id FROM SysTenant t JOIN SysOrg o ON o.Id=t.OrgId WHERE o.Code='UATTEST_CHL' AND t.Status=1 LIMIT 1);
SELECT COUNT(*) = 5 FROM DevMonitor
  WHERE tenant_id = (SELECT t.Id FROM SysTenant t JOIN SysOrg o ON o.Id=t.OrgId WHERE o.Code='UATTEST_CHL' AND t.Status=1 LIMIT 1);
SELECT COUNT(*) = 6 FROM ProdLineDetail
  WHERE tenant_id = (SELECT t.Id FROM SysTenant t JOIN SysOrg o ON o.Id=t.OrgId WHERE o.Code='UATTEST_CHL' AND t.Status=1 LIMIT 1);
SELECT COUNT(*) = 5 FROM ItemOpCondition
  WHERE tenant_id = (SELECT t.Id FROM SysTenant t JOIN SysOrg o ON o.Id=t.OrgId WHERE o.Code='UATTEST_CHL' AND t.Status=1 LIMIT 1);
SELECT COUNT(*) = 10 FROM ItemPackMaster
  WHERE tenant_id = (SELECT t.Id FROM SysTenant t JOIN SysOrg o ON o.Id=t.OrgId WHERE o.Code='UATTEST_CHL' AND t.Status=1 LIMIT 1);
SELECT COUNT(*) = 6 FROM ItemSubstituteDetail
  WHERE tenant_id = (SELECT t.Id FROM SysTenant t JOIN SysOrg o ON o.Id=t.OrgId WHERE o.Code='UATTEST_CHL' AND t.Status=1 LIMIT 1);
SELECT COUNT(*) = 5 FROM ic_substitute
  WHERE tenant_id = (SELECT t.Id FROM SysTenant t JOIN SysOrg o ON o.Id=t.OrgId WHERE o.Code='UATTEST_CHL' AND t.Status=1 LIMIT 1);
SELECT COUNT(*) = 4 FROM S0ProcessFlowCard
  WHERE tenant_id = (SELECT t.Id FROM SysTenant t JOIN SysOrg o ON o.Id=t.OrgId WHERE o.Code='UATTEST_CHL' AND t.Status=1 LIMIT 1);
SELECT COUNT(*) = 4 FROM wms_rwzp
  WHERE tenant_id = (SELECT t.Id FROM SysTenant t JOIN SysOrg o ON o.Id=t.OrgId WHERE o.Code='UATTEST_CHL' AND t.Status=1 LIMIT 1);
SELECT COUNT(*) = 4 FROM PriorityCode
  WHERE tenant_id = (SELECT t.Id FROM SysTenant t JOIN SysOrg o ON o.Id=t.OrgId WHERE o.Code='UATTEST_CHL' AND t.Status=1 LIMIT 1);
SELECT COUNT(*) = 6 FROM BarCodeType
  WHERE tenant_id = (SELECT t.Id FROM SysTenant t JOIN SysOrg o ON o.Id=t.OrgId WHERE o.Code='UATTEST_CHL' AND t.Status=1 LIMIT 1);
SELECT COUNT(*) = 2 FROM BarCodeNbr
  WHERE tenant_id = (SELECT t.Id FROM SysTenant t JOIN SysOrg o ON o.Id=t.OrgId WHERE o.Code='UATTEST_CHL' AND t.Status=1 LIMIT 1);
SELECT COUNT(*) = 1 FROM WorkOrdControl
  WHERE tenant_id = (SELECT t.Id FROM SysTenant t JOIN SysOrg o ON o.Id=t.OrgId WHERE o.Code='UATTEST_CHL' AND t.Status=1 LIMIT 1);
SELECT COUNT(*) = 1 FROM ado_kitting_exclude_rule
  WHERE tenant_id = (SELECT t.Id FROM SysTenant t JOIN SysOrg o ON o.Id=t.OrgId WHERE o.Code='UATTEST_CHL' AND t.Status=1 LIMIT 1);
SELECT COUNT(*) = 5 FROM S0ContractReviewCycle
  WHERE tenant_id = (SELECT t.Id FROM SysTenant t JOIN SysOrg o ON o.Id=t.OrgId WHERE o.Code='UATTEST_CHL' AND t.Status=1 LIMIT 1);
SELECT COUNT(*) = 15 FROM S0ContractReviewCycleBreakdown
  WHERE tenant_id = (SELECT t.Id FROM SysTenant t JOIN SysOrg o ON o.Id=t.OrgId WHERE o.Code='UATTEST_CHL' AND t.Status=1 LIMIT 1);

-- ---------- §2 FK 闭合（dangling 必须为 0） ----------
SELECT COUNT(*) = 0 FROM EmpSkills s
  WHERE s.tenant_id = (SELECT t.Id FROM SysTenant t JOIN SysOrg o ON o.Id=t.OrgId WHERE o.Code='UATTEST_CHL' AND t.Status=1 LIMIT 1)
    AND NOT EXISTS (SELECT 1 FROM EmployeeMaster e WHERE e.tenant_id = s.tenant_id AND e.Employee = s.Employee);
SELECT COUNT(*) = 0 FROM EmpSkills s
  WHERE s.tenant_id = (SELECT t.Id FROM SysTenant t JOIN SysOrg o ON o.Id=t.OrgId WHERE o.Code='UATTEST_CHL' AND t.Status=1 LIMIT 1)
    AND NOT EXISTS (SELECT 1 FROM PersonSkill p WHERE p.tenant_id = s.tenant_id AND p.Code = s.SkillNo);
SELECT COUNT(*) = 0 FROM ProdLineDetail d
  WHERE d.tenant_id = (SELECT t.Id FROM SysTenant t JOIN SysOrg o ON o.Id=t.OrgId WHERE o.Code='UATTEST_CHL' AND t.Status=1 LIMIT 1)
    AND NOT EXISTS (SELECT 1 FROM ItemMaster i WHERE i.tenant_id = d.tenant_id AND i.ItemNum = d.Part);
SELECT COUNT(*) = 0 FROM ProdLineDetail d
  WHERE d.tenant_id = (SELECT t.Id FROM SysTenant t JOIN SysOrg o ON o.Id=t.OrgId WHERE o.Code='UATTEST_CHL' AND t.Status=1 LIMIT 1)
    AND NOT EXISTS (SELECT 1 FROM LineMaster l WHERE l.tenant_id = d.tenant_id AND l.Line = d.Line);
SELECT COUNT(*) = 0 FROM ItemOpCondition c
  WHERE c.tenant_id = (SELECT t.Id FROM SysTenant t JOIN SysOrg o ON o.Id=t.OrgId WHERE o.Code='UATTEST_CHL' AND t.Status=1 LIMIT 1)
    AND NOT EXISTS (SELECT 1 FROM ItemMaster i WHERE i.tenant_id = c.tenant_id AND i.ItemNum = c.ItemNum);
SELECT COUNT(*) = 0 FROM PreprocessElement p
  WHERE p.tenant_id = (SELECT t.Id FROM SysTenant t JOIN SysOrg o ON o.Id=t.OrgId WHERE o.Code='UATTEST_CHL' AND t.Status=1 LIMIT 1)
    AND NOT EXISTS (SELECT 1 FROM ItemMaster i WHERE i.tenant_id = p.tenant_id AND i.ItemNum = p.item_num);
SELECT COUNT(*) = 0 FROM ItemPackMaster p
  WHERE p.tenant_id = (SELECT t.Id FROM SysTenant t JOIN SysOrg o ON o.Id=t.OrgId WHERE o.Code='UATTEST_CHL' AND t.Status=1 LIMIT 1)
    AND NOT EXISTS (SELECT 1 FROM ItemMaster i WHERE i.tenant_id = p.tenant_id AND i.ItemNum = p.ItemNum);
SELECT COUNT(*) = 0 FROM ItemSubstituteDetail s
  WHERE s.tenant_id = (SELECT t.Id FROM SysTenant t JOIN SysOrg o ON o.Id=t.OrgId WHERE o.Code='UATTEST_CHL' AND t.Status=1 LIMIT 1)
    AND (NOT EXISTS (SELECT 1 FROM ItemMaster i WHERE i.tenant_id = s.tenant_id AND i.ItemNum = s.ParentItem)
      OR NOT EXISTS (SELECT 1 FROM ItemMaster i WHERE i.tenant_id = s.tenant_id AND i.ItemNum = s.ItemNum)
      OR NOT EXISTS (SELECT 1 FROM ItemMaster i WHERE i.tenant_id = s.tenant_id AND i.ItemNum = s.SubstituteItem));
SELECT COUNT(*) = 0 FROM ic_substitute c
  WHERE c.tenant_id = (SELECT t.Id FROM SysTenant t JOIN SysOrg o ON o.Id=t.OrgId WHERE o.Code='UATTEST_CHL' AND t.Status=1 LIMIT 1)
    AND NOT EXISTS (SELECT 1 FROM ItemMaster i WHERE i.tenant_id = c.tenant_id AND i.ItemNum = c.substitute_code);
SELECT COUNT(*) = 0 FROM S0ProcessFlowCard f
  WHERE f.tenant_id = (SELECT t.Id FROM SysTenant t JOIN SysOrg o ON o.Id=t.OrgId WHERE o.Code='UATTEST_CHL' AND t.Status=1 LIMIT 1)
    AND NOT EXISTS (SELECT 1 FROM ItemMaster i WHERE i.tenant_id = f.tenant_id AND i.ItemNum = f.item_num);
SELECT COUNT(*) = 0 FROM wms_rwzp w
  WHERE w.tenant_id = (SELECT t.Id FROM SysTenant t JOIN SysOrg o ON o.Id=t.OrgId WHERE o.Code='UATTEST_CHL' AND t.Status=1 LIMIT 1)
    AND (NOT EXISTS (SELECT 1 FROM ItemMaster i WHERE i.tenant_id = w.tenant_id AND i.ItemNum = w.wlbm)
      OR NOT EXISTS (SELECT 1 FROM LocationMaster l WHERE l.tenant_id = w.tenant_id AND l.location = w.yskw)
      OR NOT EXISTS (SELECT 1 FROM LocationMaster l WHERE l.tenant_id = w.tenant_id AND l.location = w.mdkw));
SELECT COUNT(*) = 0 FROM BarCodeNbr b
  WHERE b.tenant_id = (SELECT t.Id FROM SysTenant t JOIN SysOrg o ON o.Id=t.OrgId WHERE o.Code='UATTEST_CHL' AND t.Status=1 LIMIT 1)
    AND (NOT EXISTS (SELECT 1 FROM CustMaster c WHERE c.tenant_id = b.tenant_id AND c.Cust = b.Customer)
      OR NOT EXISTS (SELECT 1 FROM BarCodeType t2 WHERE t2.tenant_id = b.tenant_id AND t2.bar_type = b.Type AND t2.domain_code = b.Domain));
SELECT COUNT(*) = 0 FROM QualitySegmentImage q
  WHERE q.tenant_id = (SELECT t.Id FROM SysTenant t JOIN SysOrg o ON o.Id=t.OrgId WHERE o.Code='UATTEST_CHL' AND t.Status=1 LIMIT 1)
    AND (NOT EXISTS (SELECT 1 FROM ImageType it WHERE it.tenant_id = q.tenant_id AND it.image_type_id = q.image_type_id)
      OR NOT EXISTS (SELECT 1 FROM ItemMaster i WHERE i.tenant_id = q.tenant_id AND i.ItemNum = q.item_num));
SELECT COUNT(*) = 0 FROM S0ContractReviewCycleBreakdown b
  WHERE b.tenant_id = (SELECT t.Id FROM SysTenant t JOIN SysOrg o ON o.Id=t.OrgId WHERE o.Code='UATTEST_CHL' AND t.Status=1 LIMIT 1)
    AND NOT EXISTS (SELECT 1 FROM S0ContractReviewCycle m
                    WHERE m.tenant_id = b.tenant_id AND m.factory_ref_id = b.factory_ref_id
                      AND m.stage_code = b.parent_stage_code);

-- ---------- §3 合同评审主从工时一致性（分解求和 = 主表 std_hours） ----------
SELECT COUNT(*) = 0 FROM (
  SELECT m.stage_code
  FROM S0ContractReviewCycle m
  JOIN S0ContractReviewCycleBreakdown b
    ON b.tenant_id = m.tenant_id AND b.factory_ref_id = m.factory_ref_id AND b.parent_stage_code = m.stage_code
  WHERE m.tenant_id = (SELECT t.Id FROM SysTenant t JOIN SysOrg o ON o.Id=t.OrgId WHERE o.Code='UATTEST_CHL' AND t.Status=1 LIMIT 1)
  GROUP BY m.stage_code, m.std_hours
  HAVING SUM(b.std_hours) <> m.std_hours
) x;

-- ---------- §4 业务键唯一性（目标租户内） ----------
SELECT COUNT(*) = 0 FROM (SELECT Code FROM PersonSkill WHERE tenant_id=(SELECT t.Id FROM SysTenant t JOIN SysOrg o ON o.Id=t.OrgId WHERE o.Code='UATTEST_CHL' AND t.Status=1 LIMIT 1) GROUP BY Code HAVING COUNT(*)>1) x;
SELECT COUNT(*) = 0 FROM (SELECT Employee, SkillNo FROM EmpSkills WHERE tenant_id=(SELECT t.Id FROM SysTenant t JOIN SysOrg o ON o.Id=t.OrgId WHERE o.Code='UATTEST_CHL' AND t.Status=1 LIMIT 1) GROUP BY Employee, SkillNo HAVING COUNT(*)>1) x;
SELECT COUNT(*) = 0 FROM (SELECT image_type_id FROM ImageType WHERE tenant_id=(SELECT t.Id FROM SysTenant t JOIN SysOrg o ON o.Id=t.OrgId WHERE o.Code='UATTEST_CHL' AND t.Status=1 LIMIT 1) GROUP BY image_type_id HAVING COUNT(*)>1) x;
SELECT COUNT(*) = 0 FROM (SELECT item_num, op, image_type_id FROM QualitySegmentImage WHERE tenant_id=(SELECT t.Id FROM SysTenant t JOIN SysOrg o ON o.Id=t.OrgId WHERE o.Code='UATTEST_CHL' AND t.Status=1 LIMIT 1) GROUP BY item_num, op, image_type_id HAVING COUNT(*)>1) x;
SELECT COUNT(*) = 0 FROM (SELECT device_number, message_code FROM PreprocessElementParam WHERE tenant_id=(SELECT t.Id FROM SysTenant t JOIN SysOrg o ON o.Id=t.OrgId WHERE o.Code='UATTEST_CHL' AND t.Status=1 LIMIT 1) GROUP BY device_number, message_code HAVING COUNT(*)>1) x;
SELECT COUNT(*) = 0 FROM (SELECT item_num, is_main FROM PreprocessElement WHERE tenant_id=(SELECT t.Id FROM SysTenant t JOIN SysOrg o ON o.Id=t.OrgId WHERE o.Code='UATTEST_CHL' AND t.Status=1 LIMIT 1) GROUP BY item_num, is_main HAVING COUNT(*)>1) x;
SELECT COUNT(*) = 0 FROM (SELECT DeviceNumber, MessageCode, CodeType FROM DevMonitor WHERE tenant_id=(SELECT t.Id FROM SysTenant t JOIN SysOrg o ON o.Id=t.OrgId WHERE o.Code='UATTEST_CHL' AND t.Status=1 LIMIT 1) GROUP BY DeviceNumber, MessageCode, CodeType HAVING COUNT(*)>1) x;
SELECT COUNT(*) = 0 FROM (SELECT Part, Op, Line FROM ProdLineDetail WHERE tenant_id=(SELECT t.Id FROM SysTenant t JOIN SysOrg o ON o.Id=t.OrgId WHERE o.Code='UATTEST_CHL' AND t.Status=1 LIMIT 1) GROUP BY Part, Op, Line HAVING COUNT(*)>1) x;
SELECT COUNT(*) = 0 FROM (SELECT ItemNum, IsMain, CodeType FROM ItemOpCondition WHERE tenant_id=(SELECT t.Id FROM SysTenant t JOIN SysOrg o ON o.Id=t.OrgId WHERE o.Code='UATTEST_CHL' AND t.Status=1 LIMIT 1) GROUP BY ItemNum, IsMain, CodeType HAVING COUNT(*)>1) x;
SELECT COUNT(*) = 0 FROM (SELECT ParentItem, ItemNum, SubstituteItem FROM ItemSubstituteDetail WHERE tenant_id=(SELECT t.Id FROM SysTenant t JOIN SysOrg o ON o.Id=t.OrgId WHERE o.Code='UATTEST_CHL' AND t.Status=1 LIMIT 1) GROUP BY ParentItem, ItemNum, SubstituteItem HAVING COUNT(*)>1) x;
SELECT COUNT(*) = 0 FROM (SELECT substitute_code, substitute_strategy, substitute_mode FROM ic_substitute WHERE tenant_id=(SELECT t.Id FROM SysTenant t JOIN SysOrg o ON o.Id=t.OrgId WHERE o.Code='UATTEST_CHL' AND t.Status=1 LIMIT 1) GROUP BY substitute_code, substitute_strategy, substitute_mode HAVING COUNT(*)>1) x;
SELECT COUNT(*) = 0 FROM (SELECT flow_card_no FROM S0ProcessFlowCard WHERE tenant_id=(SELECT t.Id FROM SysTenant t JOIN SysOrg o ON o.Id=t.OrgId WHERE o.Code='UATTEST_CHL' AND t.Status=1 LIMIT 1) GROUP BY flow_card_no HAVING COUNT(*)>1) x;
SELECT COUNT(*) = 0 FROM (SELECT Descr FROM PriorityCode WHERE tenant_id=(SELECT t.Id FROM SysTenant t JOIN SysOrg o ON o.Id=t.OrgId WHERE o.Code='UATTEST_CHL' AND t.Status=1 LIMIT 1) GROUP BY Descr HAVING COUNT(*)>1) x;
SELECT COUNT(*) = 0 FROM (SELECT domain_code, bar_type FROM BarCodeType WHERE tenant_id=(SELECT t.Id FROM SysTenant t JOIN SysOrg o ON o.Id=t.OrgId WHERE o.Code='UATTEST_CHL' AND t.Status=1 LIMIT 1) GROUP BY domain_code, bar_type HAVING COUNT(*)>1) x;
SELECT COUNT(*) = 0 FROM (SELECT Domain, Customer, Type FROM BarCodeNbr WHERE tenant_id=(SELECT t.Id FROM SysTenant t JOIN SysOrg o ON o.Id=t.OrgId WHERE o.Code='UATTEST_CHL' AND t.Status=1 LIMIT 1) GROUP BY Domain, Customer, Type HAVING COUNT(*)>1) x;
SELECT COUNT(*) = 0 FROM (SELECT stage_code FROM S0ContractReviewCycle WHERE tenant_id=(SELECT t.Id FROM SysTenant t JOIN SysOrg o ON o.Id=t.OrgId WHERE o.Code='UATTEST_CHL' AND t.Status=1 LIMIT 1) GROUP BY stage_code HAVING COUNT(*)>1) x;
SELECT COUNT(*) = 0 FROM (SELECT parent_stage_code, group_code FROM S0ContractReviewCycleBreakdown WHERE tenant_id=(SELECT t.Id FROM SysTenant t JOIN SysOrg o ON o.Id=t.OrgId WHERE o.Code='UATTEST_CHL' AND t.Status=1 LIMIT 1) GROUP BY parent_stage_code, group_code HAVING COUNT(*)>1) x;

-- ---------- §5 无 tenant 的 UK：ItemPackMaster(Domain+ItemNum) 必须做【全表】唯一断言 ----------
SELECT COUNT(*) = 0 FROM (SELECT Domain, ItemNum FROM ItemPackMaster GROUP BY Domain, ItemNum HAVING COUNT(*)>1) x;

-- ---------- §6 目标 scope：tenant / org / domain ----------
SELECT COUNT(*) = 10 FROM ItemPackMaster
  WHERE tenant_id = (SELECT t.Id FROM SysTenant t JOIN SysOrg o ON o.Id=t.OrgId WHERE o.Code='UATTEST_CHL' AND t.Status=1 LIMIT 1)
    AND company_ref_id = (SELECT t.OrgId FROM SysTenant t JOIN SysOrg o ON o.Id=t.OrgId WHERE o.Code='UATTEST_CHL' AND t.Status=1 LIMIT 1)
    AND factory_ref_id = (SELECT t.OrgId FROM SysTenant t JOIN SysOrg o ON o.Id=t.OrgId WHERE o.Code='UATTEST_CHL' AND t.Status=1 LIMIT 1);
SELECT COUNT(*) = 5 FROM PersonSkill
  WHERE tenant_id = (SELECT t.Id FROM SysTenant t JOIN SysOrg o ON o.Id=t.OrgId WHERE o.Code='UATTEST_CHL' AND t.Status=1 LIMIT 1)
    AND CompanyRefId = (SELECT t.OrgId FROM SysTenant t JOIN SysOrg o ON o.Id=t.OrgId WHERE o.Code='UATTEST_CHL' AND t.Status=1 LIMIT 1)
    AND FactoryRefId = (SELECT t.OrgId FROM SysTenant t JOIN SysOrg o ON o.Id=t.OrgId WHERE o.Code='UATTEST_CHL' AND t.Status=1 LIMIT 1);
SELECT COUNT(*) = 5 FROM S0ContractReviewCycle
  WHERE tenant_id = (SELECT t.Id FROM SysTenant t JOIN SysOrg o ON o.Id=t.OrgId WHERE o.Code='UATTEST_CHL' AND t.Status=1 LIMIT 1)
    AND factory_ref_id = (SELECT t.OrgId FROM SysTenant t JOIN SysOrg o ON o.Id=t.OrgId WHERE o.Code='UATTEST_CHL' AND t.Status=1 LIMIT 1);
SELECT COUNT(*) = 15 FROM S0ContractReviewCycleBreakdown
  WHERE tenant_id = (SELECT t.Id FROM SysTenant t JOIN SysOrg o ON o.Id=t.OrgId WHERE o.Code='UATTEST_CHL' AND t.Status=1 LIMIT 1)
    AND factory_ref_id = (SELECT t.OrgId FROM SysTenant t JOIN SysOrg o ON o.Id=t.OrgId WHERE o.Code='UATTEST_CHL' AND t.Status=1 LIMIT 1);
SELECT COUNT(*) = 4 FROM ImageType
  WHERE tenant_id = (SELECT t.Id FROM SysTenant t JOIN SysOrg o ON o.Id=t.OrgId WHERE o.Code='UATTEST_CHL' AND t.Status=1 LIMIT 1)
    AND domain = '8010';

-- ---------- §7 DEMO/AI 污染隔离（仅检查目标租户） ----------
SELECT COUNT(*) = 0 FROM ImageType
  WHERE tenant_id = (SELECT t.Id FROM SysTenant t JOIN SysOrg o ON o.Id=t.OrgId WHERE o.Code='UATTEST_CHL' AND t.Status=1 LIMIT 1)
    AND (image_type_id LIKE 'DEMO-%' OR image_type_id LIKE 'AI-%' OR descr LIKE '%[DEMO]%' OR descr LIKE '%AI测试%');
SELECT COUNT(*) = 4 FROM ImageType
  WHERE tenant_id = (SELECT t.Id FROM SysTenant t JOIN SysOrg o ON o.Id=t.OrgId WHERE o.Code='UATTEST_CHL' AND t.Status=1 LIMIT 1)
    AND image_type_id LIKE 'UATD-SOP-%';
SELECT COUNT(*) = 0 FROM QualitySegmentImage
  WHERE tenant_id = (SELECT t.Id FROM SysTenant t JOIN SysOrg o ON o.Id=t.OrgId WHERE o.Code='UATTEST_CHL' AND t.Status=1 LIMIT 1)
    AND (image_type_id LIKE 'DEMO-%' OR descr LIKE '%[DEMO]%');
SELECT COUNT(*) = 0 FROM PersonSkill
  WHERE tenant_id = (SELECT t.Id FROM SysTenant t JOIN SysOrg o ON o.Id=t.OrgId WHERE o.Code='UATTEST_CHL' AND t.Status=1 LIMIT 1)
    AND (Name LIKE '%[DEMO]%' OR Name LIKE '%AI测试%' OR Code LIKE 'AI-%');
SELECT COUNT(*) = 0 FROM DevMonitor
  WHERE tenant_id = (SELECT t.Id FROM SysTenant t JOIN SysOrg o ON o.Id=t.OrgId WHERE o.Code='UATTEST_CHL' AND t.Status=1 LIMIT 1)
    AND (DeviceDescr LIKE '%[DEMO]%' OR CodeDescr LIKE '%[DEMO]%'
      OR DeviceNumber LIKE 'DEMO-%' OR MessageCode LIKE 'DEMO-%');
SELECT COUNT(*) = 5 FROM DevMonitor
  WHERE tenant_id = (SELECT t.Id FROM SysTenant t JOIN SysOrg o ON o.Id=t.OrgId WHERE o.Code='UATTEST_CHL' AND t.Status=1 LIMIT 1)
    AND DeviceNumber LIKE 'UATD-%' AND MessageCode LIKE 'UATD-%';
SELECT COUNT(*) = 0 FROM PreprocessElementParam
  WHERE tenant_id = (SELECT t.Id FROM SysTenant t JOIN SysOrg o ON o.Id=t.OrgId WHERE o.Code='UATTEST_CHL' AND t.Status=1 LIMIT 1)
    AND (device_descr LIKE '%[DEMO]%' OR code_descr LIKE '%[DEMO]%'
      OR device_number LIKE 'DEMO-%' OR message_code LIKE 'DEMO-%');
SELECT COUNT(*) = 5 FROM PreprocessElementParam
  WHERE tenant_id = (SELECT t.Id FROM SysTenant t JOIN SysOrg o ON o.Id=t.OrgId WHERE o.Code='UATTEST_CHL' AND t.Status=1 LIMIT 1)
    AND device_number LIKE 'UATD-%' AND message_code LIKE 'UATD-%';
SELECT COUNT(*) = 0 FROM BarCodeNbr
  WHERE tenant_id = (SELECT t.Id FROM SysTenant t JOIN SysOrg o ON o.Id=t.OrgId WHERE o.Code='UATTEST_CHL' AND t.Status=1 LIMIT 1)
    AND (FirmString1Note LIKE '%[DEMO]%' OR FirmString2Note LIKE '%[DEMO]%');
SELECT COUNT(*) = 0 FROM ItemOpCondition
  WHERE tenant_id = (SELECT t.Id FROM SysTenant t JOIN SysOrg o ON o.Id=t.OrgId WHERE o.Code='UATTEST_CHL' AND t.Status=1 LIMIT 1)
    AND (Descr LIKE '%S0DEMO_BATCH1%' OR Descr LIKE '%[DEMO]%');
SELECT COUNT(*) = 0 FROM PreprocessElement
  WHERE tenant_id = (SELECT t.Id FROM SysTenant t JOIN SysOrg o ON o.Id=t.OrgId WHERE o.Code='UATTEST_CHL' AND t.Status=1 LIMIT 1)
    AND (descr LIKE '%[DEMO]%' OR item_code LIKE 'DEMO-%');
SELECT COUNT(*) = 0 FROM wms_rwzp
  WHERE tenant_id = (SELECT t.Id FROM SysTenant t JOIN SysOrg o ON o.Id=t.OrgId WHERE o.Code='UATTEST_CHL' AND t.Status=1 LIMIT 1)
    AND (rwlx LIKE '%[DEMO]%' OR zt LIKE '%[DEMO]%' OR rqpc LIKE 'DEMO-%');

-- ---------- §8 LineSkill 必须仍为 0（确认 Site 未被发明） ----------
SELECT COUNT(*) = 0 FROM LineSkillMaster
  WHERE tenant_id = (SELECT t.Id FROM SysTenant t JOIN SysOrg o ON o.Id=t.OrgId WHERE o.Code='UATTEST_CHL' AND t.Status=1 LIMIT 1);
SELECT COUNT(*) = 0 FROM LineSkillDetail
  WHERE tenant_id = (SELECT t.Id FROM SysTenant t JOIN SysOrg o ON o.Id=t.OrgId WHERE o.Code='UATTEST_CHL' AND t.Status=1 LIMIT 1);

-- ---------- §9 源租户 / 默认租户未被写入（本批只读它们） ----------
SELECT COUNT(*) = 5 FROM PersonSkill WHERE tenant_id = (SELECT t.Id FROM SysTenant t JOIN SysOrg o ON o.Id=t.OrgId WHERE o.Code='AIDOP' AND t.Status=1 LIMIT 1);
SELECT COUNT(*) = 8 FROM EmpSkills   WHERE tenant_id = (SELECT t.Id FROM SysTenant t JOIN SysOrg o ON o.Id=t.OrgId WHERE o.Code='AIDOP' AND t.Status=1 LIMIT 1);
SELECT COUNT(*) = 4 FROM ImageType   WHERE tenant_id = (SELECT t.Id FROM SysTenant t JOIN SysOrg o ON o.Id=t.OrgId WHERE o.Code='AIDOP' AND t.Status=1 LIMIT 1);
SELECT COUNT(*) = 1 FROM WorkOrdControl WHERE tenant_id = (SELECT t.Id FROM SysTenant t JOIN SysOrg o ON o.Id=t.OrgId WHERE o.Code='AIDOP' AND t.Status=1 LIMIT 1);
SELECT COUNT(*) = 10 FROM S0ContractReviewCycle WHERE tenant_id = (SELECT t.Id FROM SysTenant t JOIN SysOrg o ON o.Id=t.OrgId WHERE o.Code='AIDOP-Demo' AND t.Status=1 LIMIT 1);
SELECT COUNT(*) = 18 FROM S0ContractReviewCycleBreakdown WHERE tenant_id = (SELECT t.Id FROM SysTenant t JOIN SysOrg o ON o.Id=t.OrgId WHERE o.Code='AIDOP-Demo' AND t.Status=1 LIMIT 1);
SELECT COUNT(*) = 5 FROM ImageType WHERE tenant_id = 1300000000001;
SELECT COUNT(*) = 5 FROM PreprocessElementParam WHERE tenant_id = 1300000000001;
SELECT COUNT(*) = 5 FROM DevMonitor WHERE tenant_id = 1300000000001;

-- ---------- §10 既有 S0 主数据回归（不得被本批影响） ----------
SELECT COUNT(*) = 5 FROM CustMaster WHERE tenant_id = (SELECT t.Id FROM SysTenant t JOIN SysOrg o ON o.Id=t.OrgId WHERE o.Code='UATTEST_CHL' AND t.Status=1 LIMIT 1);
SELECT COUNT(*) = 7 FROM DepartmentMaster WHERE tenant_id = (SELECT t.Id FROM SysTenant t JOIN SysOrg o ON o.Id=t.OrgId WHERE o.Code='UATTEST_CHL' AND t.Status=1 LIMIT 1);
SELECT COUNT(*) = 26 FROM EmployeeMaster WHERE tenant_id = (SELECT t.Id FROM SysTenant t JOIN SysOrg o ON o.Id=t.OrgId WHERE o.Code='UATTEST_CHL' AND t.Status=1 LIMIT 1);
SELECT COUNT(*) = 13 FROM SuppMaster WHERE tenant_id = (SELECT t.Id FROM SysTenant t JOIN SysOrg o ON o.Id=t.OrgId WHERE o.Code='UATTEST_CHL' AND t.Status=1 LIMIT 1);
SELECT COUNT(*) = 56 FROM ItemMaster WHERE tenant_id = (SELECT t.Id FROM SysTenant t JOIN SysOrg o ON o.Id=t.OrgId WHERE o.Code='UATTEST_CHL' AND t.Status=1 LIMIT 1);
SELECT COUNT(*) = 18 FROM LocationMaster WHERE tenant_id = (SELECT t.Id FROM SysTenant t JOIN SysOrg o ON o.Id=t.OrgId WHERE o.Code='UATTEST_CHL' AND t.Status=1 LIMIT 1);
SELECT COUNT(*) = 20 FROM LineMaster WHERE tenant_id = (SELECT t.Id FROM SysTenant t JOIN SysOrg o ON o.Id=t.OrgId WHERE o.Code='UATTEST_CHL' AND t.Status=1 LIMIT 1);
SELECT COUNT(*) = 7 FROM WorkCtrMaster WHERE tenant_id = (SELECT t.Id FROM SysTenant t JOIN SysOrg o ON o.Id=t.OrgId WHERE o.Code='UATTEST_CHL' AND t.Status=1 LIMIT 1);
SELECT COUNT(*) = 5 FROM qms_inspection_method WHERE tenant_id = (SELECT t.Id FROM SysTenant t JOIN SysOrg o ON o.Id=t.OrgId WHERE o.Code='UATTEST_CHL' AND t.Status=1 LIMIT 1);
SELECT COUNT(*) = 7 FROM qms_jygf WHERE tenant_id = (SELECT t.Id FROM SysTenant t JOIN SysOrg o ON o.Id=t.OrgId WHERE o.Code='UATTEST_CHL' AND t.Status=1 LIMIT 1);
