S8MonitoringService.cs 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. using Admin.NET.Plugin.AiDOP.Dto.S8;
  2. using Admin.NET.Plugin.AiDOP.Entity.S8;
  3. using Admin.NET.Plugin.AiDOP.Infrastructure.S8;
  4. namespace Admin.NET.Plugin.AiDOP.Service.S8;
  5. public class S8MonitoringService : ITransient
  6. {
  7. private readonly SqlSugarRepository<AdoS8Exception> _rep;
  8. private readonly SqlSugarRepository<AdoS8ExceptionType> _typeRep;
  9. private readonly SqlSugarRepository<SysOrg> _orgRep;
  10. public S8MonitoringService(
  11. SqlSugarRepository<AdoS8Exception> rep,
  12. SqlSugarRepository<AdoS8ExceptionType> typeRep,
  13. SqlSugarRepository<SysOrg> orgRep)
  14. {
  15. _rep = rep;
  16. _typeRep = typeRep;
  17. _orgRep = orgRep;
  18. }
  19. /// <summary>
  20. /// 9宫格数据:S1-S7 订单健康分布 + S8业务类别汇总 + S9部门汇总。
  21. /// 数据来源 ado_s8_exception 聚合;异常表为空时返回全 0(不再返回 Demo 数据)。
  22. /// </summary>
  23. public async Task<AdoS8OrderGridDto> GetOrderGridAsync(long tenantId = 1, long factoryId = 1)
  24. {
  25. var events = await _rep.AsQueryable()
  26. .Where(e => e.TenantId == tenantId && e.FactoryId == factoryId && !e.IsDeleted)
  27. .Select(e => new
  28. {
  29. e.ModuleCode,
  30. e.Severity,
  31. e.Status,
  32. e.TimeoutFlag,
  33. e.ExceptionTypeCode,
  34. e.SceneCode,
  35. e.ResponsibleDeptId,
  36. e.CreatedAt,
  37. e.ClosedAt
  38. })
  39. .ToListAsync();
  40. // ── Modules:按 module_code 聚合 ──
  41. var modules = S8ModuleCode.All.Select(mc =>
  42. {
  43. var rows = events.Where(e => e.ModuleCode == mc).ToList();
  44. var unclosed = rows.Where(e => e.Status != "CLOSED").ToList();
  45. var red = unclosed.Count(e => e.Severity == "CRITICAL" || e.Severity == "HIGH");
  46. var yellow = unclosed.Count(e => e.Severity == "MEDIUM");
  47. var green = unclosed.Count(e => e.Severity == "LOW");
  48. var closed = rows.Count(e => e.Status == "CLOSED");
  49. var total = rows.Count;
  50. return new AdoS8ModuleOrderSummary
  51. {
  52. ModuleCode = mc,
  53. ModuleLabel = S8ModuleCode.Label(mc),
  54. Green = green,
  55. Yellow = yellow,
  56. Red = red,
  57. Total = total,
  58. Frequency = total,
  59. AvgProcessHours = AvgHours(rows.Select(r => (r.CreatedAt, r.ClosedAt))),
  60. CloseRate = total == 0 ? 0 : Math.Round(closed * 100.0 / total, 1),
  61. };
  62. }).ToList();
  63. // ── ByCategory:按异常类型的 domain_code/type_code 聚合为 5 大业务类别 ──
  64. var typeMap = (await _typeRep.AsQueryable()
  65. .Where(t => (t.TenantId == 0 && t.FactoryId == 0)
  66. || (t.TenantId == tenantId && t.FactoryId == factoryId))
  67. .ToListAsync())
  68. .GroupBy(t => t.TypeCode)
  69. .ToDictionary(g => g.Key, g => g.OrderByDescending(x => x.FactoryId).First());
  70. var byCategory = events
  71. .Where(e => !string.IsNullOrEmpty(e.ExceptionTypeCode) && typeMap.ContainsKey(e.ExceptionTypeCode!))
  72. .GroupBy(e => CategoryOf(typeMap[e.ExceptionTypeCode!]))
  73. .Where(g => !string.IsNullOrEmpty(g.Key))
  74. .Select(g =>
  75. {
  76. var total = g.Count();
  77. var closed = g.Count(e => e.Status == "CLOSED");
  78. return new AdoS8CategorySummary
  79. {
  80. Category = g.Key!,
  81. Total = total,
  82. AvgProcessHours = AvgHours(g.Select(e => (e.CreatedAt, e.ClosedAt))),
  83. CloseRate = total == 0 ? 0 : Math.Round(closed * 100.0 / total, 1),
  84. };
  85. })
  86. .OrderBy(c => CategoryOrder(c.Category))
  87. .ToList();
  88. // ── ByDept:按 ResponsibleDeptId 聚合,JOIN SysOrg 取部门名称 ──
  89. var deptIds = events.Select(e => e.ResponsibleDeptId).Where(id => id > 0).Distinct().ToList();
  90. var deptNameMap = deptIds.Count == 0
  91. ? new Dictionary<long, string>()
  92. : (await _orgRep.AsQueryable().Where(o => deptIds.Contains(o.Id)).Select(o => new { o.Id, o.Name }).ToListAsync())
  93. .ToDictionary(o => o.Id, o => o.Name);
  94. var byDept = events
  95. .Where(e => e.ResponsibleDeptId > 0)
  96. .GroupBy(e => e.ResponsibleDeptId)
  97. .Select(g =>
  98. {
  99. var total = g.Count();
  100. var closed = g.Count(e => e.Status == "CLOSED");
  101. return new AdoS8DeptSummary
  102. {
  103. DeptName = deptNameMap.TryGetValue(g.Key, out var n) ? n : $"部门{g.Key}",
  104. Total = total,
  105. AvgProcessHours = AvgHours(g.Select(e => (e.CreatedAt, e.ClosedAt))),
  106. CloseRate = total == 0 ? 0 : Math.Round(closed * 100.0 / total, 1),
  107. };
  108. })
  109. .OrderByDescending(d => d.Total)
  110. .ToList();
  111. return new AdoS8OrderGridDto
  112. {
  113. Modules = modules,
  114. ByCategory = byCategory,
  115. ByDept = byDept,
  116. };
  117. }
  118. private static double AvgHours(IEnumerable<(DateTime createdAt, DateTime? closedAt)> items)
  119. {
  120. var closed = items.Where(x => x.closedAt.HasValue).ToList();
  121. if (closed.Count == 0) return 0;
  122. var avg = closed.Average(x => (x.closedAt!.Value - x.createdAt).TotalHours);
  123. return Math.Round(avg, 1);
  124. }
  125. /// <summary>异常类型 → 业务类别(与 Overview 页 5 张类别卡对应)。</summary>
  126. private static string CategoryOf(AdoS8ExceptionType t) => t.TypeCode switch
  127. {
  128. "ORDER_CHANGE" => "订单评审",
  129. "DELIVERY_DELAY" => "总装发货",
  130. "PENDING_SHIPMENT" => "总装发货",
  131. "EQUIP_FAULT" => "本体生产",
  132. "MATERIAL_SHORTAGE" => "本体生产",
  133. "QUALITY_DEFECT" => "本体生产",
  134. "SUPPLIER_ETA_ISSUE" => "材料采购",
  135. "SUPPLIER_SHIP_ISSUE" => "材料采购",
  136. "WH_INBOUND_ISSUE" => "材料采购",
  137. "IQC_ISSUE" => "材料采购",
  138. "WH_PUTAWAY_ISSUE" => "材料采购",
  139. "WH_KIT_ISSUE" => "本体生产",
  140. "WH_ISSUE_OUT_ISSUE" => "本体生产",
  141. _ => string.Empty,
  142. };
  143. private static int CategoryOrder(string category) => category switch
  144. {
  145. "订单评审" => 1,
  146. "产品设计" => 2,
  147. "材料采购" => 3,
  148. "本体生产" => 4,
  149. "总装发货" => 5,
  150. _ => 99,
  151. };
  152. /// <summary>
  153. /// 异常监控汇总:按 module_code 分组统计红/黄/绿/超时数。
  154. /// 供综合全景页顶部徽标和模块汇总表使用。
  155. /// </summary>
  156. public async Task<AdoS8MonitoringSummaryDto> GetSummaryAsync(AdoS8MonitoringSummaryQueryDto q)
  157. {
  158. var query = _rep.AsQueryable()
  159. .Where(e => e.TenantId == q.TenantId && e.FactoryId == q.FactoryId && !e.IsDeleted)
  160. .WhereIF(!string.IsNullOrWhiteSpace(q.SceneCode), e => e.SceneCode == q.SceneCode)
  161. .WhereIF(!string.IsNullOrWhiteSpace(q.ModuleCode), e => e.ModuleCode == q.ModuleCode)
  162. .WhereIF(q.BizDateFrom.HasValue, e => e.CreatedAt >= q.BizDateFrom!.Value)
  163. .WhereIF(q.BizDateTo.HasValue, e => e.CreatedAt <= q.BizDateTo!.Value);
  164. // 聚合到内存(数据量在可控范围内,避免复杂 GROUP BY 兼容性问题)
  165. var raw = await query
  166. .Select(e => new
  167. {
  168. e.ModuleCode,
  169. e.SceneCode,
  170. e.Severity,
  171. e.TimeoutFlag,
  172. e.Status
  173. })
  174. .ToListAsync();
  175. var byModule = raw
  176. .GroupBy(e => new { mc = e.ModuleCode ?? string.Empty, sc = e.SceneCode ?? string.Empty })
  177. .Select(g => new AdoS8ModuleSummaryItem
  178. {
  179. ModuleCode = g.Key.mc,
  180. ModuleLabel = S8ModuleCode.Label(g.Key.mc),
  181. SceneCode = g.Key.sc,
  182. SceneLabel = S8SceneCode.Label(g.Key.sc),
  183. Total = g.Count(),
  184. Red = g.Count(e => e.Severity == "CRITICAL" || e.Severity == "HIGH"),
  185. Yellow = g.Count(e => e.Severity == "MEDIUM"),
  186. Green = g.Count(e => e.Severity == "LOW"),
  187. Timeout = g.Count(e => e.TimeoutFlag && e.Status != "CLOSED")
  188. })
  189. // 按 S8ModuleCode.All 顺序排列
  190. .OrderBy(r => Array.IndexOf(S8ModuleCode.All, r.ModuleCode))
  191. .ToList();
  192. return new AdoS8MonitoringSummaryDto
  193. {
  194. Total = raw.Count,
  195. Red = raw.Count(e => e.Severity == "CRITICAL" || e.Severity == "HIGH"),
  196. Yellow = raw.Count(e => e.Severity == "MEDIUM"),
  197. Green = raw.Count(e => e.Severity == "LOW"),
  198. Timeout = raw.Count(e => e.TimeoutFlag && e.Status != "CLOSED"),
  199. ByModule = byModule
  200. };
  201. }
  202. }