using AsbCloudApp.Data.Subsystems;
using AsbCloudApp.Requests;
using AsbCloudApp.Services;
using AsbCloudApp.Services.Subsystems;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
using AsbCloudInfrastructure;
using System;
using AsbCloudApp.Data;
namespace AsbCloudWebApi.Controllers.Subsystems
{
///
/// Наработка подсистем
///
[Route("api/[controller]")]
[ApiController]
[Authorize]
public class SubsystemOperationTimeController : ControllerBase
{
private readonly ISubsystemOperationTimeService subsystemOperationTimeService;
private readonly IWellService wellService;
private readonly ISubsystemService subsystemService;
public SubsystemOperationTimeController(ISubsystemOperationTimeService subsystemOperationTimeService, IWellService wellService, ISubsystemService subsystemService)
{
this.subsystemOperationTimeService = subsystemOperationTimeService;
this.wellService = wellService;
this.subsystemService = subsystemService;
}
///
/// получить статистику
///
[HttpGet("stat")]
[ProducesResponseType(typeof(IEnumerable), (int)System.Net.HttpStatusCode.OK)]
public async Task GetStatAsync([FromQuery] SubsystemOperationTimeRequest request, CancellationToken token = default)
{
if (!await UserHasAccesToWellAsync(request.IdWell, token))
return Forbid();
if (!await IsValidRequest(request, token))
return BadRequest("Запрашиваемый диапазон должен заканчиваться за 2 часа до текущего времени(после приведения к UTC).");
var subsystemResult = await subsystemOperationTimeService.GetStatAsync(request, token);
return Ok(subsystemResult);
}
///
/// получить список подсистем общий.
///
[HttpGet("subsystem")]
[ProducesResponseType(typeof(IEnumerable), (int)System.Net.HttpStatusCode.OK)]
public async Task GetSubsystemAsync([FromQuery] int? idWell, CancellationToken token = default)
{
if (idWell.HasValue)
if (!await UserHasAccesToWellAsync(idWell.Value, token))
return Forbid();
var result = await subsystemService.GetSubsystemAsync(idWell, token);
return Ok(result);
}
///
/// получить доступный диапазон дат наработки подсистемы.
///
[HttpGet("rangeDate")]
[ProducesResponseType(typeof(DatesRangeDto), (int)System.Net.HttpStatusCode.OK)]
public async Task GetDateRangeOperationTimeAsync([FromQuery] SubsystemOperationTimeRequest request, CancellationToken token = default)
{
if (!await UserHasAccesToWellAsync(request.IdWell, token))
return Forbid();
var result = await subsystemOperationTimeService.GetDateRangeOperationTimeAsync(request, token);
return Ok(result);
}
///
/// получить список наработок подсистем
///
[HttpGet("operationTime")]
[ProducesResponseType(typeof(IEnumerable), (int)System.Net.HttpStatusCode.OK)]
public async Task GetOperationTimeAsync(
[FromQuery] SubsystemOperationTimeRequest request,
CancellationToken token = default)
{
if (!await UserHasAccesToWellAsync(request.IdWell, token))
return Forbid();
if (!await IsValidRequest(request, token))
return BadRequest("Запрашиваемый диапазон должен заканчиваться за 2 часа до текущего времени(после приведения к UTC).");
var result = await subsystemOperationTimeService.GetOperationTimeAsync(request, token);
return Ok(result);
}
///
/// Удалить наработки.
///
///
///
///
[HttpDelete]
[Permission]
[ProducesResponseType(typeof(int), (int)System.Net.HttpStatusCode.OK)]
public async Task DeleteAsync(
[FromQuery] SubsystemOperationTimeRequest request,
CancellationToken token)
{
if (!await UserHasAccesToWellAsync(request.IdWell, token))
return Forbid();
if (!await IsValidRequest(request, token))
return BadRequest("Запрашиваемый диапазон должен заканчиваться за 2 часа до текущего времени(после приведения к UTC).");
var result = await subsystemOperationTimeService.DeleteAsync(request, token);
return Ok(result);
}
protected async Task UserHasAccesToWellAsync(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;
}
protected async Task IsValidRequest(SubsystemOperationTimeRequest request, CancellationToken token)
{
var well = await wellService.GetOrDefaultAsync(request.IdWell, token);
if (well is not null && request.LtDate.HasValue)
{
var ltDate = (DateTimeOffset)request.LtDate;
var utcDateRequest = ltDate.ToRemoteDateTime(well.Timezone.Hours);
if (utcDateRequest.AddHours(2) > DateTime.UtcNow)
{
return false;
}
}
return true;
}
}
}