using Admin.NET.Plugin.AiDOP.Dto.SmartOps; using Admin.NET.Plugin.AiDOP.SmartOps; using Microsoft.AspNetCore.Http; namespace Admin.NET.Plugin.AiDOP.Controllers; [ApiController] [Route("api/smart-ops/kpi-targets")] [Authorize] [NonUnify] public class AdoSmartOpsKpiTargetController : ControllerBase { private readonly KpiTargetService _service; private readonly KpiTargetExcelService _excel; private readonly KpiTargetMigrationService _migration; public AdoSmartOpsKpiTargetController( KpiTargetService service, KpiTargetExcelService excel, KpiTargetMigrationService migration) { _service = service; _excel = excel; _migration = migration; } [HttpGet] public async Task List([FromQuery] string metricCode, [FromQuery] long? factoryId, CancellationToken ct) => await Guard(async () => Ok(await _service.ListAsync(metricCode, factoryId, ct))); [HttpGet("current")] public async Task Current([FromQuery] string metricCode, [FromQuery] long factoryId, [FromQuery] DateTime? bizDate, CancellationToken ct) => await Guard(async () => Ok(await _service.ResolveCurrentAsync(metricCode, factoryId, bizDate, ct))); [HttpPost] public async Task Create([FromBody] KpiTargetCreateDto dto, CancellationToken ct) => await Guard(async () => Ok(await _service.CreateAsync(dto, KpiTargetConfigSource.Manual, null, ct))); [HttpPut("{id:long}")] public async Task Update(long id, [FromBody] KpiTargetUpdateDto dto, CancellationToken ct) => await Guard(async () => Ok(await _service.UpdateAsync(id, dto, ct))); [HttpPost("{id:long}/disable")] public async Task Disable(long id, CancellationToken ct) => await Guard(async () => { await _service.DisableAsync(id, ct); return Ok(new { ok = true }); }); [HttpGet("template")] public Task Template() => _excel.TemplateAsync(); [HttpPost("import/validate")] public async Task ImportValidate(IFormFile file, [FromQuery] long factoryId = 0, CancellationToken ct = default) => await Guard(async () => Ok(await _excel.ValidateAsync(file, factoryId, ct))); [HttpPost("import/{batchId:long}/commit")] public async Task ImportCommit(long batchId, CancellationToken ct) => await Guard(async () => Ok(await _excel.CommitAsync(batchId, ct))); [HttpGet("import/{batchId:long}/errors")] public Task ImportErrors(long batchId, CancellationToken ct) => Guard(() => _excel.ErrorsAsync(batchId, ct)); [HttpGet("legacy-stats")] public async Task LegacyStats(CancellationToken ct) => await Guard(async () => Ok(await _service.LegacyStatsAsync(ct))); [HttpGet("migration/preview")] public async Task MigrationPreview(CancellationToken ct) => await Guard(async () => Ok(await _migration.PreviewAsync(ct))); [HttpPost("migration/commit")] public async Task MigrationCommit(CancellationToken ct) => await Guard(async () => Ok(await _migration.CommitAsync(ct))); private async Task Guard(Func> action) { try { return await action(); } catch (KpiTargetValidationException ex) { return BadRequest(new { message = ex.Message }); } catch (KpiTargetNotFoundException ex) { return NotFound(new { message = ex.Message }); } catch (KpiTargetConflictException ex) { return Conflict(new { message = ex.Message }); } catch (KpiTargetDuplicateScopeException ex) { return Conflict(new { message = ex.Message }); } } }