DD.WellWorkover.Cloud/AsbCloudWebApi/Controllers/Subsystems/SubsystemController.cs

110 lines
4.0 KiB
C#
Raw Permalink Normal View History

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.Linq;
using System.Threading;
using System.Threading.Tasks;
2024-08-19 10:01:07 +05:00
namespace AsbCloudWebApi.Controllers.Subsystems;
/// <summary>
/// Наработка подсистем
/// </summary>
[Route("api/[controller]")]
[ApiController]
[Authorize]
public class SubsystemController : ControllerBase
{
2024-08-19 10:01:07 +05:00
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;
}
/// <summary>
2024-08-19 10:01:07 +05:00
/// получить статистику
/// </summary>
2024-08-19 10:01:07 +05:00
[HttpGet("stat")]
[ProducesResponseType(typeof(IEnumerable<SubsystemStatDto>), (int)System.Net.HttpStatusCode.OK)]
[ProducesResponseType(typeof(ValidationProblemDetails), (int)System.Net.HttpStatusCode.BadRequest)]
public async Task<IActionResult> GetStatAsync([FromQuery] SubsystemRequest request, CancellationToken token)
{
2024-08-19 10:01:07 +05:00
if (!await UserHasAccessToWellAsync(request.IdWell, token))
return Forbid();
var subsystemResult = await subsystemService.GetStatAsync(request, token);
return Ok(subsystemResult);
}
2024-08-19 10:01:07 +05:00
/// <summary>
/// получить период, за который будет рассчитываться статистика
/// </summary>
[HttpGet("operationsPeriod/{idWell}")]
[ProducesResponseType(typeof(DatesRangeDto), (int)System.Net.HttpStatusCode.OK)]
public async Task<IActionResult> GetStatDateRangeAsync([FromRoute] int idWell, CancellationToken token)
{
if (!await UserHasAccessToWellAsync(idWell, token))
return Forbid();
2024-08-19 10:01:07 +05:00
var dateRange = telemetryDataSaubService.GetRange(idWell);
2024-08-19 10:01:07 +05:00
return Ok(dateRange);
}
2024-08-19 10:01:07 +05:00
[HttpGet("drillerDetectedOperationStat")]
[ProducesResponseType(typeof(IEnumerable<DrillerDetectedOperationStatDto>), (int)System.Net.HttpStatusCode.OK)]
public async Task<IActionResult> GetByWellsAsync([FromQuery] GetStatRequest request,
CancellationToken token)
{
if (!request.IdsWells.Any())
return NoContent();
2024-08-19 10:01:07 +05:00
foreach(var idWell in request.IdsWells)
if (!await UserHasAccessToWellAsync(idWell, token))
return Forbid();
2024-08-19 10:01:07 +05:00
var result = await subsystemService.GetByWellsAsync(request, token);
2024-08-19 10:01:07 +05:00
return Ok(result);
}
2024-09-18 12:59:25 +05:00
/// <summary>
/// получить статистику по плановым и фактическим подсистемам на скважинах
/// </summary>
[HttpGet("/api/serviceWork/subsystemWells")]
[Permission("SubsystemStatPlanFact.get")]
2024-09-18 12:59:25 +05:00
[ProducesResponseType(typeof(IEnumerable<SubsystemPlanFactStatDto>), (int)System.Net.HttpStatusCode.OK)]
public async Task<IActionResult> GetStatPlanFactAsync([FromQuery] SubsystemPlanFactRequest request, CancellationToken token)
{
foreach (var idWell in request.IdsWell)
{
if (!await UserHasAccessToWellAsync(idWell, token))
return Forbid();
}
var subsystemStat = await subsystemService.GetStatPlanFactByWellsAsync(request, token);
return Ok(subsystemStat);
}
2024-08-19 10:01:07 +05:00
private async Task<bool> 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;
}
}