DD.WellWorkover.Cloud/AsbCloudWebApi/Controllers/OperationStatController.cs

174 lines
7.2 KiB
C#
Raw Normal View History

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>
2021-11-23 14:07:43 +05:00
[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();
2022-03-16 16:07:37 +05:00
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>
2021-11-23 14:07:43 +05:00
[HttpGet("telemetry/{uid}/ropStat")]
[ProducesResponseType(typeof(ClusterRopStatDto), (int)System.Net.HttpStatusCode.OK)]
2021-11-23 14:07:43 +05:00
[AllowAnonymous]
public async Task<IActionResult> GetClusterRopStatByUidAsync([FromRoute] string uid,
CancellationToken token = default)
{
2022-03-16 16:07:37 +05:00
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]
2021-08-25 11:30:50 +05:00
[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;
2021-08-25 11:30:50 +05:00
// 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]
2021-08-25 11:30:50 +05:00
[Route("well/{idWell}/stat")]
[Permission]
2021-08-25 11:30:50 +05:00
[ProducesResponseType(typeof(StatWellDto), (int)System.Net.HttpStatusCode.OK)]
public async Task<IActionResult> GetStatWellAsync(int idWell,
CancellationToken token = default)
{
2021-08-25 11:30:50 +05:00
if (!await CanUserAccessToWellAsync(idWell, token).ConfigureAwait(false))
return Forbid();
2022-03-02 16:21:07 +05:00
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);
}
}
}