-- 1.0.387 verify: UATTEST_CHL 租户应已补入 3 条客户。
--
-- 约定：AutoVersionUpdate 取每条语句「首行首列」当布尔断言，必须为真。
--   返回 0 行会得到 NULL 判假并中断启动，故断言一律返回标量真值。
-- 环境容错：源/目标租户不存在的库上主脚本为空操作，断言短路为真。

-- 1) 3 条目标客户均已落到 UATTEST_CHL 租户
SELECT IF(
  (SELECT COUNT(*) FROM SysTenant t JOIN SysOrg o ON o.Id = t.OrgId
    WHERE o.Code = 'UATTEST_CHL' AND t.Status = 1) = 0
  OR (SELECT COUNT(*) FROM SysTenant t JOIN SysOrg o ON o.Id = t.OrgId
       WHERE o.Code = 'AIDOP' AND t.Status = 1) = 0,
  1,
  (SELECT COUNT(*) FROM CustMaster 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 c.Cust IN ('10000000', '10000002', '10000004'))
) AS ok_seeded;

-- 2) 未破坏唯一键：目标租户内 (factory_ref_id, Cust) 无重复
SELECT IF(
  (SELECT COUNT(*) FROM (
     SELECT c.factory_ref_id, c.Cust
       FROM CustMaster 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)
      GROUP BY c.factory_ref_id, c.Cust
     HAVING COUNT(*) > 1
   ) dup) = 0,
  1, 0
) AS ok_no_duplicate;
