S5MdpSyncTransformService.cs 60 KB

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