AidopKanbanController.Diagnosis.cs 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. using Admin.NET.Plugin.AiDOP.Entity;
  2. using Admin.NET.Plugin.AiDOP.Infrastructure;
  3. namespace Admin.NET.Plugin.AiDOP.Controllers;
  4. public partial class AidopKanbanController
  5. {
  6. /// <summary>
  7. /// 智慧诊断:按模块和当前 L1 指标返回 L2/L3/L4 诊断矩阵。
  8. /// </summary>
  9. [HttpGet("smart-diagnosis/{moduleCode}")]
  10. public async Task<IActionResult> GetSmartDiagnosis(
  11. string moduleCode,
  12. [FromQuery] string? metricCode = null,
  13. [FromQuery] long factoryId = 1,
  14. [FromQuery] string? dateStart = null,
  15. [FromQuery] string? dateEnd = null,
  16. [FromQuery] string? product = null,
  17. [FromQuery] string? orderNo = null,
  18. [FromQuery] string? productionLine = null,
  19. [FromQuery] string? customer = null,
  20. [FromQuery] string? supplier = null,
  21. [FromQuery] string? material = null,
  22. [FromQuery] string? poNo = null,
  23. [FromQuery] string? warehouse = null,
  24. [FromQuery] string? workOrder = null,
  25. [FromQuery] string? equipment = null,
  26. [FromQuery] string? outboundNo = null)
  27. {
  28. var mc = NormalizeDynamicDashboardModule(moduleCode);
  29. if (mc == null) return BadRequest(new { message = "moduleCode 仅支持 S1/S2/S3/S4/S5/S6/S7/S9" });
  30. var tenantId = AidopTenantHelper.GetTenantId(HttpContext);
  31. var filter = ParseDashboardFilter(
  32. dateStart, dateEnd, product, orderNo, productionLine,
  33. customer, supplier, material, poNo, warehouse, workOrder, equipment, outboundNo);
  34. var kpis = await _db.Queryable<AdoSmartOpsKpiMaster>()
  35. .Where(x => x.TenantId == tenantId && x.ModuleCode == mc && x.IsEnabled)
  36. .OrderBy(x => x.MetricLevel)
  37. .OrderBy(x => x.SortNo)
  38. .ToListAsync();
  39. var valueBundle = await ResolveDashboardValuesBundleAsync(tenantId, factoryId, mc, filter, kpis);
  40. var values = valueBundle.Values;
  41. var snaps = values.ToDictionary(
  42. x => x.Key,
  43. x => new SmartDiagnosisTree.ValueSnap(x.Value.MetricValue, x.Value.TargetValue),
  44. StringComparer.OrdinalIgnoreCase);
  45. var l1Metrics = kpis.Where(x => x.MetricLevel == 1).OrderBy(x => x.SortNo).ToList();
  46. var resolved = SmartDiagnosisTree.ResolveRoot(l1Metrics, snaps, metricCode);
  47. if (resolved.Error != null)
  48. return BadRequest(new { message = resolved.Error });
  49. var root = resolved.Root;
  50. if (root == null)
  51. return Ok(new { moduleCode = mc, metricCode, title = $"{mc} 智慧诊断", stages = Array.Empty<object>(), hasChildren = false });
  52. var children = SmartDiagnosisTree.DirectChildren(kpis, root.Id, 2);
  53. var stagePayload = children.Select((kpi, index) =>
  54. {
  55. var current = BuildDiagnosisValue(kpi, values);
  56. var l3Rows = SmartDiagnosisTree.DirectChildren(kpis, kpi.Id, 3);
  57. var l4Rows = l3Rows.ToDictionary(
  58. x => x.MetricCode,
  59. x => SmartDiagnosisTree.DirectChildren(kpis, x.Id, 4)
  60. .Select(y => BuildDiagnosisNode(y, values))
  61. .ToList(),
  62. StringComparer.OrdinalIgnoreCase);
  63. return new
  64. {
  65. key = kpi.MetricCode,
  66. name = kpi.MetricName,
  67. flowCaption = kpi.MetricName,
  68. dept = string.IsNullOrWhiteSpace(kpi.Department) ? "未配置" : kpi.Department,
  69. kpi = FormatDiagnosisValue(current.Target, kpi.Unit),
  70. actualDays = FormatDiagnosisValue(current.Current, kpi.Unit),
  71. actualRate = SmartDiagnosisTree.BuildDiagnosisRate(current.Current, current.Target, kpi.Direction),
  72. status = current.Status,
  73. callout = BuildDiagnosisCallout(kpi.MetricName, current.Status, current.GapLabel),
  74. calloutTone = current.Status,
  75. sortNo = kpi.SortNo > 0 ? kpi.SortNo : index + 1,
  76. tier1 = l3Rows.Select(x => BuildDiagnosisNode(x, values)).ToList(),
  77. tier2 = l4Rows
  78. };
  79. }).ToList();
  80. var rootValue = BuildDiagnosisValue(root, values);
  81. var worstStage = stagePayload
  82. .OrderByDescending(x => SmartDiagnosisTree.DiagnosisSeverity(x.status))
  83. .ThenBy(x => x.sortNo)
  84. .FirstOrDefault();
  85. var selectionLabel = resolved.SelectionMode == SmartDiagnosisTree.SelectionGlobalWorst
  86. ? $"全局自动选择:{root.MetricName}"
  87. : $"当前指标:{root.MetricName}";
  88. return Ok(new
  89. {
  90. moduleCode = mc,
  91. metricCode = root.MetricCode,
  92. metricName = root.MetricName,
  93. title = $"{mc} {ResolveDashboardModuleName(mc)} · 智慧诊断",
  94. subtitle = selectionLabel,
  95. selectionMode = resolved.SelectionMode,
  96. hasChildren = children.Count > 0,
  97. dataSource = BuildDataSourcePayload(valueBundle),
  98. root = new
  99. {
  100. metricCode = root.MetricCode,
  101. metricName = root.MetricName,
  102. currentValue = rootValue.Current,
  103. targetValue = rootValue.Target,
  104. unit = root.Unit ?? "",
  105. status = rootValue.Status,
  106. gapLabel = rootValue.GapLabel,
  107. department = string.IsNullOrWhiteSpace(root.Department) ? "未配置" : root.Department
  108. },
  109. pmSummary = new
  110. {
  111. node = worstStage?.name ?? root.MetricName,
  112. leadDelta = rootValue.GapLabel ?? "0"
  113. },
  114. stages = stagePayload
  115. });
  116. }
  117. private static object BuildDiagnosisNode(AdoSmartOpsKpiMaster kpi, Dictionary<string, DashboardMetricValueModel> values)
  118. {
  119. var v = BuildDiagnosisValue(kpi, values);
  120. return new
  121. {
  122. key = kpi.MetricCode,
  123. name = kpi.MetricName,
  124. plan = FormatDiagnosisValue(v.Target, kpi.Unit),
  125. actual = FormatDiagnosisValue(v.Current, kpi.Unit),
  126. status = v.Status,
  127. dept = string.IsNullOrWhiteSpace(kpi.Department) ? "未配置" : kpi.Department
  128. };
  129. }
  130. private static DiagnosisValue BuildDiagnosisValue(AdoSmartOpsKpiMaster kpi, Dictionary<string, DashboardMetricValueModel> values)
  131. {
  132. values.TryGetValue(kpi.MetricCode, out var value);
  133. var status = AidopS4KpiMerge.AchievementLevel(
  134. value?.MetricValue,
  135. value?.TargetValue,
  136. kpi.Direction ?? "higher_is_better",
  137. kpi.YellowThreshold,
  138. kpi.RedThreshold);
  139. var gap = AidopS4KpiMerge.GapValue(value?.MetricValue, value?.TargetValue);
  140. return new DiagnosisValue(value?.MetricValue, value?.TargetValue, status, FormatGapLabelGeneric(gap, kpi.Unit));
  141. }
  142. private static string FormatDiagnosisValue(decimal? value, string? unit)
  143. {
  144. if (value == null) return "-";
  145. var rounded = decimal.Round(value.Value, 2);
  146. return $"{rounded:0.##}{unit ?? ""}";
  147. }
  148. private static string BuildDiagnosisCallout(string? metricName, string status, string? gapLabel)
  149. {
  150. var name = string.IsNullOrWhiteSpace(metricName) ? "当前指标" : metricName;
  151. return status switch
  152. {
  153. "red" => $"{name} 未达标,期量差 {gapLabel ?? "-"},建议优先定位责任环节。",
  154. "yellow" => $"{name} 存在预警,期量差 {gapLabel ?? "-"},建议持续跟踪。",
  155. "green" => $"{name} 达标,可作为后续环节输入。",
  156. _ => $"{name} 暂无完整诊断数据。"
  157. };
  158. }
  159. private sealed record DiagnosisValue(decimal? Current, decimal? Target, string Status, string? GapLabel);
  160. }