using AsbCloudApp.Data; using AsbCloudApp.Data.DetectedOperation; using AsbCloudApp.Requests; using AsbCloudApp.Services; using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Mvc; using System.Collections.Generic; using System.Threading; using System.Threading.Tasks; using AsbCloudApp.Exceptions; using AsbCloudApp.Repositories; using AsbCloudInfrastructure.Services.DetectOperations; using Microsoft.AspNetCore.Http; namespace AsbCloudWebApi.Controllers.SAUB { /// /// Операции определенные по телеметрии САУБ /// [Route("api/well/{idWell}/[controller]")] [ApiController] [Authorize] public class DetectedOperationController : ControllerBase { private readonly IDetectedOperationRepository detectedOperationRepository; private readonly IDetectedOperationService detectedOperationService; private readonly IWellService wellService; private readonly DetectedOperationExportService detectedOperationExportService; public DetectedOperationController(IDetectedOperationService detectedOperationService, IWellService wellService, DetectedOperationExportService detectedOperationExportService, IDetectedOperationRepository detectedOperationRepository) { this.detectedOperationService = detectedOperationService; this.wellService = wellService; this.detectedOperationExportService = detectedOperationExportService; this.detectedOperationRepository = detectedOperationRepository; } /// /// Добавить операции /// /// /// /// /// [HttpPost] [ProducesResponseType(typeof(int), StatusCodes.Status200OK)] public async Task InsertRangeAsync(int idWell, IEnumerable dtos, CancellationToken token) { var idUser = await AssertUserHasAccessToWellAsync(idWell, token); var result = await detectedOperationRepository.InsertRange(idUser, dtos, token); return Ok(result); } /// /// Обновить операции /// /// /// /// /// [HttpPut] [ProducesResponseType(typeof(int), StatusCodes.Status200OK)] public async Task UpdateRangeAsync(int idWell, IEnumerable dtos, CancellationToken token) { var idUser = await AssertUserHasAccessToWellAsync(idWell, token); var result = await detectedOperationRepository.UpdateRange(idUser, dtos, token); return Ok(result); } /// /// Удалить операции /// /// /// /// /// [HttpDelete] [ProducesResponseType(typeof(int), StatusCodes.Status200OK)] public async Task DeleteRangeAsync(int idWell, IEnumerable ids, CancellationToken token) { await AssertUserHasAccessToWellAsync(idWell, token); var result = await detectedOperationRepository.DeleteRange(ids, token); return Ok(result); } /// /// получить справочник операций. Отличается от операций заводимых вручную. /// При задании id скважины вернет только те операции, которые определились в телеметрии этой скважины. /// /// [опционально] id скважины /// /// [HttpGet] [Route("/api/well/[controller]/categories")] [ProducesResponseType(typeof(IEnumerable), StatusCodes.Status200OK)] public async Task GetCategoriesAsync([FromQuery] int? idWell, CancellationToken token) { var result = await detectedOperationService.GetCategoriesAsync(idWell, token); return Ok(result); } /// /// Получить фильтрованный список операций по телеметрии САУБ /// /// /// /// /// [HttpGet] [ProducesResponseType(typeof(DetectedOperationDto), StatusCodes.Status200OK)] public async Task GetAsync(int idWell, [FromQuery] DetectedOperationRequest request, CancellationToken token) { await AssertUserHasAccessToWellAsync(idWell, token); var well = await wellService.GetOrDefaultAsync(idWell, token); if (well?.IdTelemetry is null) return NoContent(); var requestToService = new DetectedOperationByTelemetryRequest(well.IdTelemetry.Value, request); var result = await detectedOperationRepository.Get(requestToService, token); return Ok(result); } /// /// Получить статистику по фильтрованному списку операций по телеметрии САУБ /// /// /// /// /// [HttpGet("stat")] [ProducesResponseType(typeof(IEnumerable), StatusCodes.Status200OK)] public async Task GetStatAsync(int idWell, [FromQuery] DetectedOperationRequest request, CancellationToken token) { await AssertUserHasAccessToWellAsync(idWell, token); var requestToService = new DetectedOperationByWellRequest(idWell, request); var result = await detectedOperationService.GetOperationsStatAsync(requestToService, token); return Ok(result); } /// /// Создает excel файл с операциями по скважине /// /// id скважины /// [HttpGet("export")] [Permission] [ProducesResponseType(typeof(PhysicalFileResult), StatusCodes.Status200OK, "application/octet-stream")] [ProducesResponseType(StatusCodes.Status204NoContent)] [ProducesResponseType(typeof(ValidationProblemDetails), (int)System.Net.HttpStatusCode.BadRequest)] public async Task ExportAsync(int idWell, CancellationToken token) { var idCompany = User.GetCompanyId(); if (idCompany is null) return Forbid(); var host = $"{HttpContext.Request.Scheme}://{HttpContext.Request.Host}"; var stream = await detectedOperationExportService.ExportAsync(idWell, host, token); return File(stream, "application/octet-stream", "operations.xlsx"); } private async Task AssertUserHasAccessToWellAsync(int idWell, CancellationToken token) { var idUser = User.GetUserId(); var idCompany = User.GetCompanyId(); if (!idUser.HasValue) throw new ForbidException("Неизвестный пользователь"); if (!idCompany.HasValue) throw new ForbidException("Нет доступа к скважине"); if (!await wellService.IsCompanyInvolvedInWellAsync(idCompany.Value, idWell, token)) throw new ForbidException("Нет доступа к скважине"); return idUser.Value; } } }