forked from ddrilling/AsbCloudServer
167 lines
7.1 KiB
C#
167 lines
7.1 KiB
C#
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")]
|
||
[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.GetRopStatByIdWellAsync(
|
||
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)
|
||
{
|
||
// if (!await CanUserAccessToWellAsync(idWell, token).ConfigureAwait(false))
|
||
// return Forbid();
|
||
|
||
var result = await operationsStatService.GetRopStatByUidAsync(
|
||
uid, 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
|
||
[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")]
|
||
[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")]
|
||
[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.GetStatWellAsync(idWell, token)
|
||
.ConfigureAwait(false);
|
||
return Ok(result);
|
||
}
|
||
|
||
/// <summary>
|
||
/// Получает данные для графика глубина-днь
|
||
/// </summary>
|
||
/// <param name="idWell"></param>
|
||
/// <param name="token"></param>
|
||
/// <returns></returns>
|
||
[HttpGet]
|
||
[Route("well/{idWell}/tvd")]
|
||
[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);
|
||
}
|
||
}
|
||
}
|