S5MdpSyncTransformService.cs 59 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152
  1. using Admin.NET.Core.Service;
  2. using Admin.NET.Plugin.AiDOP.Infrastructure;
  3. using Admin.NET.Plugin.AiDOP.SmartOps;
  4. using Microsoft.Extensions.Logging;
  5. using System.Text.Json;
  6. namespace Admin.NET.Plugin.AiDOP.MaterialWarehouse;
  7. /// <summary>
  8. /// S5 物料仓储 — KPI 计算与刷新。L1 读中立标准层。
  9. /// 结果落 dwd_* 与 ado_s9_kpi_value_l1_day。
  10. /// </summary>
  11. public class S5MdpSyncTransformService : ITransient
  12. {
  13. private readonly ISqlSugarClient _db;
  14. private readonly TransformRunLogFinalizer _runLogFinalizer;
  15. private readonly SysNoticeService _sysNoticeService;
  16. private readonly ILogger<S5MdpSyncTransformService> _logger;
  17. private const string JobCode = "S5_MDP_SYNC_TRANSFORM";
  18. private const string JobName = "S5 物料仓储 MDP 同步与转换";
  19. private const string ModuleCode = "S5";
  20. private const string L2ValueTable = "ado_s9_kpi_value_l2_day";
  21. private const string L3ValueTable = "ado_s9_kpi_value_l3_day";
  22. // FAILURE-NOTIFICATION-1:超级管理员 superAdmin.NET(AccountType=999)
  23. private const long NoticeReceiverUserId = 1300000000101L;
  24. private const string NoticeReceiverUserName = "超级管理员";
  25. private readonly SmartOps.KpiCalcDispatcher _kpiCalcDispatcher;
  26. private readonly SmartOps.KpiDimensionRunService _dimensionRun;
  27. private readonly IKpiTargetResolver _kpiTargetResolver;
  28. private readonly InventoryMdpSyncService _inventoryMdpSync;
  29. private readonly PurchaseReceiptMdpSyncService _purchaseReceiptMdpSync;
  30. private readonly DataPlatform.T8BaseInboundMdpSyncService _t8Inbound;
  31. private readonly SmartOps.S9CompositeKpiWriter _s9Composite;
  32. public S5MdpSyncTransformService(
  33. ISqlSugarClient db,
  34. SysNoticeService sysNoticeService,
  35. ILogger<S5MdpSyncTransformService> logger,
  36. SmartOps.KpiCalcDispatcher kpiCalcDispatcher,
  37. SmartOps.KpiDimensionRunService dimensionRun,
  38. IKpiTargetResolver kpiTargetResolver,
  39. InventoryMdpSyncService inventoryMdpSync,
  40. PurchaseReceiptMdpSyncService purchaseReceiptMdpSync,
  41. DataPlatform.T8BaseInboundMdpSyncService t8Inbound,
  42. TransformRunLogFinalizer runLogFinalizer,
  43. SmartOps.S9CompositeKpiWriter s9Composite)
  44. {
  45. _db = db;
  46. _runLogFinalizer = runLogFinalizer;
  47. _sysNoticeService = sysNoticeService;
  48. _logger = logger;
  49. _kpiCalcDispatcher = kpiCalcDispatcher;
  50. _dimensionRun = dimensionRun;
  51. _kpiTargetResolver = kpiTargetResolver;
  52. _inventoryMdpSync = inventoryMdpSync;
  53. _purchaseReceiptMdpSync = purchaseReceiptMdpSync;
  54. _t8Inbound = t8Inbound;
  55. _s9Composite = s9Composite;
  56. }
  57. public async Task<S5MdpSyncTransformResult> RunFullAsync(
  58. CancellationToken cancellationToken = default,
  59. string triggerType = "AUTO",
  60. S5MdpRefreshOption? option = null)
  61. {
  62. cancellationToken.ThrowIfCancellationRequested();
  63. option ??= S5MdpRefreshOption.Default();
  64. NormalizeOption(option);
  65. var now = DateTime.Now;
  66. var batchId = $"S5_MDP_FULL_{now:yyyyMMddHHmmss}";
  67. var normalizedTrigger = NormalizeTriggerType(triggerType);
  68. var runLogId = await InsertTransformRunLogAsync(batchId, now, normalizedTrigger, option);
  69. var result = new S5MdpSyncTransformResult
  70. {
  71. BatchId = batchId,
  72. RunLogId = runLogId,
  73. TriggerType = normalizedTrigger,
  74. SourceZtid = option.SourceZtid,
  75. TargetTenantId = option.TargetTenantId,
  76. TargetFactoryId = option.TargetFactoryId,
  77. BizDate = option.BizDate,
  78. BizMonth = option.BizMonth,
  79. DailyPeriodStart = option.DailyPeriodStart,
  80. DailyPeriodEnd = option.DailyPeriodEnd,
  81. MonthlyPeriodStart = option.MonthlyPeriodStart,
  82. MonthlyPeriodEnd = option.MonthlyPeriodEnd
  83. };
  84. try
  85. {
  86. var receiptSync = await _purchaseReceiptMdpSync.RunInboundAsync(
  87. option.TargetTenantId, true, cancellationToken);
  88. result.StageRows = receiptSync.RowsWrittenStg;
  89. result.StandardRows = receiptSync.StdRows
  90. + await _inventoryMdpSync.TransformTransStdFromStgAsync(
  91. option.TargetTenantId, cancellationToken);
  92. var sub16 = await BuildS5L1001MaterialOnlineCycleAsync(batchId, now, option, normalizedTrigger, cancellationToken);
  93. result.MergeSub("S5_L1_001", sub16);
  94. var sub17 = await BuildS5L1002MaterialOnlineFulfillmentAsync(batchId, now, option, normalizedTrigger, cancellationToken);
  95. result.MergeSub("S5_L1_002", sub17);
  96. var sub18 = await BuildS5L1003MaterialWarehouseEfficiencyAsync(batchId, now, option, normalizedTrigger, cancellationToken);
  97. result.MergeSub("S5_L1_003", sub18);
  98. var sub19 = await BuildS5L1004MaterialInventoryTurnoverAsync(batchId, now, option, normalizedTrigger, cancellationToken);
  99. result.MergeSub("S5_L1_004", sub19);
  100. var currentBizDate = option.BizDate;
  101. var currentPeriodStart = option.DailyPeriodStart;
  102. var currentPeriodEnd = option.DailyPeriodEnd;
  103. const int backfillDays = 14;
  104. for (var dayOffset = backfillDays - 1; dayOffset >= 0; dayOffset--)
  105. {
  106. option.BizDate = currentBizDate.AddDays(-dayOffset);
  107. option.DailyPeriodStart = option.BizDate.Date;
  108. option.DailyPeriodEnd = option.BizDate.Date.AddDays(1).AddSeconds(-1);
  109. result.MergeSub("S5_L2_001", await BuildS5L2001ReceiptCycleAsync(
  110. batchId, now, option, normalizedTrigger, cancellationToken));
  111. result.MergeSub("S5_L2_002", await BuildS5L2002ReceiptFulfillmentAsync(
  112. batchId, now, option, normalizedTrigger, cancellationToken));
  113. result.MergeSub("S5_L2_003", await BuildS5L2003IqcCycleAsync(
  114. batchId, now, option, normalizedTrigger, cancellationToken));
  115. result.MergeSub("S5_L2_004", await BuildS5L2004IqcFulfillmentAsync(
  116. batchId, now, option, normalizedTrigger, cancellationToken));
  117. foreach (var metricCode in new[]
  118. {
  119. "S5_L2_005", "S5_L2_006", "S5_L2_007", "S5_L2_008", "S5_L2_009",
  120. "S5_L2_010", "S5_L2_011", "S5_L2_012", "S5_L2_013", "S5_L2_014",
  121. "S5_L2_015", "S5_L3_001", "S5_L3_002", "S5_L3_003", "S5_L3_004",
  122. "S5_L3_005"
  123. })
  124. {
  125. var sub = await BuildS5WarehouseStageKpiAsync(
  126. metricCode, batchId, now, option, normalizedTrigger, cancellationToken);
  127. result.MergeSub(metricCode, sub);
  128. }
  129. }
  130. option.BizDate = currentBizDate;
  131. option.DailyPeriodStart = currentPeriodStart;
  132. option.DailyPeriodEnd = currentPeriodEnd;
  133. result.KpiRows += await _s9Composite.WriteRecentAsync(
  134. option.TargetTenantId, option.TargetFactoryId, option.BizDate, cancellationToken);
  135. await MarkTransformRunSuccessAsync(runLogId, now, result);
  136. return result;
  137. }
  138. catch (Exception ex)
  139. {
  140. // 宿主关停不是转换失败:交给 finally 收口为 ABORTED,不污染 FAILED 语义。
  141. if (!_runLogFinalizer.IsHostStopping)
  142. await MarkTransformRunFailedAsync(runLogId, now, ex.Message, batchId);
  143. throw;
  144. }
  145. finally
  146. {
  147. await _runLogFinalizer.FinalizeIfHostStoppingAsync(runLogId, now);
  148. }
  149. }
  150. // ─────────────────────────────────────────────────────────────────────────
  151. // KPI 实现(方老师 v5.4 KPI J 列 SQL 原逻辑直发 T8)
  152. // ─────────────────────────────────────────────────────────────────────────
  153. /// <summary>S5_L1_001 物料上线周期 = 领料到线时间减采购收货时间。数据准备始终执行,最终聚合由计算配置分发器接管。</summary>
  154. private async Task<KpiBuildSubResult> BuildS5L1001MaterialOnlineCycleAsync(
  155. string batchId, DateTime now, S5MdpRefreshOption option, string triggerType, CancellationToken ct)
  156. {
  157. var sub = new KpiBuildSubResult();
  158. const string sqlOnline = @"
  159. select item_num as item_code, min(approved_time) as approved_time
  160. from mdp_std_inv_trans
  161. where tenant_id=@tenantId and biz_doc_type='PROD_ISSUE'
  162. and summary_flag=0 and void_flag=0 and approved_flag=1 AND (@sourceSystem='' OR source_system=@sourceSystem) AND (source_system<>'T8' OR domain=@sourceDomain)
  163. group by item_num";
  164. const string sqlReceipt = @"
  165. select item_num as item_code, min(approved_time) as approved_time
  166. from mdp_std_inv_trans
  167. where tenant_id=@tenantId and biz_doc_type='PUR_RECEIPT'
  168. and summary_flag=0 and void_flag=0 and approved_flag=1 AND (@sourceSystem='' OR source_system=@sourceSystem) AND (source_system<>'T8' OR domain=@sourceDomain)
  169. group by item_num";
  170. var p = new[]
  171. {
  172. new SugarParameter("@tenantId", option.TargetTenantId),
  173. new SugarParameter("@sourceDomain", option.SourceZtid),
  174. new SugarParameter("@sourceSystem", "")
  175. };
  176. var onlineRows = await _db.Ado.SqlQueryAsync<S5OnlineCycleRow>(sqlOnline, p);
  177. var receiptRows = await _db.Ado.SqlQueryAsync<S5OnlineCycleRow>(sqlReceipt, p);
  178. sub.T8Rows = onlineRows.Count + receiptRows.Count;
  179. var onlineByCode = onlineRows.Where(r => !string.IsNullOrEmpty(r.item_code))
  180. .ToDictionary(r => r.item_code!, r => r.approved_time, StringComparer.OrdinalIgnoreCase);
  181. var receiptByCode = receiptRows.Where(r => !string.IsNullOrEmpty(r.item_code))
  182. .ToDictionary(r => r.item_code!, r => r.approved_time, StringComparer.OrdinalIgnoreCase);
  183. var allCodes = new HashSet<string>(onlineByCode.Keys, StringComparer.OrdinalIgnoreCase);
  184. allCodes.UnionWith(receiptByCode.Keys);
  185. var dwdAffected = 0;
  186. var cycleDaysList = new List<int>();
  187. foreach (var code in allCodes)
  188. {
  189. ct.ThrowIfCancellationRequested();
  190. var online = onlineByCode.GetValueOrDefault(code);
  191. var receipt = receiptByCode.GetValueOrDefault(code);
  192. int? cycleDays = null;
  193. if (online.HasValue && receipt.HasValue)
  194. {
  195. cycleDays = (int)(online.Value.Date - receipt.Value.Date).TotalDays;
  196. cycleDaysList.Add(cycleDays.Value);
  197. }
  198. dwdAffected += await _db.Ado.ExecuteCommandAsync(@"
  199. INSERT INTO dwd_material_online_cycle
  200. (tenant_id, factory_id, biz_date, source_ztid, item_code, online_date, receipt_date, cycle_days, batch_id, create_time)
  201. VALUES
  202. (@tenantId, @factoryId, @bizDate, @sourceDomain, @itemCode, @online, @receipt, @cycleDays, @batchId, @now)
  203. ON DUPLICATE KEY UPDATE
  204. online_date=VALUES(online_date), receipt_date=VALUES(receipt_date),
  205. cycle_days=VALUES(cycle_days), batch_id=VALUES(batch_id), update_time=@now",
  206. new SugarParameter("@tenantId", option.TargetTenantId),
  207. new SugarParameter("@factoryId", option.TargetFactoryId),
  208. new SugarParameter("@bizDate", option.BizDate),
  209. new SugarParameter("@sourceDomain", option.SourceZtid),
  210. new SugarParameter("@itemCode", code),
  211. new SugarParameter("@online", online),
  212. new SugarParameter("@receipt", receipt),
  213. new SugarParameter("@cycleDays", cycleDays),
  214. new SugarParameter("@batchId", batchId),
  215. new SugarParameter("@now", now));
  216. }
  217. sub.DwdRows = dwdAffected;
  218. // 数据准备(dwd 明细)已完成。最终 KPI 聚合交计算配置分发器:
  219. // 无配置/LEGACY_CODE → 用下面 legacy 均值;CONFIG_SQL → 执行已发布只读 SQL;
  220. // CONFIG_SQL 失败不 fallback、不写值、保留上一成功值(ShouldUpsert=false)。
  221. decimal? legacyValue = cycleDaysList.Count > 0 ? (decimal)cycleDaysList.Average() : null;
  222. var legacyDenom = cycleDaysList.Count > 0 ? "OK" : "NO_NUMERATOR";
  223. var dispatch = await _kpiCalcDispatcher.DispatchAsync(
  224. "S5_L1_001", ModuleCode, option.TargetTenantId, option.TargetFactoryId,
  225. option.BizDate, option.DailyPeriodStart, option.DailyPeriodEnd, option.SourceZtid,
  226. batchId, triggerType, legacyValue, legacyDenom, ct);
  227. sub.KpiRows = dispatch.ShouldUpsert
  228. ? await UpsertKpiValueAsync("S5_L1_001", option.BizDate, dispatch.MetricValue, now, option)
  229. : 0;
  230. sub.DenominatorStatus = dispatch.DenominatorStatus;
  231. // 调度:SUMMARY 成功/NO_DATA 后触发对应 DIMENSION 跑批(共享 BatchId;SUMMARY FAILED 不触发)。
  232. if (dispatch.ShouldUpsert)
  233. {
  234. try
  235. {
  236. await _dimensionRun.RunDimensionAsync(
  237. "S5_L1_001", ModuleCode, option.TargetTenantId, option.BizDate, batchId, triggerType, ct);
  238. }
  239. catch (Exception ex)
  240. {
  241. _logger.LogWarning(ex, "S5_L1_001 维度跑批异常(不影响汇总链路)");
  242. }
  243. }
  244. return sub;
  245. }
  246. /// <summary>S5_L1_002 物料上线满足率 = 开工日期前完成上线行数 / 工单物料总行数。</summary>
  247. private async Task<KpiBuildSubResult> BuildS5L1002MaterialOnlineFulfillmentAsync(
  248. string batchId, DateTime now, S5MdpRefreshOption option, string triggerType, CancellationToken ct)
  249. {
  250. var sub = new KpiBuildSubResult();
  251. const string sqlNumer = @"
  252. select ref_task_no as task_no, count(*) as codenum
  253. from (
  254. select ref_task_no, item_num
  255. from mdp_std_inv_trans h
  256. left join (
  257. select work_order_no as task_no, min(start_work_date) as start_work_date
  258. from mdp_std_s6_report
  259. where tenant_id=@tenantId
  260. group by work_order_no
  261. ) c on h.ref_task_no=c.task_no
  262. where h.tenant_id=@tenantId and h.biz_doc_type='PROD_ISSUE'
  263. and h.summary_flag=0 and h.void_flag=0 and h.approved_flag=1
  264. AND (@sourceSystem='' OR h.source_system=@sourceSystem) AND (h.source_system<>'T8' OR h.domain=@sourceDomain)
  265. and h.approved_time<=c.start_work_date
  266. group by h.ref_task_no, h.item_num
  267. ) n
  268. group by task_no";
  269. // Q14:以工单头为驱动 LEFT JOIN BOM。无 BOM 行的工单仍出现,行数为 0(计入分母、不计入分子)。
  270. const string sqlDenom = @"
  271. select h.work_order as order_no, count(b.source_row_id) as listnum
  272. from mdp_std_work_order_schedule h
  273. left join mdp_std_work_order_bom b
  274. on b.tenant_id=h.tenant_id and b.source_system=h.source_system and b.order_no=h.work_order
  275. where h.tenant_id=@tenantId and h.doc_type='PROD_TASK'
  276. and h.void_flag=0 and h.approved_flag=1
  277. group by h.work_order";
  278. var p = new[]
  279. {
  280. new SugarParameter("@tenantId", option.TargetTenantId),
  281. new SugarParameter("@sourceDomain", option.SourceZtid),
  282. new SugarParameter("@sourceSystem", "")
  283. };
  284. var numerRows = await _db.Ado.SqlQueryAsync<S5FulfillmentNumerRow>(sqlNumer, p);
  285. var denomRows = await _db.Ado.SqlQueryAsync<S5FulfillmentDenomRow>(sqlDenom, p);
  286. sub.T8Rows = numerRows.Count + denomRows.Count;
  287. var numerByOrder = numerRows.Where(r => !string.IsNullOrEmpty(r.task_no))
  288. .ToDictionary(r => r.task_no!, r => r.codenum, StringComparer.OrdinalIgnoreCase);
  289. var dwdAffected = 0;
  290. var rateList = new List<decimal>();
  291. foreach (var d in denomRows)
  292. {
  293. ct.ThrowIfCancellationRequested();
  294. if (string.IsNullOrEmpty(d.order_no)) continue;
  295. var beforeKg = numerByOrder.GetValueOrDefault(d.order_no, 0);
  296. // 无 BOM 行:满足行数为 0,该工单以 0 计入各单比率的平均(分母含它,分子不含)。
  297. var rate = d.listnum > 0
  298. ? Math.Round((decimal)beforeKg / d.listnum, 4)
  299. : 0m;
  300. rateList.Add(rate);
  301. dwdAffected += await _db.Ado.ExecuteCommandAsync(@"
  302. INSERT INTO dwd_material_online_fulfillment
  303. (tenant_id, factory_id, biz_date, source_ztid, work_order_no,
  304. before_kgdate_rows, total_rows, fulfillment_rate, batch_id, create_time)
  305. VALUES
  306. (@tenantId, @factoryId, @bizDate, @sourceDomain, @workOrderNo, @beforeKg, @total, @rate, @batchId, @now)
  307. ON DUPLICATE KEY UPDATE
  308. before_kgdate_rows=VALUES(before_kgdate_rows),
  309. total_rows=VALUES(total_rows),
  310. fulfillment_rate=VALUES(fulfillment_rate),
  311. batch_id=VALUES(batch_id), update_time=@now",
  312. new SugarParameter("@tenantId", option.TargetTenantId),
  313. new SugarParameter("@factoryId", option.TargetFactoryId),
  314. new SugarParameter("@bizDate", option.BizDate),
  315. new SugarParameter("@sourceDomain", option.SourceZtid),
  316. new SugarParameter("@workOrderNo", d.order_no),
  317. new SugarParameter("@beforeKg", beforeKg),
  318. new SugarParameter("@total", d.listnum),
  319. new SugarParameter("@rate", rate),
  320. new SugarParameter("@batchId", batchId),
  321. new SugarParameter("@now", now));
  322. }
  323. sub.DwdRows = dwdAffected;
  324. // 数据准备(dwd 逐单明细)已完成。最终 KPI 聚合交计算配置分发器:
  325. // 无配置/LEGACY_CODE → 用下面 legacy 均值-of-比率×100;CONFIG_SQL → 执行已发布只读 SQL;
  326. // CONFIG_SQL 失败不 fallback、不写值、保留上一成功值(ShouldUpsert=false)。
  327. decimal? legacyValue = rateList.Count > 0
  328. ? Math.Round(rateList.Average() * 100m, 4) // 百分号
  329. : null;
  330. var legacyDenom = rateList.Count > 0 ? "OK" : "NO_VALID_ORDER";
  331. var dispatch = await _kpiCalcDispatcher.DispatchAsync(
  332. "S5_L1_002", ModuleCode, option.TargetTenantId, option.TargetFactoryId,
  333. option.BizDate, option.DailyPeriodStart, option.DailyPeriodEnd, option.SourceZtid,
  334. batchId, triggerType, legacyValue, legacyDenom, ct);
  335. sub.KpiRows = dispatch.ShouldUpsert
  336. ? await UpsertKpiValueAsync("S5_L1_002", option.BizDate, dispatch.MetricValue, now, option)
  337. : 0;
  338. sub.DenominatorStatus = dispatch.DenominatorStatus;
  339. // 调度:SUMMARY 成功/NO_DATA 后触发对应 DIMENSION 跑批(共享 BatchId;SUMMARY FAILED 不触发)。
  340. if (dispatch.ShouldUpsert)
  341. {
  342. try
  343. {
  344. await _dimensionRun.RunDimensionAsync(
  345. "S5_L1_002", ModuleCode, option.TargetTenantId, option.BizDate, batchId, triggerType, ct);
  346. }
  347. catch (Exception ex)
  348. {
  349. _logger.LogWarning(ex, "S5_L1_002 维度跑批异常(不影响汇总链路)");
  350. }
  351. }
  352. return sub;
  353. }
  354. /// <summary>S5_L1_003 物料仓储人效 = 领料数量 / 在职仓储岗位人数。</summary>
  355. private async Task<KpiBuildSubResult> BuildS5L1003MaterialWarehouseEfficiencyAsync(
  356. string batchId, DateTime now, S5MdpRefreshOption option, string triggerType, CancellationToken ct)
  357. {
  358. var sub = new KpiBuildSubResult();
  359. const string sqlNumer = @"
  360. select sum(qty_change) as qty_change
  361. from mdp_std_inv_trans
  362. where tenant_id=@tenantId and biz_doc_type='PROD_ISSUE'
  363. and summary_flag=0 and void_flag=0 and approved_flag=1 AND (@sourceSystem='' OR source_system=@sourceSystem) AND (source_system<>'T8' OR domain=@sourceDomain)
  364. and approved_time between @startDate and @endDate";
  365. const string sqlDenom = @"
  366. select count(*) as penum
  367. from mdp_std_employee
  368. where tenant_id=@tenantId and employment_status='ACTIVE' and position_code='WAREHOUSE' AND (@sourceSystem='' OR source_system=@sourceSystem) AND (source_system<>'T8' OR domain=@sourceDomain)";
  369. var pNumer = new[]
  370. {
  371. new SugarParameter("@tenantId", option.TargetTenantId),
  372. new SugarParameter("@startDate", option.MonthlyPeriodStart),
  373. new SugarParameter("@endDate", option.MonthlyPeriodEnd),
  374. new SugarParameter("@sourceDomain", option.SourceZtid),
  375. new SugarParameter("@sourceSystem", "")
  376. };
  377. var pDenom = new[]
  378. {
  379. new SugarParameter("@tenantId", option.TargetTenantId),
  380. new SugarParameter("@sourceDomain", option.SourceZtid),
  381. new SugarParameter("@sourceSystem", "")
  382. };
  383. var numerRows = await _db.Ado.SqlQueryAsync<S5SumQtyRow>(sqlNumer, pNumer);
  384. var denomRows = await _db.Ado.SqlQueryAsync<S5CountRow>(sqlDenom, pDenom);
  385. sub.T8Rows = numerRows.Count + denomRows.Count;
  386. decimal? onlineQty = numerRows.FirstOrDefault()?.qty_change;
  387. int? headcount = denomRows.FirstOrDefault()?.penum;
  388. // 分母 = 0 或 NULL:efficiency 写 NULL,并标记 denominator_status;不伪装真实 0
  389. decimal? efficiency = null;
  390. string denomStatus;
  391. if (!headcount.HasValue || headcount.Value <= 0)
  392. {
  393. denomStatus = "NO_HEADCOUNT";
  394. }
  395. else if (!onlineQty.HasValue)
  396. {
  397. denomStatus = "NO_NUMERATOR";
  398. }
  399. else
  400. {
  401. efficiency = Math.Round(onlineQty.Value / headcount.Value, 4);
  402. denomStatus = "OK";
  403. }
  404. sub.DenominatorStatus = denomStatus;
  405. // 月度 KPI 用 biz_month 唯一键,整月 1 行
  406. var dwdAffected = await _db.Ado.ExecuteCommandAsync(@"
  407. INSERT INTO dwd_material_warehouse_efficiency
  408. (tenant_id, factory_id, biz_month, source_ztid, period_start, period_end,
  409. online_qty, warehouse_headcount, efficiency, denominator_status, batch_id, create_time)
  410. VALUES
  411. (@tenantId, @factoryId, @bizMonth, @sourceDomain, @periodStart, @periodEnd,
  412. @onlineQty, @headcount, @efficiency, @denomStatus, @batchId, @now)
  413. ON DUPLICATE KEY UPDATE
  414. period_start=VALUES(period_start), period_end=VALUES(period_end),
  415. online_qty=VALUES(online_qty), warehouse_headcount=VALUES(warehouse_headcount),
  416. efficiency=VALUES(efficiency), denominator_status=VALUES(denominator_status),
  417. batch_id=VALUES(batch_id), update_time=@now",
  418. new SugarParameter("@tenantId", option.TargetTenantId),
  419. new SugarParameter("@factoryId", option.TargetFactoryId),
  420. new SugarParameter("@bizMonth", option.BizMonth),
  421. new SugarParameter("@sourceDomain", option.SourceZtid),
  422. new SugarParameter("@periodStart", option.MonthlyPeriodStart),
  423. new SugarParameter("@periodEnd", option.MonthlyPeriodEnd),
  424. new SugarParameter("@onlineQty", onlineQty),
  425. new SugarParameter("@headcount", headcount),
  426. new SugarParameter("@efficiency", efficiency),
  427. new SugarParameter("@denomStatus", denomStatus),
  428. new SugarParameter("@batchId", batchId),
  429. new SugarParameter("@now", now));
  430. sub.DwdRows = dwdAffected;
  431. // 月度 KPI 最终聚合交分发器;bizDate=月末、period=当月窗口;
  432. // legacyValue=efficiency、legacyDenom 保留 NO_HEADCOUNT/NO_NUMERATOR(CONFIG_SQL 下塌缩为 NO_DATA)。
  433. var dispatch = await _kpiCalcDispatcher.DispatchAsync(
  434. "S5_L1_003", ModuleCode, option.TargetTenantId, option.TargetFactoryId,
  435. option.MonthlyPeriodEnd, option.MonthlyPeriodStart, option.MonthlyPeriodEnd, option.SourceZtid,
  436. batchId, triggerType, efficiency, denomStatus, ct);
  437. sub.KpiRows = dispatch.ShouldUpsert
  438. ? await UpsertKpiValueAsync("S5_L1_003", option.MonthlyPeriodEnd, dispatch.MetricValue, now, option)
  439. : 0;
  440. sub.DenominatorStatus = dispatch.DenominatorStatus;
  441. // 调度:SUMMARY 成功/NO_DATA 后触发人效月度 DIMENSION 跑批(月度:@biz_date=月末派生 biz_month,与 SUMMARY 同月)。
  442. if (dispatch.ShouldUpsert)
  443. {
  444. try
  445. {
  446. await _dimensionRun.RunDimensionAsync(
  447. "S5_L1_003", ModuleCode, option.TargetTenantId, option.MonthlyPeriodEnd, batchId, triggerType, ct);
  448. }
  449. catch (Exception ex)
  450. {
  451. _logger.LogWarning(ex, "S5_L1_003 维度跑批异常(不影响汇总链路)");
  452. }
  453. }
  454. return sub;
  455. }
  456. /// <summary>S5_L1_004 品类物料库存周转 = D1/D2 × 30;D1=je3 月均库存金额,D2=je2 出库成本。</summary>
  457. private async Task<KpiBuildSubResult> BuildS5L1004MaterialInventoryTurnoverAsync(
  458. string batchId, DateTime now, S5MdpRefreshOption option, string triggerType, CancellationToken ct)
  459. {
  460. var sub = new KpiBuildSubResult();
  461. await _t8Inbound.TryMaterializeInventoryBalanceAsync(
  462. option.TargetTenantId, option.TargetFactoryId, option.SourceZtid,
  463. option.TvfPeriodEndYyyymm, batchId, now);
  464. const string sqlMonthly = @"
  465. select warehouse_code as ckcode, warehouse_name as ckname,
  466. item_code as code, item_code as cname,
  467. category_code as pcode, category_name as pname,
  468. avg_balance_amount as je3, issue_cost_amount as je2
  469. from mdp_std_inventory_balance_monthly
  470. where tenant_id=@tenantId and period_ym=@periodYm";
  471. var p = new[]
  472. {
  473. new SugarParameter("@tenantId", option.TargetTenantId),
  474. new SugarParameter("@periodYm", option.TvfPeriodEndYyyymm)
  475. };
  476. var tvfRows = await _db.Ado.SqlQueryAsync<S5InventoryTurnoverRow>(sqlMonthly, p);
  477. sub.T8Rows = tvfRows.Count;
  478. var dwdAffected = 0;
  479. var turnoverDaysList = new List<decimal>();
  480. foreach (var r in tvfRows)
  481. {
  482. ct.ThrowIfCancellationRequested();
  483. // 周转天数:D2=0 或 NULL 时 NULL,不伪装 0
  484. decimal? turnoverDays = (r.je2.HasValue && r.je2.Value > 0m && r.je3.HasValue)
  485. ? Math.Round(r.je3.Value / r.je2.Value * 30m, 4)
  486. : null;
  487. if (turnoverDays.HasValue) turnoverDaysList.Add(turnoverDays.Value);
  488. dwdAffected += await _db.Ado.ExecuteCommandAsync(@"
  489. INSERT INTO dwd_material_inventory_turnover
  490. (tenant_id, factory_id, biz_month, source_ztid, period_start_yyyymm, period_end_yyyymm,
  491. warehouse_code, warehouse_name, item_code, item_name, category_code, category_name,
  492. avg_inventory_value, monthly_outbound_cost, turnover_days, batch_id, create_time)
  493. VALUES
  494. (@tenantId, @factoryId, @bizMonth, @sourceDomain, @startYm, @endYm,
  495. @ckcode, @ckname, @itemCode, @itemName, @pcode, @pname,
  496. @je3, @je2, @turnoverDays, @batchId, @now)
  497. ON DUPLICATE KEY UPDATE
  498. warehouse_name=VALUES(warehouse_name), item_name=VALUES(item_name),
  499. category_code=VALUES(category_code), category_name=VALUES(category_name),
  500. avg_inventory_value=VALUES(avg_inventory_value),
  501. monthly_outbound_cost=VALUES(monthly_outbound_cost),
  502. turnover_days=VALUES(turnover_days),
  503. period_start_yyyymm=VALUES(period_start_yyyymm),
  504. period_end_yyyymm=VALUES(period_end_yyyymm),
  505. batch_id=VALUES(batch_id), update_time=@now",
  506. new SugarParameter("@tenantId", option.TargetTenantId),
  507. new SugarParameter("@factoryId", option.TargetFactoryId),
  508. new SugarParameter("@bizMonth", option.BizMonth),
  509. new SugarParameter("@sourceDomain", option.SourceZtid),
  510. new SugarParameter("@startYm", option.TvfPeriodStartYyyymm),
  511. new SugarParameter("@endYm", option.TvfPeriodEndYyyymm),
  512. new SugarParameter("@ckcode", r.ckcode ?? ""),
  513. new SugarParameter("@ckname", r.ckname),
  514. new SugarParameter("@itemCode", r.code ?? ""),
  515. new SugarParameter("@itemName", r.cname),
  516. new SugarParameter("@pcode", r.pcode),
  517. new SugarParameter("@pname", r.pname),
  518. new SugarParameter("@je3", r.je3),
  519. new SugarParameter("@je2", r.je2),
  520. new SugarParameter("@turnoverDays", turnoverDays),
  521. new SugarParameter("@batchId", batchId),
  522. new SugarParameter("@now", now));
  523. }
  524. sub.DwdRows = dwdAffected;
  525. // KPI 值:所有品类周转天数算术平均;无任一可计算品类时 NULL
  526. decimal? metricValue = turnoverDaysList.Count > 0
  527. ? Math.Round(turnoverDaysList.Average(), 4)
  528. : null;
  529. if (!metricValue.HasValue)
  530. {
  531. var warehouseFallback = await _db.Ado.SqlQuerySingleAsync<S5StageKpiRow>(
  532. WarehouseTurnoverSql("MAT_RECEIPT", "MAT_RECEIPT"),
  533. new SugarParameter("@TenantId", option.TargetTenantId),
  534. new SugarParameter("@FactoryId", option.TargetFactoryId),
  535. new SugarParameter("@MetricCode", "S5_L1_004"),
  536. new SugarParameter("@sourceDomain", option.SourceZtid),
  537. new SugarParameter("@sourceSystem", ""));
  538. metricValue = warehouseFallback?.MetricValue;
  539. }
  540. // S5_L1_004 保留 LEGACY_TVF:分发器 LEGACY_TVF 分支直接回传上面的 TVF 均值,
  541. // 不经 KpiSqlReadOnlyExecutor;仅统一 run-log 记录引擎状态(配置登记为 LEGACY_TVF)。
  542. // TVF(Rep_总账_存货_V3)/参数/口径/CommandTimeout 全不变,不建 CONFIG_SQL 版本。
  543. var legacyDenom = turnoverDaysList.Count > 0 ? "OK" : "NO_VALID_OUTBOUND_COST";
  544. var dispatch = await _kpiCalcDispatcher.DispatchAsync(
  545. "S5_L1_004", ModuleCode, option.TargetTenantId, option.TargetFactoryId,
  546. option.MonthlyPeriodEnd, option.MonthlyPeriodStart, option.MonthlyPeriodEnd, option.SourceZtid,
  547. batchId, triggerType, metricValue, legacyDenom, ct);
  548. sub.KpiRows = dispatch.ShouldUpsert
  549. ? await UpsertKpiValueAsync("S5_L1_004", option.MonthlyPeriodEnd, dispatch.MetricValue, now, option)
  550. : 0;
  551. sub.DenominatorStatus = dispatch.DenominatorStatus;
  552. // 调度:SUMMARY 成功/NO_DATA 后触发对应 DIMENSION 跑批(月度:DIMENSION_SQL 用 @biz_date 派生 biz_month,与 SUMMARY 同月)。
  553. if (dispatch.ShouldUpsert)
  554. {
  555. try
  556. {
  557. await _dimensionRun.RunDimensionAsync(
  558. "S5_L1_004", ModuleCode, option.TargetTenantId, option.MonthlyPeriodEnd, batchId, triggerType, ct);
  559. }
  560. catch (Exception ex)
  561. {
  562. _logger.LogWarning(ex, "S5_L1_004 维度跑批异常(不影响汇总链路)");
  563. }
  564. }
  565. return sub;
  566. }
  567. // ─────────────────────────────────────────────────────────────────────────
  568. // 写入 / 日志 封装
  569. // ─────────────────────────────────────────────────────────────────────────
  570. /// <summary>S5_L2_001 物料收货周期 = 实际收货日期 - 约定履约日期。</summary>
  571. private async Task<KpiBuildSubResult> BuildS5L2001ReceiptCycleAsync(
  572. string batchId, DateTime now, S5MdpRefreshOption option, string triggerType, CancellationToken ct)
  573. {
  574. const string sql = """
  575. SELECT ROUND(AVG(TIMESTAMPDIFF(HOUR, perform_date, rct_date) / 24), 4) AS MetricValue,
  576. COUNT(1) AS RowCount
  577. FROM (
  578. SELECT STR_TO_DATE(NULLIF(JSON_UNQUOTE(JSON_EXTRACT(raw_data,'$.PerformDate')), 'null'), '%Y-%m-%d %H:%i:%s.%f') AS perform_date,
  579. STR_TO_DATE(NULLIF(JSON_UNQUOTE(JSON_EXTRACT(raw_data,'$.RctDate')), 'null'), '%Y-%m-%d %H:%i:%s.%f') AS rct_date
  580. FROM mdp_stg_purchase_receipt
  581. WHERE tenant_id=@TenantId
  582. AND source_table='PurOrdRctMaster'
  583. ) t
  584. WHERE perform_date IS NOT NULL AND rct_date IS NOT NULL AND rct_date >= perform_date
  585. """;
  586. return await DispatchAverageAsync("S5_L2_001", sql, "NO_RECEIPT_CYCLE", batchId, now, option, triggerType, ct, L2ValueTable);
  587. }
  588. /// <summary>S5_L2_002 物料收货满足率 = 约定日期内完成收货的收货单占比。</summary>
  589. private async Task<KpiBuildSubResult> BuildS5L2002ReceiptFulfillmentAsync(
  590. string batchId, DateTime now, S5MdpRefreshOption option, string triggerType, CancellationToken ct)
  591. {
  592. const string sql = """
  593. SELECT ROUND(100 * SUM(CASE WHEN rct_date <= perform_date THEN 1 ELSE 0 END) / NULLIF(COUNT(1), 0), 4) AS MetricValue,
  594. COUNT(1) AS RowCount
  595. FROM (
  596. SELECT STR_TO_DATE(NULLIF(JSON_UNQUOTE(JSON_EXTRACT(raw_data,'$.PerformDate')), 'null'), '%Y-%m-%d %H:%i:%s.%f') AS perform_date,
  597. STR_TO_DATE(NULLIF(JSON_UNQUOTE(JSON_EXTRACT(raw_data,'$.RctDate')), 'null'), '%Y-%m-%d %H:%i:%s.%f') AS rct_date
  598. FROM mdp_stg_purchase_receipt
  599. WHERE tenant_id=@TenantId
  600. AND source_table='PurOrdRctMaster'
  601. ) t
  602. WHERE perform_date IS NOT NULL AND rct_date IS NOT NULL
  603. """;
  604. return await DispatchAverageAsync("S5_L2_002", sql, "NO_RECEIPT_FULFILLMENT", batchId, now, option, triggerType, ct, L2ValueTable);
  605. }
  606. /// <summary>S5_L2_003 物料检验周期 = 检验完成时间 - 检验开始时间。</summary>
  607. private async Task<KpiBuildSubResult> BuildS5L2003IqcCycleAsync(
  608. string batchId, DateTime now, S5MdpRefreshOption option, string triggerType, CancellationToken ct)
  609. {
  610. const string sql = """
  611. SELECT ROUND(AVG(TIMESTAMPDIFF(HOUR, FINSPESTARTDATE, FINSPEENDDATE) / 24), 4) AS MetricValue,
  612. COUNT(1) AS RowCount
  613. FROM qms_qcp_inspbill
  614. WHERE tenant_id=@TenantId
  615. AND FINSPESTARTDATE IS NOT NULL
  616. AND FINSPEENDDATE IS NOT NULL
  617. AND FINSPEENDDATE >= FINSPESTARTDATE
  618. """;
  619. return await DispatchAverageAsync("S5_L2_003", sql, "NO_IQC_CYCLE", batchId, now, option, triggerType, ct, L2ValueTable);
  620. }
  621. /// <summary>S5_L2_004 物料检验满足率 = 报检后 72 小时内完成检验的单据占比。</summary>
  622. private async Task<KpiBuildSubResult> BuildS5L2004IqcFulfillmentAsync(
  623. string batchId, DateTime now, S5MdpRefreshOption option, string triggerType, CancellationToken ct)
  624. {
  625. const string sql = """
  626. SELECT ROUND(100 * SUM(CASE WHEN FINSPEENDDATE IS NOT NULL
  627. AND TIMESTAMPDIFF(HOUR, FCREATETIME, FINSPEENDDATE) <= 72 THEN 1 ELSE 0 END)
  628. / NULLIF(COUNT(1), 0), 4) AS MetricValue,
  629. COUNT(1) AS RowCount
  630. FROM qms_qcp_inspbill
  631. WHERE tenant_id=@TenantId
  632. AND FCREATETIME IS NOT NULL
  633. """;
  634. return await DispatchAverageAsync("S5_L2_004", sql, "NO_IQC_FULFILLMENT", batchId, now, option, triggerType, ct, L2ValueTable);
  635. }
  636. private Task<KpiBuildSubResult> BuildS5WarehouseStageKpiAsync(
  637. string metricCode, string batchId, DateTime now, S5MdpRefreshOption option,
  638. string triggerType, CancellationToken ct)
  639. {
  640. var (sql, emptyDenom, table) = metricCode switch
  641. {
  642. "S5_L2_005" => (WarehouseCycleSql("MAT_IQC_RELEASE", "MAT_PUTAWAY"), "NO_MATERIAL_PUTAWAY_CYCLE", L2ValueTable),
  643. "S5_L2_006" => (WarehouseEfficiencySql("MAT_PUTAWAY"), "NO_MATERIAL_PUTAWAY_OPERATOR", L2ValueTable),
  644. "S5_L2_007" => (WarehouseTurnoverSql("MAT_PUTAWAY", "MAT_RECEIPT"), "NO_MATERIAL_RECEIPT_COST", L2ValueTable),
  645. "S5_L2_008" => (WarehouseCycleSql("MAT_PUTAWAY", "MAT_PICK"), "NO_MATERIAL_PICK_CYCLE", L2ValueTable),
  646. "S5_L2_009" => (WarehouseSatisfactionSql("MAT_PICK"), "NO_MATERIAL_PICK_REQUIRED_DATE", L2ValueTable),
  647. "S5_L2_010" => (WarehouseEfficiencySql("MAT_PICK"), "NO_MATERIAL_PICK_OPERATOR", L2ValueTable),
  648. "S5_L2_011" => (WarehouseTurnoverSql("MAT_PICK", "MAT_ISSUE"), "NO_MATERIAL_ISSUE_COST", L2ValueTable),
  649. "S5_L2_012" => (WarehouseCycleSql("MAT_ISSUE", "MAT_LINE"), "NO_MATERIAL_LINE_CYCLE", L2ValueTable),
  650. "S5_L2_013" => (WarehouseSatisfactionSql("MAT_LINE"), "NO_MATERIAL_LINE_REQUIRED_DATE", L2ValueTable),
  651. "S5_L2_014" => (WarehouseEfficiencySql("MAT_LINE"), "NO_MATERIAL_LINE_OPERATOR", L2ValueTable),
  652. "S5_L2_015" => (WarehouseTurnoverSql("MAT_LINE", "MAT_ISSUE"), "NO_MATERIAL_LINE_COST", L2ValueTable),
  653. "S5_L3_001" => (WarehouseEfficiencySql("MAT_RECEIPT"), "NO_MATERIAL_RECEIPT_OPERATOR", L3ValueTable),
  654. "S5_L3_002" => (WarehouseTurnoverSql("MAT_RECEIPT", "MAT_RECEIPT"), "NO_MATERIAL_RECEIPT_COST", L3ValueTable),
  655. "S5_L3_003" => (WarehouseEfficiencySql("MAT_IQC_RELEASE"), "NO_MATERIAL_IQC_OPERATOR", L3ValueTable),
  656. "S5_L3_004" => (WarehouseTurnoverSql("MAT_IQC_RELEASE", "MAT_RECEIPT"), "NO_MATERIAL_IQC_COST", L3ValueTable),
  657. "S5_L3_005" => (WarehouseSatisfactionSql("MAT_PUTAWAY"), "NO_MATERIAL_PUTAWAY_REQUIRED_DATE", L3ValueTable),
  658. _ => throw new ArgumentOutOfRangeException(nameof(metricCode), metricCode, "不支持的 S5 仓储阶段指标")
  659. };
  660. return DispatchAverageAsync(
  661. metricCode, sql, emptyDenom, batchId, now, option, triggerType, ct, table);
  662. }
  663. private static string WarehouseCycleSql(string fromStage, string toStage) =>
  664. $"""
  665. SELECT ROUND(AVG(TIMESTAMPDIFF(MINUTE,a.trans_time,b.trans_time)/1440),4) AS MetricValue,
  666. COUNT(*) AS RowCount
  667. FROM mdp_std_inv_trans a
  668. INNER JOIN mdp_std_inv_trans b
  669. ON b.tenant_id=a.tenant_id AND b.source_system=a.source_system
  670. AND b.item_num=a.item_num AND b.lot_serial=a.lot_serial
  671. AND b.trans_type='{toStage}'
  672. WHERE a.tenant_id=@TenantId AND a.trans_type='{fromStage}'
  673. AND (@sourceSystem='' OR a.source_system=@sourceSystem) AND (a.source_system<>'T8' OR a.domain=@sourceDomain)
  674. AND a.trans_time IS NOT NULL AND b.trans_time>=a.trans_time
  675. """;
  676. private static string WarehouseSatisfactionSql(string stage) =>
  677. $"""
  678. SELECT ROUND(100 * SUM(CASE WHEN trans_time<=eff_date THEN 1 ELSE 0 END)
  679. / NULLIF(COUNT(*),0),4) AS MetricValue,
  680. COUNT(*) AS RowCount
  681. FROM mdp_std_inv_trans
  682. WHERE tenant_id=@TenantId AND trans_type='{stage}'
  683. AND (@sourceSystem='' OR source_system=@sourceSystem) AND (source_system<>'T8' OR domain=@sourceDomain)
  684. AND trans_time IS NOT NULL AND eff_date IS NOT NULL
  685. """;
  686. private static string WarehouseEfficiencySql(string stage) =>
  687. $"""
  688. SELECT ROUND(COUNT(DISTINCT NULLIF(lot_serial,''))
  689. / NULLIF(COUNT(DISTINCT NULLIF(TRIM(create_user),'')),0),4) AS MetricValue,
  690. COUNT(*) AS RowCount
  691. FROM mdp_std_inv_trans
  692. WHERE tenant_id=@TenantId AND trans_type='{stage}'
  693. AND (@sourceSystem='' OR source_system=@sourceSystem) AND (source_system<>'T8' OR domain=@sourceDomain)
  694. """;
  695. private static string WarehouseTurnoverSql(string inventoryStage, string flowStage) =>
  696. $"""
  697. SELECT ROUND(
  698. 30 * SUM(CASE WHEN trans_type='{inventoryStage}'
  699. THEN IFNULL(end_balance,0)
  700. * IFNULL(CAST(NULLIF(dimension1,'') AS DECIMAL(18,4)),1)
  701. ELSE 0 END)
  702. / NULLIF(SUM(CASE WHEN trans_type='{flowStage}'
  703. THEN ABS(IFNULL(qty_change,0))
  704. * IFNULL(CAST(NULLIF(dimension1,'') AS DECIMAL(18,4)),1)
  705. ELSE 0 END),0),
  706. 4) AS MetricValue,
  707. SUM(CASE WHEN trans_type='{inventoryStage}' THEN 1 ELSE 0 END) AS RowCount
  708. FROM mdp_std_inv_trans
  709. WHERE tenant_id=@TenantId AND trans_type IN ('{inventoryStage}','{flowStage}')
  710. AND (@sourceSystem='' OR source_system=@sourceSystem) AND (source_system<>'T8' OR domain=@sourceDomain)
  711. """;
  712. private async Task<KpiBuildSubResult> DispatchAverageAsync(
  713. string metricCode, string sql, string emptyDenom,
  714. string batchId, DateTime now, S5MdpRefreshOption option, string triggerType, CancellationToken ct,
  715. string valueTable)
  716. {
  717. var row = await _db.Ado.SqlQuerySingleAsync<S5StageKpiRow>(sql,
  718. new SugarParameter("@TenantId", option.TargetTenantId),
  719. new SugarParameter("@sourceDomain", option.SourceZtid),
  720. new SugarParameter("@sourceSystem", ""));
  721. decimal? value = row?.RowCount > 0 ? row.MetricValue : null;
  722. var denom = value.HasValue ? "OK" : emptyDenom;
  723. var dispatch = await _kpiCalcDispatcher.DispatchAsync(
  724. metricCode, ModuleCode, option.TargetTenantId, option.TargetFactoryId,
  725. option.BizDate, option.BizDate, option.BizDate, option.SourceZtid,
  726. batchId, triggerType, value, denom, ct);
  727. var sub = new KpiBuildSubResult
  728. {
  729. T8Rows = row?.RowCount ?? 0,
  730. KpiRows = dispatch.ShouldUpsert
  731. ? await UpsertKpiValueAsync(metricCode, option.BizDate, dispatch.MetricValue, now, option, valueTable)
  732. : 0,
  733. DenominatorStatus = dispatch.DenominatorStatus
  734. };
  735. try
  736. {
  737. if (dispatch.ShouldUpsert)
  738. {
  739. await _dimensionRun.RunDimensionAsync(
  740. metricCode, ModuleCode, option.TargetTenantId, option.BizDate, batchId, triggerType, ct);
  741. }
  742. }
  743. catch (Exception ex)
  744. {
  745. _logger.LogWarning(ex, "{MetricCode} 维度跑批异常(不影响汇总链路)", metricCode);
  746. }
  747. return sub;
  748. }
  749. private async Task<int> UpsertKpiValueAsync(string metricCode, DateTime bizDate, decimal? metricValue, DateTime now, S5MdpRefreshOption option, string valueTable = "ado_s9_kpi_value_l1_day")
  750. {
  751. // 沿用 S3 UpsertS3KpiValueAsync 范式:先查现存行 → UPDATE;不存在 → SELECT MAX(id)+1 显式生成 id 后 INSERT。
  752. // ado_s9_kpi_value_l1_day.id 为手工分配主键(无 AUTO_INCREMENT),必须显式 set;
  753. // metric_value 允许 NULL(分母缺失不得伪装真实 0)。
  754. // FIX-2:截断时分秒(月度 KPI 入参可能为 YYYY-MM-DD 23:59:59),保证 SELECT WHERE biz_date=@BizDate 与 DB date 列匹配,避免重复 INSERT。
  755. // FIX-1:tenant_id/factory_id 取自 option,默认仍为 1300000000001/1,不破坏 Demo。
  756. bizDate = bizDate.Date;
  757. var snap = await _kpiTargetResolver.ResolveAsync(option.TargetTenantId, option.TargetFactoryId, metricCode, ModuleCode, bizDate);
  758. var meta = await _db.Ado.SqlQuerySingleAsync<dynamic>(
  759. "SELECT Direction, YellowThreshold, RedThreshold FROM ado_smart_ops_kpi_master WHERE TenantId=@TenantId AND MetricCode=@MetricCode AND IsEnabled=1 LIMIT 1",
  760. new SugarParameter("@TenantId", option.TargetTenantId),
  761. new SugarParameter("@MetricCode", metricCode));
  762. var status = AidopS4KpiMerge.AchievementLevel(
  763. metricValue,
  764. snap.TargetValue,
  765. (string?)meta?.Direction ?? "higher_is_better",
  766. (decimal?)meta?.YellowThreshold,
  767. (decimal?)meta?.RedThreshold);
  768. var existingId = await _db.Ado.GetLongAsync(
  769. $"SELECT IFNULL((SELECT id FROM {valueTable} WHERE tenant_id=@TenantId AND factory_id=@FactoryId " +
  770. "AND module_code=@ModuleCode AND metric_code=@MetricCode AND biz_date=@BizDate AND is_deleted=0 " +
  771. "ORDER BY id LIMIT 1), 0)",
  772. new List<SugarParameter>
  773. {
  774. new("@TenantId", option.TargetTenantId),
  775. new("@FactoryId", option.TargetFactoryId),
  776. new("@ModuleCode", ModuleCode),
  777. new("@MetricCode", metricCode),
  778. new("@BizDate", bizDate)
  779. });
  780. if (existingId > 0)
  781. {
  782. return await _db.Ado.ExecuteCommandAsync(
  783. $"UPDATE {valueTable} SET metric_value=@MetricValue, target_value=@TargetValue, status_color=@StatusColor, " +
  784. "target_config_id=@TargetConfigId, target_source=@TargetSource, target_resolved_at=@TargetResolvedAt, " +
  785. "calc_time=@Now, update_time=@Now, is_deleted=0, is_active=1 WHERE id=@Id",
  786. new SugarParameter("@MetricValue", metricValue),
  787. new SugarParameter("@TargetValue", KpiTargetSnapshotSql.ValueOrDbNull(snap)),
  788. new SugarParameter("@StatusColor", status),
  789. new SugarParameter("@TargetConfigId", KpiTargetSnapshotSql.ConfigIdOrDbNull(snap)),
  790. new SugarParameter("@TargetSource", KpiTargetSnapshotSql.SourceOrDbNull(snap)),
  791. new SugarParameter("@TargetResolvedAt", snap.ResolvedAt),
  792. new SugarParameter("@Now", now),
  793. new SugarParameter("@Id", existingId));
  794. }
  795. var nextId = Yitter.IdGenerator.YitIdHelper.NextId();
  796. return await _db.Ado.ExecuteCommandAsync($@"
  797. INSERT INTO {valueTable}
  798. (id, tenant_id, org_id, company_id, factory_id, status, biz_date,
  799. create_time, update_time, is_deleted, is_active,
  800. module_code, metric_code, metric_value, target_value, status_color, calc_time,
  801. target_config_id, target_source, target_resolved_at)
  802. VALUES
  803. (@Id, @TenantId, NULL, NULL, @FactoryId, NULL, @BizDate,
  804. @Now, @Now, 0, 1,
  805. @ModuleCode, @MetricCode, @MetricValue, @TargetValue, @StatusColor, @Now,
  806. @TargetConfigId, @TargetSource, @TargetResolvedAt)",
  807. new SugarParameter("@Id", nextId),
  808. new SugarParameter("@TenantId", option.TargetTenantId),
  809. new SugarParameter("@FactoryId", option.TargetFactoryId),
  810. new SugarParameter("@BizDate", bizDate),
  811. new SugarParameter("@Now", now),
  812. new SugarParameter("@ModuleCode", ModuleCode),
  813. new SugarParameter("@MetricCode", metricCode),
  814. new SugarParameter("@MetricValue", metricValue),
  815. new SugarParameter("@TargetValue", KpiTargetSnapshotSql.ValueOrDbNull(snap)),
  816. new SugarParameter("@StatusColor", status),
  817. new SugarParameter("@TargetConfigId", KpiTargetSnapshotSql.ConfigIdOrDbNull(snap)),
  818. new SugarParameter("@TargetSource", KpiTargetSnapshotSql.SourceOrDbNull(snap)),
  819. new SugarParameter("@TargetResolvedAt", snap.ResolvedAt));
  820. }
  821. private async Task<long> InsertTransformRunLogAsync(string batchId, DateTime startedAt, string triggerType, S5MdpRefreshOption option)
  822. {
  823. await _db.Ado.ExecuteCommandAsync(@"
  824. INSERT INTO mdp_transform_run_log
  825. (tenant_id, job_code, job_name, trigger_type, batch_id, status, start_time, stage_rows, standard_rows, dwd_rows, create_time, update_time)
  826. VALUES
  827. (@TenantId, @JobCode, @JobName, @TriggerType, @BatchId, 'RUNNING', @StartTime, 0, 0, 0, @StartTime, @StartTime)",
  828. new SugarParameter("@TenantId", option.TargetTenantId),
  829. new SugarParameter("@JobCode", JobCode),
  830. new SugarParameter("@JobName", JobName),
  831. new SugarParameter("@TriggerType", triggerType),
  832. new SugarParameter("@BatchId", batchId),
  833. new SugarParameter("@StartTime", startedAt));
  834. return await _db.Ado.GetLongAsync(
  835. "SELECT id FROM mdp_transform_run_log WHERE batch_id=@BatchId ORDER BY id DESC LIMIT 1",
  836. new List<SugarParameter> { new("@BatchId", batchId) });
  837. }
  838. private async Task MarkTransformRunSuccessAsync(long runLogId, DateTime startedAt, S5MdpSyncTransformResult result)
  839. {
  840. var finishedAt = DateTime.Now;
  841. await _db.Ado.ExecuteCommandAsync(@"
  842. UPDATE mdp_transform_run_log
  843. SET status='SUCCESS', end_time=@EndTime, duration_ms=@DurationMs,
  844. stage_rows=@StageRows, standard_rows=@StandardRows, dwd_rows=@DwdRows,
  845. summary_json=@SummaryJson, update_time=CURRENT_TIMESTAMP
  846. WHERE id=@Id",
  847. new SugarParameter("@EndTime", finishedAt),
  848. new SugarParameter("@DurationMs", (int)(finishedAt - startedAt).TotalMilliseconds),
  849. new SugarParameter("@StageRows", result.StageRows),
  850. new SugarParameter("@StandardRows", result.StandardRows),
  851. new SugarParameter("@DwdRows", result.DwdRows),
  852. new SugarParameter("@SummaryJson", BuildRunSummaryJson(result)),
  853. new SugarParameter("@Id", runLogId));
  854. }
  855. private async Task MarkTransformRunFailedAsync(long runLogId, DateTime startedAt, string message, string batchId)
  856. {
  857. bool runLogUpdated = false;
  858. try
  859. {
  860. var finishedAt = DateTime.Now;
  861. await _db.Ado.ExecuteCommandAsync(@"
  862. UPDATE mdp_transform_run_log
  863. SET status='FAILED', end_time=@EndTime, duration_ms=@DurationMs,
  864. error_message=@ErrorMessage, update_time=CURRENT_TIMESTAMP
  865. WHERE id=@Id",
  866. new SugarParameter("@EndTime", finishedAt),
  867. new SugarParameter("@DurationMs", (int)(finishedAt - startedAt).TotalMilliseconds),
  868. new SugarParameter("@ErrorMessage", Truncate(message, 2000)),
  869. new SugarParameter("@Id", runLogId));
  870. runLogUpdated = true;
  871. }
  872. catch (Exception ex)
  873. {
  874. // 写库本身失败兜底:远端 MySQL 瞬断导致 MarkFailed 自身也连不上
  875. Console.Error.WriteLine($"[S5MdpSyncTransform] MarkTransformRunFailed write failed (runLogId={runLogId}): {ex.Message}");
  876. }
  877. // FAILURE-NOTIFICATION-1:写库 FAILED 成功后发通知给超级管理员;通知失败不影响主流程
  878. if (!runLogUpdated) return;
  879. try
  880. {
  881. await _sysNoticeService.AddNotice(new AddNoticeInput
  882. {
  883. Title = "S5 物料仓储 T8 KPI 跑批失败",
  884. Content = $"模块:S5 物料仓储\n批次ID:{batchId}\n失败时间:{DateTime.Now:yyyy-MM-dd HH:mm:ss}\n错误信息:{Truncate(message, 1000)}\n\n请查看 mdp_transform_run_log 获取完整错误与重试记录。",
  885. Type = NoticeTypeEnum.NOTICE,
  886. PublicTime = DateTime.Now,
  887. Status = NoticeStatusEnum.PUBLIC,
  888. PublicUserId = NoticeReceiverUserId,
  889. PublicUserName = NoticeReceiverUserName
  890. });
  891. }
  892. catch (Exception notifyEx)
  893. {
  894. _logger.LogError(notifyEx, "[S5MdpSyncTransform] SysNotice 发送失败 (runLogId={RunLogId}, batchId={BatchId})", runLogId, batchId);
  895. }
  896. }
  897. private static string BuildRunSummaryJson(S5MdpSyncTransformResult r)
  898. {
  899. var summary = new
  900. {
  901. batchId = r.BatchId,
  902. sourceZtid = r.SourceZtid,
  903. bizDate = r.BizDate.ToString("yyyy-MM-dd"),
  904. bizMonth = r.BizMonth,
  905. triggerType = r.TriggerType,
  906. dwdRows = r.DwdRows,
  907. kpiRows = r.KpiRows,
  908. perKpiDwdRows = r.PerKpiDwdRows,
  909. perKpiKpiRows = r.PerKpiKpiRows,
  910. denominatorStatus = r.KpiDenominatorStatus,
  911. tvfPeriod = $"{r.MonthlyPeriodStart:yyyy-MM-dd}~{r.MonthlyPeriodEnd:yyyy-MM-dd}"
  912. };
  913. return JsonSerializer.Serialize(summary);
  914. }
  915. private static string NormalizeTriggerType(string s) =>
  916. string.IsNullOrWhiteSpace(s) ? "AUTO" : s.Trim().ToUpperInvariant();
  917. private static void NormalizeOption(S5MdpRefreshOption option)
  918. {
  919. var d = S5MdpRefreshOption.Default();
  920. if (option.TargetFactoryId <= 0) option.TargetFactoryId = d.TargetFactoryId;
  921. if (string.IsNullOrWhiteSpace(option.SourceZtid)) option.SourceZtid = d.SourceZtid;
  922. // 目标租户由源账套映射决定,禁止固定默认/兜底
  923. option.TargetTenantId = AidopSourceTenantMap.ResolveTenantId(option.SourceZtid, option.TargetTenantId);
  924. if (option.BizDate == default) option.BizDate = d.BizDate;
  925. if (string.IsNullOrWhiteSpace(option.BizMonth)) option.BizMonth = d.BizMonth;
  926. if (option.DailyPeriodStart == default) option.DailyPeriodStart = d.DailyPeriodStart;
  927. if (option.DailyPeriodEnd == default) option.DailyPeriodEnd = d.DailyPeriodEnd;
  928. if (option.MonthlyPeriodStart == default) option.MonthlyPeriodStart = d.MonthlyPeriodStart;
  929. if (option.MonthlyPeriodEnd == default) option.MonthlyPeriodEnd = d.MonthlyPeriodEnd;
  930. if (string.IsNullOrWhiteSpace(option.TvfPeriodStartYyyymm)) option.TvfPeriodStartYyyymm = d.TvfPeriodStartYyyymm;
  931. if (string.IsNullOrWhiteSpace(option.TvfPeriodEndYyyymm)) option.TvfPeriodEndYyyymm = d.TvfPeriodEndYyyymm;
  932. }
  933. private static string Truncate(string s, int max) =>
  934. string.IsNullOrEmpty(s) ? "" : (s.Length <= max ? s : s.Substring(0, max));
  935. }
  936. // ─────────────────────────────────────────────────────────────────────────────
  937. // Refresh 入参与结果 DTO
  938. // ─────────────────────────────────────────────────────────────────────────────
  939. public sealed class S5MdpRefreshOption
  940. {
  941. /// <summary>源账套编码,实测当前唯一值为 pbxfxp。</summary>
  942. public string SourceZtid { get; set; } = "pbxfxp";
  943. /// <summary>KPI/DWD 落库目标租户;≤0 时由 <see cref="AidopSourceTenantMap"/> 按 SourceZtid 解析。</summary>
  944. public long TargetTenantId { get; set; }
  945. /// <summary>KPI/DWD 落库目标工厂;默认 1。</summary>
  946. public long TargetFactoryId { get; set; } = 1L;
  947. /// <summary>日 T+1 KPI 的业务日期(默认昨天)。</summary>
  948. public DateTime BizDate { get; set; }
  949. /// <summary>月 M+1 KPI 的业务月 YYYY-MM(默认上月)。</summary>
  950. public string BizMonth { get; set; } = "";
  951. /// <summary>日 T+1 KPI 区间起(含),默认昨天 00:00。</summary>
  952. public DateTime DailyPeriodStart { get; set; }
  953. /// <summary>日 T+1 KPI 区间止(含),默认昨天 23:59:59。</summary>
  954. public DateTime DailyPeriodEnd { get; set; }
  955. /// <summary>月 M+1 KPI 区间起(含),默认上月 1 日。</summary>
  956. public DateTime MonthlyPeriodStart { get; set; }
  957. /// <summary>月 M+1 KPI 区间止(含),默认上月末日。</summary>
  958. public DateTime MonthlyPeriodEnd { get; set; }
  959. /// <summary>TVF Rep_总账_存货_V3 入参起期 YYYYMM。</summary>
  960. public string TvfPeriodStartYyyymm { get; set; } = "";
  961. /// <summary>TVF Rep_总账_存货_V3 入参止期 YYYYMM。</summary>
  962. public string TvfPeriodEndYyyymm { get; set; } = "";
  963. public static S5MdpRefreshOption Default()
  964. {
  965. var today = DateTime.Today;
  966. var yesterday = today.AddDays(-1);
  967. var lastMonth = today.AddMonths(-1);
  968. var monthStart = new DateTime(lastMonth.Year, lastMonth.Month, 1);
  969. var monthEnd = monthStart.AddMonths(1).AddDays(-1);
  970. return new S5MdpRefreshOption
  971. {
  972. SourceZtid = "pbxfxp",
  973. TargetTenantId = 0,
  974. TargetFactoryId = 1L,
  975. BizDate = yesterday,
  976. BizMonth = lastMonth.ToString("yyyy-MM"),
  977. DailyPeriodStart = yesterday,
  978. DailyPeriodEnd = yesterday.AddDays(1).AddSeconds(-1),
  979. MonthlyPeriodStart = monthStart,
  980. MonthlyPeriodEnd = monthEnd.AddDays(1).AddSeconds(-1),
  981. TvfPeriodStartYyyymm = monthStart.ToString("yyyyMM"),
  982. TvfPeriodEndYyyymm = monthEnd.ToString("yyyyMM")
  983. };
  984. }
  985. }
  986. public sealed class S5MdpSyncTransformResult
  987. {
  988. public string BatchId { get; set; } = "";
  989. public long RunLogId { get; set; }
  990. public string TriggerType { get; set; } = "AUTO";
  991. public string SourceZtid { get; set; } = "";
  992. public long TargetTenantId { get; set; }
  993. public long TargetFactoryId { get; set; }
  994. public DateTime BizDate { get; set; }
  995. public string BizMonth { get; set; } = "";
  996. public DateTime DailyPeriodStart { get; set; }
  997. public DateTime DailyPeriodEnd { get; set; }
  998. public DateTime MonthlyPeriodStart { get; set; }
  999. public DateTime MonthlyPeriodEnd { get; set; }
  1000. public int StageRows { get; set; }
  1001. public int StandardRows { get; set; }
  1002. public int DwdRows { get; set; }
  1003. public int KpiRows { get; set; }
  1004. public Dictionary<string, int> PerKpiDwdRows { get; } = new();
  1005. public Dictionary<string, int> PerKpiKpiRows { get; } = new();
  1006. public List<string> KpiDenominatorStatus { get; } = new();
  1007. public void MergeSub(string kpiCode, KpiBuildSubResult sub)
  1008. {
  1009. PerKpiDwdRows[kpiCode] = sub.DwdRows;
  1010. PerKpiKpiRows[kpiCode] = sub.KpiRows;
  1011. DwdRows += sub.DwdRows;
  1012. KpiRows += sub.KpiRows;
  1013. KpiDenominatorStatus.Add($"{kpiCode}:{sub.DenominatorStatus}");
  1014. }
  1015. }
  1016. public sealed class KpiBuildSubResult
  1017. {
  1018. public int T8Rows { get; set; }
  1019. public int DwdRows { get; set; }
  1020. public int KpiRows { get; set; }
  1021. public string DenominatorStatus { get; set; } = "OK";
  1022. }
  1023. // ─────────────────────────────────────────────────────────────────────────────
  1024. // T8 result set 投影类型(与方老师 SQL SELECT 列名严格一致;SqlSugar 映射)
  1025. // ─────────────────────────────────────────────────────────────────────────────
  1026. internal sealed class S5OnlineCycleRow
  1027. {
  1028. public string? item_code { get; set; }
  1029. public DateTime? approved_time { get; set; }
  1030. }
  1031. internal sealed class S5FulfillmentNumerRow
  1032. {
  1033. public string? task_no { get; set; }
  1034. public int codenum { get; set; }
  1035. }
  1036. internal sealed class S5FulfillmentDenomRow
  1037. {
  1038. public string? order_no { get; set; }
  1039. public int listnum { get; set; }
  1040. }
  1041. internal sealed class S5SumQtyRow
  1042. {
  1043. public decimal? qty_change { get; set; }
  1044. }
  1045. internal sealed class S5CountRow
  1046. {
  1047. public int penum { get; set; }
  1048. }
  1049. internal sealed class S5StageKpiRow
  1050. {
  1051. public decimal? MetricValue { get; set; }
  1052. public int RowCount { get; set; }
  1053. }
  1054. internal sealed class S5InventoryTurnoverRow
  1055. {
  1056. public string? ckcode { get; set; }
  1057. public string? ckname { get; set; }
  1058. public string? code { get; set; }
  1059. public string? cname { get; set; }
  1060. public string? pcode { get; set; }
  1061. public string? pname { get; set; }
  1062. public decimal? je3 { get; set; }
  1063. public decimal? je2 { get; set; }
  1064. }