using AsbCloudApp.Data;
using AsbCloudApp.Data.Subsystems;
using AsbCloudApp.Requests;
using AsbCloudApp.Services;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
namespace AsbCloudWebApi.Controllers.Subsystems
{
///
/// Наработка подсистем
///
[Route("api/[controller]")]
[ApiController]
[Authorize]
public class SubsystemController : ControllerBase
{
private readonly ISubsystemService subsystemService;
private readonly ITelemetryDataSaubService telemetryDataSaubService;
private readonly IWellService wellService;
public SubsystemController(
ISubsystemService subsystemService,
IWellService wellService,
ITelemetryDataSaubService telemetryDataSaubService)
{
this.subsystemService = subsystemService;
this.wellService = wellService;
this.telemetryDataSaubService = telemetryDataSaubService;
}
///
/// получить статистику
///
[HttpGet("stat")]
[ProducesResponseType(typeof(IEnumerable), (int)System.Net.HttpStatusCode.OK)]
[ProducesResponseType(typeof(ValidationProblemDetails), (int)System.Net.HttpStatusCode.BadRequest)]
public async Task GetStatAsync([FromQuery] SubsystemRequest request, CancellationToken token)
{
if (!await UserHasAccessToWellAsync(request.IdWell, token))
return Forbid();
var subsystemResult = await subsystemService.GetStatAsync(request, token);
return Ok(subsystemResult);
}
///
/// получить период, за который будет рассчитываться статистика
///
[HttpGet("operationsPeriod/{idWell}")]
[ProducesResponseType(typeof(DatesRangeDto), (int)System.Net.HttpStatusCode.OK)]
public async Task GetStatDateRangeAsync([FromRoute] int idWell, CancellationToken token)
{
if (!await UserHasAccessToWellAsync(idWell, token))
return Forbid();
var dateRange = telemetryDataSaubService.GetRange(idWell);
return Ok(dateRange);
}
[HttpGet("operationsReport/{idWell}")]
[ProducesResponseType(typeof(DrillerDetectedOperationStatDto), (int)System.Net.HttpStatusCode.OK)]
public async Task GetStatDateRangeAsync([FromRoute] int idWell, GetStatRequest request,
CancellationToken token)
{
if (!await UserHasAccessToWellAsync(idWell, token))
return Forbid();
var result = await subsystemService.GetByWellsAsync(request, token);
return Ok(result);
}
private async Task 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;
}
}
}