forked from ddrilling/AsbCloudServer
144 lines
6.3 KiB
C#
144 lines
6.3 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.Threading;
|
||
using System.Threading.Tasks;
|
||
using AsbCloudInfrastructure.Services.DetectOperations;
|
||
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;
|
||
private readonly DetectedOperationExportService detectedOperationExportService;
|
||
|
||
public DetectedOperationController(IDetectedOperationService detectedOperationService, IWellService wellService,
|
||
DetectedOperationExportService detectedOperationExportService)
|
||
{
|
||
this.detectedOperationService = detectedOperationService;
|
||
this.wellService = wellService;
|
||
this.detectedOperationExportService = detectedOperationExportService;
|
||
}
|
||
|
||
/// <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] DetectedOperationByWellRequest request,
|
||
CancellationToken token)
|
||
{
|
||
if (!await UserHasAccessToWellAsync(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] DetectedOperationByWellRequest request,
|
||
CancellationToken token)
|
||
{
|
||
if (!await UserHasAccessToWellAsync(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] DetectedOperationByWellRequest request,
|
||
CancellationToken token)
|
||
{
|
||
if (!await UserHasAccessToWellAsync(request.IdWell, token))
|
||
return Forbid();
|
||
|
||
var result = await detectedOperationService.DeleteAsync(request, token);
|
||
return Ok(result);
|
||
}
|
||
|
||
protected async Task<bool> UserHasAccessToWellAsync(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="token"></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, 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");
|
||
}
|
||
}
|
||
}
|