DD.WellWorkover.Cloud/AsbCloudWebApi/Controllers/OperationStatController.cs
2022-03-16 16:07:37 +05:00

174 lines
7.2 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.Services;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
namespace AsbCloudWebApi.Controllers
{
/// <summary>
/// Контроллер статистики по операциям на скважине
/// </summary>
[ApiController]
[Authorize]
[Route("api")]
public class OperationStatController : ControllerBase
{
private readonly IOperationsStatService operationsStatService;
private readonly IWellService wellService;
public OperationStatController(IOperationsStatService sectionsService, IWellService wellService)
{
this.operationsStatService = sectionsService;
this.wellService = wellService;
}
/// <summary>
/// Формирует данные по среднему и максимальному МСП на кусту по id скважины
/// </summary>
/// <param name="idWell">id скважины с данного куста (через нее будут получены данные)</param>
/// <param name="token"></param>
/// <returns>Возвращает данные по среднему и максимальному МСП на кусту</returns>
[HttpGet("well/{idWell}/ropStat")]
[Permission]
[ProducesResponseType(typeof(ClusterRopStatDto), (int)System.Net.HttpStatusCode.OK)]
public async Task<IActionResult> GetClusterRopStatByIdWellAsync([FromRoute] int idWell,
CancellationToken token = default)
{
if (!await CanUserAccessToWellAsync(idWell, token).ConfigureAwait(false))
return Forbid();
var result = await operationsStatService.GetRopStatAsync(
idWell, token).ConfigureAwait(false);
return Ok(result);
}
/// <summary>
/// Формирует данные по среднему и максимальному МСП на кусту по uid панели
/// </summary>
/// <param name="uid">id передающей данные панели</param>
/// <param name="token"></param>
/// <returns>Возвращает данные по среднему и максимальному МСП на кусту</returns>
[HttpGet("telemetry/{uid}/ropStat")]
[ProducesResponseType(typeof(ClusterRopStatDto), (int)System.Net.HttpStatusCode.OK)]
[AllowAnonymous]
public async Task<IActionResult> GetClusterRopStatByUidAsync([FromRoute] string uid,
CancellationToken token = default)
{
var idWell = wellService.TelemetryService.GetIdWellByTelemetryUid(uid);
if(idWell is null)
return NoContent();
var result = await operationsStatService.GetRopStatAsync(
(int)idWell, token).ConfigureAwait(false);
return Ok(result);
}
/// <summary>
/// Получает статистику по скважинам куста
/// </summary>
/// <param name="idCluster">id куста</param>
/// <param name="token"></param>
/// <returns></returns>
[HttpGet]
[Route("cluster/{idCluster}/stat")] // TODO: Это статистика кластера, перенести в ClusterOperationStatController
[Permission]
[ProducesResponseType(typeof(StatClusterDto), (int)System.Net.HttpStatusCode.OK)]
public async Task<IActionResult> GetStatClusterAsync(int idCluster,
CancellationToken token = default)
{
int? idCompanyOrNull = User.GetCompanyId();
if(idCompanyOrNull is null)
return Forbid();
int idCompany = idCompanyOrNull ?? 0;
// TODO: Fix next commented lines
//if (!await CanUserAccessToWellAsync(idCluster, token).ConfigureAwait(false))
// return Forbid();
var result = await operationsStatService.GetStatClusterAsync(idCluster, idCompany, token)
.ConfigureAwait(false);
return Ok(result);
}
/// <summary>
/// Получает статистику по списку скважин
/// </summary>
/// <param name="idWells">список скважин</param>
/// <param name="token"></param>
/// <returns></returns>
[HttpGet]
[Route("wellsStats")]
[Permission]
[ProducesResponseType(typeof(IEnumerable<StatWellDto>), (int)System.Net.HttpStatusCode.OK)]
public async Task<IActionResult> GetWellsStatAsync([FromQuery]IEnumerable<int> idWells, CancellationToken token = default)
{
var protectedIdWells = idWells.Where(CanUserAccessToWell);
var result = await operationsStatService.GetWellsStatAsync(protectedIdWells, token)
.ConfigureAwait(false);
return Ok(result);
}
/// <summary>
/// Получает статистику по скважине
/// </summary>
/// <param name="idWell">id скважины</param>
/// <param name="token"></param>
/// <returns></returns>
[HttpGet]
[Route("well/{idWell}/stat")]
[Permission]
[ProducesResponseType(typeof(StatWellDto), (int)System.Net.HttpStatusCode.OK)]
public async Task<IActionResult> GetStatWellAsync(int idWell,
CancellationToken token = default)
{
if (!await CanUserAccessToWellAsync(idWell, token).ConfigureAwait(false))
return Forbid();
var result = await operationsStatService.GetWellStatAsync(idWell, token)
.ConfigureAwait(false);
return Ok(result);
}
/// <summary>
/// Получает данные для графика глубина-день
/// </summary>
/// <param name="idWell"></param>
/// <param name="token"></param>
/// <returns></returns>
[HttpGet]
[Route("well/{idWell}/tvd")]
[Permission]
[ProducesResponseType(typeof(IEnumerable<PlanFactPredictBase<WellOperationDto>>), (int)System.Net.HttpStatusCode.OK)]
public async Task<IActionResult> GetTvdAsync(int idWell,
CancellationToken token = default)
{
if (!await CanUserAccessToWellAsync(idWell, token).ConfigureAwait(false))
return Forbid();
var result = await operationsStatService.GetTvdAsync(idWell, token)
.ConfigureAwait(false);
return Ok(result);
}
private bool CanUserAccessToWell(int idWell)
{
int? idCompany = User.GetCompanyId();
return idCompany is not null && wellService.IsCompanyInvolvedInWell((int)idCompany, idWell);
}
private async Task<bool> CanUserAccessToWellAsync(int idWell, CancellationToken token = default)
{
int? idCompany = User.GetCompanyId();
return idCompany is not null && await wellService.IsCompanyInvolvedInWellAsync((int)idCompany,
idWell, token).ConfigureAwait(false);
}
}
}