S6MdpSyncTransformService.cs 20 KB

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