| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161 |
- using SqlSugar;
- namespace Admin.NET.Plugin.AiDOP.Controllers;
- [ApiController]
- [Route("api/[controller]")]
- [AllowAnonymous]
- [NonUnify]
- public class AidopKanbanController : ControllerBase
- {
- private readonly ISqlSugarClient _db;
- public AidopKanbanController(ISqlSugarClient db)
- {
- _db = db;
- }
- [HttpGet("home-l1")]
- public async Task<IActionResult> GetHomeL1([FromQuery] long tenantId = 1, [FromQuery] long factoryId = 1)
- {
- const string sql = """
- SELECT module_code AS ModuleCode, metric_code AS MetricCode, metric_value AS MetricValue,
- target_value AS TargetValue, status_color AS StatusColor, trend_flag AS TrendFlag
- FROM ado_s9_kpi_value_l1_day
- WHERE tenant_id=@tenantId AND factory_id=@factoryId AND is_deleted=0
- ORDER BY module_code
- """;
- var rows = await _db.Ado.SqlQueryAsync<HomeL1Dto>(sql, new { tenantId, factoryId });
- return Ok(rows);
- }
- [HttpGet("s8-alerts")]
- public async Task<IActionResult> GetS8Alerts([FromQuery] long tenantId = 1, [FromQuery] long factoryId = 1)
- {
- const string sql = """
- SELECT DATE_FORMAT(alert_time, '%H:%i') AS Time,
- IFNULL(SUBSTRING_INDEX(metric_code, '_', 1), 'S8') AS Module,
- IFNULL(message, '异常告警') AS Message,
- IFNULL(level_code, 'medium') AS LevelCode
- FROM ado_s8_alert_record
- WHERE tenant_id=@tenantId AND factory_id=@factoryId AND is_deleted=0
- ORDER BY alert_time DESC
- LIMIT 6
- """;
- var rows = await _db.Ado.SqlQueryAsync<S8AlertDto>(sql, new { tenantId, factoryId });
- return Ok(rows.Select(x => new
- {
- time = x.Time,
- module = x.Module,
- message = x.Message,
- level = x.LevelCode,
- levelLabel = x.LevelCode switch
- {
- "critical" => "严重",
- "high" => "高",
- "medium" => "中",
- _ => "一般"
- }
- }));
- }
- [HttpGet("module-detail")]
- public async Task<IActionResult> GetModuleDetail([FromQuery] string moduleCode = "S1", [FromQuery] long tenantId = 1, [FromQuery] long factoryId = 1)
- {
- moduleCode = string.IsNullOrWhiteSpace(moduleCode) ? "S1" : moduleCode.ToUpperInvariant();
- const string l2Sql = """
- SELECT module_code AS ModuleCode, metric_code AS MetricCode, metric_code AS MetricName, metric_value AS MetricValue,
- target_value AS TargetValue, status_color AS StatusColor, trend_flag AS TrendFlag, NULL AS StatDate
- FROM ado_s9_kpi_value_l2_day
- WHERE tenant_id=@tenantId AND factory_id=@factoryId AND is_deleted=0 AND module_code=@moduleCode
- ORDER BY id DESC, metric_code
- LIMIT 30
- """;
- const string l3Sql = """
- SELECT module_code AS ModuleCode, metric_code AS MetricCode, metric_code AS MetricName, metric_value AS MetricValue,
- target_value AS TargetValue, status_color AS StatusColor, trend_flag AS TrendFlag, NULL AS StatDate
- FROM ado_s9_kpi_value_l3_day
- WHERE tenant_id=@tenantId AND factory_id=@factoryId AND is_deleted=0 AND module_code=@moduleCode
- ORDER BY id DESC, metric_code
- LIMIT 60
- """;
- const string alertSql = """
- SELECT DATE_FORMAT(alert_time, '%H:%i:%s') AS Time,
- IFNULL(level_code, 'medium') AS LevelCode,
- IFNULL(message, '异常告警') AS Message
- FROM ado_s8_alert_record
- WHERE tenant_id=@tenantId AND factory_id=@factoryId AND is_deleted=0
- AND (UPPER(IFNULL(module_code,''))=@moduleCode OR UPPER(IFNULL(SUBSTRING_INDEX(metric_code, '_', 1), ''))=@moduleCode)
- ORDER BY alert_time DESC
- LIMIT 20
- """;
- const string l2FallbackSql = """
- SELECT '' AS ModuleCode, metric_code AS MetricCode, metric_code AS MetricName, metric_value AS MetricValue,
- target_value AS TargetValue, status_color AS StatusColor, trend_flag AS TrendFlag, NULL AS StatDate
- FROM ado_s9_kpi_value_l2_day
- WHERE tenant_id=@tenantId AND factory_id=@factoryId AND is_deleted=0
- ORDER BY id DESC
- LIMIT 30
- """;
- const string l3FallbackSql = """
- SELECT '' AS ModuleCode, metric_code AS MetricCode, metric_code AS MetricName, metric_value AS MetricValue,
- target_value AS TargetValue, status_color AS StatusColor, trend_flag AS TrendFlag, NULL AS StatDate
- FROM ado_s9_kpi_value_l3_day
- WHERE tenant_id=@tenantId AND factory_id=@factoryId AND is_deleted=0
- ORDER BY id DESC
- LIMIT 60
- """;
- var l2 = new List<KpiDetailDto>();
- var l3 = new List<KpiDetailDto>();
- var alerts = new List<S8AlertDto>();
- try { l2 = await _db.Ado.SqlQueryAsync<KpiDetailDto>(l2Sql, new { moduleCode, tenantId, factoryId }); }
- catch { l2 = await _db.Ado.SqlQueryAsync<KpiDetailDto>(l2FallbackSql, new { tenantId, factoryId }); }
- try { l3 = await _db.Ado.SqlQueryAsync<KpiDetailDto>(l3Sql, new { moduleCode, tenantId, factoryId }); }
- catch { l3 = await _db.Ado.SqlQueryAsync<KpiDetailDto>(l3FallbackSql, new { tenantId, factoryId }); }
- try { alerts = await _db.Ado.SqlQueryAsync<S8AlertDto>(alertSql, new { moduleCode, tenantId, factoryId }); }
- catch { alerts = new List<S8AlertDto>(); }
- return Ok(new
- {
- moduleCode,
- l2,
- l3,
- alerts = alerts.Select(x => new
- {
- time = x.Time,
- message = x.Message,
- level = x.LevelCode
- })
- });
- }
- private sealed class HomeL1Dto
- {
- public string? ModuleCode { get; set; }
- public string? MetricCode { get; set; }
- public decimal? MetricValue { get; set; }
- public decimal? TargetValue { get; set; }
- public string? StatusColor { get; set; }
- public string? TrendFlag { get; set; }
- }
- private sealed class S8AlertDto
- {
- public string? Time { get; set; }
- public string? Module { get; set; }
- public string? Message { get; set; }
- public string? LevelCode { get; set; }
- }
- private sealed class KpiDetailDto
- {
- public string? ModuleCode { get; set; }
- public string? MetricCode { get; set; }
- public string? MetricName { get; set; }
- public decimal? MetricValue { get; set; }
- public decimal? TargetValue { get; set; }
- public string? StatusColor { get; set; }
- public string? TrendFlag { get; set; }
- public DateTime? StatDate { get; set; }
- }
- }
|