2022-12-27 14:30:52 +05:00
|
|
|
|
using AsbCloudApp.Data.ProcessMap;
|
2023-02-02 12:04:50 +05:00
|
|
|
|
using AsbCloudApp.Repositories;
|
2022-04-11 18:00:34 +05:00
|
|
|
|
using AsbCloudApp.Services;
|
2023-05-29 17:50:20 +05:00
|
|
|
|
using AsbCloudWebApi.SignalR;
|
2022-04-11 18:00:34 +05:00
|
|
|
|
using Microsoft.AspNetCore.Authorization;
|
|
|
|
|
using Microsoft.AspNetCore.Mvc;
|
2023-05-29 17:50:20 +05:00
|
|
|
|
using Microsoft.AspNetCore.SignalR;
|
2021-10-14 10:18:43 +05:00
|
|
|
|
using System;
|
2021-10-13 17:34:32 +05:00
|
|
|
|
using System.Collections.Generic;
|
2023-07-18 14:09:54 +05:00
|
|
|
|
using System.ComponentModel.DataAnnotations;
|
|
|
|
|
using System.IO;
|
2023-06-30 17:31:11 +05:00
|
|
|
|
using System.Linq;
|
2021-10-13 17:34:32 +05:00
|
|
|
|
using System.Threading;
|
|
|
|
|
using System.Threading.Tasks;
|
2023-07-18 14:09:54 +05:00
|
|
|
|
using Microsoft.AspNetCore.Http;
|
2021-10-13 17:34:32 +05:00
|
|
|
|
|
|
|
|
|
namespace AsbCloudWebApi.Controllers
|
|
|
|
|
{
|
|
|
|
|
/// <summary>
|
2022-12-07 08:49:21 +05:00
|
|
|
|
/// РТК
|
2021-10-13 17:34:32 +05:00
|
|
|
|
/// </summary>
|
|
|
|
|
[ApiController]
|
2022-06-09 11:19:52 +05:00
|
|
|
|
[Route("api/[controller]")]
|
2021-10-14 16:16:35 +05:00
|
|
|
|
[Authorize]
|
2023-04-03 14:59:59 +05:00
|
|
|
|
public class ProcessMapController : CrudWellRelatedController<ProcessMapPlanDto, IProcessMapPlanRepository>
|
2021-10-13 17:34:32 +05:00
|
|
|
|
{
|
|
|
|
|
private readonly ITelemetryService telemetryService;
|
2023-05-29 17:50:20 +05:00
|
|
|
|
private readonly IHubContext<TelemetryHub> telemetryHubContext;
|
2023-04-03 14:59:59 +05:00
|
|
|
|
private readonly IProcessMapReportMakerService processMapReportService;
|
2023-09-05 16:23:40 +05:00
|
|
|
|
private readonly IProcessMapService processMapService;
|
2023-07-18 14:09:54 +05:00
|
|
|
|
private readonly IProcessMapPlanImportService processMapPlanImportService;
|
2023-09-05 16:23:40 +05:00
|
|
|
|
private readonly IUserRepository userRepository;
|
2021-10-13 17:34:32 +05:00
|
|
|
|
|
2023-05-29 18:03:15 +05:00
|
|
|
|
private const string SirnalRMethodGetDataName = "UpdateProcessMap";
|
2023-05-29 17:50:20 +05:00
|
|
|
|
|
2023-02-08 10:42:54 +05:00
|
|
|
|
public ProcessMapController(
|
2023-02-08 15:46:16 +05:00
|
|
|
|
IWellService wellService,
|
2023-04-03 14:59:59 +05:00
|
|
|
|
IProcessMapPlanRepository repository,
|
|
|
|
|
IProcessMapReportMakerService processMapReportService,
|
2023-09-05 16:23:40 +05:00
|
|
|
|
IProcessMapService processMapService,
|
2023-05-29 17:50:20 +05:00
|
|
|
|
ITelemetryService telemetryService,
|
2023-07-18 14:09:54 +05:00
|
|
|
|
IHubContext<TelemetryHub> telemetryHubContext,
|
2023-09-05 16:23:40 +05:00
|
|
|
|
IProcessMapPlanImportService processMapPlanImportService,
|
|
|
|
|
IUserRepository userRepository)
|
2022-12-14 08:41:19 +05:00
|
|
|
|
: base(wellService, repository)
|
2021-10-13 17:34:32 +05:00
|
|
|
|
{
|
|
|
|
|
this.telemetryService = telemetryService;
|
2023-05-29 17:50:20 +05:00
|
|
|
|
this.telemetryHubContext = telemetryHubContext;
|
2022-12-14 08:41:19 +05:00
|
|
|
|
this.processMapReportService = processMapReportService;
|
2023-02-08 10:42:54 +05:00
|
|
|
|
this.processMapService = processMapService;
|
2023-07-18 14:09:54 +05:00
|
|
|
|
this.processMapPlanImportService = processMapPlanImportService;
|
2023-09-05 16:23:40 +05:00
|
|
|
|
this.userRepository = userRepository;
|
2023-02-08 10:42:54 +05:00
|
|
|
|
|
2021-10-13 17:34:32 +05:00
|
|
|
|
}
|
2022-04-11 18:00:34 +05:00
|
|
|
|
|
2021-10-13 17:34:32 +05:00
|
|
|
|
/// <summary>
|
2022-01-05 17:50:45 +05:00
|
|
|
|
/// Возвращает все значения для коридоров бурения по uid панели
|
2021-10-13 17:34:32 +05:00
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="uid"> uid панели </param>
|
2021-10-14 10:18:43 +05:00
|
|
|
|
/// <param name="updateFrom"> Дата, с которой следует искать новые параметры </param>
|
2021-10-13 17:34:32 +05:00
|
|
|
|
/// <param name="token"> Токен отмены задачи </param>
|
2022-01-05 17:50:45 +05:00
|
|
|
|
/// <returns> Список параметров для коридоров бурения </returns>
|
2023-08-08 12:21:09 +05:00
|
|
|
|
[HttpGet("/api/telemetry/{uid}/drillFlowChart")]
|
2022-12-07 12:39:13 +05:00
|
|
|
|
[Obsolete("use GetByUidAsync(..) instead")]
|
2021-10-14 16:16:35 +05:00
|
|
|
|
[AllowAnonymous]
|
2023-03-31 16:57:20 +05:00
|
|
|
|
[ProducesResponseType(typeof(IEnumerable<ProcessMapPlanDto>), (int)System.Net.HttpStatusCode.OK)]
|
2022-12-07 12:39:13 +05:00
|
|
|
|
public IActionResult GetByTelemetry(string uid, DateTime updateFrom, CancellationToken token)
|
|
|
|
|
{
|
|
|
|
|
var idWell = telemetryService.GetIdWellByTelemetryUid(uid);
|
|
|
|
|
if (idWell is null)
|
|
|
|
|
return BadRequest($"Wrong uid {uid}");
|
2023-06-30 17:31:11 +05:00
|
|
|
|
return Ok(Enumerable.Empty<ProcessMapPlanDto>());
|
2022-12-07 12:39:13 +05:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Возвращает РТК по uid телеметрии
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="uid"> uid телеметрии </param>
|
|
|
|
|
/// <param name="updateFrom"> Дата, с которой следует искать новые параметры </param>
|
|
|
|
|
/// <param name="token"> Токен отмены задачи </param>
|
|
|
|
|
/// <returns> Список параметров для коридоров бурения </returns>
|
2023-08-08 12:21:09 +05:00
|
|
|
|
[HttpGet("/api/telemetry/{uid}/processMap")]
|
2022-12-07 12:39:13 +05:00
|
|
|
|
[AllowAnonymous]
|
2023-03-31 16:57:20 +05:00
|
|
|
|
[ProducesResponseType(typeof(IEnumerable<ProcessMapPlanDto>), (int)System.Net.HttpStatusCode.OK)]
|
2022-12-07 12:39:13 +05:00
|
|
|
|
public async Task<IActionResult> GetByUidAsync(string uid, DateTime updateFrom, CancellationToken token)
|
2021-10-13 17:34:32 +05:00
|
|
|
|
{
|
2021-12-07 18:27:52 +05:00
|
|
|
|
var idWell = telemetryService.GetIdWellByTelemetryUid(uid);
|
2021-10-14 16:16:35 +05:00
|
|
|
|
if (idWell is null)
|
|
|
|
|
return BadRequest($"Wrong uid {uid}");
|
2022-04-11 18:00:34 +05:00
|
|
|
|
|
2022-06-09 11:19:52 +05:00
|
|
|
|
var dto = await service.GetAllAsync((int)idWell,
|
2021-10-14 10:18:43 +05:00
|
|
|
|
updateFrom, token);
|
2022-04-11 18:00:34 +05:00
|
|
|
|
|
2021-10-13 17:34:32 +05:00
|
|
|
|
return Ok(dto);
|
|
|
|
|
}
|
2022-04-11 18:00:34 +05:00
|
|
|
|
|
2022-12-05 12:39:25 +05:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Выгрузка расширенной РТК
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="wellId"></param>
|
2022-12-14 08:41:19 +05:00
|
|
|
|
/// /// <param name="token"></param>
|
2022-12-05 12:39:25 +05:00
|
|
|
|
/// <returns></returns>
|
2023-08-08 12:21:09 +05:00
|
|
|
|
[HttpGet("report/{wellId}")]
|
2023-08-01 11:17:46 +05:00
|
|
|
|
[ProducesResponseType(typeof(PhysicalFileResult), (int)System.Net.HttpStatusCode.OK, "application/octet-stream")]
|
|
|
|
|
[ProducesResponseType(StatusCodes.Status204NoContent)]
|
2022-12-14 08:41:19 +05:00
|
|
|
|
public async Task<IActionResult> GetReportFileAsync(int wellId, CancellationToken token)
|
2022-12-05 12:39:25 +05:00
|
|
|
|
{
|
2022-12-14 08:41:19 +05:00
|
|
|
|
var stream = await processMapReportService.MakeReportAsync(wellId, token);
|
|
|
|
|
if (stream != null)
|
|
|
|
|
{
|
|
|
|
|
var well = await wellService.GetOrDefaultAsync(wellId, token);
|
|
|
|
|
if (well is null)
|
|
|
|
|
return NoContent();
|
2023-08-01 11:17:46 +05:00
|
|
|
|
|
|
|
|
|
var fileName = $"РТК по скважине {well.Caption} куст {well.Cluster}.xlsx";
|
|
|
|
|
return File(stream, fileName);
|
2022-12-14 08:41:19 +05:00
|
|
|
|
}
|
2023-08-01 11:17:46 +05:00
|
|
|
|
|
|
|
|
|
return NoContent();
|
2022-12-05 12:39:25 +05:00
|
|
|
|
}
|
2022-12-07 08:47:41 +05:00
|
|
|
|
|
2023-02-08 10:42:54 +05:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Выгрузка режимной карты по бурению скважины
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="wellId"></param>
|
|
|
|
|
/// <param name="token"></param>
|
|
|
|
|
/// <returns></returns>
|
2023-08-08 12:21:09 +05:00
|
|
|
|
[HttpGet("report/{wellId}/data")]
|
2023-02-08 10:42:54 +05:00
|
|
|
|
[ProducesResponseType(typeof(IEnumerable<ProcessMapReportDto>), (int)System.Net.HttpStatusCode.OK)]
|
|
|
|
|
public async Task<IActionResult> GetDrillProcessMap(int wellId, CancellationToken token)
|
|
|
|
|
{
|
2023-04-06 10:25:51 +05:00
|
|
|
|
var data = await processMapService.GetProcessMapReportAsync(wellId, token);
|
2023-02-08 15:46:16 +05:00
|
|
|
|
return Ok(data);
|
2023-02-08 10:42:54 +05:00
|
|
|
|
}
|
|
|
|
|
|
2022-12-07 08:47:41 +05:00
|
|
|
|
/// <summary>
|
2023-07-19 09:17:59 +05:00
|
|
|
|
/// Добавить запись плановой РТК
|
2022-12-07 08:47:41 +05:00
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="value"></param>
|
|
|
|
|
/// <param name="token"></param>
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
[HttpPost]
|
2023-03-31 16:57:20 +05:00
|
|
|
|
public override async Task<ActionResult<int>> InsertAsync([FromBody] ProcessMapPlanDto value, CancellationToken token)
|
2022-12-07 08:47:41 +05:00
|
|
|
|
{
|
2023-09-05 16:23:40 +05:00
|
|
|
|
if (!await CanUserEditProcessMapAsync(value.IdWell, token))
|
|
|
|
|
return Forbid();
|
|
|
|
|
|
2022-12-07 08:47:41 +05:00
|
|
|
|
value.IdUser = User.GetUserId() ?? -1;
|
2023-05-29 17:50:20 +05:00
|
|
|
|
var result = await base.InsertAsync(value, token);
|
2023-05-31 15:58:30 +05:00
|
|
|
|
await NotifyUsersBySignalR(value.IdWell, token);
|
2023-05-29 17:50:20 +05:00
|
|
|
|
return result;
|
2022-12-07 08:47:41 +05:00
|
|
|
|
}
|
|
|
|
|
|
2022-12-07 12:39:13 +05:00
|
|
|
|
/// <summary>
|
2023-07-19 09:17:59 +05:00
|
|
|
|
/// Редактировать запись по id плановой РТК
|
2022-12-07 08:47:41 +05:00
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="value">запись</param>
|
|
|
|
|
/// <param name="token"></param>
|
|
|
|
|
/// <returns>1 - успешно отредактировано, 0 - нет</returns>
|
|
|
|
|
[HttpPut]
|
2023-03-31 16:57:20 +05:00
|
|
|
|
public override async Task<ActionResult<int>> UpdateAsync([FromBody] ProcessMapPlanDto value, CancellationToken token)
|
2022-12-07 08:47:41 +05:00
|
|
|
|
{
|
2023-09-05 16:23:40 +05:00
|
|
|
|
if (!await CanUserEditProcessMapAsync(value.IdWell, token))
|
|
|
|
|
return Forbid();
|
|
|
|
|
|
2022-12-07 08:47:41 +05:00
|
|
|
|
value.IdUser = User.GetUserId() ?? -1;
|
2023-05-29 17:50:20 +05:00
|
|
|
|
var result = await base.UpdateAsync(value, token);
|
2023-05-31 15:58:30 +05:00
|
|
|
|
await NotifyUsersBySignalR(value.IdWell, token);
|
2023-05-29 17:50:20 +05:00
|
|
|
|
return result;
|
2022-12-07 08:47:41 +05:00
|
|
|
|
}
|
2023-05-19 16:51:41 +05:00
|
|
|
|
|
2023-07-18 14:09:54 +05:00
|
|
|
|
/// <summary>
|
2023-07-19 09:17:59 +05:00
|
|
|
|
/// Возвращает шаблон файла импорта плановой РТК
|
2023-07-18 14:09:54 +05:00
|
|
|
|
/// </summary>
|
|
|
|
|
/// <returns>Запрашиваемый файл</returns>
|
2023-08-08 12:21:09 +05:00
|
|
|
|
[HttpGet("template")]
|
2023-08-01 11:17:46 +05:00
|
|
|
|
[ProducesResponseType(typeof(PhysicalFileResult), (int)System.Net.HttpStatusCode.OK, "application/octet-stream")]
|
2023-07-18 14:09:54 +05:00
|
|
|
|
public async Task<IActionResult> GetTemplateAsync(CancellationToken cancellationToken)
|
|
|
|
|
{
|
|
|
|
|
var stream = await processMapPlanImportService.GetExcelTemplateStreamAsync(cancellationToken);
|
|
|
|
|
var fileName = "ЕЦП_шаблон_файла_РТК.xlsx";
|
2023-08-10 12:08:02 +05:00
|
|
|
|
return File(stream, "application/octet-stream", fileName);
|
2023-07-18 14:09:54 +05:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
2023-07-19 09:17:59 +05:00
|
|
|
|
/// Импортирует плановой РТК из excel (xlsx) файла
|
2023-07-18 14:09:54 +05:00
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="idWell">Id скважины</param>
|
2023-07-24 16:00:40 +05:00
|
|
|
|
/// <param name="options">Удалить РТК перед импортом = 1, если файл валидный</param>
|
2023-07-18 14:09:54 +05:00
|
|
|
|
/// <param name="file">Загружаемый файл</param>
|
|
|
|
|
/// <param name="cancellationToken"></param>
|
|
|
|
|
/// <returns></returns>
|
2023-08-08 12:21:09 +05:00
|
|
|
|
[HttpPost("import/{idWell}/{options}")]
|
2023-07-18 14:09:54 +05:00
|
|
|
|
public async Task<IActionResult> ImportAsync(int idWell,
|
2023-07-24 16:00:40 +05:00
|
|
|
|
int options,
|
2023-07-18 14:09:54 +05:00
|
|
|
|
[Required] IFormFile file,
|
|
|
|
|
CancellationToken cancellationToken)
|
|
|
|
|
{
|
|
|
|
|
int? idUser = User.GetUserId();
|
2023-09-05 16:23:40 +05:00
|
|
|
|
|
2023-07-18 14:09:54 +05:00
|
|
|
|
if (idUser is null)
|
|
|
|
|
return Forbid();
|
|
|
|
|
|
2023-09-05 16:23:40 +05:00
|
|
|
|
if (!await CanUserEditProcessMapAsync(idWell, cancellationToken))
|
|
|
|
|
return Forbid();
|
|
|
|
|
|
2023-07-18 14:09:54 +05:00
|
|
|
|
if (Path.GetExtension(file.FileName).ToLower() != ".xlsx")
|
|
|
|
|
return BadRequest("Требуется xlsx файл.");
|
|
|
|
|
|
|
|
|
|
using Stream stream = file.OpenReadStream();
|
|
|
|
|
|
2023-07-26 18:23:53 +05:00
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
await processMapPlanImportService.ImportAsync(idWell,
|
|
|
|
|
idUser.Value,
|
|
|
|
|
(options & 1) > 0,
|
|
|
|
|
stream,
|
|
|
|
|
cancellationToken);
|
|
|
|
|
}
|
|
|
|
|
catch (FileFormatException ex)
|
|
|
|
|
{
|
|
|
|
|
return BadRequest(ex.Message);
|
|
|
|
|
}
|
2023-07-18 14:09:54 +05:00
|
|
|
|
|
|
|
|
|
return Ok();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
2023-07-19 09:17:59 +05:00
|
|
|
|
/// Экспорт плановой РТК в excel
|
2023-07-18 14:09:54 +05:00
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="idWell">Id скважины</param>
|
|
|
|
|
/// <param name="cancellationToken"></param>
|
|
|
|
|
/// <returns></returns>
|
2023-08-08 12:21:09 +05:00
|
|
|
|
[HttpGet("export/{idWell}")]
|
2023-08-01 11:17:46 +05:00
|
|
|
|
[ProducesResponseType(typeof(PhysicalFileResult), (int)System.Net.HttpStatusCode.OK, "application/octet-stream")]
|
|
|
|
|
[ProducesResponseType(StatusCodes.Status204NoContent)]
|
2023-07-24 16:00:40 +05:00
|
|
|
|
public async Task<IActionResult> ExportAsync(int idWell, CancellationToken cancellationToken)
|
2023-07-18 14:09:54 +05:00
|
|
|
|
{
|
|
|
|
|
int? idUser = User.GetUserId();
|
|
|
|
|
|
|
|
|
|
if (idUser is null)
|
|
|
|
|
return Forbid();
|
|
|
|
|
|
|
|
|
|
var well = await wellService.GetOrDefaultAsync(idWell, cancellationToken);
|
|
|
|
|
|
|
|
|
|
if (well is null)
|
|
|
|
|
return NoContent();
|
|
|
|
|
|
|
|
|
|
var stream = await processMapPlanImportService.ExportAsync(idWell, cancellationToken);
|
|
|
|
|
var fileName = $"РТК-план по скважине {well.Caption} куст {well.Cluster}.xlsx";
|
2023-08-10 12:08:02 +05:00
|
|
|
|
return File(stream, "application/octet-stream", fileName);
|
2023-07-18 14:09:54 +05:00
|
|
|
|
}
|
2023-09-05 16:23:40 +05:00
|
|
|
|
|
|
|
|
|
private async Task<bool> CanUserEditProcessMapAsync(int idWell, CancellationToken token)
|
|
|
|
|
{
|
|
|
|
|
var idUser = User.GetUserId();
|
|
|
|
|
|
|
|
|
|
var well = await wellService.GetOrDefaultAsync(idWell, token);
|
|
|
|
|
|
|
|
|
|
if (!idUser.HasValue || well is null)
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
return well.IdState != 2 || userRepository.HasPermission(idUser.Value, "ProcessMap.editCompletedWell");
|
|
|
|
|
}
|
|
|
|
|
|
2023-05-29 17:50:20 +05:00
|
|
|
|
private async Task NotifyUsersBySignalR(int idWell, CancellationToken token)
|
|
|
|
|
{
|
|
|
|
|
var dtos = await service.GetAllAsync(idWell, null, token);
|
|
|
|
|
_ = Task.Run(() => telemetryHubContext.Clients.Group($"well_{idWell}")
|
|
|
|
|
.SendAsync(SirnalRMethodGetDataName, dtos), CancellationToken.None);
|
|
|
|
|
}
|
|
|
|
|
}
|
2021-10-13 17:34:32 +05:00
|
|
|
|
}
|