Merge pull request '#19189208 Импорт операций по скважине' (#158) from feature/import_well_operations into dev

Reviewed-on: http://test.digitaldrilling.ru:8080/DDrilling/AsbCloudServer/pulls/158
Reviewed-by: Никита Фролов <ng.frolov@digitaldrilling.ru>
This commit is contained in:
Никита Фролов 2023-12-01 11:26:43 +05:00
commit 6ef0d62cb9
5 changed files with 151 additions and 138 deletions

View File

@ -1,5 +1,5 @@
using System.Threading;
using System.Threading.Tasks;
using System.Collections.Generic;
using AsbCloudApp.Data;
using AsbCloudApp.Data.WellOperationImport;
namespace AsbCloudApp.Services.WellOperationImport;
@ -16,7 +16,5 @@ public interface IWellOperationImportService
/// <param name="idUser"></param>
/// <param name="idType"></param>
/// <param name="sheet"></param>
/// <param name="deleteBeforeImport"></param>
/// <param name="cancellationToken"></param>
Task ImportAsync(int idWell, int idUser, int idType, SheetDto sheet, bool deleteBeforeImport, CancellationToken cancellationToken);
IEnumerable<WellOperationDto> Import(int idWell, int idUser, int idType, SheetDto sheet);
}

View File

