AidopKanbanController.cs 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. using SqlSugar;
  2. namespace Admin.NET.Plugin.AiDOP.Controllers;
  3. [ApiController]
  4. [Route("api/[controller]")]
  5. [AllowAnonymous]
  6. [NonUnify]
  7. public class AidopKanbanController : ControllerBase
  8. {
  9. private readonly ISqlSugarClient _db;
  10. public AidopKanbanController(ISqlSugarClient db)
  11. {
  12. _db = db;
  13. }
  14. [HttpGet("home-l1")]
  15. public async Task<IActionResult> GetHomeL1([FromQuery] long tenantId = 1, [FromQuery] long factoryId = 1)
  16. {
  17. const string sql = """
  18. SELECT module_code AS ModuleCode, metric_code AS MetricCode, metric_value AS MetricValue,
  19. target_value AS TargetValue, status_color AS StatusColor, trend_flag AS TrendFlag
  20. FROM ado_s9_kpi_value_l1_day
  21. WHERE tenant_id=@tenantId AND factory_id=@factoryId AND is_deleted=0
  22. ORDER BY module_code
  23. """;
  24. var rows = await _db.Ado.SqlQueryAsync<HomeL1Dto>(sql, new { tenantId, factoryId });
  25. return Ok(rows);
  26. }
  27. [HttpGet("s8-alerts")]
  28. public async Task<IActionResult> GetS8Alerts([FromQuery] long tenantId = 1, [FromQuery] long factoryId = 1)
  29. {
  30. const string sql = """
  31. SELECT DATE_FORMAT(alert_time, '%H:%i') AS Time,
  32. IFNULL(SUBSTRING_INDEX(metric_code, '_', 1), 'S8') AS Module,
  33. IFNULL(message, '异常告警') AS Message,
  34. IFNULL(level_code, 'medium') AS LevelCode
  35. FROM ado_s8_alert_record
  36. WHERE tenant_id=@tenantId AND factory_id=@factoryId AND is_deleted=0
  37. ORDER BY alert_time DESC
  38. LIMIT 6
  39. """;
  40. var rows = await _db.Ado.SqlQueryAsync<S8AlertDto>(sql, new { tenantId, factoryId });
  41. return Ok(rows.Select(x => new
  42. {
  43. time = x.Time,
  44. module = x.Module,
  45. message = x.Message,
  46. level = x.LevelCode,
  47. levelLabel = x.LevelCode switch
  48. {
  49. "critical" => "严重",
  50. "high" => "高",
  51. "medium" => "中",
  52. _ => "一般"
  53. }
  54. }));
  55. }
  56. [HttpGet("module-detail")]
  57. public async Task<IActionResult> GetModuleDetail([FromQuery] string moduleCode = "S1", [FromQuery] long tenantId = 1, [FromQuery] long factoryId = 1)
  58. {
  59. moduleCode = string.IsNullOrWhiteSpace(moduleCode) ? "S1" : moduleCode.ToUpperInvariant();
  60. const string l2Sql = """
  61. SELECT module_code AS ModuleCode, metric_code AS MetricCode, metric_code AS MetricName, metric_value AS MetricValue,
  62. target_value AS TargetValue, status_color AS StatusColor, trend_flag AS TrendFlag, NULL AS StatDate
  63. FROM ado_s9_kpi_value_l2_day
  64. WHERE tenant_id=@tenantId AND factory_id=@factoryId AND is_deleted=0 AND module_code=@moduleCode
  65. ORDER BY id DESC, metric_code
  66. LIMIT 30
  67. """;
  68. const string l3Sql = """
  69. SELECT module_code AS ModuleCode, metric_code AS MetricCode, metric_code AS MetricName, metric_value AS MetricValue,
  70. target_value AS TargetValue, status_color AS StatusColor, trend_flag AS TrendFlag, NULL AS StatDate
  71. FROM ado_s9_kpi_value_l3_day
  72. WHERE tenant_id=@tenantId AND factory_id=@factoryId AND is_deleted=0 AND module_code=@moduleCode
  73. ORDER BY id DESC, metric_code
  74. LIMIT 60
  75. """;
  76. const string alertSql = """
  77. SELECT DATE_FORMAT(alert_time, '%H:%i:%s') AS Time,
  78. IFNULL(level_code, 'medium') AS LevelCode,
  79. IFNULL(message, '异常告警') AS Message
  80. FROM ado_s8_alert_record
  81. WHERE tenant_id=@tenantId AND factory_id=@factoryId AND is_deleted=0
  82. AND (UPPER(IFNULL(module_code,''))=@moduleCode OR UPPER(IFNULL(SUBSTRING_INDEX(metric_code, '_', 1), ''))=@moduleCode)
  83. ORDER BY alert_time DESC
  84. LIMIT 20
  85. """;
  86. const string l2FallbackSql = """
  87. SELECT '' AS ModuleCode, metric_code AS MetricCode, metric_code AS MetricName, metric_value AS MetricValue,
  88. target_value AS TargetValue, status_color AS StatusColor, trend_flag AS TrendFlag, NULL AS StatDate
  89. FROM ado_s9_kpi_value_l2_day
  90. WHERE tenant_id=@tenantId AND factory_id=@factoryId AND is_deleted=0
  91. ORDER BY id DESC
  92. LIMIT 30
  93. """;
  94. const string l3FallbackSql = """
  95. SELECT '' AS ModuleCode, metric_code AS MetricCode, metric_code AS MetricName, metric_value AS MetricValue,
  96. target_value AS TargetValue, status_color AS StatusColor, trend_flag AS TrendFlag, NULL AS StatDate
  97. FROM ado_s9_kpi_value_l3_day
  98. WHERE tenant_id=@tenantId AND factory_id=@factoryId AND is_deleted=0
  99. ORDER BY id DESC
  100. LIMIT 60
  101. """;
  102. var l2 = new List<KpiDetailDto>();
  103. var l3 = new List<KpiDetailDto>();
  104. var alerts = new List<S8AlertDto>();
  105. try { l2 = await _db.Ado.SqlQueryAsync<KpiDetailDto>(l2Sql, new { moduleCode, tenantId, factoryId }); }
  106. catch { l2 = await _db.Ado.SqlQueryAsync<KpiDetailDto>(l2FallbackSql, new { tenantId, factoryId }); }
  107. try { l3 = await _db.Ado.SqlQueryAsync<KpiDetailDto>(l3Sql, new { moduleCode, tenantId, factoryId }); }
  108. catch { l3 = await _db.Ado.SqlQueryAsync<KpiDetailDto>(l3FallbackSql, new { tenantId, factoryId }); }
  109. try { alerts = await _db.Ado.SqlQueryAsync<S8AlertDto>(alertSql, new { moduleCode, tenantId, factoryId }); }
  110. catch { alerts = new List<S8AlertDto>(); }
  111. return Ok(new
  112. {
  113. moduleCode,
  114. l2,
  115. l3,
  116. alerts = alerts.Select(x => new
  117. {
  118. time = x.Time,
  119. message = x.Message,
  120. level = x.LevelCode
  121. })
  122. });
  123. }
  124. private sealed class HomeL1Dto
  125. {
  126. public string? ModuleCode { get; set; }
  127. public string? MetricCode { get; set; }
  128. public decimal? MetricValue { get; set; }
  129. public decimal? TargetValue { get; set; }
  130. public string? StatusColor { get; set; }
  131. public string? TrendFlag { get; set; }
  132. }
  133. private sealed class S8AlertDto
  134. {
  135. public string? Time { get; set; }
  136. public string? Module { get; set; }
  137. public string? Message { get; set; }
  138. public string? LevelCode { get; set; }
  139. }
  140. private sealed class KpiDetailDto
  141. {
  142. public string? ModuleCode { get; set; }
  143. public string? MetricCode { get; set; }
  144. public string? MetricName { get; set; }
  145. public decimal? MetricValue { get; set; }
  146. public decimal? TargetValue { get; set; }
  147. public string? StatusColor { get; set; }
  148. public string? TrendFlag { get; set; }
  149. public DateTime? StatDate { get; set; }
  150. }
  151. }