S5MdpSyncTransformService.cs 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764
  1. using Admin.NET.Core.Service;
  2. using Admin.NET.Plugin.AiDOP.Infrastructure;
  3. using Microsoft.Extensions.Logging;
  4. using System.Text.Json;
  5. namespace Admin.NET.Plugin.AiDOP.MaterialWarehouse;
  6. /// <summary>
  7. /// S5 物料仓储 — KPI 计算与刷新转换服务。边界:8 迁 1 留。
  8. /// 已中台化(读本地标准层 mdp_std_t8_*,由 T8BaseInboundMdpSyncService 从 T8 贴源→标准):
  9. /// S5_L1_001 物料上线周期 / S5_L1_002 物料上线满足率(依赖 Cj_Bg_Head_Rep + kc_dd_list_cllist,已贴源)/ S5_L1_003 物料仓储人效。
  10. /// 仍 legacy 直连 T8(QueryT8Async,ConfigId=t8_v5):
  11. /// S5_L1_004 品类物料库存周转(依赖 T8 TVF Rep_总账_存货_V3,报表聚合结果无逐行主键,暂不中台化)。
  12. /// 结果统一落 dwd_t8_* 与 ado_s9_kpi_value_l1_day。计算口径沿用方老师 v5.4 KPI J 列。
  13. /// </summary>
  14. public class S5MdpSyncTransformService : ITransient
  15. {
  16. private readonly ISqlSugarClient _db;
  17. private readonly SysNoticeService _sysNoticeService;
  18. private readonly ILogger<S5MdpSyncTransformService> _logger;
  19. private const string JobCode = "S5_MDP_SYNC_TRANSFORM";
  20. private const string JobName = "S5 物料仓储 MDP 同步与转换";
  21. private const string T8ConfigId = "t8_v5";
  22. // 加固:legacy 直连 T8 查询的命令超时上限(秒)。防止 T8 TVF(Rep_总账_存货_V3) 慢或挂起时
  23. // 查询无限期阻塞、进而长时间持有刷新锁(见 AidopT8KpiManualRefreshService)。
  24. // 仅加超时护栏,不改 S5_L1_004 的 SQL / 参数 / 计算口径。(S5_L1_002 已中台化,不再走 QueryT8Async。)
  25. private const int T8CommandTimeoutSeconds = 60;
  26. private const string ModuleCode = "S5";
  27. // FAILURE-NOTIFICATION-1:超级管理员 superAdmin.NET(AccountType=999)
  28. private const long NoticeReceiverUserId = 1300000000101L;
  29. private const string NoticeReceiverUserName = "超级管理员";
  30. private readonly SmartOps.KpiCalcDispatcher _kpiCalcDispatcher;
  31. public S5MdpSyncTransformService(
  32. ISqlSugarClient db,
  33. SysNoticeService sysNoticeService,
  34. ILogger<S5MdpSyncTransformService> logger,
  35. SmartOps.KpiCalcDispatcher kpiCalcDispatcher)
  36. {
  37. _db = db;
  38. _sysNoticeService = sysNoticeService;
  39. _logger = logger;
  40. _kpiCalcDispatcher = kpiCalcDispatcher;
  41. }
  42. public async Task<S5MdpSyncTransformResult> RunFullAsync(
  43. CancellationToken cancellationToken = default,
  44. string triggerType = "AUTO",
  45. S5MdpRefreshOption? option = null)
  46. {
  47. cancellationToken.ThrowIfCancellationRequested();
  48. option ??= S5MdpRefreshOption.Default();
  49. NormalizeOption(option);
  50. var now = DateTime.Now;
  51. var batchId = $"S5_MDP_FULL_{now:yyyyMMddHHmmss}";
  52. var normalizedTrigger = NormalizeTriggerType(triggerType);
  53. var runLogId = await InsertTransformRunLogAsync(batchId, now, normalizedTrigger, option);
  54. var result = new S5MdpSyncTransformResult
  55. {
  56. BatchId = batchId,
  57. RunLogId = runLogId,
  58. TriggerType = normalizedTrigger,
  59. SourceZtid = option.SourceZtid,
  60. TargetTenantId = option.TargetTenantId,
  61. TargetFactoryId = option.TargetFactoryId,
  62. BizDate = option.BizDate,
  63. BizMonth = option.BizMonth,
  64. DailyPeriodStart = option.DailyPeriodStart,
  65. DailyPeriodEnd = option.DailyPeriodEnd,
  66. MonthlyPeriodStart = option.MonthlyPeriodStart,
  67. MonthlyPeriodEnd = option.MonthlyPeriodEnd
  68. };
  69. try
  70. {
  71. // 路径 A:直发 T8 SQL,不做 stg / std 中间层
  72. result.StageRows = 0;
  73. result.StandardRows = 0;
  74. var sub16 = await BuildS5L1001MaterialOnlineCycleAsync(batchId, now, option, normalizedTrigger, cancellationToken);
  75. result.MergeSub("S5_L1_001", sub16);
  76. var sub17 = await BuildS5L1002MaterialOnlineFulfillmentAsync(batchId, now, option, cancellationToken);
  77. result.MergeSub("S5_L1_002", sub17);
  78. var sub18 = await BuildS5L1003MaterialWarehouseEfficiencyAsync(batchId, now, option, cancellationToken);
  79. result.MergeSub("S5_L1_003", sub18);
  80. var sub19 = await BuildS5L1004MaterialInventoryTurnoverAsync(batchId, now, option, cancellationToken);
  81. result.MergeSub("S5_L1_004", sub19);
  82. await MarkTransformRunSuccessAsync(runLogId, now, result);
  83. return result;
  84. }
  85. catch (Exception ex)
  86. {
  87. await MarkTransformRunFailedAsync(runLogId, now, ex.Message, batchId);
  88. throw;
  89. }
  90. }
  91. // ─────────────────────────────────────────────────────────────────────────
  92. // KPI 实现(方老师 v5.4 KPI J 列 SQL 原逻辑直发 T8)
  93. // ─────────────────────────────────────────────────────────────────────────
  94. /// <summary>S5_L1_001 物料上线周期 = 配送到产线日期(lbs=生产领料) - 收货日期(lbs=采购入库)。
  95. /// 数据准备(写 dwd 明细)始终执行;最终 KPI 聚合由计算配置分发器接管(LEGACY_CODE/CONFIG_SQL)。</summary>
  96. private async Task<KpiBuildSubResult> BuildS5L1001MaterialOnlineCycleAsync(
  97. string batchId, DateTime now, S5MdpRefreshOption option, string triggerType, CancellationToken ct)
  98. {
  99. var sub = new KpiBuildSubResult();
  100. // 双模式:读本地标准层 mdp_std_t8_*(源 identity Id→src_id),语义等价于原直发 T8 SQL。
  101. const string sqlOnline = @"
  102. select b.code as code, min(a.shtime) as shtime
  103. from mdp_std_t8_kc_tz_head a
  104. inner join mdp_std_t8_kc_tz_list b on a.src_id=b.idid
  105. where a.ztid=@ztid and a.lbs='生产领料' and a.hzyn=0 and a.zfyn=0 and a.shyn=1
  106. group by b.code";
  107. const string sqlReceipt = @"
  108. select b.code as code, min(a.shtime) as shtime
  109. from mdp_std_t8_kc_tz_head a
  110. inner join mdp_std_t8_kc_tz_list b on a.src_id=b.idid
  111. where a.ztid=@ztid and a.lbs='采购入库' and a.hzyn=0 and a.zfyn=0 and a.shyn=1
  112. group by b.code";
  113. var p = new[] { new SugarParameter("@ztid", option.SourceZtid) };
  114. var onlineRows = await _db.Ado.SqlQueryAsync<S5OnlineCycleRow>(sqlOnline, p);
  115. var receiptRows = await _db.Ado.SqlQueryAsync<S5OnlineCycleRow>(sqlReceipt, p);
  116. sub.T8Rows = onlineRows.Count + receiptRows.Count;
  117. var onlineByCode = onlineRows.Where(r => !string.IsNullOrEmpty(r.code))
  118. .ToDictionary(r => r.code!, r => r.shtime, StringComparer.OrdinalIgnoreCase);
  119. var receiptByCode = receiptRows.Where(r => !string.IsNullOrEmpty(r.code))
  120. .ToDictionary(r => r.code!, r => r.shtime, StringComparer.OrdinalIgnoreCase);
  121. var allCodes = new HashSet<string>(onlineByCode.Keys, StringComparer.OrdinalIgnoreCase);
  122. allCodes.UnionWith(receiptByCode.Keys);
  123. var dwdAffected = 0;
  124. var cycleDaysList = new List<int>();
  125. foreach (var code in allCodes)
  126. {
  127. ct.ThrowIfCancellationRequested();
  128. var online = onlineByCode.GetValueOrDefault(code);
  129. var receipt = receiptByCode.GetValueOrDefault(code);
  130. int? cycleDays = null;
  131. if (online.HasValue && receipt.HasValue)
  132. {
  133. cycleDays = (int)(online.Value.Date - receipt.Value.Date).TotalDays;
  134. cycleDaysList.Add(cycleDays.Value);
  135. }
  136. dwdAffected += await _db.Ado.ExecuteCommandAsync(@"
  137. INSERT INTO dwd_t8_material_online_cycle
  138. (tenant_id, factory_id, biz_date, source_ztid, item_code, online_date, receipt_date, cycle_days, batch_id, create_time)
  139. VALUES
  140. (@tenantId, @factoryId, @bizDate, @ztid, @itemCode, @online, @receipt, @cycleDays, @batchId, @now)
  141. ON DUPLICATE KEY UPDATE
  142. online_date=VALUES(online_date), receipt_date=VALUES(receipt_date),
  143. cycle_days=VALUES(cycle_days), batch_id=VALUES(batch_id), update_time=@now",
  144. new SugarParameter("@tenantId", option.TargetTenantId),
  145. new SugarParameter("@factoryId", option.TargetFactoryId),
  146. new SugarParameter("@bizDate", option.BizDate),
  147. new SugarParameter("@ztid", option.SourceZtid),
  148. new SugarParameter("@itemCode", code),
  149. new SugarParameter("@online", online),
  150. new SugarParameter("@receipt", receipt),
  151. new SugarParameter("@cycleDays", cycleDays),
  152. new SugarParameter("@batchId", batchId),
  153. new SugarParameter("@now", now));
  154. }
  155. sub.DwdRows = dwdAffected;
  156. // 数据准备(dwd 明细)已完成。最终 KPI 聚合交计算配置分发器:
  157. // 无配置/LEGACY_CODE → 用下面 legacy 均值;CONFIG_SQL → 执行已发布只读 SQL;
  158. // CONFIG_SQL 失败不 fallback、不写值、保留上一成功值(ShouldUpsert=false)。
  159. decimal? legacyValue = cycleDaysList.Count > 0 ? (decimal)cycleDaysList.Average() : null;
  160. var legacyDenom = cycleDaysList.Count > 0 ? "OK" : "NO_NUMERATOR";
  161. var dispatch = await _kpiCalcDispatcher.DispatchAsync(
  162. "S5_L1_001", ModuleCode, option.TargetTenantId, option.TargetFactoryId,
  163. option.BizDate, option.DailyPeriodStart, option.DailyPeriodEnd, option.SourceZtid,
  164. batchId, triggerType, legacyValue, legacyDenom, ct);
  165. sub.KpiRows = dispatch.ShouldUpsert
  166. ? await UpsertKpiValueAsync("S5_L1_001", option.BizDate, dispatch.MetricValue, now, option)
  167. : 0;
  168. sub.DenominatorStatus = dispatch.DenominatorStatus;
  169. return sub;
  170. }
  171. /// <summary>S5_L1_002 物料上线满足率 = 开工日期前完成上线行数 / 工单物料总行数。</summary>
  172. private async Task<KpiBuildSubResult> BuildS5L1002MaterialOnlineFulfillmentAsync(
  173. string batchId, DateTime now, S5MdpRefreshOption option, CancellationToken ct)
  174. {
  175. var sub = new KpiBuildSubResult();
  176. // 中台化:读本地标准层 mdp_std_t8_*(kc_tz_head.Id→src_id、Cj_Bg_Head_Rep/kc_dd_list_cllist 已贴源),
  177. // 口径与原直发 T8 SQL 逐项等价(分子/分母/JOIN/去重/shtime<=kgdate/count 均不变),不再直连 t8_v5。
  178. const string sqlNumer = @"
  179. select lynoid as lynoid, count(*) as codenum
  180. from (
  181. select h.lynoid as lynoid, l.code as code
  182. from mdp_std_t8_kc_tz_head h
  183. inner join mdp_std_t8_kc_tz_list l on h.src_id=l.idid
  184. left join (
  185. select noid as noid, min(kgdate) as kgdate
  186. from mdp_std_t8_cj_bg_head_rep
  187. where ztid=@ztid group by noid
  188. ) c on h.lynoid=c.noid
  189. where h.ztid=@ztid and h.lbs='生产领料' and h.hzyn=0 and h.zfyn=0 and h.shyn=1 and h.shtime<=c.kgdate
  190. group by h.lynoid, l.code
  191. ) n
  192. group by lynoid";
  193. const string sqlDenom = @"
  194. select h.noid as noid, count(l.src_id) as listnum
  195. from mdp_std_t8_kc_dd_head h
  196. left join mdp_std_t8_kc_dd_list_cllist l on h.src_id=l.idid
  197. where h.ztid=@ztid and h.lbs='生产任务' and h.zf=0 and h.shyn=1
  198. group by h.noid";
  199. var p = new[] { new SugarParameter("@ztid", option.SourceZtid) };
  200. var numerRows = await _db.Ado.SqlQueryAsync<S5FulfillmentNumerRow>(sqlNumer, p);
  201. var denomRows = await _db.Ado.SqlQueryAsync<S5FulfillmentDenomRow>(sqlDenom, p);
  202. sub.T8Rows = numerRows.Count + denomRows.Count;
  203. var numerByOrder = numerRows.Where(r => !string.IsNullOrEmpty(r.lynoid))
  204. .ToDictionary(r => r.lynoid!, r => r.codenum, StringComparer.OrdinalIgnoreCase);
  205. var dwdAffected = 0;
  206. var rateList = new List<decimal>();
  207. foreach (var d in denomRows)
  208. {
  209. ct.ThrowIfCancellationRequested();
  210. if (string.IsNullOrEmpty(d.noid)) continue;
  211. var beforeKg = numerByOrder.GetValueOrDefault(d.noid, 0);
  212. decimal? rate = d.listnum > 0
  213. ? Math.Round((decimal)beforeKg / d.listnum, 4)
  214. : null; // 分母为 0 时不伪装真实 0
  215. if (rate.HasValue) rateList.Add(rate.Value);
  216. dwdAffected += await _db.Ado.ExecuteCommandAsync(@"
  217. INSERT INTO dwd_t8_material_online_fulfillment
  218. (tenant_id, factory_id, biz_date, source_ztid, work_order_no,
  219. before_kgdate_rows, total_rows, fulfillment_rate, batch_id, create_time)
  220. VALUES
  221. (@tenantId, @factoryId, @bizDate, @ztid, @workOrderNo, @beforeKg, @total, @rate, @batchId, @now)
  222. ON DUPLICATE KEY UPDATE
  223. before_kgdate_rows=VALUES(before_kgdate_rows),
  224. total_rows=VALUES(total_rows),
  225. fulfillment_rate=VALUES(fulfillment_rate),
  226. batch_id=VALUES(batch_id), update_time=@now",
  227. new SugarParameter("@tenantId", option.TargetTenantId),
  228. new SugarParameter("@factoryId", option.TargetFactoryId),
  229. new SugarParameter("@bizDate", option.BizDate),
  230. new SugarParameter("@ztid", option.SourceZtid),
  231. new SugarParameter("@workOrderNo", d.noid),
  232. new SugarParameter("@beforeKg", beforeKg),
  233. new SugarParameter("@total", d.listnum),
  234. new SugarParameter("@rate", rate),
  235. new SugarParameter("@batchId", batchId),
  236. new SugarParameter("@now", now));
  237. }
  238. sub.DwdRows = dwdAffected;
  239. decimal? metricValue = rateList.Count > 0
  240. ? Math.Round(rateList.Average() * 100m, 4) // 百分号
  241. : null;
  242. sub.KpiRows = await UpsertKpiValueAsync("S5_L1_002", option.BizDate, metricValue, now, option);
  243. sub.DenominatorStatus = rateList.Count > 0 ? "OK" : "NO_VALID_ORDER";
  244. return sub;
  245. }
  246. /// <summary>S5_L1_003 物料仓储人效 = SUM(slzx where lbs=生产领料) / count(gw=仓管)。</summary>
  247. private async Task<KpiBuildSubResult> BuildS5L1003MaterialWarehouseEfficiencyAsync(
  248. string batchId, DateTime now, S5MdpRefreshOption option, CancellationToken ct)
  249. {
  250. var sub = new KpiBuildSubResult();
  251. const string sqlNumer = @"
  252. select sum(b.slzx) as slzx
  253. from mdp_std_t8_kc_tz_head a
  254. inner join mdp_std_t8_kc_tz_list b on a.src_id=b.idid
  255. where a.ztid=@ztid and a.lbs='生产领料' and a.hzyn=0 and a.zfyn=0 and a.shyn=1
  256. and a.shtime between @startDate and @endDate";
  257. const string sqlDenom = @"
  258. select count(*) as penum
  259. from mdp_std_t8_sys_pelist
  260. where ztid=@ztid and zzzt='在职' and gw='仓管'";
  261. var pNumer = new[]
  262. {
  263. new SugarParameter("@ztid", option.SourceZtid),
  264. new SugarParameter("@startDate", option.MonthlyPeriodStart),
  265. new SugarParameter("@endDate", option.MonthlyPeriodEnd)
  266. };
  267. var pDenom = new[] { new SugarParameter("@ztid", option.SourceZtid) };
  268. var numerRows = await _db.Ado.SqlQueryAsync<S5SumQtyRow>(sqlNumer, pNumer);
  269. var denomRows = await _db.Ado.SqlQueryAsync<S5CountRow>(sqlDenom, pDenom);
  270. sub.T8Rows = numerRows.Count + denomRows.Count;
  271. decimal? onlineQty = numerRows.FirstOrDefault()?.slzx;
  272. int? headcount = denomRows.FirstOrDefault()?.penum;
  273. // 分母 = 0 或 NULL:efficiency 写 NULL,并标记 denominator_status;不伪装真实 0
  274. decimal? efficiency = null;
  275. string denomStatus;
  276. if (!headcount.HasValue || headcount.Value <= 0)
  277. {
  278. denomStatus = "NO_HEADCOUNT";
  279. }
  280. else if (!onlineQty.HasValue)
  281. {
  282. denomStatus = "NO_NUMERATOR";
  283. }
  284. else
  285. {
  286. efficiency = Math.Round(onlineQty.Value / headcount.Value, 4);
  287. denomStatus = "OK";
  288. }
  289. sub.DenominatorStatus = denomStatus;
  290. // 月度 KPI 用 biz_month 唯一键,整月 1 行
  291. var dwdAffected = await _db.Ado.ExecuteCommandAsync(@"
  292. INSERT INTO dwd_t8_material_warehouse_efficiency
  293. (tenant_id, factory_id, biz_month, source_ztid, period_start, period_end,
  294. online_qty, warehouse_headcount, efficiency, denominator_status, batch_id, create_time)
  295. VALUES
  296. (@tenantId, @factoryId, @bizMonth, @ztid, @periodStart, @periodEnd,
  297. @onlineQty, @headcount, @efficiency, @denomStatus, @batchId, @now)
  298. ON DUPLICATE KEY UPDATE
  299. period_start=VALUES(period_start), period_end=VALUES(period_end),
  300. online_qty=VALUES(online_qty), warehouse_headcount=VALUES(warehouse_headcount),
  301. efficiency=VALUES(efficiency), denominator_status=VALUES(denominator_status),
  302. batch_id=VALUES(batch_id), update_time=@now",
  303. new SugarParameter("@tenantId", option.TargetTenantId),
  304. new SugarParameter("@factoryId", option.TargetFactoryId),
  305. new SugarParameter("@bizMonth", option.BizMonth),
  306. new SugarParameter("@ztid", option.SourceZtid),
  307. new SugarParameter("@periodStart", option.MonthlyPeriodStart),
  308. new SugarParameter("@periodEnd", option.MonthlyPeriodEnd),
  309. new SugarParameter("@onlineQty", onlineQty),
  310. new SugarParameter("@headcount", headcount),
  311. new SugarParameter("@efficiency", efficiency),
  312. new SugarParameter("@denomStatus", denomStatus),
  313. new SugarParameter("@batchId", batchId),
  314. new SugarParameter("@now", now));
  315. sub.DwdRows = dwdAffected;
  316. // 月度 KPI 入日表:用月末日作 biz_date,月内每天可由聚合 API 再分发;分母缺失时 metric_value=NULL
  317. sub.KpiRows = await UpsertKpiValueAsync("S5_L1_003", option.MonthlyPeriodEnd, efficiency, now, option);
  318. return sub;
  319. }
  320. /// <summary>S5_L1_004 品类物料库存周转 = D1/D2 × 30;D1=je3 月均库存金额,D2=je2 出库成本。</summary>
  321. private async Task<KpiBuildSubResult> BuildS5L1004MaterialInventoryTurnoverAsync(
  322. string batchId, DateTime now, S5MdpRefreshOption option, CancellationToken ct)
  323. {
  324. var sub = new KpiBuildSubResult();
  325. // TVF:Rep_总账_存货_V3(账套, '普通', '正常', 起期 YYYYMM, 止期 YYYYMM)
  326. const string sqlTvf = @"
  327. select ckcode as ckcode, ckname as ckname,
  328. code as code, cname as cname,
  329. pcode as pcode, pname as pname,
  330. je3 as je3, je2 as je2
  331. from dbo.Rep_总账_存货_V3(@ztid, N'普通', N'正常', @startYm, @endYm)";
  332. var p = new[]
  333. {
  334. new SugarParameter("@ztid", option.SourceZtid),
  335. new SugarParameter("@startYm", option.TvfPeriodStartYyyymm),
  336. new SugarParameter("@endYm", option.TvfPeriodEndYyyymm)
  337. };
  338. var tvfRows = await QueryT8Async<S5InventoryTurnoverRow>(sqlTvf, p);
  339. sub.T8Rows = tvfRows.Count;
  340. var dwdAffected = 0;
  341. var turnoverDaysList = new List<decimal>();
  342. foreach (var r in tvfRows)
  343. {
  344. ct.ThrowIfCancellationRequested();
  345. // 周转天数:D2=0 或 NULL 时 NULL,不伪装 0
  346. decimal? turnoverDays = (r.je2.HasValue && r.je2.Value > 0m && r.je3.HasValue)
  347. ? Math.Round(r.je3.Value / r.je2.Value * 30m, 4)
  348. : null;
  349. if (turnoverDays.HasValue) turnoverDaysList.Add(turnoverDays.Value);
  350. dwdAffected += await _db.Ado.ExecuteCommandAsync(@"
  351. INSERT INTO dwd_t8_material_inventory_turnover
  352. (tenant_id, factory_id, biz_month, source_ztid, period_start_yyyymm, period_end_yyyymm,
  353. warehouse_code, warehouse_name, item_code, item_name, category_code, category_name,
  354. avg_inventory_value, monthly_outbound_cost, turnover_days, batch_id, create_time)
  355. VALUES
  356. (@tenantId, @factoryId, @bizMonth, @ztid, @startYm, @endYm,
  357. @ckcode, @ckname, @itemCode, @itemName, @pcode, @pname,
  358. @je3, @je2, @turnoverDays, @batchId, @now)
  359. ON DUPLICATE KEY UPDATE
  360. warehouse_name=VALUES(warehouse_name), item_name=VALUES(item_name),
  361. category_code=VALUES(category_code), category_name=VALUES(category_name),
  362. avg_inventory_value=VALUES(avg_inventory_value),
  363. monthly_outbound_cost=VALUES(monthly_outbound_cost),
  364. turnover_days=VALUES(turnover_days),
  365. period_start_yyyymm=VALUES(period_start_yyyymm),
  366. period_end_yyyymm=VALUES(period_end_yyyymm),
  367. batch_id=VALUES(batch_id), update_time=@now",
  368. new SugarParameter("@tenantId", option.TargetTenantId),
  369. new SugarParameter("@factoryId", option.TargetFactoryId),
  370. new SugarParameter("@bizMonth", option.BizMonth),
  371. new SugarParameter("@ztid", option.SourceZtid),
  372. new SugarParameter("@startYm", option.TvfPeriodStartYyyymm),
  373. new SugarParameter("@endYm", option.TvfPeriodEndYyyymm),
  374. new SugarParameter("@ckcode", r.ckcode ?? ""),
  375. new SugarParameter("@ckname", r.ckname),
  376. new SugarParameter("@itemCode", r.code ?? ""),
  377. new SugarParameter("@itemName", r.cname),
  378. new SugarParameter("@pcode", r.pcode),
  379. new SugarParameter("@pname", r.pname),
  380. new SugarParameter("@je3", r.je3),
  381. new SugarParameter("@je2", r.je2),
  382. new SugarParameter("@turnoverDays", turnoverDays),
  383. new SugarParameter("@batchId", batchId),
  384. new SugarParameter("@now", now));
  385. }
  386. sub.DwdRows = dwdAffected;
  387. // KPI 值:所有品类周转天数算术平均;无任一可计算品类时 NULL
  388. decimal? metricValue = turnoverDaysList.Count > 0
  389. ? Math.Round(turnoverDaysList.Average(), 4)
  390. : null;
  391. sub.KpiRows = await UpsertKpiValueAsync("S5_L1_004", option.MonthlyPeriodEnd, metricValue, now, option);
  392. sub.DenominatorStatus = turnoverDaysList.Count > 0 ? "OK" : "NO_VALID_OUTBOUND_COST";
  393. return sub;
  394. }
  395. // ─────────────────────────────────────────────────────────────────────────
  396. // 跨库 / 写入 / 日志 封装
  397. // ─────────────────────────────────────────────────────────────────────────
  398. // legacy 直连 T8:仅 S5_L1_004(Rep_总账_存货_V3 TVF,报表聚合结果无逐行主键)仍用;
  399. // 已中台化的 S5_L1_001/002/003 均读 mdp_std_t8_*,不再走此方法。
  400. private async Task<List<T>> QueryT8Async<T>(string sql, SugarParameter[] parameters)
  401. {
  402. var t8 = _db.AsTenant().GetConnectionScope(T8ConfigId);
  403. t8.Ado.CommandTimeOut = T8CommandTimeoutSeconds;
  404. return await t8.Ado.SqlQueryAsync<T>(sql, parameters);
  405. }
  406. private async Task<int> UpsertKpiValueAsync(string metricCode, DateTime bizDate, decimal? metricValue, DateTime now, S5MdpRefreshOption option)
  407. {
  408. // 沿用 S3 UpsertS3KpiValueAsync 范式:先查现存行 → UPDATE;不存在 → SELECT MAX(id)+1 显式生成 id 后 INSERT。
  409. // ado_s9_kpi_value_l1_day.id 为手工分配主键(无 AUTO_INCREMENT),必须显式 set;
  410. // metric_value 允许 NULL(分母缺失不得伪装真实 0)。
  411. // FIX-2:截断时分秒(月度 KPI 入参可能为 YYYY-MM-DD 23:59:59),保证 SELECT WHERE biz_date=@BizDate 与 DB date 列匹配,避免重复 INSERT。
  412. // FIX-1:tenant_id/factory_id 取自 option,默认仍为 1300000000001/1,不破坏 Demo。
  413. bizDate = bizDate.Date;
  414. var existingId = await _db.Ado.GetLongAsync(
  415. "SELECT IFNULL((SELECT id FROM ado_s9_kpi_value_l1_day WHERE tenant_id=@TenantId AND factory_id=@FactoryId " +
  416. "AND module_code=@ModuleCode AND metric_code=@MetricCode AND biz_date=@BizDate AND is_deleted=0 " +
  417. "ORDER BY id LIMIT 1), 0)",
  418. new List<SugarParameter>
  419. {
  420. new("@TenantId", option.TargetTenantId),
  421. new("@FactoryId", option.TargetFactoryId),
  422. new("@ModuleCode", ModuleCode),
  423. new("@MetricCode", metricCode),
  424. new("@BizDate", bizDate)
  425. });
  426. if (existingId > 0)
  427. {
  428. return await _db.Ado.ExecuteCommandAsync(
  429. "UPDATE ado_s9_kpi_value_l1_day SET metric_value=@MetricValue, calc_time=@Now, " +
  430. "update_time=@Now, is_deleted=0, is_active=1 WHERE id=@Id",
  431. new SugarParameter("@MetricValue", metricValue),
  432. new SugarParameter("@Now", now),
  433. new SugarParameter("@Id", existingId));
  434. }
  435. var nextId = await _db.Ado.GetLongAsync(
  436. "SELECT COALESCE(MAX(id), 0) + 1 FROM ado_s9_kpi_value_l1_day");
  437. return await _db.Ado.ExecuteCommandAsync(@"
  438. INSERT INTO ado_s9_kpi_value_l1_day
  439. (id, tenant_id, org_id, company_id, factory_id, status, biz_date,
  440. create_time, update_time, is_deleted, is_active,
  441. module_code, metric_code, metric_value, calc_time)
  442. VALUES
  443. (@Id, @TenantId, NULL, NULL, @FactoryId, NULL, @BizDate,
  444. @Now, @Now, 0, 1,
  445. @ModuleCode, @MetricCode, @MetricValue, @Now)",
  446. new SugarParameter("@Id", nextId),
  447. new SugarParameter("@TenantId", option.TargetTenantId),
  448. new SugarParameter("@FactoryId", option.TargetFactoryId),
  449. new SugarParameter("@BizDate", bizDate),
  450. new SugarParameter("@Now", now),
  451. new SugarParameter("@ModuleCode", ModuleCode),
  452. new SugarParameter("@MetricCode", metricCode),
  453. new SugarParameter("@MetricValue", metricValue));
  454. }
  455. private async Task<long> InsertTransformRunLogAsync(string batchId, DateTime startedAt, string triggerType, S5MdpRefreshOption option)
  456. {
  457. await _db.Ado.ExecuteCommandAsync(@"
  458. INSERT INTO mdp_transform_run_log
  459. (tenant_id, job_code, job_name, trigger_type, batch_id, status, start_time, stage_rows, standard_rows, dwd_rows, create_time, update_time)
  460. VALUES
  461. (@TenantId, @JobCode, @JobName, @TriggerType, @BatchId, 'RUNNING', @StartTime, 0, 0, 0, @StartTime, @StartTime)",
  462. new SugarParameter("@TenantId", option.TargetTenantId),
  463. new SugarParameter("@JobCode", JobCode),
  464. new SugarParameter("@JobName", JobName),
  465. new SugarParameter("@TriggerType", triggerType),
  466. new SugarParameter("@BatchId", batchId),
  467. new SugarParameter("@StartTime", startedAt));
  468. return await _db.Ado.GetLongAsync(
  469. "SELECT id FROM mdp_transform_run_log WHERE batch_id=@BatchId ORDER BY id DESC LIMIT 1",
  470. new List<SugarParameter> { new("@BatchId", batchId) });
  471. }
  472. private async Task MarkTransformRunSuccessAsync(long runLogId, DateTime startedAt, S5MdpSyncTransformResult result)
  473. {
  474. var finishedAt = DateTime.Now;
  475. await _db.Ado.ExecuteCommandAsync(@"
  476. UPDATE mdp_transform_run_log
  477. SET status='SUCCESS', end_time=@EndTime, duration_ms=@DurationMs,
  478. stage_rows=@StageRows, standard_rows=@StandardRows, dwd_rows=@DwdRows,
  479. summary_json=@SummaryJson, update_time=CURRENT_TIMESTAMP
  480. WHERE id=@Id",
  481. new SugarParameter("@EndTime", finishedAt),
  482. new SugarParameter("@DurationMs", (int)(finishedAt - startedAt).TotalMilliseconds),
  483. new SugarParameter("@StageRows", result.StageRows),
  484. new SugarParameter("@StandardRows", result.StandardRows),
  485. new SugarParameter("@DwdRows", result.DwdRows),
  486. new SugarParameter("@SummaryJson", BuildRunSummaryJson(result)),
  487. new SugarParameter("@Id", runLogId));
  488. }
  489. private async Task MarkTransformRunFailedAsync(long runLogId, DateTime startedAt, string message, string batchId)
  490. {
  491. bool runLogUpdated = false;
  492. try
  493. {
  494. var finishedAt = DateTime.Now;
  495. await _db.Ado.ExecuteCommandAsync(@"
  496. UPDATE mdp_transform_run_log
  497. SET status='FAILED', end_time=@EndTime, duration_ms=@DurationMs,
  498. error_message=@ErrorMessage, update_time=CURRENT_TIMESTAMP
  499. WHERE id=@Id",
  500. new SugarParameter("@EndTime", finishedAt),
  501. new SugarParameter("@DurationMs", (int)(finishedAt - startedAt).TotalMilliseconds),
  502. new SugarParameter("@ErrorMessage", Truncate(message, 2000)),
  503. new SugarParameter("@Id", runLogId));
  504. runLogUpdated = true;
  505. }
  506. catch (Exception ex)
  507. {
  508. // 写库本身失败兜底:远端 MySQL 瞬断导致 MarkFailed 自身也连不上
  509. Console.Error.WriteLine($"[S5MdpSyncTransform] MarkTransformRunFailed write failed (runLogId={runLogId}): {ex.Message}");
  510. }
  511. // FAILURE-NOTIFICATION-1:写库 FAILED 成功后发通知给超级管理员;通知失败不影响主流程
  512. if (!runLogUpdated) return;
  513. try
  514. {
  515. await _sysNoticeService.AddNotice(new AddNoticeInput
  516. {
  517. Title = "S5 物料仓储 T8 KPI 跑批失败",
  518. Content = $"模块:S5 物料仓储\n批次ID:{batchId}\n失败时间:{DateTime.Now:yyyy-MM-dd HH:mm:ss}\n错误信息:{Truncate(message, 1000)}\n\n请查看 mdp_transform_run_log 获取完整错误与重试记录。",
  519. Type = NoticeTypeEnum.NOTICE,
  520. PublicTime = DateTime.Now,
  521. Status = NoticeStatusEnum.PUBLIC,
  522. PublicUserId = NoticeReceiverUserId,
  523. PublicUserName = NoticeReceiverUserName
  524. });
  525. }
  526. catch (Exception notifyEx)
  527. {
  528. _logger.LogError(notifyEx, "[S5MdpSyncTransform] SysNotice 发送失败 (runLogId={RunLogId}, batchId={BatchId})", runLogId, batchId);
  529. }
  530. }
  531. private static string BuildRunSummaryJson(S5MdpSyncTransformResult r)
  532. {
  533. var summary = new
  534. {
  535. batchId = r.BatchId,
  536. sourceZtid = r.SourceZtid,
  537. bizDate = r.BizDate.ToString("yyyy-MM-dd"),
  538. bizMonth = r.BizMonth,
  539. triggerType = r.TriggerType,
  540. dwdRows = r.DwdRows,
  541. kpiRows = r.KpiRows,
  542. perKpiDwdRows = r.PerKpiDwdRows,
  543. perKpiKpiRows = r.PerKpiKpiRows,
  544. denominatorStatus = r.KpiDenominatorStatus,
  545. tvfPeriod = $"{r.MonthlyPeriodStart:yyyy-MM-dd}~{r.MonthlyPeriodEnd:yyyy-MM-dd}"
  546. };
  547. return JsonSerializer.Serialize(summary);
  548. }
  549. private static string NormalizeTriggerType(string s) =>
  550. string.IsNullOrWhiteSpace(s) ? "AUTO" : s.Trim().ToUpperInvariant();
  551. private static void NormalizeOption(S5MdpRefreshOption option)
  552. {
  553. var d = S5MdpRefreshOption.Default();
  554. if (option.TargetFactoryId <= 0) option.TargetFactoryId = d.TargetFactoryId;
  555. if (string.IsNullOrWhiteSpace(option.SourceZtid)) option.SourceZtid = d.SourceZtid;
  556. // 目标租户由 T8 账套(ztid)映射决定,禁止固定默认/兜底
  557. option.TargetTenantId = AidopSourceTenantMap.ResolveTenantId(option.SourceZtid, option.TargetTenantId);
  558. if (option.BizDate == default) option.BizDate = d.BizDate;
  559. if (string.IsNullOrWhiteSpace(option.BizMonth)) option.BizMonth = d.BizMonth;
  560. if (option.DailyPeriodStart == default) option.DailyPeriodStart = d.DailyPeriodStart;
  561. if (option.DailyPeriodEnd == default) option.DailyPeriodEnd = d.DailyPeriodEnd;
  562. if (option.MonthlyPeriodStart == default) option.MonthlyPeriodStart = d.MonthlyPeriodStart;
  563. if (option.MonthlyPeriodEnd == default) option.MonthlyPeriodEnd = d.MonthlyPeriodEnd;
  564. if (string.IsNullOrWhiteSpace(option.TvfPeriodStartYyyymm)) option.TvfPeriodStartYyyymm = d.TvfPeriodStartYyyymm;
  565. if (string.IsNullOrWhiteSpace(option.TvfPeriodEndYyyymm)) option.TvfPeriodEndYyyymm = d.TvfPeriodEndYyyymm;
  566. }
  567. private static string Truncate(string s, int max) =>
  568. string.IsNullOrEmpty(s) ? "" : (s.Length <= max ? s : s.Substring(0, max));
  569. }
  570. // ─────────────────────────────────────────────────────────────────────────────
  571. // Refresh 入参与结果 DTO
  572. // ─────────────────────────────────────────────────────────────────────────────
  573. public sealed class S5MdpRefreshOption
  574. {
  575. /// <summary>T8 账套(kc_tz_head.ztid);实测当前唯一账套为 pbxfxp。</summary>
  576. public string SourceZtid { get; set; } = "pbxfxp";
  577. /// <summary>KPI/DWD 落库目标租户;≤0 时由 <see cref="AidopSourceTenantMap"/> 按 SourceZtid 解析。</summary>
  578. public long TargetTenantId { get; set; }
  579. /// <summary>KPI/DWD 落库目标工厂;默认 1。</summary>
  580. public long TargetFactoryId { get; set; } = 1L;
  581. /// <summary>日 T+1 KPI 的业务日期(默认昨天)。</summary>
  582. public DateTime BizDate { get; set; }
  583. /// <summary>月 M+1 KPI 的业务月 YYYY-MM(默认上月)。</summary>
  584. public string BizMonth { get; set; } = "";
  585. /// <summary>日 T+1 KPI 区间起(含),默认昨天 00:00。</summary>
  586. public DateTime DailyPeriodStart { get; set; }
  587. /// <summary>日 T+1 KPI 区间止(含),默认昨天 23:59:59。</summary>
  588. public DateTime DailyPeriodEnd { get; set; }
  589. /// <summary>月 M+1 KPI 区间起(含),默认上月 1 日。</summary>
  590. public DateTime MonthlyPeriodStart { get; set; }
  591. /// <summary>月 M+1 KPI 区间止(含),默认上月末日。</summary>
  592. public DateTime MonthlyPeriodEnd { get; set; }
  593. /// <summary>TVF Rep_总账_存货_V3 入参起期 YYYYMM。</summary>
  594. public string TvfPeriodStartYyyymm { get; set; } = "";
  595. /// <summary>TVF Rep_总账_存货_V3 入参止期 YYYYMM。</summary>
  596. public string TvfPeriodEndYyyymm { get; set; } = "";
  597. public static S5MdpRefreshOption Default()
  598. {
  599. var today = DateTime.Today;
  600. var yesterday = today.AddDays(-1);
  601. var lastMonth = today.AddMonths(-1);
  602. var monthStart = new DateTime(lastMonth.Year, lastMonth.Month, 1);
  603. var monthEnd = monthStart.AddMonths(1).AddDays(-1);
  604. return new S5MdpRefreshOption
  605. {
  606. SourceZtid = "pbxfxp",
  607. TargetTenantId = 0,
  608. TargetFactoryId = 1L,
  609. BizDate = yesterday,
  610. BizMonth = lastMonth.ToString("yyyy-MM"),
  611. DailyPeriodStart = yesterday,
  612. DailyPeriodEnd = yesterday.AddDays(1).AddSeconds(-1),
  613. MonthlyPeriodStart = monthStart,
  614. MonthlyPeriodEnd = monthEnd.AddDays(1).AddSeconds(-1),
  615. TvfPeriodStartYyyymm = monthStart.ToString("yyyyMM"),
  616. TvfPeriodEndYyyymm = monthEnd.ToString("yyyyMM")
  617. };
  618. }
  619. }
  620. public sealed class S5MdpSyncTransformResult
  621. {
  622. public string BatchId { get; set; } = "";
  623. public long RunLogId { get; set; }
  624. public string TriggerType { get; set; } = "AUTO";
  625. public string SourceZtid { get; set; } = "";
  626. public long TargetTenantId { get; set; }
  627. public long TargetFactoryId { get; set; }
  628. public DateTime BizDate { get; set; }
  629. public string BizMonth { get; set; } = "";
  630. public DateTime DailyPeriodStart { get; set; }
  631. public DateTime DailyPeriodEnd { get; set; }
  632. public DateTime MonthlyPeriodStart { get; set; }
  633. public DateTime MonthlyPeriodEnd { get; set; }
  634. public int StageRows { get; set; }
  635. public int StandardRows { get; set; }
  636. public int DwdRows { get; set; }
  637. public int KpiRows { get; set; }
  638. public Dictionary<string, int> PerKpiDwdRows { get; } = new();
  639. public Dictionary<string, int> PerKpiKpiRows { get; } = new();
  640. public List<string> KpiDenominatorStatus { get; } = new();
  641. public void MergeSub(string kpiCode, KpiBuildSubResult sub)
  642. {
  643. PerKpiDwdRows[kpiCode] = sub.DwdRows;
  644. PerKpiKpiRows[kpiCode] = sub.KpiRows;
  645. DwdRows += sub.DwdRows;
  646. KpiRows += sub.KpiRows;
  647. KpiDenominatorStatus.Add($"{kpiCode}:{sub.DenominatorStatus}");
  648. }
  649. }
  650. public sealed class KpiBuildSubResult
  651. {
  652. public int T8Rows { get; set; }
  653. public int DwdRows { get; set; }
  654. public int KpiRows { get; set; }
  655. public string DenominatorStatus { get; set; } = "OK";
  656. }
  657. // ─────────────────────────────────────────────────────────────────────────────
  658. // T8 result set 投影类型(与方老师 SQL SELECT 列名严格一致;SqlSugar 映射)
  659. // ─────────────────────────────────────────────────────────────────────────────
  660. internal sealed class S5OnlineCycleRow
  661. {
  662. public string? code { get; set; }
  663. public DateTime? shtime { get; set; }
  664. }
  665. internal sealed class S5FulfillmentNumerRow
  666. {
  667. public string? lynoid { get; set; }
  668. public int codenum { get; set; }
  669. }
  670. internal sealed class S5FulfillmentDenomRow
  671. {
  672. public string? noid { get; set; }
  673. public int listnum { get; set; }
  674. }
  675. internal sealed class S5SumQtyRow
  676. {
  677. public decimal? slzx { get; set; }
  678. }
  679. internal sealed class S5CountRow
  680. {
  681. public int penum { get; set; }
  682. }
  683. internal sealed class S5InventoryTurnoverRow
  684. {
  685. public string? ckcode { get; set; }
  686. public string? ckname { get; set; }
  687. public string? code { get; set; }
  688. public string? cname { get; set; }
  689. public string? pcode { get; set; }
  690. public string? pname { get; set; }
  691. public decimal? je3 { get; set; }
  692. public decimal? je2 { get; set; }
  693. }