DD.WellWorkover.Cloud/AsbCloudWebApi/Controllers/Subsystems/SubsystemOperationTimeController.cs
eugeniy_ivanov 1613f9a9f4 - корректировка модели SubsystemStatDto
- корректировка метода GetDepthInterval
- корректировка валидатора в контроллере SubsystemOperationTime
2022-09-21 13:06:40 +05:00

128 lines
5.7 KiB
C#

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;
namespace AsbCloudWebApi.Controllers.Subsystems
{
/// <summary>
/// Наработка подсистем
/// </summary>
[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;
}
/// <summary>
/// получить статистику
/// </summary>
[HttpGet("stat")]
[ProducesResponseType(typeof(IEnumerable<SubsystemStatDto>), (int)System.Net.HttpStatusCode.OK)]
public async Task<IActionResult> GetStatAsync([FromQuery] SubsystemOperationTimeRequest request, CancellationToken token = default)
{
if (!await UserHasAccesToWellAsync(request.IdWell, token))
return Forbid();
if (!await TimeRequestValidate(request, token))
return BadRequest("Запрашиваемый диапазон должен заканчиваться за 2 часа до текущего времени(после приведения к UTC).");
var subsystemResult = await subsystemOperationTimeService.GetStatAsync(request, token);
return Ok(subsystemResult);
}
/// <summary>
/// получить список подсистем общий.
/// </summary>
[HttpGet("subsystem")]
[ProducesResponseType(typeof(IEnumerable<SubsystemDto>), (int)System.Net.HttpStatusCode.OK)]
public async Task<IActionResult> 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);
}
/// <summary>
/// получить список наработок подсистем
/// </summary>
[HttpGet("operationTime")]
[ProducesResponseType(typeof(IEnumerable<SubsystemOperationTimeDto>), (int)System.Net.HttpStatusCode.OK)]
public async Task<IActionResult> GetOperationTimeAsync(
[FromQuery] SubsystemOperationTimeRequest request,
CancellationToken token = default)
{
if (!await UserHasAccesToWellAsync(request.IdWell, token))
return Forbid();
if (!await TimeRequestValidate(request, token))
return BadRequest("Запрашиваемый диапазон должен заканчиваться за 2 часа до текущего времени(после приведения к UTC).");
var result = await subsystemOperationTimeService.GetOperationTimeAsync(request, token);
return Ok(result);
}
/// <summary>
/// Удалить наработки.
/// </summary>
/// <param name="request"></param>
/// <param name="token"></param>
/// <returns></returns>
[HttpDelete]
[Permission]
[ProducesResponseType(typeof(int), (int)System.Net.HttpStatusCode.OK)]
public async Task<IActionResult> DeleteAsync(
[FromQuery] SubsystemOperationTimeRequest request,
CancellationToken token)
{
if (!await UserHasAccesToWellAsync(request.IdWell, token))
return Forbid();
if (!await TimeRequestValidate(request, token))
return BadRequest("Запрашиваемый диапазон должен заканчиваться за 2 часа до текущего времени(после приведения к UTC).");
var result = await subsystemOperationTimeService.DeleteAsync(request, token);
return Ok(result);
}
protected async Task<bool> 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<bool> TimeRequestValidate(SubsystemOperationTimeRequest request, CancellationToken token)
{
var well = await wellService.GetOrDefaultAsync(request.IdWell, token);
if (well is not null)
{
var ltDate = (DateTimeOffset)request.LtDate;
var utcDateRequest = ltDate.ToRemoteDateTime(well.Timezone.Hours);
if (utcDateRequest.AddHours(2) < DateTime.UtcNow)
{
return true;
}
}
return false;
}
}
}