2022-12-22 18:08:58 +05:00
|
|
|
|
using AsbCloudApp.Data;
|
2023-02-13 09:10:48 +05:00
|
|
|
|
using AsbCloudApp.Repositories;
|
2022-12-22 18:08:58 +05:00
|
|
|
|
using AsbCloudApp.Services;
|
|
|
|
|
using Microsoft.AspNetCore.Authorization;
|
|
|
|
|
using Microsoft.AspNetCore.Http;
|
|
|
|
|
using Microsoft.AspNetCore.Mvc;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.IO;
|
|
|
|
|
using System.Threading;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
|
|
|
|
|
namespace AsbCloudWebApi.Controllers
|
|
|
|
|
{
|
|
|
|
|
#nullable enable
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Плановая траектория (загрузка и хранение)
|
|
|
|
|
/// </summary>
|
|
|
|
|
[Route("api/well/{idWell}/plannedTrajectory")]
|
|
|
|
|
[ApiController]
|
|
|
|
|
[Authorize]
|
|
|
|
|
public class PlannedTrajectoryController : ControllerBase
|
|
|
|
|
{
|
|
|
|
|
private readonly IWellService wellService;
|
|
|
|
|
private readonly IPlannedTrajectoryImportService plannedTrajectoryImportService;
|
2023-02-13 09:10:48 +05:00
|
|
|
|
private readonly IPlannedTrajectoryRepository plannedTrajectoryRepository;
|
2023-02-13 09:19:11 +05:00
|
|
|
|
private readonly ITrajectoryVisualizationService trajectoryVisualizationService;
|
2022-12-22 18:08:58 +05:00
|
|
|
|
|
2023-02-13 09:19:11 +05:00
|
|
|
|
public PlannedTrajectoryController(IWellService wellService,
|
|
|
|
|
IPlannedTrajectoryImportService plannedTrajectoryImportService,
|
|
|
|
|
IPlannedTrajectoryRepository plannedTrajectoryRepository,
|
|
|
|
|
ITrajectoryVisualizationService trajectoryVisualizationService)
|
2022-12-22 18:08:58 +05:00
|
|
|
|
{
|
|
|
|
|
this.plannedTrajectoryImportService = plannedTrajectoryImportService;
|
|
|
|
|
this.wellService = wellService;
|
2023-02-13 09:10:48 +05:00
|
|
|
|
this.plannedTrajectoryRepository = plannedTrajectoryRepository;
|
2023-02-13 09:19:11 +05:00
|
|
|
|
this.trajectoryVisualizationService = trajectoryVisualizationService;
|
2022-12-22 18:08:58 +05:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Возвращает шаблон для заполнения строк плановой траектории
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <returns>Запрашиваемый файл</returns>
|
|
|
|
|
[HttpGet]
|
|
|
|
|
[Route("template")]
|
|
|
|
|
[AllowAnonymous]
|
|
|
|
|
[ProducesResponseType(typeof(PhysicalFileResult), (int)System.Net.HttpStatusCode.OK)]
|
2022-12-25 23:16:36 +05:00
|
|
|
|
public IActionResult GetTemplate()
|
2022-12-22 18:08:58 +05:00
|
|
|
|
{
|
|
|
|
|
var stream = plannedTrajectoryImportService.GetTemplateFile();
|
|
|
|
|
var fileName = "ЕЦП_шаблон_файла_плановая_траектория.xlsx";
|
|
|
|
|
return File(stream, "application/octet-stream", fileName);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Формируем excel файл с текущими строками плановой траектории
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="idWell">id скважины</param>
|
|
|
|
|
/// <param name="token"> Токен отмены задачи </param>
|
|
|
|
|
/// <returns>Запрашиваемый файл</returns>
|
|
|
|
|
[HttpGet]
|
|
|
|
|
[Route("export")]
|
|
|
|
|
[ProducesResponseType(typeof(PhysicalFileResult), (int)System.Net.HttpStatusCode.OK)]
|
|
|
|
|
public async Task<IActionResult> ExportAsync([FromRoute] int idWell, CancellationToken token = default)
|
|
|
|
|
{
|
2022-12-27 00:02:49 +05:00
|
|
|
|
if (!await CanUserAccessToWellAsync(idWell,
|
|
|
|
|
token).ConfigureAwait(false))
|
2022-12-22 18:08:58 +05:00
|
|
|
|
return Forbid();
|
2022-12-27 00:02:49 +05:00
|
|
|
|
var stream = await plannedTrajectoryImportService.ExportAsync(idWell, token);
|
2023-01-12 12:07:31 +05:00
|
|
|
|
var fileName = await plannedTrajectoryImportService.GetFileNameAsync(idWell, token);
|
2022-12-22 18:08:58 +05:00
|
|
|
|
return File(stream, "application/octet-stream", fileName);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Импортирует координаты из excel (xlsx) файла
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="idWell">id скважины</param>
|
|
|
|
|
/// <param name="files">Коллекция из одного файла xlsx</param>
|
2023-01-12 16:22:36 +05:00
|
|
|
|
/// <param name="deleteBeforeImport">Удалить операции перед импортом, если фал валидный</param>
|
2022-12-22 18:08:58 +05:00
|
|
|
|
/// <param name="token"> Токен отмены задачи </param>
|
2022-12-29 04:25:08 +05:00
|
|
|
|
/// <returns>количество успешно записанных строк в БД</returns>
|
2022-12-22 18:08:58 +05:00
|
|
|
|
[HttpPost]
|
2023-01-12 12:07:31 +05:00
|
|
|
|
[Route("import/{deleteBeforeImport}")]
|
2023-01-11 14:59:54 +05:00
|
|
|
|
[ProducesResponseType(typeof(int), (int)System.Net.HttpStatusCode.OK)]
|
2022-12-22 18:08:58 +05:00
|
|
|
|
public async Task<IActionResult> ImportAsync(int idWell,
|
|
|
|
|
[FromForm] IFormFileCollection files,
|
2023-01-12 12:07:31 +05:00
|
|
|
|
bool deleteBeforeImport,
|
|
|
|
|
CancellationToken token)
|
2022-12-22 18:08:58 +05:00
|
|
|
|
{
|
|
|
|
|
int? idUser = User.GetUserId();
|
|
|
|
|
if (!idUser.HasValue)
|
|
|
|
|
return Forbid();
|
2022-12-29 04:25:08 +05:00
|
|
|
|
if (!await CanUserAccessToWellAsync(idWell,
|
|
|
|
|
token).ConfigureAwait(false))
|
2022-12-22 18:08:58 +05:00
|
|
|
|
return Forbid();
|
|
|
|
|
if (files.Count < 1)
|
|
|
|
|
return BadRequest("нет файла");
|
|
|
|
|
var file = files[0];
|
|
|
|
|
if (Path.GetExtension(file.FileName).ToLower() != ".xlsx")
|
|
|
|
|
return BadRequest("Требуется xlsx файл.");
|
|
|
|
|
using Stream stream = file.OpenReadStream();
|
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
{
|
2023-01-12 12:07:31 +05:00
|
|
|
|
var result = await plannedTrajectoryImportService.ImportAsync(idWell, idUser.Value, stream, deleteBeforeImport, token);
|
2022-12-29 04:25:08 +05:00
|
|
|
|
return Ok(result);
|
2022-12-22 18:08:58 +05:00
|
|
|
|
}
|
|
|
|
|
catch (FileFormatException ex)
|
|
|
|
|
{
|
|
|
|
|
return BadRequest(ex.Message);
|
2022-12-29 04:25:08 +05:00
|
|
|
|
}
|
2022-12-22 18:08:58 +05:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Получаем список всех строк координат плановой траектории (для клиента)
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="idWell">id скважины</param>
|
|
|
|
|
/// <param name="token"> Токен отмены задачи </param>
|
2023-01-11 14:59:54 +05:00
|
|
|
|
/// <returns>Список добавленных координат плановой траектории</returns>
|
2022-12-22 18:08:58 +05:00
|
|
|
|
[HttpGet]
|
2023-01-11 14:59:54 +05:00
|
|
|
|
[Route("getRows")]
|
2022-12-22 18:08:58 +05:00
|
|
|
|
[ProducesResponseType(typeof(IEnumerable<PlannedTrajectoryDto>), (int)System.Net.HttpStatusCode.OK)]
|
2023-01-11 14:59:54 +05:00
|
|
|
|
public async Task<IActionResult> GetAsync([FromRoute] int idWell, CancellationToken token = default)
|
2022-12-22 18:08:58 +05:00
|
|
|
|
{
|
2022-12-29 04:25:08 +05:00
|
|
|
|
if (!await CanUserAccessToWellAsync(idWell,
|
|
|
|
|
token).ConfigureAwait(false))
|
2022-12-22 18:08:58 +05:00
|
|
|
|
return Forbid();
|
2023-02-13 09:10:48 +05:00
|
|
|
|
var result = await plannedTrajectoryRepository.GetAsync(idWell, token);
|
2022-12-22 18:08:58 +05:00
|
|
|
|
return Ok(result);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
2022-12-27 21:45:03 +05:00
|
|
|
|
/// Добавить одну новую строчку координат для плановой траектории
|
2022-12-22 18:08:58 +05:00
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="idWell"></param>
|
|
|
|
|
/// <param name="row"></param>
|
|
|
|
|
/// <param name="token"></param>
|
2022-12-29 04:25:08 +05:00
|
|
|
|
/// <returns>количество успешно записанных строк в БД</returns>
|
2022-12-22 18:08:58 +05:00
|
|
|
|
[HttpPost]
|
2022-12-29 04:25:08 +05:00
|
|
|
|
[Route("addRow")]
|
2023-01-11 14:59:54 +05:00
|
|
|
|
[ProducesResponseType(typeof(int), (int)System.Net.HttpStatusCode.OK)]
|
|
|
|
|
public async Task<IActionResult> AddAsync(int idWell, [FromBody] PlannedTrajectoryDto row,
|
2022-12-22 18:08:58 +05:00
|
|
|
|
CancellationToken token = default)
|
|
|
|
|
{
|
|
|
|
|
if (!await CanUserAccessToWellAsync(idWell, token).ConfigureAwait(false))
|
|
|
|
|
return Forbid();
|
2023-01-11 14:59:54 +05:00
|
|
|
|
var idUser = User.GetUserId();
|
2022-12-27 21:45:03 +05:00
|
|
|
|
if (!idUser.HasValue)
|
|
|
|
|
return Forbid();
|
2022-12-29 04:25:08 +05:00
|
|
|
|
row.IdUser = idUser.Value;
|
|
|
|
|
row.IdWell = idWell;
|
2023-02-13 09:10:48 +05:00
|
|
|
|
var result = await plannedTrajectoryRepository.AddAsync(row, token);
|
2022-12-27 21:45:03 +05:00
|
|
|
|
return Ok(result);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Добавить массив строчек координат для плановой траектории
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="idWell"></param>
|
|
|
|
|
/// <param name="rows"></param>
|
|
|
|
|
/// <param name="token"></param>
|
2022-12-29 04:25:08 +05:00
|
|
|
|
/// <returns>количество успешно записанных строк в БД</returns>
|
2022-12-27 21:45:03 +05:00
|
|
|
|
[HttpPost]
|
2022-12-29 04:25:08 +05:00
|
|
|
|
[Route("addRangeRows")]
|
2023-01-11 14:59:54 +05:00
|
|
|
|
[ProducesResponseType(typeof(int), (int)System.Net.HttpStatusCode.OK)]
|
|
|
|
|
public async Task<IActionResult> AddRangeAsync(int idWell, [FromBody] IEnumerable<PlannedTrajectoryDto> rows,
|
2022-12-27 21:45:03 +05:00
|
|
|
|
CancellationToken token = default)
|
|
|
|
|
{
|
|
|
|
|
if (!await CanUserAccessToWellAsync(idWell, token).ConfigureAwait(false))
|
|
|
|
|
return Forbid();
|
|
|
|
|
int? idUser = User.GetUserId();
|
|
|
|
|
if (!idUser.HasValue)
|
|
|
|
|
return Forbid();
|
2022-12-29 04:25:08 +05:00
|
|
|
|
foreach (var item in rows)
|
|
|
|
|
{
|
|
|
|
|
item.IdUser = idUser.Value;
|
|
|
|
|
item.IdWell = idWell;
|
|
|
|
|
}
|
2023-02-13 09:10:48 +05:00
|
|
|
|
var result = await plannedTrajectoryRepository.AddRangeAsync(rows, token);
|
2022-12-22 18:08:58 +05:00
|
|
|
|
return Ok(result);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Изменить выбранную строку с координатами
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="idWell"></param>
|
2023-01-12 12:07:31 +05:00
|
|
|
|
/// <param name="idRow"></param>
|
2022-12-22 18:08:58 +05:00
|
|
|
|
/// <param name="row"></param>
|
|
|
|
|
/// <param name="token"></param>
|
2022-12-29 04:25:08 +05:00
|
|
|
|
/// <returns>количество успешно обновленных строк в БД</returns>
|
2022-12-22 18:08:58 +05:00
|
|
|
|
[HttpPut("{idRow}")]
|
2023-01-11 14:59:54 +05:00
|
|
|
|
[ProducesResponseType(typeof(int), (int)System.Net.HttpStatusCode.OK)]
|
2023-01-12 12:07:31 +05:00
|
|
|
|
public async Task<IActionResult> UpdateAsync(int idWell, int idRow,
|
2022-12-22 18:08:58 +05:00
|
|
|
|
[FromBody] PlannedTrajectoryDto row, CancellationToken token = default)
|
2022-12-29 04:25:08 +05:00
|
|
|
|
{
|
2022-12-22 18:08:58 +05:00
|
|
|
|
if (!await CanUserAccessToWellAsync(idWell, token).ConfigureAwait(false))
|
|
|
|
|
return Forbid();
|
|
|
|
|
int? idUser = User.GetUserId();
|
|
|
|
|
if (!idUser.HasValue)
|
|
|
|
|
return Forbid();
|
2023-01-12 12:07:31 +05:00
|
|
|
|
row.Id = idRow;
|
2022-12-29 04:25:08 +05:00
|
|
|
|
row.IdUser = idUser.Value;
|
|
|
|
|
row.IdWell = idWell;
|
2023-02-13 09:10:48 +05:00
|
|
|
|
var result = await plannedTrajectoryRepository.UpdateAsync(row, token);
|
2022-12-29 04:25:08 +05:00
|
|
|
|
return Ok(result);
|
2022-12-22 18:08:58 +05:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Удалить выбранную строку с координатами
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="idWell"></param>
|
|
|
|
|
/// <param name="idRow"></param>
|
|
|
|
|
/// <param name="token"></param>
|
2022-12-29 04:25:08 +05:00
|
|
|
|
/// <returns>количество успешно удаленных строк из БД</returns>
|
|
|
|
|
[HttpDelete("{idRow}")]
|
2022-12-22 18:08:58 +05:00
|
|
|
|
[ProducesResponseType(typeof(int), (int)System.Net.HttpStatusCode.OK)]
|
2023-01-11 14:59:54 +05:00
|
|
|
|
public async Task<IActionResult> DeleteAsync(int idWell, int idRow, CancellationToken token = default)
|
2022-12-22 18:08:58 +05:00
|
|
|
|
{
|
|
|
|
|
if (!await CanUserAccessToWellAsync(idWell,
|
|
|
|
|
token).ConfigureAwait(false))
|
|
|
|
|
return Forbid();
|
|
|
|
|
|
2023-02-13 09:10:48 +05:00
|
|
|
|
var result = await plannedTrajectoryRepository.DeleteRangeAsync(new int[] { idRow }, token);
|
2022-12-22 18:08:58 +05:00
|
|
|
|
|
|
|
|
|
return Ok(result);
|
|
|
|
|
}
|
|
|
|
|
|
2023-02-13 09:19:11 +05:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Получение координат для визуализации траектории
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="idWell"></param>
|
|
|
|
|
/// <param name="token"></param>
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
[HttpGet]
|
|
|
|
|
[ProducesResponseType(typeof(IEnumerable<TrajectoryVisualizationDto>), (int)System.Net.HttpStatusCode.OK)]
|
|
|
|
|
public async Task<IActionResult> GetTrajectoryAsync(int idWell, CancellationToken token)
|
|
|
|
|
{
|
2023-02-13 12:39:45 +05:00
|
|
|
|
if (!await CanUserAccessToWellAsync(idWell,
|
|
|
|
|
token).ConfigureAwait(false))
|
|
|
|
|
return Forbid();
|
|
|
|
|
|
2023-02-13 09:19:11 +05:00
|
|
|
|
var result = await trajectoryVisualizationService.GetTrajectoryAsync(idWell, token);
|
|
|
|
|
return Ok(result);
|
|
|
|
|
}
|
|
|
|
|
|
2022-12-22 18:08:58 +05:00
|
|
|
|
private async Task<bool> CanUserAccessToWellAsync(int idWell, CancellationToken token = default)
|
|
|
|
|
{
|
|
|
|
|
int? idCompany = User.GetCompanyId();
|
|
|
|
|
return idCompany is not null && await wellService.IsCompanyInvolvedInWellAsync((int)idCompany,
|
|
|
|
|
idWell, token).ConfigureAwait(false);
|
2022-12-29 04:25:08 +05:00
|
|
|
|
}
|
2022-12-22 18:08:58 +05:00
|
|
|
|
}
|
2022-12-29 04:25:08 +05:00
|
|
|
|
#nullable disable
|
2022-12-22 18:08:58 +05:00
|
|
|
|
}
|