DD.WellWorkover.Cloud/AsbCloudWebApi/Controllers/SAUB/DetectedOperationController.cs

144 lines
6.3 KiB
C#
Raw Normal View History

2022-04-28 15:04:13 +05:00
using AsbCloudApp.Data;
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;
using Microsoft.AspNetCore.Authorization;
2022-04-28 15:04:13 +05:00
using Microsoft.AspNetCore.Mvc;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
2022-04-28 15:04:13 +05:00
using System.Threading;
2022-06-15 14:57:37 +05:00
using System.Threading.Tasks;
using AsbCloudInfrastructure.Services.DetectOperations;
using Microsoft.AspNetCore.Http;
2022-04-28 15:04:13 +05:00
namespace AsbCloudWebApi.Controllers.SAUB
{
/// <summary>
/// Операции определенные по телеметрии САУБ
/// </summary>
[Route("api/[controller]")]
2022-04-28 15:04:13 +05:00
[ApiController]
[Authorize]
2022-04-28 15:04:13 +05:00
public class DetectedOperationController : ControllerBase
{
private readonly IDetectedOperationService detectedOperationService;
private readonly IWellService wellService;
private readonly DetectedOperationExportService detectedOperationExportService;
public DetectedOperationController(IDetectedOperationService detectedOperationService, IWellService wellService,
DetectedOperationExportService detectedOperationExportService)
2022-04-28 15:04:13 +05:00
{
this.detectedOperationService = detectedOperationService;
this.wellService = wellService;
this.detectedOperationExportService = detectedOperationExportService;
2022-04-28 15:04:13 +05:00
}
/// <summary>
/// получить справочник операций. Отличается от операций заводимых вручную.
/// При задании id скважины вернет только те операции, которые определились в телеметрии этой скважины.
2022-04-28 15:04:13 +05:00
/// </summary>
/// <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)]
public async Task<IActionResult> GetCategoriesAsync([FromQuery] int? idWell, CancellationToken token)
2022-04-28 15:04:13 +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]
[ProducesResponseType(typeof(DetectedOperationListDto), (int)System.Net.HttpStatusCode.OK)]
2022-04-28 15:04:13 +05:00
public async Task<IActionResult> GetAsync(
[FromQuery] DetectedOperationRequest request,
CancellationToken token)
2022-04-28 15:04:13 +05:00
{
if (!await UserHasAccesToWellAsync(request.IdWell, token))
2022-04-28 15:04:13 +05:00
return Forbid();
var result = await detectedOperationService.GetAsync(request, 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("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);
}
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,
CancellationToken token)
2022-04-28 15:04:13 +05:00
{
if (!await UserHasAccesToWellAsync(request.IdWell, token))
2022-04-28 15:04:13 +05:00
return Forbid();
var result = await detectedOperationService.DeleteAsync(request, token);
2022-04-28 15:04:13 +05:00
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="idDomain">Идентификатор домена</param>
[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, [Range(1, 2)]int idDomain, CancellationToken token)
{
var idCompany = User.GetCompanyId();
if (idCompany is null)
return Forbid();
var stream = await detectedOperationExportService.ExportAsync(idWell, idDomain, token);
return File(stream, "application/octet-stream", "operations.xlsx");
}
2022-04-28 15:04:13 +05:00
}
}