S8MonitoringService.cs 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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. public S8MonitoringService(SqlSugarRepository<AdoS8Exception> rep)
  9. {
  10. _rep = rep;
  11. }
  12. /// <summary>
  13. /// 9宫格数据:S1-S7 订单健康分布 + S8业务类别汇总 + S9部门汇总。
  14. /// 当前返回 Mock 数据,待 related_object_code 关联订单就绪后替换为真实查询。
  15. /// </summary>
  16. public Task<AdoS8OrderGridDto> GetOrderGridAsync()
  17. {
  18. var modules = new List<AdoS8ModuleOrderSummary>
  19. {
  20. new() { ModuleCode="S1", ModuleLabel="S1·销售评审", Green=25, Yellow=15, Red=10, Total=50, Frequency=12.5, AvgProcessHours=4.2, CloseRate=78.0 },
  21. new() { ModuleCode="S2", ModuleLabel="S2·计划排产", Green=30, Yellow=10, Red=5, Total=45, Frequency=8.3, AvgProcessHours=3.8, CloseRate=85.0 },
  22. new() { ModuleCode="S3", ModuleLabel="S3·物料套料", Green=18, Yellow=8, Red=3, Total=29, Frequency=6.7, AvgProcessHours=6.1, CloseRate=72.0 },
  23. new() { ModuleCode="S4", ModuleLabel="S4·采购执行", Green=22, Yellow=9, Red=4, Total=35, Frequency=9.4, AvgProcessHours=8.3, CloseRate=68.0 },
  24. new() { ModuleCode="S5", ModuleLabel="S5·IQC入库", Green=28, Yellow=6, Red=2, Total=36, Frequency=5.2, AvgProcessHours=2.9, CloseRate=91.0 },
  25. new() { ModuleCode="S6", ModuleLabel="S6·生产完工", Green=20, Yellow=12, Red=7, Total=39, Frequency=11.8, AvgProcessHours=5.7, CloseRate=71.0 },
  26. new() { ModuleCode="S7", ModuleLabel="S7·成品出库", Green=16, Yellow=8, Red=6, Total=30, Frequency=10.1, AvgProcessHours=3.4, CloseRate=74.0 },
  27. };
  28. foreach (var m in modules)
  29. m.Total = m.Green + m.Yellow + m.Red;
  30. var byCategory = new List<AdoS8CategorySummary>
  31. {
  32. new() { Category="订单评审", Total=18, AvgProcessHours=3.5, CloseRate=82.0 },
  33. new() { Category="产品设计", Total=9, AvgProcessHours=6.1, CloseRate=75.0 },
  34. new() { Category="材料采购", Total=22, AvgProcessHours=8.3, CloseRate=68.0 },
  35. new() { Category="本体生产", Total=31, AvgProcessHours=5.7, CloseRate=71.0 },
  36. new() { Category="总装发货", Total=8, AvgProcessHours=2.9, CloseRate=90.0 },
  37. };
  38. var byDept = new List<AdoS8DeptSummary>
  39. {
  40. new() { DeptName="市场部", Total=14, AvgProcessHours=3.2, CloseRate=85.0 },
  41. new() { DeptName="研发中心", Total=9, AvgProcessHours=7.4, CloseRate=72.0 },
  42. new() { DeptName="供应链部", Total=22, AvgProcessHours=6.8, CloseRate=65.0 },
  43. new() { DeptName="生产部", Total=31, AvgProcessHours=5.1, CloseRate=74.0 },
  44. };
  45. return Task.FromResult(new AdoS8OrderGridDto
  46. {
  47. Modules = modules,
  48. ByCategory = byCategory,
  49. ByDept = byDept
  50. });
  51. }
  52. /// <summary>
  53. /// 异常监控汇总:按 module_code 分组统计红/黄/绿/超时数。
  54. /// 供综合全景页顶部徽标和模块汇总表使用。
  55. /// </summary>
  56. public async Task<AdoS8MonitoringSummaryDto> GetSummaryAsync(AdoS8MonitoringSummaryQueryDto q)
  57. {
  58. var query = _rep.AsQueryable()
  59. .Where(e => e.TenantId == q.TenantId && e.FactoryId == q.FactoryId && !e.IsDeleted)
  60. .WhereIF(!string.IsNullOrWhiteSpace(q.SceneCode), e => e.SceneCode == q.SceneCode)
  61. .WhereIF(!string.IsNullOrWhiteSpace(q.ModuleCode), e => e.ModuleCode == q.ModuleCode)
  62. .WhereIF(q.BizDateFrom.HasValue, e => e.CreatedAt >= q.BizDateFrom!.Value)
  63. .WhereIF(q.BizDateTo.HasValue, e => e.CreatedAt <= q.BizDateTo!.Value);
  64. // 聚合到内存(数据量在可控范围内,避免复杂 GROUP BY 兼容性问题)
  65. var raw = await query
  66. .Select(e => new
  67. {
  68. e.ModuleCode,
  69. e.SceneCode,
  70. e.Severity,
  71. e.TimeoutFlag,
  72. e.Status
  73. })
  74. .ToListAsync();
  75. var byModule = raw
  76. .GroupBy(e => new { mc = e.ModuleCode ?? string.Empty, sc = e.SceneCode ?? string.Empty })
  77. .Select(g => new AdoS8ModuleSummaryItem
  78. {
  79. ModuleCode = g.Key.mc,
  80. ModuleLabel = S8ModuleCode.Label(g.Key.mc),
  81. SceneCode = g.Key.sc,
  82. SceneLabel = S8SceneCode.Label(g.Key.sc),
  83. Total = g.Count(),
  84. Red = g.Count(e => e.Severity == "CRITICAL" || e.Severity == "HIGH"),
  85. Yellow = g.Count(e => e.Severity == "MEDIUM"),
  86. Green = g.Count(e => e.Severity == "LOW"),
  87. Timeout = g.Count(e => e.TimeoutFlag && e.Status != "CLOSED")
  88. })
  89. // 按 S8ModuleCode.All 顺序排列
  90. .OrderBy(r => Array.IndexOf(S8ModuleCode.All, r.ModuleCode))
  91. .ToList();
  92. return new AdoS8MonitoringSummaryDto
  93. {
  94. Total = raw.Count,
  95. Red = raw.Count(e => e.Severity == "CRITICAL" || e.Severity == "HIGH"),
  96. Yellow = raw.Count(e => e.Severity == "MEDIUM"),
  97. Green = raw.Count(e => e.Severity == "LOW"),
  98. Timeout = raw.Count(e => e.TimeoutFlag && e.Status != "CLOSED"),
  99. ByModule = byModule
  100. };
  101. }
  102. }