using System.Security.Claims; using System.Threading; using System.Threading.Tasks; using ClassNotify.Server.Services; using ClassNotify.Shared.Models; using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Mvc; using Microsoft.Extensions.Logging; namespace ClassNotify.Server.Controllers; /// /// 管理员终端版本升级管控接口 (P2.5 RC1-4 Phase 1) /// 严格限权 SuperAdmin,单设备更新流转与状态追踪 /// [ApiController] [Authorize] public sealed class AdminDeviceUpdatesController : ControllerBase { private readonly IDeviceUpdateService _updateService; private readonly ILogger _logger; public AdminDeviceUpdatesController( IDeviceUpdateService updateService, ILogger logger) { _updateService = updateService; _logger = logger; } private string GetActorUsername() => User.FindFirst(ClaimTypes.Name)?.Value ?? User.FindFirst(ClaimTypes.NameIdentifier)?.Value ?? "Admin"; private string GetActorRole() => User.FindFirst(ClaimTypes.Role)?.Value ?? (User.IsInRole("SuperAdmin") ? "SuperAdmin" : "SchoolAdmin"); private string? GetActorTenantId() => User.FindFirst("tenant_id")?.Value ?? User.FindFirst("TenantId")?.Value; private string GetClientIp() => HttpContext.Connection.RemoteIpAddress?.ToString() ?? "127.0.0.1"; /// /// 请求对单台设备执行版本升级 (U3: SuperAdmin-only, U5: Single active update) /// [HttpPost("api/admin/devices/{deviceId}/update")] public async Task CreateDeviceUpdate( [FromRoute] string deviceId, [FromBody] CreateDeviceUpdateRequest? request, CancellationToken ct) { var actorUser = GetActorUsername(); var actorRole = GetActorRole(); var actorTenant = GetActorTenantId(); var clientIp = GetClientIp(); var result = await _updateService.CreateUpdateRunAsync( deviceId, request ?? new CreateDeviceUpdateRequest(), actorUser, actorRole, actorTenant, clientIp, ct); if (result.StatusCode == 202) { var locationUrl = $"/api/admin/device-updates/{result.Dto!.UpdateRunId}"; return Accepted(locationUrl, result.Dto); } if (result.StatusCode == 403) { return StatusCode(403, new { errorCode = result.ErrorCode, message = result.ErrorMessage }); } if (result.StatusCode == 404) { return NotFound(new { errorCode = result.ErrorCode, message = result.ErrorMessage }); } if (result.StatusCode == 409) { return Conflict(new { errorCode = result.ErrorCode, updateRunId = result.ExistingUpdateRunId, message = result.ErrorMessage }); } return BadRequest(new { errorCode = result.ErrorCode, message = result.ErrorMessage }); } /// /// 查询指定单次升级任务执行状态 /// [HttpGet("api/admin/device-updates/{updateRunId}")] public async Task GetDeviceUpdateRun( [FromRoute] string updateRunId, CancellationToken ct) { var actorRole = GetActorRole(); var result = await _updateService.GetDeviceUpdateRunAsync(updateRunId, actorRole, ct); if (result.StatusCode == 403) { return StatusCode(403, new { errorCode = result.ErrorCode, message = result.ErrorMessage }); } if (result.StatusCode == 404) { return NotFound(new { errorCode = result.ErrorCode, message = result.ErrorMessage }); } return Ok(result.Data); } /// /// 获取指定设备的历史版本升级任务 (支持页面刷新恢复) /// [HttpGet("api/admin/devices/{deviceId}/updates")] public async Task GetDeviceUpdateHistory( [FromRoute] string deviceId, [FromQuery] int take = 5, CancellationToken ct = default) { var actorRole = GetActorRole(); var result = await _updateService.GetDeviceUpdateHistoryAsync(deviceId, take, actorRole, ct); if (result.StatusCode == 403) { return StatusCode(403, new { errorCode = result.ErrorCode, message = result.ErrorMessage }); } return Ok(result.Data); } }