@ -2,12 +2,9 @@ using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using AsbCloudApp.Data;
using AsbCloudApp.Data.WellOperationImport;
using AsbCloudApp.Repositories;
using AsbCloudApp.Requests;
using AsbCloudApp.Services.WellOperationImport;
namespace AsbCloudInfrastructure.Services.WellOperationImport;
@ -25,14 +22,14 @@ public class WellOperationImportService : IWellOperationImportService
this.wellOperationRepository = wellOperationRepository;
}
public async Task ImportAsync(int idWell, int idUser, int idType, SheetDto sheet, bool deleteBeforeImport, CancellationToken cancellationToken)
public IEnumerable<WellOperationDto> Import(int idWell, int idUser, int idType, SheetDto sheet)
{
var validationErrors = new List<string>();
var sections = wellOperationRepository.GetSectionTypes();
var categories = wellOperationRepository.GetCategories(false);
var operations = new List<WellOperationDto>();
var wellOperations = new List<WellOperationDto>();
foreach (var row in sheet.Rows)
{
@ -62,14 +59,14 @@ public class WellOperationImportService : IWellOperationImportService
throw new FileFormatException(
$"Лист '{sheet.Name}'. Строка '{row.Number}' неправильно получена дата начала операции");
if (operations.LastOrDefault()?.DateStart > row.Date)
if (wellOperations.LastOrDefault()?.DateStart > row.Date)
throw new FileFormatException(
$"Лист '{sheet.Name}' строка '{row.Number}' дата позднее даты предыдущей операции");
if (row.Duration is not (>= 0d and <= 240d))
throw new FileFormatException($"Лист '{sheet.Name}'. Строка '{row.Number}' некорректная длительность операции");
operations.Add(new WellOperationDto
wellOperations.Add(new WellOperationDto
{
IdWell = idWell,
IdUser = idUser,
@ -89,26 +86,12 @@ public class WellOperationImportService : IWellOperationImportService
}
}
if (operations.Any() && operations.Min(o => o.DateStart) - operations.Max(o => o.DateStart) > drillingDurationLimitMax)
if (wellOperations.Any() && wellOperations.Min(o => o.DateStart) - wellOperations.Max(o => o.DateStart) > drillingDurationLimitMax)
validationErrors.Add($"Лист {sheet.Name} содержит диапазон дат больше {drillingDurationLimitMax}");
if (validationErrors.Any())
throw new FileFormatException(string.Join("\r\n", validationErrors));
if (!operations.Any())
return;
if (deleteBeforeImport)
{
var existingOperations = await wellOperationRepository.GetAsync(new WellOperationRequest
{
IdWell = idWell,
OperationType = idType
}, cancellationToken);
await wellOperationRepository.DeleteAsync(existingOperations.Select(o => o.Id), cancellationToken);
}
await wellOperationRepository.InsertRangeAsync(operations, cancellationToken);
return wellOperations;
}
}

View File

@ -254,7 +254,7 @@ public class DailyReportServiceTest
wellServiceMock.GetOrDefaultAsync(Arg.Any<int>(), Arg.Any<CancellationToken>())
.ReturnsForAnyArgs(fakeWell);
trajectoryFactNnbRepositoryMock.GetAsync(Arg.Any<int>(), Arg.Any<CancellationToken>())
trajectoryFactNnbRepositoryMock.GetByRequestAsync(Arg.Any<TrajectoryRequest>(), Arg.Any<CancellationToken>())
.ReturnsForAnyArgs(new[] { fakeLastFactTrajectory });
wellOperationRepositoryMock.GetAsync(Arg.Any<WellOperationRequest>(), Arg.Any<CancellationToken>())

View File

@ -9,8 +9,10 @@ using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.IO;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using AsbCloudApp.Data.WellOperationImport;
using AsbCloudApp.Services.WellOperationImport;
using AsbCloudApp.Data.WellOperationImport.Options;
using AsbCloudApp.Exceptions;
@ -204,37 +206,87 @@ namespace AsbCloudWebApi.Controllers
return Ok(result);
}
/// <summary>
/// Добавляет новую операцию на скважину
/// </summary>
/// <param name="idWell">Id скважины</param>
/// <param name="idType">Тип добавляемой операции</param>
/// <param name="wellOperation">Добавляемая операция</param>
/// <param name="cancellationToken"></param>
/// <returns>Количество добавленных в БД записей</returns>
[HttpPost("{idType:int}")]
[Permission]
[ProducesResponseType(typeof(int), StatusCodes.Status200OK)]
[ProducesResponseType(typeof(ValidationProblemDetails), StatusCodes.Status400BadRequest)]
public async Task<IActionResult> InsertAsync(
[Range(1, int.MaxValue, ErrorMessage = "Id скважины не может быть меньше 1")] int idWell,
[Range(0, 1, ErrorMessage = "Тип операции недопустим. Допустимые: 0, 1")] int idType,
WellOperationDto wellOperation,
CancellationToken cancellationToken)
{
if (!await CanUserAccessToWellAsync(idWell, cancellationToken))
return Forbid();
if (!await CanUserEditWellOperationsAsync(idWell, cancellationToken))
return Forbid();
wellOperation.IdWell = idWell;
wellOperation.LastUpdateDate = DateTimeOffset.UtcNow;
wellOperation.IdUser = User.GetUserId();
wellOperation.IdType = idType;
var result = await operationRepository.InsertRangeAsync(new[] { wellOperation }, cancellationToken);
return Ok(result);
}
/// <summary>
/// Добавляет новые операции на скважине
/// </summary>
/// <param name="idWell">id скважины</param>
/// <param name="values">Данные о добавляемых операциях</param>
/// <param name="token">Токен отмены задачи</param>
/// <returns>Количество добавленных в БД строк</returns>
[HttpPost]
/// <param name="idWell">Id скважины</param>
/// <param name="wellOperations">Добавляемые операции</param>
/// <param name="idType">Тип добавляемых операций</param>
/// <param name="deleteBeforeInsert">Удалить операции перед сохранением</param>
/// <param name="cancellationToken"></param>
/// <returns>Количество добавленных в БД записей</returns>
[HttpPost("{idType:int}/{deleteBeforeInsert:bool}")]
[Permission]
[ProducesResponseType(typeof(IEnumerable<WellOperationDto>), (int)System.Net.HttpStatusCode.OK)]
[ProducesResponseType(typeof(int), StatusCodes.Status200OK)]
[ProducesResponseType(typeof(ValidationProblemDetails), StatusCodes.Status400BadRequest)]
public async Task<IActionResult> InsertRangeAsync(
[Range(1, int.MaxValue, ErrorMessage = "Id скважины не может быть меньше 1")] int idWell,
[FromBody] IEnumerable<WellOperationDto> values,
CancellationToken token)
[Range(1, int.MaxValue, ErrorMessage = "Id скважины не может быть меньше 1")] int idWell,
[Range(0, 1, ErrorMessage = "Тип операции недопустим. Допустимые: 0, 1")] int idType,
bool deleteBeforeInsert,
[FromBody] IEnumerable<WellOperationDto> wellOperations,
CancellationToken cancellationToken)
{
if (!await CanUserAccessToWellAsync(idWell, token))
return Forbid();
if (!await CanUserEditWellOperationsAsync(idWell, token))
if (!await CanUserAccessToWellAsync(idWell, cancellationToken))
return Forbid();
foreach (var value in values)
if (!await CanUserEditWellOperationsAsync(idWell, cancellationToken))
return Forbid();
if (deleteBeforeInsert && wellOperations.Any())
{
value.IdWell = idWell;
value.LastUpdateDate = DateTimeOffset.UtcNow;
value.IdUser = User.GetUserId();
var existingOperations = await operationRepository.GetAsync(new WellOperationRequest
{
IdWell = idWell,
OperationType = idType
}, cancellationToken);
await operationRepository.DeleteAsync(existingOperations.Select(o => o.Id), cancellationToken);
}
foreach (var wellOperation in wellOperations)
{
wellOperation.IdWell = idWell;
wellOperation.LastUpdateDate = DateTimeOffset.UtcNow;
wellOperation.IdUser = User.GetUserId();
wellOperation.IdType = idType;
}
var result = await operationRepository.InsertRangeAsync(values, token)
.ConfigureAwait(false);
var result = await operationRepository.InsertRangeAsync(wellOperations, cancellationToken);
return Ok(result);
}
@ -299,46 +351,19 @@ namespace AsbCloudWebApi.Controllers
/// <param name="idWell">id скважины</param>
/// <param name="options">Параметры для парсинга файла</param>
/// <param name="files">Коллекция из одного файла xlsx</param>
/// <param name="deleteBeforeImport">Удалить операции перед импортом = 1, если файл валидный</param>
/// <param name="token"></param>
/// <param name="cancellationToken"></param>
/// <returns></returns>
[HttpPost("import/default/{deleteBeforeImport}")]
[HttpPost("import/default")]
[ProducesResponseType(typeof(IEnumerable<WellOperationDto>), StatusCodes.Status200OK)]
[ProducesResponseType(typeof(ValidationProblemDetails), StatusCodes.Status400BadRequest)]
[ProducesResponseType(StatusCodes.Status204NoContent)]
[Permission]
public async Task<IActionResult> ImportDefaultExcelFileAsync(int idWell,
public Task<IActionResult> ImportDefaultExcelFileAsync(int idWell,
[FromQuery] WellOperationImportDefaultOptionsDto options,
[FromForm] IFormFileCollection files,
[Range(0, 1, ErrorMessage = "Недопустимое значение. Допустимые: 0, 1")] int deleteBeforeImport,
CancellationToken token)
{
var idUser = User.GetUserId();
if (!idUser.HasValue)
throw new ForbidException("Неизвестный пользователь");
await AssertUserHasAccessToImportWellOperationsAsync(idWell, token);
if (files.Count < 1)
return this.ValidationBadRequest(nameof(files), "Нет файла");
var file = files[0];
if (Path.GetExtension(file.FileName).ToLower() != ".xlsx")
return this.ValidationBadRequest(nameof(files), "Требуется xlsx файл.");
using Stream stream = file.OpenReadStream();
try
{
var sheet = wellOperationDefaultExcelParser.Parse(stream, options);
await wellOperationImportService.ImportAsync(idWell, idUser.Value, options.IdType, sheet, (deleteBeforeImport & 1) > 0, token);
}
catch (FileFormatException ex)
{
return this.ValidationBadRequest(nameof(files), ex.Message);
}
return Ok();
}
CancellationToken cancellationToken) => ImportExcelFileAsync(idWell, files, options,
(stream, _) => wellOperationDefaultExcelParser.Parse(stream, options),
cancellationToken);
/// <summary>
/// Импорт операций из excel (xlsx) файла. ГПНХ (Хантос)
@ -346,46 +371,19 @@ namespace AsbCloudWebApi.Controllers
/// <param name="idWell">id скважины</param>
/// <param name="options">Параметры для парсинга файла</param>
/// <param name="files">Коллекция из одного файла xlsx</param>
/// <param name="deleteBeforeImport">Удалить операции перед импортом = 1, если файл валидный</param>
/// <param name="token"></param>
/// <param name="cancellationToken"></param>
/// <returns></returns>
[HttpPost("import/gazpromKhantos/{deleteBeforeImport}")]
[HttpPost("import/gazpromKhantos")]
[ProducesResponseType(typeof(IEnumerable<WellOperationDto>), StatusCodes.Status200OK)]
[ProducesResponseType(typeof(ValidationProblemDetails), StatusCodes.Status400BadRequest)]
[ProducesResponseType(StatusCodes.Status204NoContent)]
[Permission]
public async Task<IActionResult> ImportGazpromKhantosExcelFileAsync(int idWell,
[FromQuery] WellOperationImportGazpromKhantosOptionsDto options,
[FromForm] IFormFileCollection files,
[Range(0, 1, ErrorMessage = "Недопустимое значение. Допустимые: 0, 1")] int deleteBeforeImport,
CancellationToken token)
{
var idUser = User.GetUserId();
if (!idUser.HasValue)
throw new ForbidException("Неизвестный пользователь");
await AssertUserHasAccessToImportWellOperationsAsync(idWell, token);
if (files.Count < 1)
return this.ValidationBadRequest(nameof(files), "Нет файла");
var file = files[0];
if (Path.GetExtension(file.FileName).ToLower() != ".xlsx")
return this.ValidationBadRequest(nameof(files), "Требуется xlsx файл.");
using Stream stream = file.OpenReadStream();
try
{
var sheet = wellOperationGazpromKhantosExcelParser.Parse(stream, options);
await wellOperationImportService.ImportAsync(idWell, idUser.Value, options.IdType, sheet, (deleteBeforeImport & 1) > 0, token);
}
catch (FileFormatException ex)
{
return this.ValidationBadRequest(nameof(files), ex.Message);
}
return Ok();
}
public Task<IActionResult> ImportGazpromKhantosExcelFileAsync(int idWell,
[FromQuery] WellOperationImportGazpromKhantosOptionsDto options,
[FromForm] IFormFileCollection files,
CancellationToken cancellationToken) => ImportExcelFileAsync(idWell, files, options,
(stream, _) => wellOperationGazpromKhantosExcelParser.Parse(stream, options),
cancellationToken);
/// <summary>
/// Создает excel файл с операциями по скважине
@ -453,7 +451,11 @@ namespace AsbCloudWebApi.Controllers
return File(stream, "application/octet-stream", fileName);
}
private async Task AssertUserHasAccessToImportWellOperationsAsync(int idWell, CancellationToken token)
private async Task<IActionResult> ImportExcelFileAsync<TOptions>(int idWell, [FromForm] IFormFileCollection files,
TOptions options,
Func<Stream, TOptions, SheetDto> parseMethod,
CancellationToken cancellationToken)
where TOptions : IWellOperationImportOptions
{
var idCompany = User.GetCompanyId();
var idUser = User.GetUserId();
@ -461,16 +463,54 @@ namespace AsbCloudWebApi.Controllers
if (!idCompany.HasValue || !idUser.HasValue)
throw new ForbidException("Неизвестный пользователь");
if (!await CanUserAccessToWellAsync(idWell, token))
if (!await CanUserAccessToWellAsync(idWell, cancellationToken))
throw new ForbidException("Нет доступа к скважине");
if (!await CanUserEditWellOperationsAsync(idWell, token))
if (!await CanUserEditWellOperationsAsync(idWell, cancellationToken))
throw new ForbidException("Недостаточно прав для редактирования ГГД на завершенной скважине");
if (!await wellService.IsCompanyInvolvedInWellAsync(idCompany.Value, idWell, token))
if (!await wellService.IsCompanyInvolvedInWellAsync(idCompany.Value, idWell, cancellationToken))
throw new ForbidException("Скважина недоступна для компании");
}
if (files.Count < 1)
return this.ValidationBadRequest(nameof(files), "Нет файла");
var file = files[0];
if (Path.GetExtension(file.FileName).ToLower() != ".xlsx")
return this.ValidationBadRequest(nameof(files), "Требуется xlsx файл.");
using Stream stream = file.OpenReadStream();
try
{
var sheet = parseMethod(stream, options);
var wellOperations = wellOperationImportService.Import(idWell, idUser.Value, options.IdType, sheet)
.OrderBy(w => w.DateStart);
var dateStart = wellOperations.Min(w => w.DateStart);
foreach (var wellOperation in wellOperations)
wellOperation.Day = (wellOperation.DateStart - dateStart).TotalDays;
if (!wellOperations.Any())
return NoContent();
return Ok(wellOperations);
}
catch (FileFormatException ex)
{
return this.ValidationBadRequest(nameof(files), ex.Message);
}
}
private async Task<bool> CanUserAccessToWellAsync(int idWell, CancellationToken token)
{
int? idCompany = User.GetCompanyId();
return idCompany is not null && await wellService.IsCompanyInvolvedInWellAsync((int)idCompany,
idWell, token).ConfigureAwait(false);
}
private async Task<bool> CanUserEditWellOperationsAsync(int idWell, CancellationToken token)
{
var idUser = User.GetUserId();
@ -485,13 +525,5 @@ namespace AsbCloudWebApi.Controllers
return well.IdState != 2 || userRepository.HasPermission(idUser.Value, "WellOperation.editCompletedWell");
}
private async Task<bool> CanUserAccessToWellAsync(int idWell, CancellationToken token)
{
int? idCompany = User.GetCompanyId();
return idCompany is not null && await wellService.IsCompanyInvolvedInWellAsync((int)idCompany,
idWell, token).ConfigureAwait(false);
}
}
}
}