2022-04-28 15:04:13 +05:00
|
|
|
|
using AsbCloudApp.Data;
|
2022-08-04 15:06:17 +05:00
|
|
|
|
using AsbCloudApp.Data.DetectedOperation;
|
2022-06-15 14:57:37 +05:00
|
|
|
|
using AsbCloudApp.Requests;
|
2022-04-28 15:04:13 +05:00
|
|
|
|
using AsbCloudApp.Services;
|
2022-06-17 17:21:14 +05:00
|
|
|
|
using Microsoft.AspNetCore.Authorization;
|
2022-04-28 15:04:13 +05:00
|
|
|
|
using Microsoft.AspNetCore.Mvc;
|
|
|
|
|
using System.Collections.Generic;
|
2022-08-04 15:06:17 +05:00
|
|
|
|
using System.Linq;
|
2022-04-28 15:04:13 +05:00
|
|
|
|
using System.Threading;
|
2022-06-15 14:57:37 +05:00
|
|
|
|
using System.Threading.Tasks;
|
2023-08-01 11:17:46 +05:00
|
|
|
|
using Microsoft.AspNetCore.Http;
|
2022-04-28 15:04:13 +05:00
|
|
|
|
|
|
|
|
|
namespace AsbCloudWebApi.Controllers.SAUB
|
|
|
|
|
{
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Операции определенные по телеметрии САУБ
|
|
|
|
|
/// </summary>
|
2022-06-17 17:21:14 +05:00
|
|
|
|
[Route("api/[controller]")]
|
2022-04-28 15:04:13 +05:00
|
|
|
|
[ApiController]
|
2022-06-17 17:21:14 +05:00
|
|
|
|
[Authorize]
|
2022-04-28 15:04:13 +05:00
|
|
|
|
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>
|
2022-06-17 17:21:14 +05:00
|
|
|
|
/// получить справочник операций. Отличается от операций заводимых вручную.
|
|
|
|
|
/// При задании id скважины вернет только те операции, которые определились в телеметрии этой скважины.
|
2022-04-28 15:04:13 +05:00
|
|
|
|
/// </summary>
|
2022-06-17 17:21:14 +05:00
|
|
|
|
/// <param name="idWell">[опционально] id скважины</param>
|
2022-04-28 15:04:13 +05:00
|
|
|
|
/// <param name="token"></param>
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
[HttpGet("categories")]
|
|
|
|
|
[ProducesResponseType(typeof(IEnumerable<WellOperationCategoryDto>), (int)System.Net.HttpStatusCode.OK)]
|
2023-07-03 14:33:34 +05:00
|
|
|
|
public async Task<IActionResult> GetCategoriesAsync([FromQuery] int? idWell, CancellationToken token)
|
2022-04-28 15:04:13 +05:00
|
|
|
|
{
|
2022-06-17 17:21:14 +05:00
|
|
|
|
var result = await detectedOperationService.GetCategoriesAsync(idWell, token);
|
2022-04-28 15:04:13 +05:00
|
|
|
|
return Ok(result);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Получить фильтрованный список операций по телеметрии САУБ
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="request"></param>
|
|
|
|
|
/// <param name="token"></param>
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
[HttpGet]
|
2022-06-14 15:35:31 +05:00
|
|
|
|
[ProducesResponseType(typeof(DetectedOperationListDto), (int)System.Net.HttpStatusCode.OK)]
|
2022-04-28 15:04:13 +05:00
|
|
|
|
public async Task<IActionResult> GetAsync(
|
|
|
|
|
[FromQuery] DetectedOperationRequest request,
|
2023-07-03 14:33:34 +05:00
|
|
|
|
CancellationToken token)
|
2022-04-28 15:04:13 +05:00
|
|
|
|
{
|
2022-06-17 17:21:14 +05:00
|
|
|
|
if (!await UserHasAccesToWellAsync(request.IdWell, token))
|
2022-04-28 15:04:13 +05:00
|
|
|
|
return Forbid();
|
|
|
|
|
|
2022-06-17 17:21:14 +05:00
|
|
|
|
var result = await detectedOperationService.GetAsync(request, token);
|
2022-04-28 15:04:13 +05:00
|
|
|
|
return Ok(result);
|
|
|
|
|
}
|
|
|
|
|
|
2022-08-04 15:06:17 +05:00
|
|
|
|
/// <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,
|
2023-07-03 14:33:34 +05:00
|
|
|
|
CancellationToken token)
|
2022-08-04 15:06:17 +05:00
|
|
|
|
{
|
|
|
|
|
if (!await UserHasAccesToWellAsync(request.IdWell, token))
|
|
|
|
|
return Forbid();
|
|
|
|
|
|
|
|
|
|
var result = await detectedOperationService.GetOperationsStatAsync(request, token);
|
|
|
|
|
return Ok(result);
|
|
|
|
|
}
|
|
|
|
|
|
2022-04-28 15:04:13 +05:00
|
|
|
|
/// <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,
|
2022-06-17 17:21:14 +05:00
|
|
|
|
CancellationToken token)
|
2022-04-28 15:04:13 +05:00
|
|
|
|
{
|
2022-06-17 17:21:14 +05:00
|
|
|
|
if (!await UserHasAccesToWellAsync(request.IdWell, token))
|
2022-04-28 15:04:13 +05:00
|
|
|
|
return Forbid();
|
|
|
|
|
|
2022-06-17 17:21:14 +05:00
|
|
|
|
var result = await detectedOperationService.DeleteAsync(request, token);
|
2022-04-28 15:04:13 +05:00
|
|
|
|
return Ok(result);
|
|
|
|
|
}
|
2022-06-17 17:21:14 +05:00
|
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
}
|
2022-08-04 15:06:17 +05:00
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Создает excel файл с операциями по скважине
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="idWell">id скважины</param>
|
|
|
|
|
/// <param name="idCluster"></param>
|
|
|
|
|
/// <param name="token"> Токен отмены задачи </param>
|
|
|
|
|
/// <returns>Запрашиваемый файл</returns>
|
2023-08-08 12:21:09 +05:00
|
|
|
|
[HttpGet("export")]
|
2022-08-04 15:06:17 +05:00
|
|
|
|
[Permission]
|
2023-08-01 11:17:46 +05:00
|
|
|
|
[ProducesResponseType(typeof(PhysicalFileResult), (int)System.Net.HttpStatusCode.OK, "application/octet-stream")]
|
|
|
|
|
[ProducesResponseType(StatusCodes.Status204NoContent)]
|
2023-07-03 14:33:34 +05:00
|
|
|
|
public async Task<IActionResult> ExportAsync(int? idWell, int? idCluster, CancellationToken token)
|
2022-08-04 15:06:17 +05:00
|
|
|
|
{
|
|
|
|
|
if (idCluster is null && idWell is null)
|
|
|
|
|
return this.MakeBadRequest(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)
|
|
|
|
|
{
|
2023-02-15 18:02:36 +05:00
|
|
|
|
var companyWells = await wellService.GetAsync(new() { IdCompany = idCompany }, token);
|
2022-08-04 15:06:17 +05:00
|
|
|
|
idsWells = companyWells.Where(w => w.IdCluster == idCluster)
|
|
|
|
|
.Select(w=>w.Id);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
if (!await wellService.IsCompanyInvolvedInWellAsync((int)idCompany,
|
2023-05-17 12:31:29 +05:00
|
|
|
|
idWell!.Value, token).ConfigureAwait(false))
|
2022-08-04 15:06:17 +05:00
|
|
|
|
return Forbid();
|
|
|
|
|
idsWells = new List<int> { (int)idWell };
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var stream = await detectedOperationService.ExportAsync(idsWells, token);
|
|
|
|
|
var fileName = "operations.xlsx";
|
2023-08-01 11:17:46 +05:00
|
|
|
|
return File(stream, fileName);
|
2022-08-04 15:06:17 +05:00
|
|
|
|
}
|
2022-04-28 15:04:13 +05:00
|
|
|
|
}
|
|
|
|
|
}
|