DD.WellWorkover.Cloud/AsbCloudWebApi/Controllers/SAUB/DetectedOperationController.cs
2023-09-28 17:42:30 +05:00

160 lines
6.8 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

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.Linq;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;
namespace AsbCloudWebApi.Controllers.SAUB
{
/// <summary>
/// Операции определенные по телеметрии САУБ
/// </summary>
[Route("api/[controller]")]
[ApiController]
[Authorize]
public class DetectedOperationController : ControllerBase
{
private readonly IDetectedOperationService detectedOperationService;
private readonly IWellService wellService;
public DetectedOperationController(IDetectedOperationService detectedOperationService, IWellService wellService)
{
this.detectedOperationService = detectedOperationService;
this.wellService = wellService;
}
/// <summary>
/// получить справочник операций. Отличается от операций заводимых вручную.
/// При задании id скважины вернет только те операции, которые определились в телеметрии этой скважины.
/// </summary>
/// <param name="idWell">[опционально] id скважины</param>
/// <param name="token"></param>
/// <returns></returns>
[HttpGet("categories")]
[ProducesResponseType(typeof(IEnumerable<WellOperationCategoryDto>), (int)System.Net.HttpStatusCode.OK)]
public async Task<IActionResult> GetCategoriesAsync([FromQuery] int? idWell, CancellationToken token)
{
var result = await detectedOperationService.GetCategoriesAsync(idWell, token);
return Ok(result);
}
/// <summary>
/// Получить фильтрованный список операций по телеметрии САУБ
/// </summary>
/// <param name="request"></param>
/// <param name="token"></param>
/// <returns></returns>
[HttpGet]
[ProducesResponseType(typeof(DetectedOperationListDto), (int)System.Net.HttpStatusCode.OK)]
public async Task<IActionResult> GetAsync(
[FromQuery] DetectedOperationRequest request,
CancellationToken token)
{
if (!await UserHasAccesToWellAsync(request.IdWell, token))
return Forbid();
var result = await detectedOperationService.GetAsync(request, token);
return Ok(result);
}
/// <summary>
/// Получить статистику по фильтрованному списку операций по телеметрии САУБ
/// </summary>
/// <param name="request"></param>
/// <param name="token"></param>
/// <returns></returns>
[HttpGet("stat")]
[ProducesResponseType(typeof(IEnumerable<DetectedOperationStatDto>), (int)System.Net.HttpStatusCode.OK)]
public async Task<IActionResult> GetStatAsync(
[FromQuery] DetectedOperationRequest request,
CancellationToken token)
{
if (!await UserHasAccesToWellAsync(request.IdWell, token))
return Forbid();
var result = await detectedOperationService.GetOperationsStatAsync(request, token);
return Ok(result);
}
/// <summary>
/// Удалить операции.
/// Удаленные операции будут определены повторно сервисом автоматизированного определения операций.
/// Может потребоваться при изменении алгоритмов определения
/// </summary>
/// <param name="request"></param>
/// <param name="token"></param>
/// <returns></returns>
[HttpDelete]
[Permission]
[ProducesResponseType(typeof(int), (int)System.Net.HttpStatusCode.OK)]
public async Task<IActionResult> DeleteAsync(
[FromQuery] DetectedOperationRequest request,
CancellationToken token)
{
if (!await UserHasAccesToWellAsync(request.IdWell, token))
return Forbid();
var result = await detectedOperationService.DeleteAsync(request, token);
return Ok(result);
}
protected async Task<bool> UserHasAccesToWellAsync(int idWell, CancellationToken token)
{
var idCompany = User.GetCompanyId();
if (idCompany is not null &&
await wellService.IsCompanyInvolvedInWellAsync((int)idCompany, idWell, token)
.ConfigureAwait(false))
return true;
return false;
}
/// <summary>
/// Создает excel файл с операциями по скважине
/// </summary>
/// <param name="idWell">id скважины</param>
/// <param name="idCluster"></param>
/// <param name="token"> Токен отмены задачи </param>
/// <returns>Запрашиваемый файл</returns>
[HttpGet("export")]
[Permission]
[ProducesResponseType(typeof(PhysicalFileResult), (int)System.Net.HttpStatusCode.OK, "application/octet-stream")]
[ProducesResponseType(StatusCodes.Status204NoContent)]
[ProducesResponseType(typeof(ValidationProblemDetails), (int)System.Net.HttpStatusCode.BadRequest)]
public async Task<IActionResult> ExportAsync(int? idWell, int? idCluster, CancellationToken token)
{
if (idCluster is null && idWell is null)
return this.ValidationBadRequest(nameof(idWell), $"One of {nameof(idWell)} or {nameof(idCluster)} mast be set.");
int? idCompany = User.GetCompanyId();
if (idCompany is null)
return Forbid();
IEnumerable<int> idsWells;
if (idCluster is not null)
{
var companyWells = await wellService.GetAsync(new() { IdCompany = idCompany }, token);
idsWells = companyWells.Where(w => w.IdCluster == idCluster)
.Select(w=>w.Id);
}
else
{
if (!await wellService.IsCompanyInvolvedInWellAsync((int)idCompany,
idWell!.Value, token).ConfigureAwait(false))
return Forbid();
idsWells = new List<int> { (int)idWell };
}
var stream = await detectedOperationService.ExportAsync(idsWells, token);
var fileName = "operations.xlsx";
return File(stream, fileName);
}
}
}