S7MdpSyncTransformService.cs 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542
  1. using Admin.NET.Core.Service;
  2. using Microsoft.Extensions.Logging;
  3. using System.Text.Json;
  4. namespace Admin.NET.Plugin.AiDOP.FinishedWarehouse;
  5. /// <summary>
  6. /// S7 成品仓储 — T8 KPI 数据底座与刷新转换服务。
  7. /// 路径 A:方老师 v5.4 KPI 字段对照表 J 列 SQL 原逻辑直发 T8 SQL Server(ConfigId=t8_v5)。
  8. /// 包含 KPI:S7_L1_001 订单发货周期 / S7_L1_002 订单发货满足率 / S7_L1_003 成品仓储人效。
  9. /// </summary>
  10. public class S7MdpSyncTransformService : ITransient
  11. {
  12. private readonly ISqlSugarClient _db;
  13. private readonly SysNoticeService _sysNoticeService;
  14. private readonly ILogger<S7MdpSyncTransformService> _logger;
  15. private const string JobCode = "S7_MDP_SYNC_TRANSFORM";
  16. private const string JobName = "S7 成品仓储 MDP 同步与转换";
  17. private const string T8ConfigId = "t8_v5";
  18. private const string ModuleCode = "S7";
  19. // FAILURE-NOTIFICATION-1:超级管理员 superAdmin.NET(AccountType=999)
  20. private const long NoticeReceiverUserId = 1300000000101L;
  21. private const string NoticeReceiverUserName = "超级管理员";
  22. public S7MdpSyncTransformService(
  23. ISqlSugarClient db,
  24. SysNoticeService sysNoticeService,
  25. ILogger<S7MdpSyncTransformService> logger)
  26. {
  27. _db = db;
  28. _sysNoticeService = sysNoticeService;
  29. _logger = logger;
  30. }
  31. public async Task<S7MdpSyncTransformResult> RunFullAsync(
  32. CancellationToken cancellationToken = default,
  33. string triggerType = "AUTO",
  34. S7MdpRefreshOption? option = null)
  35. {
  36. cancellationToken.ThrowIfCancellationRequested();
  37. option ??= S7MdpRefreshOption.Default();
  38. var now = DateTime.Now;
  39. var batchId = $"S7_MDP_FULL_{now:yyyyMMddHHmmss}";
  40. var normalizedTrigger = NormalizeTriggerType(triggerType);
  41. var runLogId = await InsertTransformRunLogAsync(batchId, now, normalizedTrigger);
  42. var result = new S7MdpSyncTransformResult
  43. {
  44. BatchId = batchId,
  45. RunLogId = runLogId,
  46. TriggerType = normalizedTrigger,
  47. SourceZtid = option.SourceZtid,
  48. BizDate = option.BizDate,
  49. BizMonth = option.BizMonth,
  50. MonthlyPeriodStart = option.MonthlyPeriodStart,
  51. MonthlyPeriodEnd = option.MonthlyPeriodEnd
  52. };
  53. try
  54. {
  55. result.StageRows = 0;
  56. result.StandardRows = 0;
  57. var sub25 = await BuildS7L1001OrderShipmentCycleAsync(batchId, now, option, cancellationToken);
  58. result.MergeSub("S7_L1_001", sub25);
  59. var sub26 = await BuildS7L1002OrderShipmentFulfillmentAsync(batchId, now, option, cancellationToken);
  60. result.MergeSub("S7_L1_002", sub26);
  61. var sub27 = await BuildS7L1003FinishedWarehouseEfficiencyAsync(batchId, now, option, cancellationToken);
  62. result.MergeSub("S7_L1_003", sub27);
  63. await MarkTransformRunSuccessAsync(runLogId, now, result);
  64. return result;
  65. }
  66. catch (Exception ex)
  67. {
  68. await MarkTransformRunFailedAsync(runLogId, now, ex.Message, batchId);
  69. throw;
  70. }
  71. }
  72. // ─────────────────────────────────────────────────────────────────────────
  73. /// <summary>S7_L1_001 订单发货周期 = 最晚发货日期 - 最早 FQC 报检日期(5 表 JOIN)。</summary>
  74. private async Task<KpiBuildSubResult> BuildS7L1001OrderShipmentCycleAsync(
  75. string batchId, DateTime now, S7MdpRefreshOption option, CancellationToken ct)
  76. {
  77. var sub = new KpiBuildSubResult();
  78. const string sql = @"
  79. select noid as noid,
  80. datediff(day, min(shdate), max(shtime)) as scts
  81. from (
  82. select a.noid as noid, b.code as code,
  83. IsNull(c.shdate, b.addtime) as shdate,
  84. (case when b.gdyn=1 then b.gdtime else d.shtime end) as shtime,
  85. (case when b.gdyn=1 or b.sl<=b.slzx then 1 else 0 end) as wczt
  86. from kc_dd_head a with(nolock)
  87. left join kc_dd_list b with(nolock) on a.Id=b.idid
  88. left join (
  89. select min(b.id) as id, b.lynoid as lynoid, b.code as code,
  90. max(a.shtime) as shtime, sum(b.slzx) as slzx
  91. from kc_tz_head a with(nolock)
  92. inner join kc_tz_list b with(nolock) on a.Id=b.idid
  93. where a.ztid=@ztid and a.lbs='销售出库' and a.hzyn=0 and a.zfyn=0 and a.shyn=1
  94. group by b.lynoid, b.code
  95. ) d on b.rwnoid=d.lynoid and b.code=d.code
  96. left join kc_zj_list c on a.ztid=c.ztid and c.lyid=b.id and c.zjyn=1
  97. where a.ztid=@ztid and a.lbs='销售订单' and a.zf=0 and a.shyn=1
  98. ) n
  99. group by noid
  100. having min(wczt)=1";
  101. var rows = await QueryT8Async<S7CycleRow>(sql, new[] { new SugarParameter("@ztid", option.SourceZtid) });
  102. sub.T8Rows = rows.Count;
  103. var dwdAffected = 0;
  104. var cycleList = new List<int>();
  105. foreach (var r in rows)
  106. {
  107. ct.ThrowIfCancellationRequested();
  108. if (string.IsNullOrEmpty(r.noid)) continue;
  109. if (r.scts.HasValue) cycleList.Add(r.scts.Value);
  110. dwdAffected += await _db.Ado.ExecuteCommandAsync(@"
  111. INSERT INTO dwd_t8_order_shipment_cycle
  112. (tenant_id, factory_id, biz_date, source_ztid, order_no, cycle_days, batch_id, create_time)
  113. VALUES
  114. (0, 1, @bizDate, @ztid, @orderNo, @cycleDays, @batchId, @now)
  115. ON DUPLICATE KEY UPDATE
  116. cycle_days=VALUES(cycle_days),
  117. batch_id=VALUES(batch_id), update_time=@now",
  118. new SugarParameter("@bizDate", option.BizDate),
  119. new SugarParameter("@ztid", option.SourceZtid),
  120. new SugarParameter("@orderNo", r.noid),
  121. new SugarParameter("@cycleDays", r.scts),
  122. new SugarParameter("@batchId", batchId),
  123. new SugarParameter("@now", now));
  124. }
  125. sub.DwdRows = dwdAffected;
  126. decimal? metricValue = cycleList.Count > 0
  127. ? Math.Round((decimal)cycleList.Average(), 4)
  128. : null;
  129. sub.KpiRows = await UpsertKpiValueAsync("S7_L1_001", option.BizDate, metricValue, now);
  130. sub.DenominatorStatus = cycleList.Count > 0 ? "OK" : "NO_COMPLETED_ORDER";
  131. return sub;
  132. }
  133. /// <summary>S7_L1_002 订单发货满足率 = (交期前发货行数 / 该订单总行数) × 100%。</summary>
  134. private async Task<KpiBuildSubResult> BuildS7L1002OrderShipmentFulfillmentAsync(
  135. string batchId, DateTime now, S7MdpRefreshOption option, CancellationToken ct)
  136. {
  137. var sub = new KpiBuildSubResult();
  138. const string sql = @"
  139. select noid as noid,
  140. count(noid) as total_rows,
  141. sum(wczt) as in_window_rows
  142. from (
  143. select a.noid as noid, b.rwnoid as rwnoid, b.code as code,
  144. (case when sum(d.slzx)>=b.sl then 1 else 0 end) as wczt
  145. from kc_dd_head a with(nolock)
  146. left join kc_dd_list b with(nolock) on a.Id=b.idid
  147. left join (
  148. select b.lynoid as lynoid, b.code as code,
  149. convert(varchar(10), a.shtime, 23) as shtime, b.slzx as slzx
  150. from kc_tz_head a with(nolock)
  151. inner join kc_tz_list b with(nolock) on a.Id=b.idid
  152. where a.ztid=@ztid and a.lbs='销售出库' and a.hzyn=0 and a.zfyn=0 and a.shyn=1
  153. ) d on b.rwnoid=d.lynoid and b.code=d.code and d.shtime<=b.jhdate
  154. where a.ztid=@ztid and a.lbs='销售订单' and a.zf=0 and a.shyn=1
  155. group by a.noid, b.rwnoid, b.code, b.sl
  156. ) n
  157. group by noid";
  158. var rows = await QueryT8Async<S7FulfillmentRow>(sql, new[] { new SugarParameter("@ztid", option.SourceZtid) });
  159. sub.T8Rows = rows.Count;
  160. var dwdAffected = 0;
  161. var rateList = new List<decimal>();
  162. foreach (var r in rows)
  163. {
  164. ct.ThrowIfCancellationRequested();
  165. if (string.IsNullOrEmpty(r.noid)) continue;
  166. decimal? rate = (r.total_rows > 0)
  167. ? Math.Round((decimal)r.in_window_rows / r.total_rows * 100m, 4)
  168. : null;
  169. if (rate.HasValue) rateList.Add(rate.Value);
  170. dwdAffected += await _db.Ado.ExecuteCommandAsync(@"
  171. INSERT INTO dwd_t8_order_shipment_fulfillment
  172. (tenant_id, factory_id, biz_date, source_ztid, order_no,
  173. total_rows, in_window_rows, fulfillment_rate, batch_id, create_time)
  174. VALUES
  175. (0, 1, @bizDate, @ztid, @orderNo,
  176. @total, @inWindow, @rate, @batchId, @now)
  177. ON DUPLICATE KEY UPDATE
  178. total_rows=VALUES(total_rows), in_window_rows=VALUES(in_window_rows),
  179. fulfillment_rate=VALUES(fulfillment_rate),
  180. batch_id=VALUES(batch_id), update_time=@now",
  181. new SugarParameter("@bizDate", option.BizDate),
  182. new SugarParameter("@ztid", option.SourceZtid),
  183. new SugarParameter("@orderNo", r.noid),
  184. new SugarParameter("@total", r.total_rows),
  185. new SugarParameter("@inWindow", r.in_window_rows),
  186. new SugarParameter("@rate", rate),
  187. new SugarParameter("@batchId", batchId),
  188. new SugarParameter("@now", now));
  189. }
  190. sub.DwdRows = dwdAffected;
  191. decimal? metricValue = rateList.Count > 0
  192. ? Math.Round(rateList.Average(), 4)
  193. : null;
  194. sub.KpiRows = await UpsertKpiValueAsync("S7_L1_002", option.BizDate, metricValue, now);
  195. sub.DenominatorStatus = rateList.Count > 0 ? "OK" : "NO_VALID_ORDER";
  196. return sub;
  197. }
  198. /// <summary>S7_L1_003 成品仓储人效 = SUM(slzx where lbs=销售出库) / count(gw=仓管)。</summary>
  199. private async Task<KpiBuildSubResult> BuildS7L1003FinishedWarehouseEfficiencyAsync(
  200. string batchId, DateTime now, S7MdpRefreshOption option, CancellationToken ct)
  201. {
  202. var sub = new KpiBuildSubResult();
  203. const string sqlNumer = @"
  204. select b.lynoid as lynoid, b.code as code,
  205. convert(varchar(10), a.shtime, 23) as shtime, b.slzx as slzx
  206. from kc_tz_head a with(nolock)
  207. inner join kc_tz_list b with(nolock) on a.Id=b.idid
  208. where a.ztid=@ztid and a.lbs='销售出库' and a.hzyn=0 and a.zfyn=0 and a.shyn=1
  209. and convert(varchar(10), a.shtime, 23) between @startDateText and @endDateText";
  210. const string sqlDenom = @"
  211. select count(*) as penum
  212. from sys_pelist with(nolock)
  213. where ztid=@ztid and zzzt='在职' and gw='仓管'";
  214. var pNumer = new[]
  215. {
  216. new SugarParameter("@ztid", option.SourceZtid),
  217. new SugarParameter("@startDateText", option.MonthlyPeriodStart.ToString("yyyy-MM-dd")),
  218. new SugarParameter("@endDateText", option.MonthlyPeriodEnd.ToString("yyyy-MM-dd"))
  219. };
  220. var pDenom = new[] { new SugarParameter("@ztid", option.SourceZtid) };
  221. var numerRows = await QueryT8Async<S7ShipmentDetailRow>(sqlNumer, pNumer);
  222. var denomRows = await QueryT8Async<S7PeNumRow>(sqlDenom, pDenom);
  223. sub.T8Rows = numerRows.Count + denomRows.Count;
  224. decimal? shipmentQty = numerRows.Sum(r => r.slzx ?? 0m);
  225. if (numerRows.Count == 0) shipmentQty = null;
  226. int? headcount = denomRows.FirstOrDefault()?.penum;
  227. decimal? efficiency = null;
  228. string denomStatus;
  229. if (!headcount.HasValue || headcount.Value <= 0)
  230. denomStatus = "NO_HEADCOUNT";
  231. else if (!shipmentQty.HasValue)
  232. denomStatus = "NO_NUMERATOR";
  233. else
  234. {
  235. efficiency = Math.Round(shipmentQty.Value / headcount.Value, 4);
  236. denomStatus = "OK";
  237. }
  238. sub.DenominatorStatus = denomStatus;
  239. var dwdAffected = await _db.Ado.ExecuteCommandAsync(@"
  240. INSERT INTO dwd_t8_finished_warehouse_efficiency
  241. (tenant_id, factory_id, biz_month, source_ztid, period_start, period_end,
  242. shipment_qty, warehouse_headcount, efficiency, denominator_status, batch_id, create_time)
  243. VALUES
  244. (0, 1, @bizMonth, @ztid, @periodStart, @periodEnd,
  245. @shipmentQty, @headcount, @efficiency, @denomStatus, @batchId, @now)
  246. ON DUPLICATE KEY UPDATE
  247. period_start=VALUES(period_start), period_end=VALUES(period_end),
  248. shipment_qty=VALUES(shipment_qty), warehouse_headcount=VALUES(warehouse_headcount),
  249. efficiency=VALUES(efficiency), denominator_status=VALUES(denominator_status),
  250. batch_id=VALUES(batch_id), update_time=@now",
  251. new SugarParameter("@bizMonth", option.BizMonth),
  252. new SugarParameter("@ztid", option.SourceZtid),
  253. new SugarParameter("@periodStart", option.MonthlyPeriodStart),
  254. new SugarParameter("@periodEnd", option.MonthlyPeriodEnd),
  255. new SugarParameter("@shipmentQty", shipmentQty),
  256. new SugarParameter("@headcount", headcount),
  257. new SugarParameter("@efficiency", efficiency),
  258. new SugarParameter("@denomStatus", denomStatus),
  259. new SugarParameter("@batchId", batchId),
  260. new SugarParameter("@now", now));
  261. sub.DwdRows = dwdAffected;
  262. sub.KpiRows = await UpsertKpiValueAsync("S7_L1_003", option.MonthlyPeriodEnd, efficiency, now);
  263. return sub;
  264. }
  265. // ─────────────────────────────────────────────────────────────────────────
  266. private async Task<List<T>> QueryT8Async<T>(string sql, SugarParameter[] parameters)
  267. {
  268. var t8 = _db.AsTenant().GetConnectionScope(T8ConfigId);
  269. return await t8.Ado.SqlQueryAsync<T>(sql, parameters);
  270. }
  271. private async Task<int> UpsertKpiValueAsync(string metricCode, DateTime bizDate, decimal? metricValue, DateTime now)
  272. {
  273. // 沿用 S3 UpsertS3KpiValueAsync 范式:先查现存行 → UPDATE;不存在 → SELECT MAX(id)+1 显式生成 id 后 INSERT。
  274. // ado_s9_kpi_value_l1_day.id 为手工分配主键(无 AUTO_INCREMENT),必须显式 set;
  275. // metric_value 允许 NULL(分母缺失不得伪装真实 0)。
  276. // FIX-2:截断时分秒(月度 KPI 入参可能为 YYYY-MM-DD 23:59:59),保证 SELECT WHERE biz_date=@BizDate 与 DB date 列匹配,避免重复 INSERT。
  277. bizDate = bizDate.Date;
  278. var existingId = await _db.Ado.GetLongAsync(
  279. "SELECT IFNULL((SELECT id FROM ado_s9_kpi_value_l1_day WHERE tenant_id=0 AND factory_id=1 " +
  280. "AND module_code=@ModuleCode AND metric_code=@MetricCode AND biz_date=@BizDate AND is_deleted=0 " +
  281. "ORDER BY id LIMIT 1), 0)",
  282. new List<SugarParameter>
  283. {
  284. new("@ModuleCode", ModuleCode),
  285. new("@MetricCode", metricCode),
  286. new("@BizDate", bizDate)
  287. });
  288. if (existingId > 0)
  289. {
  290. return await _db.Ado.ExecuteCommandAsync(
  291. "UPDATE ado_s9_kpi_value_l1_day SET metric_value=@MetricValue, calc_time=@Now, " +
  292. "update_time=@Now, is_deleted=0, is_active=1 WHERE id=@Id",
  293. new SugarParameter("@MetricValue", metricValue),
  294. new SugarParameter("@Now", now),
  295. new SugarParameter("@Id", existingId));
  296. }
  297. var nextId = await _db.Ado.GetLongAsync(
  298. "SELECT COALESCE(MAX(id), 0) + 1 FROM ado_s9_kpi_value_l1_day");
  299. return await _db.Ado.ExecuteCommandAsync(@"
  300. INSERT INTO ado_s9_kpi_value_l1_day
  301. (id, tenant_id, org_id, company_id, factory_id, status, biz_date,
  302. create_time, update_time, is_deleted, is_active,
  303. module_code, metric_code, metric_value, calc_time)
  304. VALUES
  305. (@Id, 0, NULL, NULL, 1, NULL, @BizDate,
  306. @Now, @Now, 0, 1,
  307. @ModuleCode, @MetricCode, @MetricValue, @Now)",
  308. new SugarParameter("@Id", nextId),
  309. new SugarParameter("@BizDate", bizDate),
  310. new SugarParameter("@Now", now),
  311. new SugarParameter("@ModuleCode", ModuleCode),
  312. new SugarParameter("@MetricCode", metricCode),
  313. new SugarParameter("@MetricValue", metricValue));
  314. }
  315. private async Task<long> InsertTransformRunLogAsync(string batchId, DateTime startedAt, string triggerType)
  316. {
  317. await _db.Ado.ExecuteCommandAsync(@"
  318. INSERT INTO mdp_transform_run_log
  319. (tenant_id, job_code, job_name, trigger_type, batch_id, status, start_time, stage_rows, standard_rows, dwd_rows, create_time, update_time)
  320. VALUES
  321. (0, @JobCode, @JobName, @TriggerType, @BatchId, 'RUNNING', @StartTime, 0, 0, 0, @StartTime, @StartTime)",
  322. new SugarParameter("@JobCode", JobCode),
  323. new SugarParameter("@JobName", JobName),
  324. new SugarParameter("@TriggerType", triggerType),
  325. new SugarParameter("@BatchId", batchId),
  326. new SugarParameter("@StartTime", startedAt));
  327. return await _db.Ado.GetLongAsync(
  328. "SELECT id FROM mdp_transform_run_log WHERE batch_id=@BatchId ORDER BY id DESC LIMIT 1",
  329. new List<SugarParameter> { new("@BatchId", batchId) });
  330. }
  331. private async Task MarkTransformRunSuccessAsync(long runLogId, DateTime startedAt, S7MdpSyncTransformResult result)
  332. {
  333. var finishedAt = DateTime.Now;
  334. await _db.Ado.ExecuteCommandAsync(@"
  335. UPDATE mdp_transform_run_log
  336. SET status='SUCCESS', end_time=@EndTime, duration_ms=@DurationMs,
  337. stage_rows=@StageRows, standard_rows=@StandardRows, dwd_rows=@DwdRows,
  338. summary_json=@SummaryJson, update_time=CURRENT_TIMESTAMP
  339. WHERE id=@Id",
  340. new SugarParameter("@EndTime", finishedAt),
  341. new SugarParameter("@DurationMs", (int)(finishedAt - startedAt).TotalMilliseconds),
  342. new SugarParameter("@StageRows", result.StageRows),
  343. new SugarParameter("@StandardRows", result.StandardRows),
  344. new SugarParameter("@DwdRows", result.DwdRows),
  345. new SugarParameter("@SummaryJson", JsonSerializer.Serialize(new
  346. {
  347. batchId = result.BatchId,
  348. sourceZtid = result.SourceZtid,
  349. bizDate = result.BizDate.ToString("yyyy-MM-dd"),
  350. bizMonth = result.BizMonth,
  351. dwdRows = result.DwdRows,
  352. kpiRows = result.KpiRows,
  353. perKpiDwdRows = result.PerKpiDwdRows,
  354. perKpiKpiRows = result.PerKpiKpiRows,
  355. denominatorStatus = result.KpiDenominatorStatus
  356. })),
  357. new SugarParameter("@Id", runLogId));
  358. }
  359. private async Task MarkTransformRunFailedAsync(long runLogId, DateTime startedAt, string message, string batchId)
  360. {
  361. bool runLogUpdated = false;
  362. try
  363. {
  364. var finishedAt = DateTime.Now;
  365. await _db.Ado.ExecuteCommandAsync(@"
  366. UPDATE mdp_transform_run_log
  367. SET status='FAILED', end_time=@EndTime, duration_ms=@DurationMs,
  368. error_message=@ErrorMessage, update_time=CURRENT_TIMESTAMP
  369. WHERE id=@Id",
  370. new SugarParameter("@EndTime", finishedAt),
  371. new SugarParameter("@DurationMs", (int)(finishedAt - startedAt).TotalMilliseconds),
  372. new SugarParameter("@ErrorMessage", Truncate(message, 2000)),
  373. new SugarParameter("@Id", runLogId));
  374. runLogUpdated = true;
  375. }
  376. catch (Exception ex)
  377. {
  378. Console.Error.WriteLine($"[S7MdpSyncTransform] MarkTransformRunFailed write failed (runLogId={runLogId}): {ex.Message}");
  379. }
  380. // FAILURE-NOTIFICATION-1:写库 FAILED 成功后发通知给超级管理员;通知失败不影响主流程
  381. if (!runLogUpdated) return;
  382. try
  383. {
  384. await _sysNoticeService.AddNotice(new AddNoticeInput
  385. {
  386. Title = "S7 成品仓储 T8 KPI 跑批失败",
  387. Content = $"模块:S7 成品仓储\n批次ID:{batchId}\n失败时间:{DateTime.Now:yyyy-MM-dd HH:mm:ss}\n错误信息:{Truncate(message, 1000)}\n\n请查看 mdp_transform_run_log 获取完整错误与重试记录。",
  388. Type = NoticeTypeEnum.NOTICE,
  389. PublicTime = DateTime.Now,
  390. Status = NoticeStatusEnum.PUBLIC,
  391. PublicUserId = NoticeReceiverUserId,
  392. PublicUserName = NoticeReceiverUserName
  393. });
  394. }
  395. catch (Exception notifyEx)
  396. {
  397. _logger.LogError(notifyEx, "[S7MdpSyncTransform] SysNotice 发送失败 (runLogId={RunLogId}, batchId={BatchId})", runLogId, batchId);
  398. }
  399. }
  400. private static string NormalizeTriggerType(string s) =>
  401. string.IsNullOrWhiteSpace(s) ? "AUTO" : s.Trim().ToUpperInvariant();
  402. private static string Truncate(string s, int max) =>
  403. string.IsNullOrEmpty(s) ? "" : (s.Length <= max ? s : s.Substring(0, max));
  404. }
  405. // DTO ────────────────────────────────────────────────────────────────────────
  406. public sealed class S7MdpRefreshOption
  407. {
  408. public string SourceZtid { get; set; } = "pbxfxp";
  409. public DateTime BizDate { get; set; }
  410. public string BizMonth { get; set; } = "";
  411. public DateTime MonthlyPeriodStart { get; set; }
  412. public DateTime MonthlyPeriodEnd { get; set; }
  413. public static S7MdpRefreshOption Default()
  414. {
  415. var today = DateTime.Today;
  416. var yesterday = today.AddDays(-1);
  417. var lastMonth = today.AddMonths(-1);
  418. var monthStart = new DateTime(lastMonth.Year, lastMonth.Month, 1);
  419. var monthEnd = monthStart.AddMonths(1).AddDays(-1);
  420. return new S7MdpRefreshOption
  421. {
  422. SourceZtid = "pbxfxp",
  423. BizDate = yesterday,
  424. BizMonth = lastMonth.ToString("yyyy-MM"),
  425. MonthlyPeriodStart = monthStart,
  426. MonthlyPeriodEnd = monthEnd.AddDays(1).AddSeconds(-1)
  427. };
  428. }
  429. }
  430. public sealed class S7MdpSyncTransformResult
  431. {
  432. public string BatchId { get; set; } = "";
  433. public long RunLogId { get; set; }
  434. public string TriggerType { get; set; } = "AUTO";
  435. public string SourceZtid { get; set; } = "";
  436. public DateTime BizDate { get; set; }
  437. public string BizMonth { get; set; } = "";
  438. public DateTime MonthlyPeriodStart { get; set; }
  439. public DateTime MonthlyPeriodEnd { get; set; }
  440. public int StageRows { get; set; }
  441. public int StandardRows { get; set; }
  442. public int DwdRows { get; set; }
  443. public int KpiRows { get; set; }
  444. public Dictionary<string, int> PerKpiDwdRows { get; } = new();
  445. public Dictionary<string, int> PerKpiKpiRows { get; } = new();
  446. public List<string> KpiDenominatorStatus { get; } = new();
  447. public void MergeSub(string kpiCode, KpiBuildSubResult sub)
  448. {
  449. PerKpiDwdRows[kpiCode] = sub.DwdRows;
  450. PerKpiKpiRows[kpiCode] = sub.KpiRows;
  451. DwdRows += sub.DwdRows;
  452. KpiRows += sub.KpiRows;
  453. KpiDenominatorStatus.Add($"{kpiCode}:{sub.DenominatorStatus}");
  454. }
  455. }
  456. public sealed class KpiBuildSubResult
  457. {
  458. public int T8Rows { get; set; }
  459. public int DwdRows { get; set; }
  460. public int KpiRows { get; set; }
  461. public string DenominatorStatus { get; set; } = "OK";
  462. }
  463. // T8 result set 投影类型 ──────────────────────────────────────────────────────
  464. internal sealed class S7CycleRow
  465. {
  466. public string? noid { get; set; }
  467. public int? scts { get; set; }
  468. }
  469. internal sealed class S7FulfillmentRow
  470. {
  471. public string? noid { get; set; }
  472. public int total_rows { get; set; }
  473. public int in_window_rows { get; set; }
  474. }
  475. internal sealed class S7ShipmentDetailRow
  476. {
  477. public string? lynoid { get; set; }
  478. public string? code { get; set; }
  479. public string? shtime { get; set; }
  480. public decimal? slzx { get; set; }
  481. }
  482. internal sealed class S7PeNumRow
  483. {
  484. public int penum { get; set; }
  485. }