forked from ddrilling/AsbCloudServer
154 lines
7.1 KiB
C#
154 lines
7.1 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;
|
|
using AsbCloudApp.Data;
|
|
|
|
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 IsValidRequest(request, token))
|
|
return BadRequest("Запрашиваемый диапазон должен заканчиваться за 2 часа до текущего времени(после приведения к UTC).");
|
|
var subsystemResult = await subsystemOperationTimeService.GetStatAsync(request, token);
|
|
return Ok(subsystemResult);
|
|
}
|
|
|
|
/// <summary>
|
|
/// получить статистику по активным скважинам
|
|
/// </summary>
|
|
[HttpGet("statByActiveWell")]
|
|
[ProducesResponseType(typeof(IEnumerable<SubsystemStatDto>), (int)System.Net.HttpStatusCode.OK)]
|
|
public async Task<IActionResult> GetStatByWellAsync([FromQuery] DateTime? GtDate, DateTime? LtDate, CancellationToken token = default)
|
|
{
|
|
|
|
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("datesRange")]
|
|
[ProducesResponseType(typeof(DatesRangeDto), (int)System.Net.HttpStatusCode.OK)]
|
|
public async Task<IActionResult> 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);
|
|
}
|
|
|
|
/// <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 IsValidRequest(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 IsValidRequest(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> 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;
|
|
}
|
|
}
|
|
}
|