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("statByActiveWell")] [ProducesResponseType(typeof(IEnumerable), (int)System.Net.HttpStatusCode.OK)] public async Task GetStatByWellAsync(DateTime? gtDate, DateTime? ltDate, CancellationToken token) { var idCompany = User.GetCompanyId(); if (!idCompany.HasValue) return Forbid(); var subsystemResult = await subsystemService.GetStatByActiveWells(idCompany.Value, gtDate, ltDate, token); return Ok(subsystemResult); } 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; } } }