AidopKanbanController.DashboardPage.cs 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626
  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 module_code=@moduleCode AND is_deleted=0 AND biz_date=@bizDate
  344. AND (factory_id IS NULL OR factory_id=0 OR factory_id=1 OR factory_id=@factoryId)
  345. AND (factory_id IS NULL OR factory_id<>@tenantId)
  346. """;
  347. var rows = await _db.Ado.SqlQueryAsync<DashboardValueRow>(currentSql, new { tenantId, factoryId, moduleCode, bizDate });
  348. var trendSql = $"""
  349. SELECT metric_code AS MetricCode, biz_date AS BizDate, metric_value AS MetricValue
  350. FROM {table}
  351. WHERE tenant_id=@tenantId AND module_code=@moduleCode AND is_deleted=0
  352. AND (factory_id IS NULL OR factory_id=0 OR factory_id=1 OR factory_id=@factoryId)
  353. AND (factory_id IS NULL OR factory_id<>@tenantId)
  354. AND biz_date >= DATE_SUB(@bizDate, INTERVAL 13 DAY)
  355. ORDER BY metric_code, biz_date
  356. """;
  357. var trends = await _db.Ado.SqlQueryAsync<DashboardTrendRow>(trendSql, new { tenantId, factoryId, moduleCode, bizDate });
  358. var trendByMetric = trends
  359. .Where(x => !string.IsNullOrWhiteSpace(x.MetricCode))
  360. .GroupBy(x => x.MetricCode!, StringComparer.OrdinalIgnoreCase)
  361. .ToDictionary(
  362. x => x.Key,
  363. x => x.Select(t => new DashboardTrendPointModel(t.BizDate.ToString("yyyy-MM-dd"), t.MetricValue)).ToList(),
  364. StringComparer.OrdinalIgnoreCase);
  365. foreach (var row in rows.Where(x => !string.IsNullOrWhiteSpace(x.MetricCode)))
  366. {
  367. result[row.MetricCode!] = new DashboardMetricValueModel(
  368. level,
  369. row.MetricValue,
  370. row.TargetValue,
  371. row.StatusColor,
  372. row.TrendFlag,
  373. trendByMetric.TryGetValue(row.MetricCode!, out var pts) ? pts : new List<DashboardTrendPointModel>());
  374. }
  375. }
  376. return result;
  377. }
  378. private static List<object> BuildDashboardSections(
  379. List<DashboardWidgetModel> widgets,
  380. Dictionary<string, DashboardMetricValueModel> values,
  381. Dictionary<string, AdoSmartOpsKpiMaster> kpiByCode)
  382. {
  383. var sectionMeta = new Dictionary<string, (string Title, int Sort)>(StringComparer.OrdinalIgnoreCase)
  384. {
  385. ["overview"] = ("核心指标", 10),
  386. ["process_efficiency"] = ("流程效率分析", 20),
  387. ["quality_trend"] = ("质量与满意度趋势", 30),
  388. ["resource_load"] = ("人效与资源负荷", 40),
  389. ["inventory_warning"] = ("库存预警", 50),
  390. ["breakdown"] = ("指标分解", 60),
  391. ["trend"] = ("趋势分析", 70),
  392. ["detail"] = ("明细列表", 80)
  393. };
  394. return widgets
  395. .GroupBy(x => NormalizeSectionCode(x.SectionCode))
  396. .OrderBy(x => sectionMeta.TryGetValue(x.Key, out var meta) ? meta.Sort : 99)
  397. .Select(g =>
  398. {
  399. var meta = sectionMeta.TryGetValue(g.Key, out var m) ? m : (g.Key, 99);
  400. return new
  401. {
  402. sectionCode = g.Key,
  403. title = meta.Item1,
  404. layout = g.Key == "overview" ? "card_grid" : "panel_grid",
  405. widgets = g.OrderBy(x => x.SortNo).Select(w => BuildDashboardWidgetPayload(w, values, kpiByCode)).ToList()
  406. };
  407. })
  408. .Cast<object>()
  409. .ToList();
  410. }
  411. private static object BuildDashboardWidgetPayload(
  412. DashboardWidgetModel widget,
  413. Dictionary<string, DashboardMetricValueModel> values,
  414. Dictionary<string, AdoSmartOpsKpiMaster> kpiByCode)
  415. {
  416. var metrics = widget.MetricCodes
  417. .Where(kpiByCode.ContainsKey)
  418. .Select(code => BuildDashboardMetricPayload(kpiByCode[code], values))
  419. .ToList();
  420. return new
  421. {
  422. sectionCode = widget.SectionCode,
  423. tabMetricCode = widget.TabMetricCode,
  424. widgetCode = widget.WidgetCode,
  425. widgetType = widget.WidgetType,
  426. title = widget.Title,
  427. metricLevel = widget.MetricLevel,
  428. span = widget.Span,
  429. sortNo = widget.SortNo,
  430. style = ParseStyleJson(widget.StyleJson),
  431. metrics
  432. };
  433. }
  434. private static object BuildDashboardMetricPayload(
  435. AdoSmartOpsKpiMaster kpi,
  436. Dictionary<string, DashboardMetricValueModel> values,
  437. Dictionary<string, string>? filterModes = null,
  438. Dictionary<string, string[]>? unsupportedFilters = null)
  439. {
  440. values.TryGetValue(kpi.MetricCode, out var value);
  441. var level = value?.Level ?? kpi.MetricLevel;
  442. var evaluation = AidopS4KpiMerge.Evaluate(
  443. value?.MetricValue,
  444. value?.TargetValue,
  445. kpi.Direction ?? "higher_is_better",
  446. kpi.YellowThreshold,
  447. kpi.RedThreshold);
  448. var gap = AidopS4KpiMerge.GapValue(value?.MetricValue, value?.TargetValue);
  449. var resolvedFilterMode = filterModes?.GetValueOrDefault(kpi.MetricCode)
  450. ?? SmartOpsKpiAggregateRuleRegistry.FilterSummaryOnly;
  451. var unsupported = unsupportedFilters?.GetValueOrDefault(kpi.MetricCode) ?? Array.Empty<string>();
  452. var isFiltered = string.Equals(resolvedFilterMode, SmartOpsKpiAggregateRuleRegistry.FilterFull, StringComparison.OrdinalIgnoreCase);
  453. return new
  454. {
  455. id = kpi.Id,
  456. metricCode = kpi.MetricCode,
  457. metricName = kpi.MetricName,
  458. metricLevel = level,
  459. parentId = kpi.ParentId,
  460. unit = kpi.Unit ?? "",
  461. direction = kpi.Direction,
  462. formula = kpi.Formula,
  463. formulaExpr = kpi.FormulaExpr,
  464. formulaPreview = kpi.FormulaPreview,
  465. calcRule = kpi.CalcRule,
  466. dataSource = kpi.DataSource,
  467. department = kpi.Department,
  468. currentValue = value?.MetricValue,
  469. targetValue = value?.TargetValue,
  470. statusColor = evaluation.Status,
  471. isBlink = evaluation.Status == "red" && evaluation.IsBeyondRedThreshold,
  472. trendFlag = value?.TrendFlag,
  473. gapValue = gap,
  474. gapLabel = FormatGapLabelGeneric(gap, kpi.Unit),
  475. trend = value?.Trend ?? new List<DashboardTrendPointModel>(),
  476. filterMode = resolvedFilterMode,
  477. scopeLabel = ResolveScopeLabel(resolvedFilterMode),
  478. unsupportedFilters = unsupported,
  479. isFiltered
  480. };
  481. }
  482. private static string ResolveScopeLabel(string? filterMode) => filterMode switch
  483. {
  484. SmartOpsKpiAggregateRuleRegistry.FilterFull => "已按当前条件过滤",
  485. SmartOpsKpiAggregateRuleRegistry.FilterPartial => "部分维度已过滤",
  486. _ => "模块汇总口径"
  487. };
  488. private static List<string> ParseMetricCodes(string? json)
  489. {
  490. if (string.IsNullOrWhiteSpace(json)) return new List<string>();
  491. try
  492. {
  493. return JsonSerializer.Deserialize<List<string>>(json) ?? new List<string>();
  494. }
  495. catch
  496. {
  497. return json.Split(',', StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries).ToList();
  498. }
  499. }
  500. private static object ParseStyleJson(string? json)
  501. {
  502. if (string.IsNullOrWhiteSpace(json)) return new { };
  503. try
  504. {
  505. return JsonSerializer.Deserialize<object>(json) ?? new { };
  506. }
  507. catch
  508. {
  509. return new { raw = json };
  510. }
  511. }
  512. private static List<DashboardTabModel> BuildDashboardTabs(List<AdoSmartOpsKpiMaster> kpis)
  513. {
  514. return kpis
  515. .Where(x => x.MetricLevel == 1)
  516. .OrderBy(x => x.SortNo)
  517. .Select(x => new DashboardTabModel(x.MetricCode, x.MetricName, x.SortNo))
  518. .ToList();
  519. }
  520. public class DashboardPageSaveDto
  521. {
  522. public string? NavigationMode { get; set; }
  523. public string? TabSource { get; set; }
  524. public string? DefaultTabMetricCode { get; set; }
  525. public List<DashboardWidgetSaveDto>? Widgets { get; set; }
  526. }
  527. public class DashboardWidgetSaveDto
  528. {
  529. public string? SectionCode { get; set; }
  530. public string? TabMetricCode { get; set; }
  531. public string? WidgetCode { get; set; }
  532. public string? WidgetType { get; set; }
  533. public string? Title { get; set; }
  534. public int MetricLevel { get; set; }
  535. public List<string>? MetricCodes { get; set; }
  536. public int Span { get; set; }
  537. public int SortNo { get; set; }
  538. public string? StyleJson { get; set; }
  539. }
  540. private sealed record DashboardWidgetModel(
  541. string SectionCode,
  542. string? TabMetricCode,
  543. string WidgetCode,
  544. string WidgetType,
  545. string? Title,
  546. int MetricLevel,
  547. List<string> MetricCodes,
  548. int Span,
  549. int SortNo,
  550. string? StyleJson);
  551. private sealed record DashboardTabModel(string MetricCode, string Title, int SortNo);
  552. private sealed record DashboardMetricValueModel(
  553. int Level,
  554. decimal? MetricValue,
  555. decimal? TargetValue,
  556. string? StatusColor,
  557. string? TrendFlag,
  558. List<DashboardTrendPointModel> Trend);
  559. private sealed record DashboardTrendPointModel(string Date, decimal? Value);
  560. private sealed class DashboardValueRow
  561. {
  562. public string? MetricCode { get; set; }
  563. public decimal? MetricValue { get; set; }
  564. public decimal? TargetValue { get; set; }
  565. public string? StatusColor { get; set; }
  566. public string? TrendFlag { get; set; }
  567. }
  568. private sealed class DashboardTrendRow
  569. {
  570. public string? MetricCode { get; set; }
  571. public DateTime BizDate { get; set; }
  572. public decimal? MetricValue { get; set; }
  573. }
  574. }