using Admin.NET.Plugin.AiDOP.Dto.S8; using Admin.NET.Plugin.AiDOP.Entity.S8; using Admin.NET.Plugin.AiDOP.Infrastructure.S8; namespace Admin.NET.Plugin.AiDOP.Service.S8; public class S8MonitoringService : ITransient { private readonly SqlSugarRepository _rep; public S8MonitoringService(SqlSugarRepository rep) { _rep = rep; } /// /// 9宫格数据:S1-S7 订单健康分布 + S8业务类别汇总 + S9部门汇总。 /// 当前返回 Mock 数据,待 related_object_code 关联订单就绪后替换为真实查询。 /// public Task GetOrderGridAsync() { var modules = new List { new() { ModuleCode="S1", ModuleLabel="S1·销售评审", Green=25, Yellow=15, Red=10, Total=50, Frequency=12.5, AvgProcessHours=4.2, CloseRate=78.0 }, new() { ModuleCode="S2", ModuleLabel="S2·计划排产", Green=30, Yellow=10, Red=5, Total=45, Frequency=8.3, AvgProcessHours=3.8, CloseRate=85.0 }, new() { ModuleCode="S3", ModuleLabel="S3·物料套料", Green=18, Yellow=8, Red=3, Total=29, Frequency=6.7, AvgProcessHours=6.1, CloseRate=72.0 }, new() { ModuleCode="S4", ModuleLabel="S4·采购执行", Green=22, Yellow=9, Red=4, Total=35, Frequency=9.4, AvgProcessHours=8.3, CloseRate=68.0 }, new() { ModuleCode="S5", ModuleLabel="S5·IQC入库", Green=28, Yellow=6, Red=2, Total=36, Frequency=5.2, AvgProcessHours=2.9, CloseRate=91.0 }, new() { ModuleCode="S6", ModuleLabel="S6·生产完工", Green=20, Yellow=12, Red=7, Total=39, Frequency=11.8, AvgProcessHours=5.7, CloseRate=71.0 }, new() { ModuleCode="S7", ModuleLabel="S7·成品出库", Green=16, Yellow=8, Red=6, Total=30, Frequency=10.1, AvgProcessHours=3.4, CloseRate=74.0 }, }; foreach (var m in modules) m.Total = m.Green + m.Yellow + m.Red; var byCategory = new List { new() { Category="订单评审", Total=18, AvgProcessHours=3.5, CloseRate=82.0 }, new() { Category="产品设计", Total=9, AvgProcessHours=6.1, CloseRate=75.0 }, new() { Category="材料采购", Total=22, AvgProcessHours=8.3, CloseRate=68.0 }, new() { Category="本体生产", Total=31, AvgProcessHours=5.7, CloseRate=71.0 }, new() { Category="总装发货", Total=8, AvgProcessHours=2.9, CloseRate=90.0 }, }; var byDept = new List { new() { DeptName="市场部", Total=14, AvgProcessHours=3.2, CloseRate=85.0 }, new() { DeptName="研发中心", Total=9, AvgProcessHours=7.4, CloseRate=72.0 }, new() { DeptName="供应链部", Total=22, AvgProcessHours=6.8, CloseRate=65.0 }, new() { DeptName="生产部", Total=31, AvgProcessHours=5.1, CloseRate=74.0 }, }; return Task.FromResult(new AdoS8OrderGridDto { Modules = modules, ByCategory = byCategory, ByDept = byDept }); } /// /// 异常监控汇总:按 module_code 分组统计红/黄/绿/超时数。 /// 供综合全景页顶部徽标和模块汇总表使用。 /// public async Task GetSummaryAsync(AdoS8MonitoringSummaryQueryDto q) { var query = _rep.AsQueryable() .Where(e => e.TenantId == q.TenantId && e.FactoryId == q.FactoryId && !e.IsDeleted) .WhereIF(!string.IsNullOrWhiteSpace(q.SceneCode), e => e.SceneCode == q.SceneCode) .WhereIF(!string.IsNullOrWhiteSpace(q.ModuleCode), e => e.ModuleCode == q.ModuleCode) .WhereIF(q.BizDateFrom.HasValue, e => e.CreatedAt >= q.BizDateFrom!.Value) .WhereIF(q.BizDateTo.HasValue, e => e.CreatedAt <= q.BizDateTo!.Value); // 聚合到内存(数据量在可控范围内,避免复杂 GROUP BY 兼容性问题) var raw = await query .Select(e => new { e.ModuleCode, e.SceneCode, e.Severity, e.TimeoutFlag, e.Status }) .ToListAsync(); var byModule = raw .GroupBy(e => new { mc = e.ModuleCode ?? string.Empty, sc = e.SceneCode ?? string.Empty }) .Select(g => new AdoS8ModuleSummaryItem { ModuleCode = g.Key.mc, ModuleLabel = S8ModuleCode.Label(g.Key.mc), SceneCode = g.Key.sc, SceneLabel = S8SceneCode.Label(g.Key.sc), Total = g.Count(), Red = g.Count(e => e.Severity == "CRITICAL" || e.Severity == "HIGH"), Yellow = g.Count(e => e.Severity == "MEDIUM"), Green = g.Count(e => e.Severity == "LOW"), Timeout = g.Count(e => e.TimeoutFlag && e.Status != "CLOSED") }) // 按 S8ModuleCode.All 顺序排列 .OrderBy(r => Array.IndexOf(S8ModuleCode.All, r.ModuleCode)) .ToList(); return new AdoS8MonitoringSummaryDto { Total = raw.Count, Red = raw.Count(e => e.Severity == "CRITICAL" || e.Severity == "HIGH"), Yellow = raw.Count(e => e.Severity == "MEDIUM"), Green = raw.Count(e => e.Severity == "LOW"), Timeout = raw.Count(e => e.TimeoutFlag && e.Status != "CLOSED"), ByModule = byModule }; } }