AdoSmartOpsKpiTargetController.cs 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. using Admin.NET.Plugin.AiDOP.Dto.SmartOps;
  2. using Admin.NET.Plugin.AiDOP.SmartOps;
  3. using Microsoft.AspNetCore.Http;
  4. namespace Admin.NET.Plugin.AiDOP.Controllers;
  5. [ApiController]
  6. [Route("api/smart-ops/kpi-targets")]
  7. [Authorize]
  8. [NonUnify]
  9. public class AdoSmartOpsKpiTargetController : ControllerBase
  10. {
  11. private readonly KpiTargetService _service;
  12. private readonly KpiTargetExcelService _excel;
  13. private readonly KpiTargetMigrationService _migration;
  14. public AdoSmartOpsKpiTargetController(
  15. KpiTargetService service,
  16. KpiTargetExcelService excel,
  17. KpiTargetMigrationService migration)
  18. {
  19. _service = service;
  20. _excel = excel;
  21. _migration = migration;
  22. }
  23. [HttpGet]
  24. public async Task<IActionResult> List([FromQuery] string metricCode, [FromQuery] long? factoryId, CancellationToken ct)
  25. => await Guard(async () => Ok(await _service.ListAsync(metricCode, factoryId, ct)));
  26. [HttpGet("current")]
  27. public async Task<IActionResult> Current([FromQuery] string metricCode, [FromQuery] long factoryId, [FromQuery] DateTime? bizDate, CancellationToken ct)
  28. => await Guard(async () => Ok(await _service.ResolveCurrentAsync(metricCode, factoryId, bizDate, ct)));
  29. [HttpPost]
  30. public async Task<IActionResult> Create([FromBody] KpiTargetCreateDto dto, CancellationToken ct)
  31. => await Guard(async () => Ok(await _service.CreateAsync(dto, KpiTargetConfigSource.Manual, null, ct)));
  32. [HttpPut("{id:long}")]
  33. public async Task<IActionResult> Update(long id, [FromBody] KpiTargetUpdateDto dto, CancellationToken ct)
  34. => await Guard(async () => Ok(await _service.UpdateAsync(id, dto, ct)));
  35. [HttpPost("{id:long}/disable")]
  36. public async Task<IActionResult> Disable(long id, CancellationToken ct)
  37. => await Guard(async () =>
  38. {
  39. await _service.DisableAsync(id, ct);
  40. return Ok(new { ok = true });
  41. });
  42. [HttpGet("template")]
  43. public Task<IActionResult> Template() => _excel.TemplateAsync();
  44. [HttpPost("import/validate")]
  45. public async Task<IActionResult> ImportValidate(IFormFile file, [FromQuery] long factoryId = 0, CancellationToken ct = default)
  46. => await Guard(async () => Ok(await _excel.ValidateAsync(file, factoryId, ct)));
  47. [HttpPost("import/{batchId:long}/commit")]
  48. public async Task<IActionResult> ImportCommit(long batchId, CancellationToken ct)
  49. => await Guard(async () => Ok(await _excel.CommitAsync(batchId, ct)));
  50. [HttpGet("import/{batchId:long}/errors")]
  51. public Task<IActionResult> ImportErrors(long batchId, CancellationToken ct) =>
  52. Guard(() => _excel.ErrorsAsync(batchId, ct));
  53. [HttpGet("legacy-stats")]
  54. public async Task<IActionResult> LegacyStats(CancellationToken ct)
  55. => await Guard(async () => Ok(await _service.LegacyStatsAsync(ct)));
  56. [HttpGet("migration/preview")]
  57. public async Task<IActionResult> MigrationPreview(CancellationToken ct)
  58. => await Guard(async () => Ok(await _migration.PreviewAsync(ct)));
  59. [HttpPost("migration/commit")]
  60. public async Task<IActionResult> MigrationCommit(CancellationToken ct)
  61. => await Guard(async () => Ok(await _migration.CommitAsync(ct)));
  62. private async Task<IActionResult> Guard(Func<Task<IActionResult>> action)
  63. {
  64. try
  65. {
  66. return await action();
  67. }
  68. catch (KpiTargetValidationException ex)
  69. {
  70. return BadRequest(new { message = ex.Message });
  71. }
  72. catch (KpiTargetNotFoundException ex)
  73. {
  74. return NotFound(new { message = ex.Message });
  75. }
  76. catch (KpiTargetConflictException ex)
  77. {
  78. return Conflict(new { message = ex.Message });
  79. }
  80. catch (KpiTargetDuplicateScopeException ex)
  81. {
  82. return Conflict(new { message = ex.Message });
  83. }
  84. }
  85. }