using System.Security.Claims;
using ClassNotify.Server.Services;
using ClassNotify.Shared.Models;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
namespace ClassNotify.Server.Controllers;
///
/// 终端批量自检与故障下钻运维接口 (P2.5 RC1-2)
/// 严格仅限 SuperAdmin 权限访问
/// 满足 SCB1-SCB15 全部门禁规范
///
[ApiController]
[Route("api/admin/self-check-batches")]
[Authorize(Roles = "SuperAdmin")]
public class SelfCheckBatchesController : ControllerBase
{
private readonly ISelfCheckBatchService _batchService;
private readonly ILogger _logger;
public SelfCheckBatchesController(
ISelfCheckBatchService batchService,
ILogger logger)
{
_batchService = batchService;
_logger = logger;
}
private string GetActorUsername() =>
User.FindFirst(ClaimTypes.Name)?.Value ?? User.FindFirst(ClaimTypes.NameIdentifier)?.Value ?? "SuperAdmin";
private string GetClientIp() =>
HttpContext.Connection.RemoteIpAddress?.ToString() ?? "127.0.0.1";
///
/// 发起批量终端自检 (SCB1, SCB2, SCB3, SCB4, SCB6)
///
[HttpPost]
public async Task CreateBatch(
[FromBody] CreateSelfCheckBatchRequest request,
CancellationToken ct)
{
if (request == null)
{
return BadRequest(new { success = false, message = "请求体不可为空" });
}
try
{
var actor = GetActorUsername();
var ip = GetClientIp();
var created = await _batchService.CreateBatchAsync(request, actor, ip, ct);
return Ok(created);
}
catch (ArgumentException ex)
{
return BadRequest(new { success = false, message = ex.Message });
}
}
///
/// 获取最近批量自检批次列表及动态指标
///
[HttpGet]
public async Task GetBatches(
[FromQuery] string? status,
[FromQuery] int take = 20,
CancellationToken ct = default)
{
var batches = await _batchService.GetBatchesAsync(status, take, ct);
return Ok(batches);
}
///
/// 获取指定批次详情、完成度指标与 7 项探针故障聚合分布 (SCB12)
///
[HttpGet("{batchId}")]
public async Task GetBatchById(
string batchId,
CancellationToken ct)
{
if (string.IsNullOrWhiteSpace(batchId))
{
return BadRequest(new { success = false, message = "批次编号不可为空" });
}
var detail = await _batchService.GetBatchAsync(batchId, ct);
if (detail == null)
{
return NotFound(new { success = false, message = $"未找到批次 {batchId}" });
}
return Ok(detail);
}
///
/// 批次下设备自检运行明细多维过滤与分页查询 (SCB13, SCB14)
///
[HttpGet("{batchId}/devices")]
public async Task GetBatchDevices(
string batchId,
[FromQuery] SelfCheckBatchDeviceQuery query,
CancellationToken ct)
{
if (string.IsNullOrWhiteSpace(batchId))
{
return BadRequest(new { success = false, message = "批次编号不可为空" });
}
var paged = await _batchService.GetBatchDevicesAsync(batchId, query, ct);
return Ok(paged);
}
///
/// 单台终端自检运行明细与 7 项探针深度下钻
///
[HttpGet("{batchId}/runs/{runId}")]
public async Task GetBatchDeviceRunDetail(
string batchId,
string runId,
CancellationToken ct)
{
if (string.IsNullOrWhiteSpace(batchId) || string.IsNullOrWhiteSpace(runId))
{
return BadRequest(new { success = false, message = "批次编号与自检编号不可为空" });
}
var detail = await _batchService.GetBatchDeviceRunDetailAsync(batchId, runId, ct);
if (detail == null)
{
return NotFound(new { success = false, message = $"未找到自检运行记录 {runId}" });
}
return Ok(detail);
}
}