2023-10-20 11:24:04 +05:00
|
|
|
using AsbCloudApp.Data;
|
2023-10-23 13:43:29 +05:00
|
|
|
using AsbCloudApp.Data.DrillTestReport;
|
|
|
|
using AsbCloudApp.Data.SAUB;
|
|
|
|
using AsbCloudApp.Repositories;
|
2023-10-20 11:24:04 +05:00
|
|
|
using AsbCloudApp.Requests;
|
|
|
|
using AsbCloudApp.Services;
|
2023-10-23 13:43:29 +05:00
|
|
|
using AsbCloudWebApi.SignalR;
|
2023-10-30 12:13:38 +05:00
|
|
|
using AsbCloudWebApi.SignalR.Clients;
|
2023-10-20 11:24:04 +05:00
|
|
|
using Microsoft.AspNetCore.Authorization;
|
|
|
|
using Microsoft.AspNetCore.Http;
|
|
|
|
using Microsoft.AspNetCore.Mvc;
|
2023-10-23 13:43:29 +05:00
|
|
|
using Microsoft.AspNetCore.SignalR;
|
|
|
|
using System;
|
2024-04-22 13:40:14 +05:00
|
|
|
using System.Collections.Generic;
|
2023-10-20 11:24:04 +05:00
|
|
|
using System.ComponentModel.DataAnnotations;
|
|
|
|
using System.Net;
|
|
|
|
using System.Threading;
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
|
|
|
namespace AsbCloudWebApi.Controllers;
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Контроллер для drill_test отчётов
|
|
|
|
/// </summary>
|
|
|
|
[ApiController]
|
|
|
|
[Authorize]
|
2023-10-23 13:43:29 +05:00
|
|
|
public class DrillTestController : ControllerBase
|
2023-10-20 11:24:04 +05:00
|
|
|
{
|
|
|
|
private readonly IDrillTestReportService drillTestReportService;
|
2023-10-23 13:43:29 +05:00
|
|
|
private readonly IDrillTestRepository drillTestRepository;
|
2023-10-20 11:24:04 +05:00
|
|
|
private readonly IWellService wellService;
|
2023-10-23 13:43:29 +05:00
|
|
|
private readonly ITelemetryService telemetryService;
|
2023-10-30 12:13:38 +05:00
|
|
|
private readonly IHubContext<TelemetryHub, ITelemetryHubClient> telemetryHubContext;
|
2023-10-20 11:24:04 +05:00
|
|
|
|
2023-10-23 13:43:29 +05:00
|
|
|
|
|
|
|
public DrillTestController(
|
|
|
|
IDrillTestReportService drillTestReportService,
|
|
|
|
IDrillTestRepository drillTestRepository,
|
|
|
|
IWellService wellService,
|
|
|
|
ITelemetryService telemetryService,
|
2023-10-30 12:13:38 +05:00
|
|
|
IHubContext<TelemetryHub, ITelemetryHubClient> telemetryHubContext)
|
2023-10-20 11:24:04 +05:00
|
|
|
{
|
|
|
|
this.drillTestReportService = drillTestReportService;
|
2023-10-23 13:43:29 +05:00
|
|
|
this.drillTestRepository = drillTestRepository;
|
2023-10-20 11:24:04 +05:00
|
|
|
this.wellService = wellService;
|
2023-10-23 13:43:29 +05:00
|
|
|
this.telemetryService = telemetryService;
|
|
|
|
this.telemetryHubContext = telemetryHubContext;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Метод получения данных drill_test и drill_test_params от панели оператора.
|
|
|
|
/// Сохраняет в БД.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="uid">уникальный идентификатор записи drill_test</param>
|
2024-04-22 13:40:14 +05:00
|
|
|
/// <param name="dtos">записи drill test</param>
|
2023-10-23 13:43:29 +05:00
|
|
|
/// <param name="token"></param>
|
|
|
|
/// <returns></returns>
|
|
|
|
[AllowAnonymous]
|
|
|
|
[HttpPost("api/telemetry/{uid}/[controller]")]
|
|
|
|
public async Task<IActionResult> PostDataAsync(
|
2024-04-22 13:40:14 +05:00
|
|
|
string uid,
|
|
|
|
[FromBody] IEnumerable<DrillTestBaseDto> dtos,
|
2023-10-23 13:43:29 +05:00
|
|
|
CancellationToken token)
|
|
|
|
{
|
|
|
|
var telemetry = telemetryService.GetOrCreateTelemetryByUid(uid);
|
|
|
|
if (telemetry is null)
|
|
|
|
throw new Exception($"Telemetry with RemoteUid: {uid} does not exist.");
|
|
|
|
|
2024-04-22 13:40:14 +05:00
|
|
|
await drillTestRepository.SaveDataAsync(telemetry.Id, dtos, token);
|
2023-10-23 13:43:29 +05:00
|
|
|
var idWell = telemetryService.GetIdWellByTelemetryUid(uid);
|
|
|
|
if (idWell is not null)
|
|
|
|
_ = Task.Run(async () =>
|
|
|
|
{
|
|
|
|
var clients = telemetryHubContext.Clients.Group($"well_{idWell}");
|
2024-04-22 13:40:14 +05:00
|
|
|
await clients.ReceiveDrilltestData(dtos, token);
|
2023-10-23 13:43:29 +05:00
|
|
|
}, CancellationToken.None);
|
|
|
|
|
|
|
|
return Ok();
|
2023-10-20 11:24:04 +05:00
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Формирование отчёта
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="idWell">Id скважины</param>
|
|
|
|
/// <param name="id">Ключ entity test записи</param>
|
|
|
|
/// <param name="cancellationToken"></param>
|
|
|
|
/// <returns></returns>
|
2023-10-23 13:43:29 +05:00
|
|
|
[HttpGet("api/well/{idWell}/[controller]")]
|
2023-10-20 11:24:04 +05:00
|
|
|
[ProducesResponseType(typeof(PhysicalFileResult), (int)HttpStatusCode.OK, "application/octet-stream")]
|
|
|
|
[ProducesResponseType(StatusCodes.Status204NoContent)]
|
|
|
|
public async Task<IActionResult> 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);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Список файлов drill test отчётов
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="idWell">Id скважины</param>
|
|
|
|
/// <param name="request">Параметры запроса</param>
|
|
|
|
/// <param name="cancellationToken"></param>
|
|
|
|
/// <returns></returns>
|
2023-10-23 13:43:29 +05:00
|
|
|
[HttpGet("api/well/{idWell}/[controller]/all")]
|
|
|
|
[ProducesResponseType(typeof(PaginationContainer<DrillTestReportInfoDto>), (int)HttpStatusCode.OK)]
|
2023-10-20 11:24:04 +05:00
|
|
|
public async Task<IActionResult> 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<bool> CanUserAccessToWellAsync(int idWell, CancellationToken cancellationToken)
|
|
|
|
{
|
|
|
|
int? idCompany = User.GetCompanyId();
|
|
|
|
return idCompany is not null && await wellService.IsCompanyInvolvedInWellAsync((int)idCompany,
|
|
|
|
idWell, cancellationToken).ConfigureAwait(false);
|
|
|
|
}
|
|
|
|
}
|