PurchaseOrderCompletionMdpSyncService.cs 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668
  1. using Microsoft.Extensions.Hosting;
  2. using Microsoft.Extensions.Logging;
  3. using Microsoft.Extensions.Options;
  4. using SqlSugar;
  5. using System.Data;
  6. using System.Text;
  7. namespace Admin.NET.Plugin.AiDOP.Supply;
  8. /// <summary>
  9. /// S8 Stage-3 采购订单完成态标准事实层投影服务。
  10. ///
  11. /// <para>链路:Source B(WMS/MES 的 <c>PurOrdDetail</c>)→ <c>mdp_std_purchase_order_completion</c>。
  12. /// 目的是让 S8 Stage-3 能在【不覆盖】<c>mdp_std_purchase_order.status</c> 的前提下拿到
  13. /// 「这条采购行收货是否已关闭」这一 WMS 侧事实。</para>
  14. ///
  15. /// <para><b>Grain = 一条采购行</b>(<c>tenant_id + po_no + po_line</c>)。
  16. /// <c>domain</c> / <c>potype</c> / <c>source_row_id</c> / <c>source_id</c> 是 provenance,不进唯一键。</para>
  17. ///
  18. /// <para><b>当前状态,不是事件</b>:上游收货过程的写法是
  19. /// <c>Status = (case when 收满 then 'C' else '' end)</c>,退货会把 <c>'C'</c> 打回空串。
  20. /// 因此本服务的 UPSERT 必须无条件刷新结论,允许 COMPLETED → NOT_COMPLETED 回退。</para>
  21. ///
  22. /// <para><b>Full Reconciliation 是正确性主路径</b>:HotWatch 只覆盖活动中的采购单,
  23. /// 实测 5 个 <c>PUR_ORDER</c> 关注里 3 个已提前终止(且无一是因采购完成而终止),
  24. /// 98 条源行里 88 条根本没有对应关注。所以单靠增量必然漏判,周期性全量对账不可省。</para>
  25. ///
  26. /// <para><b>刻意不做</b>:不读 IQC、不算 ObjectCompleted、不做订单级聚合、
  27. /// 不用数量重新推导完成(唯一 Authority 是源侧 Status)、不写任何源表、不改 Source A 的任何列。</para>
  28. /// </summary>
  29. public class PurchaseOrderCompletionMdpSyncService : ITransient
  30. {
  31. private const string JobCode = "S3_PO_COMPLETION_MDP_SYNC";
  32. private const string JobName = "S3采购订单完成态标准事实层投影";
  33. /// <summary>完成结论取值域。</summary>
  34. public const string StatusCompleted = "COMPLETED";
  35. public const string StatusNotCompleted = "NOT_COMPLETED";
  36. public const string StatusUnknown = "UNKNOWN";
  37. private readonly ISqlSugarClient _db;
  38. private readonly DataPlatform.MdpSourceScopeFactory _scopeFactory;
  39. private readonly AidopPoCompletionOptions _opt;
  40. private readonly IHostEnvironment _env;
  41. private readonly ILogger<PurchaseOrderCompletionMdpSyncService> _logger;
  42. public PurchaseOrderCompletionMdpSyncService(
  43. ISqlSugarClient db,
  44. DataPlatform.MdpSourceScopeFactory scopeFactory,
  45. IOptions<AidopPoCompletionOptions> opt,
  46. IHostEnvironment env,
  47. ILogger<PurchaseOrderCompletionMdpSyncService> logger)
  48. {
  49. _db = db;
  50. _scopeFactory = scopeFactory;
  51. _opt = opt.Value;
  52. _env = env;
  53. _logger = logger;
  54. }
  55. /// <summary>
  56. /// 完成态映射。<b>这是全链路唯一的状态映射入口</b>——HotWatch 增量若接入,必须复用本方法,
  57. /// 不得另写一套。
  58. ///
  59. /// <para><c>'C'</c> → COMPLETED;空串 → NOT_COMPLETED;
  60. /// <c>null</c> 与任何其它非空值 → UNKNOWN。</para>
  61. ///
  62. /// <para>注意这里刻意<b>不</b>写成 <c>IFNULL(status,'') == ""</c>:
  63. /// NULL 表示「源侧没给值」,未知值表示「源侧给了我们不认识的值」,
  64. /// 两者都不等于「已确认尚未完成」,滑成 NOT_COMPLETED 会让下游把未知当成确定结论。</para>
  65. /// </summary>
  66. public static string MapCompletionStatus(string? rawStatus)
  67. {
  68. if (rawStatus is null) return StatusUnknown;
  69. var s = rawStatus.Trim();
  70. if (s.Length == 0) return StatusNotCompleted;
  71. return string.Equals(s, "C", StringComparison.OrdinalIgnoreCase)
  72. ? StatusCompleted
  73. : StatusUnknown;
  74. }
  75. /// <summary>写入本表时使用的 raw_status 归一:保留 NULL,其余 TRIM。</summary>
  76. public static string? NormalizeRawStatus(string? rawStatus) => rawStatus?.Trim();
  77. /// <summary>
  78. /// 全量对账(Full Reconciliation)。
  79. /// <paramref name="tenantId"/> 为 0 表示全租户;&gt;0 时只写/只淘汰该租户,绝不触碰其它租户。
  80. /// </summary>
  81. public async Task<PurchaseOrderCompletionSyncResult> RunFullAsync(
  82. long tenantId = 0,
  83. string triggerType = "AUTO",
  84. CancellationToken cancellationToken = default)
  85. {
  86. cancellationToken.ThrowIfCancellationRequested();
  87. var now = DateTime.Now;
  88. var batchId = $"S3_PO_COMPL_{(tenantId > 0 ? tenantId + "_" : "")}{now:yyyyMMddHHmmss}";
  89. var runLogId = await InsertRunLogAsync(batchId, now, triggerType, tenantId);
  90. var result = new PurchaseOrderCompletionSyncResult { BatchId = batchId, RunLogId = runLogId };
  91. try
  92. {
  93. // ① 源绑定:非开发环境必须显式绑定真实 WMS 源,绝不静默回落
  94. var binding = await ResolveSourceBindingAsync(cancellationToken);
  95. result.SourceCode = binding.SourceCode;
  96. var remote = await _scopeFactory.GetScopeAsync(binding.SourceCode, cancellationToken);
  97. // 作用域工厂对「本库样板源」与「未配账号的源」会直接返回主库连接。
  98. // 那会让本服务把 Source A 的本地表当成 WMS 执行态读进来,结论会整体错误,
  99. // 所以这里必须结构性挡住,而不是依赖配置写对。
  100. if (ReferenceEquals(remote, _db))
  101. throw new InvalidOperationException(
  102. $"源 {binding.SourceCode} 解析结果是主库连接,说明它是本库样板源或未配置独立账号,"
  103. + "不能承担 S8_PO_COMPLETION_SOURCE 角色");
  104. // ② 读源:只取本角色范围内的采购行
  105. var sourceRows = await ReadSourceRowsAsync(remote, cancellationToken);
  106. result.SourceRows = sourceRows.Count;
  107. // ③ 归属与校验
  108. var tenantIndex = await LoadLocalTenantIndexAsync(cancellationToken);
  109. var poLineIndex = await LoadLocalPoLineIndexAsync(cancellationToken);
  110. var pending = new List<CompletionRow>(sourceRows.Count);
  111. foreach (var row in sourceRows)
  112. {
  113. cancellationToken.ThrowIfCancellationRequested();
  114. if (string.IsNullOrWhiteSpace(row.PoNo) || string.IsNullOrWhiteSpace(row.PoLine))
  115. {
  116. result.SkippedRows++;
  117. continue;
  118. }
  119. var resolved = ResolveTenant(tenantIndex, row.PoNo, row.Domain);
  120. if (resolved <= 0)
  121. {
  122. result.SkippedRows++;
  123. result.TenantUnresolvedRows++;
  124. _logger.LogWarning(
  125. "[PoCompletion] 跳过:采购单 {PurOrd} 行 {Line} 无法唯一解析租户(候选数 {Count})",
  126. row.PoNo, row.PoLine, CountTenantCandidates(tenantIndex, row.PoNo, row.Domain));
  127. continue;
  128. }
  129. // 单租户刷新:其它租户的源行本轮不参与,也因此不能被本轮淘汰
  130. if (tenantId > 0 && resolved != tenantId)
  131. continue;
  132. var matches = poLineIndex.TryGetValue(LineKey(resolved, row.PoNo, row.PoLine), out var c) ? c : 0;
  133. if (matches > 1)
  134. throw new InvalidOperationException(
  135. $"数据契约破坏:租户 {resolved} 的采购行 {row.PoNo}#{row.PoLine} 在本库命中 {matches} 条,"
  136. + "唯一键 uk_po_line 应当保证唯一");
  137. if (matches == 0)
  138. {
  139. result.SkippedRows++;
  140. result.UnmatchedRows++;
  141. _logger.LogWarning(
  142. "[PoCompletion] 跳过:源行 {PurOrd}#{Line} 在本库租户 {Tenant} 下无对应采购行",
  143. row.PoNo, row.PoLine, resolved);
  144. continue;
  145. }
  146. result.MatchedRows++;
  147. var raw = NormalizeRawStatus(row.RawStatus);
  148. var completion = MapCompletionStatus(row.RawStatus);
  149. if (completion == StatusCompleted) result.CompletedRows++;
  150. else if (completion == StatusNotCompleted) result.NotCompletedRows++;
  151. else result.UnknownStatusRows++;
  152. pending.Add(new CompletionRow
  153. {
  154. TenantId = resolved,
  155. PoNo = row.PoNo.Trim(),
  156. PoLine = row.PoLine.Trim(),
  157. Domain = string.IsNullOrWhiteSpace(row.Domain) ? null : row.Domain.Trim(),
  158. Potype = string.IsNullOrWhiteSpace(row.Potype) ? null : row.Potype.Trim(),
  159. RawStatus = raw,
  160. CompletionStatus = completion,
  161. SourceSystem = binding.SourceCode,
  162. SourceId = binding.SourceId,
  163. SourceRowId = string.IsNullOrWhiteSpace(row.SourceRowId) ? null : row.SourceRowId.Trim(),
  164. SourceUpdateTime = row.SourceUpdateTime,
  165. SourceUpdateUser = string.IsNullOrWhiteSpace(row.SourceUpdateUser) ? null : row.SourceUpdateUser.Trim(),
  166. });
  167. }
  168. // ④ UPSERT:无条件刷新结论,支持 COMPLETED → NOT_COMPLETED 回退
  169. result.WrittenRows = await UpsertAsync(pending, batchId, now, cancellationToken);
  170. // ⑤ 淘汰:源侧已不存在的行回到 NOT_OBSERVED(本表的表达方式就是「没有这一行」)
  171. // 只在本轮源侧读取成功后执行;任何异常都会在上面抛出并跳过这里。
  172. result.StaleRemoved = await RetireStaleAsync(batchId, tenantId, cancellationToken);
  173. await CompleteRunLogAsync(runLogId, result, now);
  174. return result;
  175. }
  176. catch (Exception ex)
  177. {
  178. _logger.LogError(ex, "[PoCompletion] 批次 {BatchId} 失败", batchId);
  179. await FailRunLogAsync(runLogId, ex.Message);
  180. throw;
  181. }
  182. }
  183. // ── 源绑定与 fail-closed ────────────────────────────────────────────────────────
  184. /// <summary>
  185. /// 解析并校验源绑定。非开发环境下,未配置、配置为空、或仍指向 DEV/UAT 默认源,
  186. /// 一律直接失败(fail-closed),不得静默继续。
  187. /// </summary>
  188. private async Task<SourceBinding> ResolveSourceBindingAsync(CancellationToken ct)
  189. {
  190. var code = (_opt.SourceCode ?? string.Empty).Trim();
  191. var isDev = _env.IsDevelopment();
  192. if (code.Length == 0)
  193. throw new InvalidOperationException(
  194. "AiDOP:PoCompletion:SourceCode 未配置,无法确定承担 S8_PO_COMPLETION_SOURCE 角色的数据源");
  195. if (!isDev && (_opt.DevOnlySourceCodes ?? Array.Empty<string>())
  196. .Any(x => string.Equals((x ?? string.Empty).Trim(), code, StringComparison.OrdinalIgnoreCase)))
  197. throw new InvalidOperationException(
  198. $"当前环境 {_env.EnvironmentName} 非开发环境,禁止使用 DEV/UAT 数据源 {code};"
  199. + "请在 AiDOP:PoCompletion:SourceCode 显式绑定真实 WMS/MES 源");
  200. var src = await _db.Ado.SqlQuerySingleAsync<SourceBindingRow>(
  201. """
  202. SELECT id AS Id, source_type AS SourceType, IFNULL(db_user,'') AS DbUser
  203. FROM mdp_source
  204. WHERE source_code=@Code AND status=1
  205. LIMIT 1
  206. """,
  207. new List<SugarParameter> { new("@Code", code) });
  208. if (src == null)
  209. throw new InvalidOperationException($"mdp_source 未找到启用源:{code}");
  210. if (!string.Equals(src.SourceType, "DB", StringComparison.OrdinalIgnoreCase))
  211. throw new InvalidOperationException($"源 {code} 的 source_type={src.SourceType},不是 DB");
  212. if (string.IsNullOrWhiteSpace(src.DbUser))
  213. throw new InvalidOperationException(
  214. $"源 {code} 未配置数据库账号,连接会回落到主库,不能承担 S8_PO_COMPLETION_SOURCE 角色");
  215. return new SourceBinding { SourceCode = code, SourceId = src.Id };
  216. }
  217. // ── 源侧读取 ────────────────────────────────────────────────────────────────────
  218. /// <summary>
  219. /// 读 Source B 的采购行。范围 = 与自建单推送口径一致的 Potype,
  220. /// 不做无差别全表扫,也不顺带拉无关列。
  221. /// </summary>
  222. private async Task<List<SourceRow>> ReadSourceRowsAsync(ISqlSugarClient remote, CancellationToken ct)
  223. {
  224. var top = _opt.MaxSourceRows > 0 ? _opt.MaxSourceRows : 50000;
  225. var potype = (_opt.SourcePotype ?? string.Empty).Trim();
  226. var sql =
  227. $"""
  228. SELECT TOP {top}
  229. RecID, Domain, Potype, PurOrd, Line, Status, UpdateTime, UpdateUser
  230. FROM PurOrdDetail
  231. WHERE (@Potype = '' OR LOWER(LTRIM(RTRIM(ISNULL(Potype,'')))) = LOWER(@Potype))
  232. """;
  233. var table = await remote.Ado.GetDataTableAsync(sql, new List<SugarParameter> { new("@Potype", potype) });
  234. var list = new List<SourceRow>(table.Rows.Count);
  235. foreach (DataRow r in table.Rows)
  236. {
  237. ct.ThrowIfCancellationRequested();
  238. list.Add(new SourceRow
  239. {
  240. SourceRowId = Str(r, "RecID"),
  241. Domain = Str(r, "Domain"),
  242. Potype = Str(r, "Potype"),
  243. PoNo = Str(r, "PurOrd"),
  244. PoLine = Str(r, "Line"),
  245. RawStatus = Str(r, "Status"),
  246. SourceUpdateTime = Dt(r, "UpdateTime"),
  247. SourceUpdateUser = Str(r, "UpdateUser"),
  248. });
  249. }
  250. return list;
  251. }
  252. // ── 租户归属 ────────────────────────────────────────────────────────────────────
  253. /// <summary>
  254. /// 本地采购单 → 租户候选集。Domain 只用于放宽匹配(自建单本库不填 Domain),
  255. /// <b>绝不</b>用 Domain 反推租户 —— 实测同一个 Domain 8010 横跨多个 Ai-DOP 租户。
  256. /// </summary>
  257. private async Task<Dictionary<string, List<PoTenantCandidate>>> LoadLocalTenantIndexAsync(CancellationToken ct)
  258. {
  259. var rows = await _db.Ado.SqlQueryAsync<PoTenantCandidate>(
  260. """
  261. SELECT PurOrd AS PoNo, IFNULL(Domain,'') AS Domain, tenant_id AS TenantId
  262. FROM PurOrdDetail
  263. WHERE IFNULL(tenant_id,0) > 0
  264. GROUP BY PurOrd, IFNULL(Domain,''), tenant_id
  265. """);
  266. var map = new Dictionary<string, List<PoTenantCandidate>>(StringComparer.OrdinalIgnoreCase);
  267. foreach (var r in rows)
  268. {
  269. if (string.IsNullOrWhiteSpace(r.PoNo)) continue;
  270. var key = r.PoNo.Trim();
  271. if (!map.TryGetValue(key, out var bucket))
  272. map[key] = bucket = new List<PoTenantCandidate>();
  273. bucket.Add(r);
  274. }
  275. return map;
  276. }
  277. /// <summary>
  278. /// 候选租户集合。Domain 只用于<b>放宽</b>匹配(自建单本库不填 Domain,空 Domain 视为通配),
  279. /// <b>绝不</b>反过来用 Domain 推租户 —— 实测同一个 Domain 8010 横跨多个 Ai-DOP 租户。
  280. /// </summary>
  281. public static IEnumerable<long> TenantCandidates(
  282. IEnumerable<PoTenantCandidate> candidates, string poNo, string? domain)
  283. {
  284. var po = (poNo ?? string.Empty).Trim();
  285. var dom = (domain ?? string.Empty).Trim();
  286. return candidates
  287. .Where(x => string.Equals((x.PoNo ?? string.Empty).Trim(), po, StringComparison.OrdinalIgnoreCase))
  288. .Where(x => (x.Domain ?? string.Empty).Trim().Length == 0
  289. || string.Equals((x.Domain ?? string.Empty).Trim(), dom, StringComparison.OrdinalIgnoreCase))
  290. .Select(x => x.TenantId)
  291. .Distinct();
  292. }
  293. /// <summary>
  294. /// 唯一才用:0 个(UNRESOLVED)或 &gt;1 个(AMBIGUOUS)一律返回 0,交由调用方跳过。
  295. /// <b>禁止</b>退化成「取第一个」—— 那会在多租户重名时把事实写到别人名下。
  296. /// </summary>
  297. public static long ResolveTenantId(
  298. IEnumerable<PoTenantCandidate> candidates, string poNo, string? domain)
  299. {
  300. var hits = TenantCandidates(candidates, poNo, domain).Take(2).ToList();
  301. return hits.Count == 1 ? hits[0] : 0;
  302. }
  303. private static long ResolveTenant(
  304. Dictionary<string, List<PoTenantCandidate>> index, string poNo, string? domain)
  305. => index.TryGetValue(poNo.Trim(), out var bucket) ? ResolveTenantId(bucket, poNo, domain) : 0;
  306. private static int CountTenantCandidates(
  307. Dictionary<string, List<PoTenantCandidate>> index, string poNo, string? domain)
  308. => index.TryGetValue(poNo.Trim(), out var bucket)
  309. ? TenantCandidates(bucket, poNo, domain).Count()
  310. : 0;
  311. // ── 本地采购行校验 ──────────────────────────────────────────────────────────────
  312. private async Task<Dictionary<string, int>> LoadLocalPoLineIndexAsync(CancellationToken ct)
  313. {
  314. var rows = await _db.Ado.SqlQueryAsync<PoLineKeyRow>(
  315. "SELECT tenant_id AS TenantId, po_no AS PoNo, po_line AS PoLine FROM mdp_std_purchase_order");
  316. var map = new Dictionary<string, int>(StringComparer.OrdinalIgnoreCase);
  317. foreach (var r in rows)
  318. {
  319. var key = LineKey(r.TenantId, r.PoNo ?? string.Empty, r.PoLine ?? string.Empty);
  320. map[key] = map.TryGetValue(key, out var n) ? n + 1 : 1;
  321. }
  322. return map;
  323. }
  324. private static string LineKey(long tenantId, string poNo, string poLine)
  325. => $"{tenantId}|{poNo.Trim()}|{poLine.Trim()}";
  326. // ── 写入 ────────────────────────────────────────────────────────────────────────
  327. /// <summary>
  328. /// 幂等 UPSERT。结论列一律 <c>VALUES(...)</c> 无条件覆盖——这是支持
  329. /// COMPLETED → NOT_COMPLETED 回退的前提,绝不能改成「仅当新值为 C 才更新」。
  330. /// </summary>
  331. private async Task<int> UpsertAsync(
  332. List<CompletionRow> rows, string batchId, DateTime now, CancellationToken ct)
  333. {
  334. if (rows.Count == 0) return 0;
  335. var size = _opt.UpsertBatchSize > 0 ? _opt.UpsertBatchSize : 200;
  336. var written = 0;
  337. foreach (var chunk in Chunk(rows, size))
  338. {
  339. ct.ThrowIfCancellationRequested();
  340. var sb = new StringBuilder();
  341. sb.Append("""
  342. INSERT INTO mdp_std_purchase_order_completion
  343. (tenant_id, factory_id, po_no, po_line, domain, potype,
  344. raw_status, completion_status,
  345. source_system, source_id, source_row_id, source_update_time, source_update_user,
  346. observed_at, sync_batch_id, sync_time)
  347. VALUES
  348. """);
  349. var pars = new List<SugarParameter> { new("@Observed", now), new("@Batch", batchId), new("@Now", now) };
  350. for (var i = 0; i < chunk.Count; i++)
  351. {
  352. var r = chunk[i];
  353. if (i > 0) sb.Append(',');
  354. sb.Append($"(@t{i},1,@p{i},@l{i},@d{i},@y{i},@r{i},@c{i},@s{i},@i{i},@x{i},@u{i},@w{i},@Observed,@Batch,@Now)");
  355. pars.Add(new SugarParameter($"@t{i}", r.TenantId));
  356. pars.Add(new SugarParameter($"@p{i}", r.PoNo));
  357. pars.Add(new SugarParameter($"@l{i}", r.PoLine));
  358. pars.Add(new SugarParameter($"@d{i}", (object?)r.Domain ?? DBNull.Value));
  359. pars.Add(new SugarParameter($"@y{i}", (object?)r.Potype ?? DBNull.Value));
  360. pars.Add(new SugarParameter($"@r{i}", (object?)r.RawStatus ?? DBNull.Value));
  361. pars.Add(new SugarParameter($"@c{i}", r.CompletionStatus));
  362. pars.Add(new SugarParameter($"@s{i}", r.SourceSystem));
  363. pars.Add(new SugarParameter($"@i{i}", (object?)r.SourceId ?? DBNull.Value));
  364. pars.Add(new SugarParameter($"@x{i}", (object?)r.SourceRowId ?? DBNull.Value));
  365. pars.Add(new SugarParameter($"@u{i}", (object?)r.SourceUpdateTime ?? DBNull.Value));
  366. pars.Add(new SugarParameter($"@w{i}", (object?)r.SourceUpdateUser ?? DBNull.Value));
  367. }
  368. sb.Append("""
  369. ON DUPLICATE KEY UPDATE
  370. factory_id=VALUES(factory_id), domain=VALUES(domain), potype=VALUES(potype),
  371. raw_status=VALUES(raw_status), completion_status=VALUES(completion_status),
  372. source_system=VALUES(source_system), source_id=VALUES(source_id),
  373. source_row_id=VALUES(source_row_id),
  374. source_update_time=VALUES(source_update_time),
  375. source_update_user=VALUES(source_update_user),
  376. observed_at=VALUES(observed_at), sync_batch_id=VALUES(sync_batch_id),
  377. sync_time=VALUES(sync_time), update_time=CURRENT_TIMESTAMP
  378. """);
  379. await _db.Ado.ExecuteCommandAsync(sb.ToString(), pars);
  380. written += chunk.Count;
  381. }
  382. return written;
  383. }
  384. /// <summary>
  385. /// 淘汰本轮未覆盖的行 —— 它们在源侧已不存在,应回到 NOT_OBSERVED(即本表无此行)。
  386. ///
  387. /// <para>只在本方法被调用时执行,而调用点在源侧读取与写入全部成功之后;
  388. /// 读取失败会在更早处抛出,因此「源侧抓取失败却把旧事实删掉」不可能发生。</para>
  389. ///
  390. /// <para>单租户刷新只淘汰该租户 —— 租户 A 的刷新绝不允许删掉租户 B 的事实。</para>
  391. /// </summary>
  392. private async Task<int> RetireStaleAsync(string batchId, long tenantId, CancellationToken ct)
  393. {
  394. ct.ThrowIfCancellationRequested();
  395. if (tenantId > 0)
  396. {
  397. return await _db.Ado.ExecuteCommandAsync(
  398. """
  399. DELETE FROM mdp_std_purchase_order_completion
  400. WHERE tenant_id=@TenantId AND IFNULL(sync_batch_id,'') <> @BatchId
  401. """,
  402. new List<SugarParameter> { new("@TenantId", tenantId), new("@BatchId", batchId) });
  403. }
  404. return await _db.Ado.ExecuteCommandAsync(
  405. """
  406. DELETE FROM mdp_std_purchase_order_completion
  407. WHERE tenant_id > 0 AND IFNULL(sync_batch_id,'') <> @BatchId
  408. """,
  409. new List<SugarParameter> { new("@BatchId", batchId) });
  410. }
  411. // ── run log ─────────────────────────────────────────────────────────────────────
  412. private async Task<long> InsertRunLogAsync(string batchId, DateTime startedAt, string triggerType, long tenantId)
  413. {
  414. await _db.Ado.ExecuteCommandAsync(
  415. """
  416. INSERT INTO mdp_transform_run_log
  417. (tenant_id, job_code, job_name, trigger_type, batch_id, status, start_time)
  418. VALUES (@TenantId, @JobCode, @JobName, @TriggerType, @BatchId, 'RUNNING', @StartTime)
  419. """,
  420. new SugarParameter("@TenantId", tenantId),
  421. new SugarParameter("@JobCode", JobCode),
  422. new SugarParameter("@JobName", JobName),
  423. new SugarParameter("@TriggerType", NormalizeTriggerType(triggerType)),
  424. new SugarParameter("@BatchId", batchId),
  425. new SugarParameter("@StartTime", startedAt));
  426. return await _db.Ado.GetLongAsync(
  427. "SELECT id FROM mdp_transform_run_log WHERE batch_id=@BatchId ORDER BY id DESC LIMIT 1",
  428. new List<SugarParameter> { new("@BatchId", batchId) });
  429. }
  430. private async Task CompleteRunLogAsync(long runLogId, PurchaseOrderCompletionSyncResult r, DateTime startedAt)
  431. {
  432. var endedAt = DateTime.Now;
  433. // summary_json 是 JSON 列,必须写合法 JSON —— 纯文本会被 MySQL 直接拒绝。
  434. // 这里只放计数与源编码,不放任何连接信息。
  435. var summary = System.Text.Json.JsonSerializer.Serialize(new
  436. {
  437. sourceCode = r.SourceCode,
  438. sourceRows = r.SourceRows,
  439. matchedRows = r.MatchedRows,
  440. writtenRows = r.WrittenRows,
  441. completedRows = r.CompletedRows,
  442. notCompletedRows = r.NotCompletedRows,
  443. unknownStatusRows = r.UnknownStatusRows,
  444. skippedRows = r.SkippedRows,
  445. tenantUnresolvedRows = r.TenantUnresolvedRows,
  446. unmatchedRows = r.UnmatchedRows,
  447. staleRemoved = r.StaleRemoved,
  448. });
  449. await _db.Ado.ExecuteCommandAsync(
  450. """
  451. UPDATE mdp_transform_run_log
  452. SET status='SUCCESS', stage_rows=@SrcRows, standard_rows=@StdRows, end_time=@EndTime,
  453. duration_ms=@Duration, summary_json=@Summary, update_time=CURRENT_TIMESTAMP
  454. WHERE id=@Id
  455. """,
  456. new SugarParameter("@SrcRows", r.SourceRows),
  457. new SugarParameter("@StdRows", r.WrittenRows),
  458. new SugarParameter("@EndTime", endedAt),
  459. new SugarParameter("@Duration", (int)(endedAt - startedAt).TotalMilliseconds),
  460. // 刻意不截断:这是固定形状的计数 JSON(长度有界),截断会产生非法 JSON 并让整轮失败
  461. new SugarParameter("@Summary", summary),
  462. new SugarParameter("@Id", runLogId));
  463. }
  464. private async Task FailRunLogAsync(long runLogId, string message)
  465. {
  466. await _db.Ado.ExecuteCommandAsync(
  467. """
  468. UPDATE mdp_transform_run_log
  469. SET status='FAILED', end_time=@EndTime, error_message=@Msg, update_time=CURRENT_TIMESTAMP
  470. WHERE id=@Id
  471. """,
  472. new SugarParameter("@EndTime", DateTime.Now),
  473. new SugarParameter("@Msg", Truncate(message, 900)),
  474. new SugarParameter("@Id", runLogId));
  475. }
  476. // ── 工具 ────────────────────────────────────────────────────────────────────────
  477. private static string NormalizeTriggerType(string triggerType)
  478. => string.IsNullOrWhiteSpace(triggerType) ? "AUTO" : triggerType.Trim().ToUpperInvariant();
  479. private static string Truncate(string? s, int max)
  480. => string.IsNullOrEmpty(s) ? string.Empty : (s.Length <= max ? s : s[..max]);
  481. private static string? Str(DataRow row, string col)
  482. {
  483. if (!row.Table.Columns.Contains(col)) return null;
  484. var v = row[col];
  485. return v == null || v == DBNull.Value ? null : Convert.ToString(v);
  486. }
  487. private static DateTime? Dt(DataRow row, string col)
  488. {
  489. if (!row.Table.Columns.Contains(col)) return null;
  490. var v = row[col];
  491. if (v == null || v == DBNull.Value) return null;
  492. return Convert.ToDateTime(v);
  493. }
  494. private static IEnumerable<List<T>> Chunk<T>(List<T> source, int size)
  495. {
  496. for (var i = 0; i < source.Count; i += size)
  497. yield return source.GetRange(i, Math.Min(size, source.Count - i));
  498. }
  499. // ── 内部模型 ────────────────────────────────────────────────────────────────────
  500. private sealed class SourceRow
  501. {
  502. public string? SourceRowId { get; set; }
  503. public string? Domain { get; set; }
  504. public string? Potype { get; set; }
  505. public string PoNo { get; set; } = string.Empty;
  506. public string PoLine { get; set; } = string.Empty;
  507. public string? RawStatus { get; set; }
  508. public DateTime? SourceUpdateTime { get; set; }
  509. public string? SourceUpdateUser { get; set; }
  510. }
  511. private sealed class CompletionRow
  512. {
  513. public long TenantId { get; set; }
  514. public string PoNo { get; set; } = string.Empty;
  515. public string PoLine { get; set; } = string.Empty;
  516. public string? Domain { get; set; }
  517. public string? Potype { get; set; }
  518. public string? RawStatus { get; set; }
  519. public string CompletionStatus { get; set; } = StatusUnknown;
  520. public string SourceSystem { get; set; } = string.Empty;
  521. public long? SourceId { get; set; }
  522. public string? SourceRowId { get; set; }
  523. public DateTime? SourceUpdateTime { get; set; }
  524. public string? SourceUpdateUser { get; set; }
  525. }
  526. private sealed class PoLineKeyRow
  527. {
  528. public long TenantId { get; set; }
  529. public string? PoNo { get; set; }
  530. public string? PoLine { get; set; }
  531. }
  532. private sealed class SourceBinding
  533. {
  534. public string SourceCode { get; set; } = string.Empty;
  535. public long? SourceId { get; set; }
  536. }
  537. private sealed class SourceBindingRow
  538. {
  539. public long Id { get; set; }
  540. public string? SourceType { get; set; }
  541. public string? DbUser { get; set; }
  542. }
  543. }
  544. /// <summary>
  545. /// 本地采购单 → 租户候选。<c>Domain</c> 为空表示自建单(本库不填 165 账套),视为通配。
  546. /// 仅用于把 Source B 的行归到某个 Ai-DOP 租户,<b>不是</b>租户边界本身。
  547. /// </summary>
  548. public sealed class PoTenantCandidate
  549. {
  550. public string PoNo { get; set; } = string.Empty;
  551. public string Domain { get; set; } = string.Empty;
  552. public long TenantId { get; set; }
  553. }
  554. /// <summary>S3 采购完成态投影结果。</summary>
  555. public sealed class PurchaseOrderCompletionSyncResult
  556. {
  557. public string BatchId { get; set; } = string.Empty;
  558. public long RunLogId { get; set; }
  559. /// <summary>承担本次角色的数据源编码(不含任何连接信息)。</summary>
  560. public string SourceCode { get; set; } = string.Empty;
  561. /// <summary>源侧读到的行数。</summary>
  562. public int SourceRows { get; set; }
  563. /// <summary>唯一命中本地采购行的行数。</summary>
  564. public int MatchedRows { get; set; }
  565. /// <summary>实际写入(新增或刷新)的行数。</summary>
  566. public int WrittenRows { get; set; }
  567. /// <summary>被跳过的源行数(租户不可解析 + 本库无对应采购行 + 缺关键字段)。</summary>
  568. public int SkippedRows { get; set; }
  569. /// <summary>其中:租户无法唯一解析。</summary>
  570. public int TenantUnresolvedRows { get; set; }
  571. /// <summary>其中:本库无对应采购行。</summary>
  572. public int UnmatchedRows { get; set; }
  573. public int CompletedRows { get; set; }
  574. public int NotCompletedRows { get; set; }
  575. /// <summary>源侧给了 NULL 或无法识别的状态值。</summary>
  576. public int UnknownStatusRows { get; set; }
  577. /// <summary>本轮源侧已不存在、被回收为 NOT_OBSERVED 的行数。</summary>
  578. public int StaleRemoved { get; set; }
  579. }