AidopKanbanController.DashboardPage.cs 24 KB

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