DD.WellWorkover.Cloud/AsbCloudWebApi/Controllers/WellOperationStatController.cs
2021-11-19 11:12:25 +05:00

126 lines
4.9 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 WellOperationStatController : ControllerBase
{
private readonly IWellOperationsStatService operationsStatService;
private readonly IWellService wellService;
public WellOperationStatController(IWellOperationsStatService sectionsService, IWellService wellService)
{
this.operationsStatService = sectionsService;
this.wellService = wellService;
}
/// <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);
}
}
}