| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101 |
- 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<IActionResult> List([FromQuery] string metricCode, [FromQuery] long? factoryId, CancellationToken ct)
- => await Guard(async () => Ok(await _service.ListAsync(metricCode, factoryId, ct)));
- [HttpGet("current")]
- public async Task<IActionResult> 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<IActionResult> Create([FromBody] KpiTargetCreateDto dto, CancellationToken ct)
- => await Guard(async () => Ok(await _service.CreateAsync(dto, KpiTargetConfigSource.Manual, null, ct)));
- [HttpPut("{id:long}")]
- public async Task<IActionResult> 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<IActionResult> Disable(long id, CancellationToken ct)
- => await Guard(async () =>
- {
- await _service.DisableAsync(id, ct);
- return Ok(new { ok = true });
- });
- [HttpGet("template")]
- public Task<IActionResult> Template() => _excel.TemplateAsync();
- [HttpPost("import/validate")]
- public async Task<IActionResult> 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<IActionResult> ImportCommit(long batchId, CancellationToken ct)
- => await Guard(async () => Ok(await _excel.CommitAsync(batchId, ct)));
- [HttpGet("import/{batchId:long}/errors")]
- public Task<IActionResult> ImportErrors(long batchId, CancellationToken ct) =>
- Guard(() => _excel.ErrorsAsync(batchId, ct));
- [HttpGet("legacy-stats")]
- public async Task<IActionResult> LegacyStats(CancellationToken ct)
- => await Guard(async () => Ok(await _service.LegacyStatsAsync(ct)));
- [HttpGet("migration/preview")]
- public async Task<IActionResult> MigrationPreview(CancellationToken ct)
- => await Guard(async () => Ok(await _migration.PreviewAsync(ct)));
- [HttpPost("migration/commit")]
- public async Task<IActionResult> MigrationCommit(CancellationToken ct)
- => await Guard(async () => Ok(await _migration.CommitAsync(ct)));
- private async Task<IActionResult> Guard(Func<Task<IActionResult>> 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 });
- }
- }
- }
|