forked from ddrilling/AsbCloudServer
772360cb6e
Add Controller.ValidationBadRequest(..)
159 lines
6.7 KiB
C#
159 lines
6.7 KiB
C#
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)]
|
||
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);
|
||
}
|
||
}
|
||
}
|