AidopKanbanController.DashboardPage.cs 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631
  1. using System.Text.Json;
  2. using Admin.NET.Plugin.AiDOP.Entity;
  3. using Admin.NET.Plugin.AiDOP.Infrastructure;
  4. using Admin.NET.Plugin.AiDOP.SmartOps;
  5. using SqlSugar;
  6. namespace Admin.NET.Plugin.AiDOP.Controllers;
  7. public partial class AidopKanbanController
  8. {
  9. private static readonly HashSet<string> DynamicDashboardModules = new(StringComparer.OrdinalIgnoreCase)
  10. {
  11. "S1", "S2", "S3", "S4", "S5", "S6", "S7", "S9"
  12. };
  13. /// <summary>
  14. /// 通用动态详情看板:返回页面区域、组件配置、指标定义、当前值和趋势。
  15. /// </summary>
  16. [HttpGet("dashboard-page/{moduleCode}")]
  17. public async Task<IActionResult> GetDashboardPage(
  18. string moduleCode,
  19. [FromQuery] long factoryId = 1,
  20. [FromQuery] string? dateStart = null,
  21. [FromQuery] string? dateEnd = null,
  22. [FromQuery] string? product = null,
  23. [FromQuery] string? orderNo = null,
  24. [FromQuery] string? productionLine = null,
  25. [FromQuery] string? customer = null,
  26. [FromQuery] string? supplier = null,
  27. [FromQuery] string? material = null,
  28. [FromQuery] string? poNo = null,
  29. [FromQuery] string? warehouse = null,
  30. [FromQuery] string? workOrder = null,
  31. [FromQuery] string? equipment = null,
  32. [FromQuery] string? outboundNo = null)
  33. {
  34. var mc = NormalizeDynamicDashboardModule(moduleCode);
  35. if (mc == null) return BadRequest(new { message = "moduleCode 仅支持 S1/S2/S3/S4/S5/S6/S7/S9" });
  36. var tenantId = AidopTenantHelper.GetTenantId(HttpContext);
  37. var filter = ParseDashboardFilter(
  38. dateStart, dateEnd, product, orderNo, productionLine,
  39. customer, supplier, material, poNo, warehouse, workOrder, equipment, outboundNo);
  40. var kpis = await _db.Queryable<AdoSmartOpsKpiMaster>()
  41. .Where(x => x.TenantId == tenantId && x.ModuleCode == mc && x.IsEnabled)
  42. .OrderBy(x => x.MetricLevel)
  43. .OrderBy(x => x.SortNo)
  44. .ToListAsync();
  45. var kpiByCode = kpis.ToDictionary(x => x.MetricCode, StringComparer.OrdinalIgnoreCase);
  46. var pageConfig = (await _db.Queryable<AdoSmartOpsDashboardPageConfig>()
  47. .Where(x => x.TenantId == tenantId && x.FactoryId == factoryId && x.ModuleCode == mc)
  48. .ToListAsync()).FirstOrDefault();
  49. var persistedWidgets = await _db.Queryable<AdoSmartOpsDashboardWidget>()
  50. .Where(x => x.TenantId == tenantId && x.FactoryId == factoryId && x.ModuleCode == mc && x.IsEnabled == 1)
  51. .OrderBy(x => x.SortNo)
  52. .ToListAsync();
  53. var widgets = persistedWidgets.Count > 0
  54. ? persistedWidgets.Select(x => MapPersistedWidget(x, kpiByCode)).Where(x => x.MetricCodes.Count > 0).ToList()
  55. : BuildDefaultDashboardWidgets(mc, kpis);
  56. var valueBundle = await ResolveDashboardValuesBundleAsync(tenantId, factoryId, mc, filter, kpis);
  57. var values = valueBundle.Values;
  58. var sections = BuildDashboardSections(widgets, values, kpiByCode);
  59. var tabs = BuildDashboardTabs(kpis);
  60. var navigationMode = ResolveNavigationMode(pageConfig?.NavigationMode, tabs.Count);
  61. return Ok(new
  62. {
  63. moduleCode = mc,
  64. title = $"{mc} {ResolveDashboardModuleName(mc)}",
  65. subtitle = "动态运营指标看板",
  66. layoutMode = "section_grid",
  67. navigationMode,
  68. tabSource = pageConfig?.TabSource ?? "l1_metrics",
  69. defaultTabMetricCode = pageConfig?.DefaultTabMetricCode ?? tabs.FirstOrDefault()?.MetricCode,
  70. tabs,
  71. dataSource = BuildDataSourcePayload(valueBundle),
  72. metrics = kpis.Select(x => BuildDashboardMetricPayload(
  73. x, values, valueBundle.MetricFilterModes, valueBundle.UnsupportedFiltersByMetric)).ToList(),
  74. sections
  75. });
  76. }
  77. /// <summary>
  78. /// 保存动态详情看板组件配置。前端配置页可逐步接入,当前 S1 默认布局无需先保存。
  79. /// </summary>
  80. [HttpPut("dashboard-page/{moduleCode}")]
  81. public async Task<IActionResult> PutDashboardPage(string moduleCode, [FromBody] DashboardPageSaveDto? body, [FromQuery] long factoryId = 1)
  82. {
  83. var mc = NormalizeDynamicDashboardModule(moduleCode);
  84. if (mc == null) return BadRequest(new { message = "moduleCode 仅支持 S1/S2/S3/S4/S5/S6/S7/S9" });
  85. if (body?.Widgets == null) return BadRequest(new { message = "widgets 不能为空" });
  86. var tenantId = AidopTenantHelper.GetTenantId(HttpContext);
  87. var kpiCodes = await _db.Queryable<AdoSmartOpsKpiMaster>()
  88. .Where(x => x.TenantId == tenantId && x.ModuleCode == mc && x.IsEnabled)
  89. .Select(x => x.MetricCode)
  90. .ToListAsync();
  91. var validCodes = new HashSet<string>(kpiCodes, StringComparer.OrdinalIgnoreCase);
  92. var now = DateTime.Now;
  93. var rows = new List<AdoSmartOpsDashboardWidget>();
  94. var sort = 0;
  95. foreach (var w in body.Widgets)
  96. {
  97. var metricCodes = (w.MetricCodes ?? new List<string>())
  98. .Where(x => !string.IsNullOrWhiteSpace(x))
  99. .Select(x => x.Trim())
  100. .Distinct(StringComparer.OrdinalIgnoreCase)
  101. .ToList();
  102. if (metricCodes.Count == 0) continue;
  103. var invalid = metricCodes.FirstOrDefault(x => !validCodes.Contains(x));
  104. if (invalid != null) return BadRequest(new { message = $"无效指标编码: {invalid}" });
  105. sort += 10;
  106. rows.Add(new AdoSmartOpsDashboardWidget
  107. {
  108. TenantId = tenantId,
  109. FactoryId = factoryId,
  110. ModuleCode = mc,
  111. SectionCode = NormalizeSectionCode(w.SectionCode),
  112. TabMetricCode = string.IsNullOrWhiteSpace(w.TabMetricCode) ? null : w.TabMetricCode.Trim(),
  113. WidgetCode = string.IsNullOrWhiteSpace(w.WidgetCode) ? $"{mc}-W-{sort}" : w.WidgetCode.Trim(),
  114. WidgetType = NormalizeWidgetType(w.WidgetType),
  115. Title = string.IsNullOrWhiteSpace(w.Title) ? null : w.Title.Trim(),
  116. MetricLevel = Math.Max(1, Math.Min(4, w.MetricLevel)),
  117. MetricCodesJson = JsonSerializer.Serialize(metricCodes),
  118. Span = NormalizeSpan(w.Span),
  119. SortNo = w.SortNo > 0 ? w.SortNo : sort,
  120. StyleJson = string.IsNullOrWhiteSpace(w.StyleJson) ? null : w.StyleJson,
  121. IsEnabled = 1,
  122. UpdateTime = now
  123. });
  124. }
  125. try
  126. {
  127. _db.Ado.BeginTran();
  128. var navigationMode = NormalizeNavigationMode(body.NavigationMode);
  129. var tabSource = string.IsNullOrWhiteSpace(body.TabSource) ? "l1_metrics" : body.TabSource.Trim();
  130. var defaultTab = string.IsNullOrWhiteSpace(body.DefaultTabMetricCode) ? null : body.DefaultTabMetricCode.Trim();
  131. var pageRows = await _db.Updateable<AdoSmartOpsDashboardPageConfig>()
  132. .SetColumns(x => new AdoSmartOpsDashboardPageConfig
  133. {
  134. NavigationMode = navigationMode,
  135. TabSource = tabSource,
  136. DefaultTabMetricCode = defaultTab,
  137. Theme = "dark",
  138. UpdateTime = now
  139. })
  140. .Where(x => x.TenantId == tenantId && x.FactoryId == factoryId && x.ModuleCode == mc)
  141. .ExecuteCommandAsync();
  142. if (pageRows == 0)
  143. {
  144. await _db.Insertable(new AdoSmartOpsDashboardPageConfig
  145. {
  146. TenantId = tenantId,
  147. FactoryId = factoryId,
  148. ModuleCode = mc,
  149. NavigationMode = navigationMode,
  150. TabSource = tabSource,
  151. DefaultTabMetricCode = defaultTab,
  152. Theme = "dark",
  153. UpdateTime = now
  154. }).ExecuteCommandAsync();
  155. }
  156. await _db.Deleteable<AdoSmartOpsDashboardWidget>()
  157. .Where(x => x.TenantId == tenantId && x.FactoryId == factoryId && x.ModuleCode == mc)
  158. .ExecuteCommandAsync();
  159. if (rows.Count > 0) await _db.Insertable(rows).ExecuteCommandAsync();
  160. _db.Ado.CommitTran();
  161. }
  162. catch (Exception ex)
  163. {
  164. _db.Ado.RollbackTran();
  165. return BadRequest(new { message = ex.Message });
  166. }
  167. return Ok(new { ok = true, count = rows.Count });
  168. }
  169. private static string? NormalizeDynamicDashboardModule(string moduleCode)
  170. {
  171. var mc = (moduleCode ?? "").Trim().ToUpperInvariant();
  172. return DynamicDashboardModules.Contains(mc) ? mc : null;
  173. }
  174. private static string ResolveDashboardModuleName(string moduleCode) => moduleCode.ToUpperInvariant() switch
  175. {
  176. "S1" => "产销协同看板",
  177. "S2" => "订单排程看板",
  178. "S3" => "供应协同看板",
  179. "S4" => "采购执行看板",
  180. "S5" => "物料仓储看板",
  181. "S6" => "生产执行看板",
  182. "S7" => "成品仓储看板",
  183. "S9" => "运营指标看板",
  184. _ => "运营看板"
  185. };
  186. private static string NormalizeSectionCode(string? sectionCode)
  187. {
  188. var s = (sectionCode ?? "").Trim().ToLowerInvariant();
  189. return s is "overview" or "process_efficiency" or "quality_trend" or "resource_load"
  190. or "inventory_warning" or "breakdown" or "trend" or "detail" ? s : "overview";
  191. }
  192. private static string NormalizeWidgetType(string? widgetType)
  193. {
  194. var s = (widgetType ?? "").Trim().ToLowerInvariant();
  195. return s is "metric_card" or "traffic_light_card" or "progress_card" or "gauge"
  196. or "trend_line" or "line_trend" or "bar_compare" or "funnel_chart" or "heatmap"
  197. or "threshold_line" or "area_chart" or "metric_tree" or "metric_table" ? s : "metric_card";
  198. }
  199. private static string NormalizeNavigationMode(string? navigationMode)
  200. {
  201. var s = (navigationMode ?? "").Trim().ToLowerInvariant();
  202. return s is "tabs_by_l1" or "l1_cards" or "single_page" or "auto" ? s : "auto";
  203. }
  204. /// <summary>
  205. /// 全局默认:有 L1 指标则分层卡片(l1_cards),否则单页;显式配置优先于 auto。
  206. /// </summary>
  207. private static string ResolveNavigationMode(string? navigationMode, int tabCount)
  208. {
  209. var mode = NormalizeNavigationMode(navigationMode);
  210. if (mode != "auto") return mode;
  211. return tabCount > 0 ? "l1_cards" : "single_page";
  212. }
  213. private static int NormalizeSpan(int span)
  214. {
  215. if (span <= 0) return 6;
  216. if (span > 24) return 24;
  217. return span;
  218. }
  219. private static DashboardWidgetModel MapPersistedWidget(AdoSmartOpsDashboardWidget row, Dictionary<string, AdoSmartOpsKpiMaster> kpiByCode)
  220. {
  221. var codes = ParseMetricCodes(row.MetricCodesJson)
  222. .Where(kpiByCode.ContainsKey)
  223. .ToList();
  224. return new DashboardWidgetModel(
  225. row.SectionCode,
  226. row.TabMetricCode,
  227. row.WidgetCode,
  228. row.WidgetType,
  229. string.IsNullOrWhiteSpace(row.Title) && codes.Count > 0 ? kpiByCode[codes[0]].MetricName : row.Title,
  230. row.MetricLevel,
  231. codes,
  232. row.Span,
  233. row.SortNo,
  234. row.StyleJson);
  235. }
  236. private static List<DashboardWidgetModel> BuildDefaultDashboardWidgets(string moduleCode, List<AdoSmartOpsKpiMaster> kpis)
  237. {
  238. var result = new List<DashboardWidgetModel>();
  239. var l1 = kpis.Where(x => x.MetricLevel == 1).OrderBy(x => x.SortNo).Take(8).ToList();
  240. var l2 = kpis.Where(x => x.MetricLevel == 2).OrderBy(x => x.SortNo).ToList();
  241. var sort = 0;
  242. foreach (var k in l1)
  243. {
  244. sort += 10;
  245. var widgetType = ResolveDefaultWidgetType(k);
  246. result.Add(new DashboardWidgetModel(
  247. "overview",
  248. k.MetricCode,
  249. $"{moduleCode}-OV-{k.MetricCode}",
  250. widgetType,
  251. k.MetricName,
  252. 1,
  253. new List<string> { k.MetricCode },
  254. widgetType == "gauge" ? 6 : 6,
  255. sort,
  256. null));
  257. }
  258. foreach (var group in l2.GroupBy(x => x.ParentId).OrderBy(g => g.Min(x => x.SortNo)))
  259. {
  260. sort += 10;
  261. var metricCodes = group.OrderBy(x => x.SortNo).Select(x => x.MetricCode).ToList();
  262. var parent = group.Key == null ? null : kpis.FirstOrDefault(x => x.Id == group.Key);
  263. var parentName = parent?.MetricName ?? "未分组指标";
  264. var tabMetricCode = parent?.MetricCode;
  265. var sectionCode = ResolveDefaultSection(parentName);
  266. result.Add(new DashboardWidgetModel(
  267. sectionCode,
  268. tabMetricCode,
  269. $"{moduleCode}-BD-{group.Key ?? 0}",
  270. ResolveDefaultAnalysisWidgetType(parentName),
  271. parentName,
  272. 2,
  273. metricCodes,
  274. sectionCode == "process_efficiency" ? 12 : 8,
  275. sort,
  276. null));
  277. }
  278. foreach (var k in l1)
  279. {
  280. sort += 10;
  281. result.Add(new DashboardWidgetModel(
  282. ResolveDefaultSection(k.MetricName),
  283. k.MetricCode,
  284. $"{moduleCode}-TR-{k.MetricCode}",
  285. ResolveDefaultTrendWidgetType(k.MetricName),
  286. $"{k.MetricName}趋势",
  287. 1,
  288. new List<string> { k.MetricCode },
  289. 12,
  290. sort,
  291. null));
  292. }
  293. return result;
  294. }
  295. private static string ResolveDefaultWidgetType(AdoSmartOpsKpiMaster kpi)
  296. {
  297. var name = kpi.MetricName ?? "";
  298. if (name.Contains("满足率") || name.Contains("达成率") || name.Contains("合格率")) return "gauge";
  299. if (name.Contains("周期") || name.Contains("周转")) return "progress_card";
  300. return "metric_card";
  301. }
  302. private static string ResolveDefaultSection(string? metricName)
  303. {
  304. var name = metricName ?? "";
  305. if (name.Contains("满足率") || name.Contains("达成率") || name.Contains("质量")) return "quality_trend";
  306. if (name.Contains("人数") || name.Contains("人效") || name.Contains("负荷")) return "resource_load";
  307. if (name.Contains("库存") || name.Contains("周转")) return "inventory_warning";
  308. if (name.Contains("周期") || name.Contains("评审") || name.Contains("流程")) return "process_efficiency";
  309. return "breakdown";
  310. }
  311. private static string ResolveDefaultAnalysisWidgetType(string? metricName)
  312. {
  313. var name = metricName ?? "";
  314. if (name.Contains("满足率") || name.Contains("达成率")) return "line_trend";
  315. if (name.Contains("人数") || name.Contains("人效") || name.Contains("负荷")) return "heatmap";
  316. if (name.Contains("库存") || name.Contains("周转")) return "threshold_line";
  317. if (name.Contains("周期") || name.Contains("评审")) return "bar_compare";
  318. return "metric_tree";
  319. }
  320. private static string ResolveDefaultTrendWidgetType(string? metricName)
  321. {
  322. var name = metricName ?? "";
  323. if (name.Contains("库存")) return "area_chart";
  324. if (name.Contains("周转")) return "threshold_line";
  325. return "line_trend";
  326. }
  327. private async Task<Dictionary<string, DashboardMetricValueModel>> LoadDashboardValuesAsync(long tenantId, long factoryId, string moduleCode)
  328. {
  329. var result = new Dictionary<string, DashboardMetricValueModel>(StringComparer.OrdinalIgnoreCase);
  330. AidopTenantMigration.EnsureKpiValueL4Table(_db);
  331. foreach (var (table, level) in new[]
  332. {
  333. ("ado_s9_kpi_value_l1_day", 1),
  334. ("ado_s9_kpi_value_l2_day", 2),
  335. ("ado_s9_kpi_value_l3_day", 3),
  336. ("ado_s9_kpi_value_l4_day", 4)
  337. })
  338. {
  339. var bizDate = await ResolveMaxBizDateGenericAsync(table, tenantId, factoryId, moduleCode);
  340. var currentSql = $"""
  341. SELECT metric_code AS MetricCode, metric_value AS MetricValue, target_value AS TargetValue, status_color AS StatusColor, trend_flag AS TrendFlag
  342. FROM {table}
  343. WHERE tenant_id=@tenantId AND factory_id=@factoryId AND module_code=@moduleCode AND is_deleted=0 AND biz_date=@bizDate
  344. """;
  345. var rows = await _db.Ado.SqlQueryAsync<DashboardValueRow>(currentSql, new { tenantId, factoryId, moduleCode, bizDate });
  346. var trendSql = $"""
  347. SELECT metric_code AS MetricCode, biz_date AS BizDate, metric_value AS MetricValue
  348. FROM {table}
  349. WHERE tenant_id=@tenantId AND factory_id=@factoryId AND module_code=@moduleCode AND is_deleted=0
  350. AND biz_date >= DATE_SUB(@bizDate, INTERVAL 13 DAY)
  351. ORDER BY metric_code, biz_date
  352. """;
  353. var trends = await _db.Ado.SqlQueryAsync<DashboardTrendRow>(trendSql, new { tenantId, factoryId, moduleCode, bizDate });
  354. var trendByMetric = trends
  355. .Where(x => !string.IsNullOrWhiteSpace(x.MetricCode))
  356. .GroupBy(x => x.MetricCode!, StringComparer.OrdinalIgnoreCase)
  357. .ToDictionary(
  358. x => x.Key,
  359. x => x.Select(t => new DashboardTrendPointModel(t.BizDate.ToString("yyyy-MM-dd"), t.MetricValue)).ToList(),
  360. StringComparer.OrdinalIgnoreCase);
  361. foreach (var row in rows.Where(x => !string.IsNullOrWhiteSpace(x.MetricCode)))
  362. {
  363. result[row.MetricCode!] = new DashboardMetricValueModel(
  364. level,
  365. row.MetricValue,
  366. row.TargetValue,
  367. row.StatusColor,
  368. row.TrendFlag,
  369. trendByMetric.TryGetValue(row.MetricCode!, out var pts) ? pts : new List<DashboardTrendPointModel>());
  370. }
  371. }
  372. return result;
  373. }
  374. private static List<object> BuildDashboardSections(
  375. List<DashboardWidgetModel> widgets,
  376. Dictionary<string, DashboardMetricValueModel> values,
  377. Dictionary<string, AdoSmartOpsKpiMaster> kpiByCode)
  378. {
  379. var sectionMeta = new Dictionary<string, (string Title, int Sort)>(StringComparer.OrdinalIgnoreCase)
  380. {
  381. ["overview"] = ("核心指标", 10),
  382. ["process_efficiency"] = ("流程效率分析", 20),
  383. ["quality_trend"] = ("质量与满意度趋势", 30),
  384. ["resource_load"] = ("人效与资源负荷", 40),
  385. ["inventory_warning"] = ("库存预警", 50),
  386. ["breakdown"] = ("指标分解", 60),
  387. ["trend"] = ("趋势分析", 70),
  388. ["detail"] = ("明细列表", 80)
  389. };
  390. return widgets
  391. .GroupBy(x => NormalizeSectionCode(x.SectionCode))
  392. .OrderBy(x => sectionMeta.TryGetValue(x.Key, out var meta) ? meta.Sort : 99)
  393. .Select(g =>
  394. {
  395. var meta = sectionMeta.TryGetValue(g.Key, out var m) ? m : (g.Key, 99);
  396. return new
  397. {
  398. sectionCode = g.Key,
  399. title = meta.Item1,
  400. layout = g.Key == "overview" ? "card_grid" : "panel_grid",
  401. widgets = g.OrderBy(x => x.SortNo).Select(w => BuildDashboardWidgetPayload(w, values, kpiByCode)).ToList()
  402. };
  403. })
  404. .Cast<object>()
  405. .ToList();
  406. }
  407. private static object BuildDashboardWidgetPayload(
  408. DashboardWidgetModel widget,
  409. Dictionary<string, DashboardMetricValueModel> values,
  410. Dictionary<string, AdoSmartOpsKpiMaster> kpiByCode)
  411. {
  412. var metrics = widget.MetricCodes
  413. .Where(kpiByCode.ContainsKey)
  414. .Select(code => BuildDashboardMetricPayload(kpiByCode[code], values))
  415. .ToList();
  416. return new
  417. {
  418. sectionCode = widget.SectionCode,
  419. tabMetricCode = widget.TabMetricCode,
  420. widgetCode = widget.WidgetCode,
  421. widgetType = widget.WidgetType,
  422. title = widget.Title,
  423. metricLevel = widget.MetricLevel,
  424. span = widget.Span,
  425. sortNo = widget.SortNo,
  426. style = ParseStyleJson(widget.StyleJson),
  427. metrics
  428. };
  429. }
  430. private static object BuildDashboardMetricPayload(
  431. AdoSmartOpsKpiMaster kpi,
  432. Dictionary<string, DashboardMetricValueModel> values,
  433. Dictionary<string, string>? filterModes = null,
  434. Dictionary<string, string[]>? unsupportedFilters = null)
  435. {
  436. values.TryGetValue(kpi.MetricCode, out var value);
  437. var level = value?.Level ?? kpi.MetricLevel;
  438. var achievement = AidopS4KpiMerge.AchievementLevel(
  439. value?.MetricValue,
  440. value?.TargetValue,
  441. kpi.Direction ?? "higher_is_better",
  442. kpi.YellowThreshold,
  443. kpi.RedThreshold);
  444. var gap = AidopS4KpiMerge.GapValue(value?.MetricValue, value?.TargetValue);
  445. var resolvedFilterMode = filterModes?.GetValueOrDefault(kpi.MetricCode)
  446. ?? SmartOpsKpiAggregateRuleRegistry.FilterSummaryOnly;
  447. var unsupported = unsupportedFilters?.GetValueOrDefault(kpi.MetricCode) ?? Array.Empty<string>();
  448. var isFiltered = string.Equals(resolvedFilterMode, SmartOpsKpiAggregateRuleRegistry.FilterFull, StringComparison.OrdinalIgnoreCase);
  449. return new
  450. {
  451. id = kpi.Id,
  452. metricCode = kpi.MetricCode,
  453. metricName = kpi.MetricName,
  454. metricLevel = level,
  455. parentId = kpi.ParentId,
  456. unit = kpi.Unit ?? "",
  457. direction = kpi.Direction,
  458. formula = kpi.Formula,
  459. formulaExpr = kpi.FormulaExpr,
  460. formulaPreview = kpi.FormulaPreview,
  461. calcRule = kpi.CalcRule,
  462. dataSource = kpi.DataSource,
  463. department = kpi.Department,
  464. currentValue = value?.MetricValue,
  465. targetValue = value?.TargetValue,
  466. statusColor = achievement,
  467. isBlink = achievement == "red" && IsBeyondRedThreshold(value?.MetricValue, value?.TargetValue, kpi.Direction),
  468. trendFlag = value?.TrendFlag,
  469. gapValue = gap,
  470. gapLabel = FormatGapLabelGeneric(gap, kpi.Unit),
  471. trend = value?.Trend ?? new List<DashboardTrendPointModel>(),
  472. filterMode = resolvedFilterMode,
  473. scopeLabel = ResolveScopeLabel(resolvedFilterMode),
  474. unsupportedFilters = unsupported,
  475. isFiltered
  476. };
  477. }
  478. private static string ResolveScopeLabel(string? filterMode) => filterMode switch
  479. {
  480. SmartOpsKpiAggregateRuleRegistry.FilterFull => "已按当前条件过滤",
  481. SmartOpsKpiAggregateRuleRegistry.FilterPartial => "部分维度已过滤",
  482. _ => "模块汇总口径"
  483. };
  484. private static List<string> ParseMetricCodes(string? json)
  485. {
  486. if (string.IsNullOrWhiteSpace(json)) return new List<string>();
  487. try
  488. {
  489. return JsonSerializer.Deserialize<List<string>>(json) ?? new List<string>();
  490. }
  491. catch
  492. {
  493. return json.Split(',', StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries).ToList();
  494. }
  495. }
  496. private static object ParseStyleJson(string? json)
  497. {
  498. if (string.IsNullOrWhiteSpace(json)) return new { };
  499. try
  500. {
  501. return JsonSerializer.Deserialize<object>(json) ?? new { };
  502. }
  503. catch
  504. {
  505. return new { raw = json };
  506. }
  507. }
  508. private static bool IsBeyondRedThreshold(decimal? current, decimal? target, string? direction)
  509. {
  510. if (current == null || target == null || target == 0) return false;
  511. var ratio = current.Value / target.Value * 100m;
  512. return string.Equals(direction, "lower_is_better", StringComparison.OrdinalIgnoreCase)
  513. ? ratio > 120m
  514. : ratio < 80m;
  515. }
  516. private static List<DashboardTabModel> BuildDashboardTabs(List<AdoSmartOpsKpiMaster> kpis)
  517. {
  518. return kpis
  519. .Where(x => x.MetricLevel == 1)
  520. .OrderBy(x => x.SortNo)
  521. .Select(x => new DashboardTabModel(x.MetricCode, x.MetricName, x.SortNo))
  522. .ToList();
  523. }
  524. public class DashboardPageSaveDto
  525. {
  526. public string? NavigationMode { get; set; }
  527. public string? TabSource { get; set; }
  528. public string? DefaultTabMetricCode { get; set; }
  529. public List<DashboardWidgetSaveDto>? Widgets { get; set; }
  530. }
  531. public class DashboardWidgetSaveDto
  532. {
  533. public string? SectionCode { get; set; }
  534. public string? TabMetricCode { get; set; }
  535. public string? WidgetCode { get; set; }
  536. public string? WidgetType { get; set; }
  537. public string? Title { get; set; }
  538. public int MetricLevel { get; set; }
  539. public List<string>? MetricCodes { get; set; }
  540. public int Span { get; set; }
  541. public int SortNo { get; set; }
  542. public string? StyleJson { get; set; }
  543. }
  544. private sealed record DashboardWidgetModel(
  545. string SectionCode,
  546. string? TabMetricCode,
  547. string WidgetCode,
  548. string WidgetType,
  549. string? Title,
  550. int MetricLevel,
  551. List<string> MetricCodes,
  552. int Span,
  553. int SortNo,
  554. string? StyleJson);
  555. private sealed record DashboardTabModel(string MetricCode, string Title, int SortNo);
  556. private sealed record DashboardMetricValueModel(
  557. int Level,
  558. decimal? MetricValue,
  559. decimal? TargetValue,
  560. string? StatusColor,
  561. string? TrendFlag,
  562. List<DashboardTrendPointModel> Trend);
  563. private sealed record DashboardTrendPointModel(string Date, decimal? Value);
  564. private sealed class DashboardValueRow
  565. {
  566. public string? MetricCode { get; set; }
  567. public decimal? MetricValue { get; set; }
  568. public decimal? TargetValue { get; set; }
  569. public string? StatusColor { get; set; }
  570. public string? TrendFlag { get; set; }
  571. }
  572. private sealed class DashboardTrendRow
  573. {
  574. public string? MetricCode { get; set; }
  575. public DateTime BizDate { get; set; }
  576. public decimal? MetricValue { get; set; }
  577. }
  578. }