using AsbCloudApp.Data;
using AsbCloudApp.Data.DrillTestReport;
using AsbCloudApp.Data.SAUB;
using AsbCloudApp.Repositories;
using AsbCloudApp.Requests;
using AsbCloudApp.Services;
using AsbCloudWebApi.SignalR;
using AsbCloudWebApi.SignalR.Clients;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.SignalR;
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Net;
using System.Threading;
using System.Threading.Tasks;
namespace AsbCloudWebApi.Controllers;
///
/// Контроллер для drill_test отчётов
///
[ApiController]
[Authorize]
public class DrillTestController : ControllerBase
{
private readonly IDrillTestReportService drillTestReportService;
private readonly IDrillTestRepository drillTestRepository;
private readonly IWellService wellService;
private readonly ITelemetryService telemetryService;
private readonly IHubContext telemetryHubContext;
public DrillTestController(
IDrillTestReportService drillTestReportService,
IDrillTestRepository drillTestRepository,
IWellService wellService,
ITelemetryService telemetryService,
IHubContext telemetryHubContext)
{
this.drillTestReportService = drillTestReportService;
this.drillTestRepository = drillTestRepository;
this.wellService = wellService;
this.telemetryService = telemetryService;
this.telemetryHubContext = telemetryHubContext;
}
///
/// Метод получения данных drill_test и drill_test_params от панели оператора.
/// Сохраняет в БД.
///
/// уникальный идентификатор записи drill_test
/// записи drill test
///
///
[AllowAnonymous]
[HttpPost("api/telemetry/{uid}/[controller]")]
public async Task PostDataAsync(
string uid,
[FromBody] IEnumerable dtos,
CancellationToken token)
{
var telemetry = telemetryService.GetOrCreateTelemetryByUid(uid);
if (telemetry is null)
throw new Exception($"Telemetry with RemoteUid: {uid} does not exist.");
await drillTestRepository.SaveDataAsync(telemetry.Id, dtos, token);
var idWell = telemetryService.GetIdWellByTelemetryUid(uid);
if (idWell is not null)
_ = Task.Run(async () =>
{
var clients = telemetryHubContext.Clients.Group($"well_{idWell}");
await clients.ReceiveDrilltestData(dtos, token);
}, CancellationToken.None);
return Ok();
}
///
/// Формирование отчёта
///
/// Id скважины
/// Ключ entity test записи
///
///
[HttpGet("api/well/{idWell}/[controller]")]
[ProducesResponseType(typeof(PhysicalFileResult), (int)HttpStatusCode.OK, "application/octet-stream")]
[ProducesResponseType(StatusCodes.Status204NoContent)]
public async Task GenerateReportAsync([FromRoute] int idWell,
[FromQuery] int id,
CancellationToken cancellationToken)
{
if (!await CanUserAccessToWellAsync(idWell, cancellationToken))
return Forbid();
var reportFile = await drillTestReportService.GenerateAsync(idWell, id, cancellationToken);
return File(reportFile.stream, "application/octet-stream", reportFile.fileName);
}
///
/// Список файлов drill test отчётов
///
/// Id скважины
/// Параметры запроса
///
///
[HttpGet("api/well/{idWell}/[controller]/all")]
[ProducesResponseType(typeof(PaginationContainer), (int)HttpStatusCode.OK)]
public async Task GetListAsync([FromRoute][Required] int idWell,
[FromQuery] FileReportRequest request,
CancellationToken cancellationToken)
{
if (!await CanUserAccessToWellAsync(idWell, cancellationToken))
return Forbid();
var reports = await drillTestReportService.GetListAsync(idWell,
request,
cancellationToken);
return Ok(reports);
}
private async Task CanUserAccessToWellAsync(int idWell, CancellationToken cancellationToken)
{
int? idCompany = User.GetCompanyId();
return idCompany is not null && await wellService.IsCompanyInvolvedInWellAsync((int)idCompany,
idWell, cancellationToken).ConfigureAwait(false);
}
}