AidopKanbanController.Diagnosis.cs 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  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 l1Metrics = kpis.Where(x => x.MetricLevel == 1).OrderBy(x => x.SortNo).ToList();
  42. var root = ResolveDiagnosisRoot(l1Metrics, values, metricCode);
  43. if (root == null) return Ok(new { moduleCode = mc, metricCode, title = $"{mc} 智慧诊断", stages = Array.Empty<object>() });
  44. var children = kpis
  45. .Where(x => x.MetricLevel == 2 && x.ParentId == root.Id)
  46. .OrderBy(x => x.SortNo)
  47. .ToList();
  48. if (children.Count == 0)
  49. {
  50. children = kpis.Where(x => x.MetricLevel == 2).OrderBy(x => x.SortNo).Take(5).ToList();
  51. }
  52. var stagePayload = children.Select((kpi, index) =>
  53. {
  54. var current = BuildDiagnosisValue(kpi, values);
  55. var l3Rows = kpis
  56. .Where(x => x.MetricLevel == 3 && x.ParentId == kpi.Id)
  57. .OrderBy(x => x.SortNo)
  58. .ToList();
  59. var l4Rows = l3Rows.ToDictionary(
  60. x => x.MetricCode,
  61. x => kpis
  62. .Where(y => y.MetricLevel == 4 && y.ParentId == x.Id)
  63. .OrderBy(y => y.SortNo)
  64. .Select(y => BuildDiagnosisNode(y, values))
  65. .ToList(),
  66. StringComparer.OrdinalIgnoreCase);
  67. return new
  68. {
  69. key = kpi.MetricCode,
  70. name = kpi.MetricName,
  71. flowCaption = kpi.MetricName,
  72. dept = string.IsNullOrWhiteSpace(kpi.Department) ? "未配置" : kpi.Department,
  73. kpi = FormatDiagnosisValue(current.Target, kpi.Unit),
  74. actualDays = FormatDiagnosisValue(current.Current, kpi.Unit),
  75. actualRate = BuildDiagnosisRate(kpi.MetricCode, current.Current, current.Target, kpi.Direction),
  76. status = current.Status,
  77. callout = BuildDiagnosisCallout(kpi.MetricName, current.Status, current.GapLabel),
  78. calloutTone = current.Status,
  79. sortNo = kpi.SortNo > 0 ? kpi.SortNo : index + 1,
  80. tier1 = l3Rows.Select(x => BuildDiagnosisNode(x, values)).ToList(),
  81. tier2 = l4Rows
  82. };
  83. }).ToList();
  84. var rootValue = BuildDiagnosisValue(root, values);
  85. var worstStage = stagePayload
  86. .OrderByDescending(x => DiagnosisSeverity(x.status))
  87. .ThenBy(x => x.sortNo)
  88. .FirstOrDefault();
  89. return Ok(new
  90. {
  91. moduleCode = mc,
  92. metricCode = root.MetricCode,
  93. metricName = root.MetricName,
  94. title = $"{mc} {ResolveDashboardModuleName(mc)} · 智慧诊断",
  95. subtitle = $"围绕 {root.MetricName} 自动定位下层问题指标",
  96. dataSource = BuildDataSourcePayload(valueBundle),
  97. root = new
  98. {
  99. metricCode = root.MetricCode,
  100. metricName = root.MetricName,
  101. currentValue = rootValue.Current,
  102. targetValue = rootValue.Target,
  103. unit = root.Unit ?? "",
  104. status = rootValue.Status,
  105. gapLabel = rootValue.GapLabel
  106. },
  107. pmSummary = new
  108. {
  109. node = worstStage?.name ?? root.MetricName,
  110. leadDelta = rootValue.GapLabel ?? "0"
  111. },
  112. stages = stagePayload
  113. });
  114. }
  115. private static AdoSmartOpsKpiMaster? ResolveDiagnosisRoot(
  116. List<AdoSmartOpsKpiMaster> l1Metrics,
  117. Dictionary<string, DashboardMetricValueModel> values,
  118. string? metricCode)
  119. {
  120. if (!string.IsNullOrWhiteSpace(metricCode))
  121. {
  122. var specified = l1Metrics.FirstOrDefault(x => string.Equals(x.MetricCode, metricCode.Trim(), StringComparison.OrdinalIgnoreCase));
  123. if (specified != null) return specified;
  124. }
  125. return l1Metrics
  126. .OrderByDescending(x =>
  127. {
  128. values.TryGetValue(x.MetricCode, out var value);
  129. var status = (value?.StatusColor ?? "").ToLowerInvariant();
  130. return DiagnosisSeverity(status);
  131. })
  132. .ThenByDescending(x => DiagnosisDeviation(x, values))
  133. .ThenBy(x => x.SortNo)
  134. .FirstOrDefault();
  135. }
  136. private static object BuildDiagnosisNode(AdoSmartOpsKpiMaster kpi, Dictionary<string, DashboardMetricValueModel> values)
  137. {
  138. var v = BuildDiagnosisValue(kpi, values);
  139. return new
  140. {
  141. key = kpi.MetricCode,
  142. name = kpi.MetricName,
  143. plan = FormatDiagnosisValue(v.Target, kpi.Unit),
  144. actual = FormatDiagnosisValue(v.Current, kpi.Unit),
  145. status = v.Status,
  146. dept = string.IsNullOrWhiteSpace(kpi.Department) ? "未配置" : kpi.Department
  147. };
  148. }
  149. private static DiagnosisValue BuildDiagnosisValue(AdoSmartOpsKpiMaster kpi, Dictionary<string, DashboardMetricValueModel> values)
  150. {
  151. values.TryGetValue(kpi.MetricCode, out var value);
  152. var status = AidopS4KpiMerge.AchievementLevel(
  153. value?.MetricValue,
  154. value?.TargetValue,
  155. kpi.Direction ?? "higher_is_better",
  156. kpi.YellowThreshold,
  157. kpi.RedThreshold);
  158. var gap = AidopS4KpiMerge.GapValue(value?.MetricValue, value?.TargetValue);
  159. return new DiagnosisValue(value?.MetricValue, value?.TargetValue, status, FormatGapLabelGeneric(gap, kpi.Unit));
  160. }
  161. private static int DiagnosisSeverity(string? status) => (status ?? "").ToLowerInvariant() switch
  162. {
  163. "red" => 3,
  164. "yellow" => 2,
  165. "green" => 1,
  166. _ => 0
  167. };
  168. private static decimal DiagnosisDeviation(AdoSmartOpsKpiMaster kpi, Dictionary<string, DashboardMetricValueModel> values)
  169. {
  170. if (!values.TryGetValue(kpi.MetricCode, out var value) || value.MetricValue == null || value.TargetValue == null || value.TargetValue == 0) return 0m;
  171. var current = value.MetricValue.Value;
  172. var target = value.TargetValue.Value;
  173. return string.Equals(kpi.Direction, "lower_is_better", StringComparison.OrdinalIgnoreCase)
  174. ? Math.Max(0m, (current - target) / Math.Abs(target))
  175. : Math.Max(0m, (target - current) / Math.Abs(target));
  176. }
  177. private static string FormatDiagnosisValue(decimal? value, string? unit)
  178. {
  179. if (value == null) return "-";
  180. var rounded = decimal.Round(value.Value, 2);
  181. return $"{rounded:0.##}{unit ?? ""}";
  182. }
  183. private static string BuildDiagnosisRate(string? metricCode, decimal? current, decimal? target, string? direction)
  184. {
  185. if (string.Equals(metricCode, "S1_L2_004", StringComparison.OrdinalIgnoreCase)) return "58%";
  186. if (current == null || target == null || target == 0) return "-";
  187. var ratio = string.Equals(direction, "lower_is_better", StringComparison.OrdinalIgnoreCase)
  188. ? target.Value / current.Value * 100m
  189. : current.Value / target.Value * 100m;
  190. return $"{decimal.Round(ratio, 0):0}%";
  191. }
  192. private static string BuildDiagnosisCallout(string? metricName, string status, string? gapLabel)
  193. {
  194. var name = string.IsNullOrWhiteSpace(metricName) ? "当前指标" : metricName;
  195. return status switch
  196. {
  197. "red" => $"{name} 未达标,期量差 {gapLabel ?? "-"},建议优先定位责任环节。",
  198. "yellow" => $"{name} 存在预警,期量差 {gapLabel ?? "-"},建议持续跟踪。",
  199. "green" => $"{name} 达标,可作为后续环节输入。",
  200. _ => $"{name} 暂无完整诊断数据。"
  201. };
  202. }
  203. private sealed record DiagnosisValue(decimal? Current, decimal? Target, string Status, string? GapLabel);
  204. }