2023-09-04 14:11:25 +05:00
|
|
|
|
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;
|
|
|
|
|
|
|
|
|
|
public class WellOperationImportService : IWellOperationImportService
|
|
|
|
|
{
|
|
|
|
|
private readonly IWellOperationRepository wellOperationRepository;
|
|
|
|
|
|
|
|
|
|
private static readonly DateTime dateLimitMin = new(2001, 1, 1, 0, 0, 0);
|
|
|
|
|
private static readonly DateTime dateLimitMax = new(2099, 1, 1, 0, 0, 0);
|
|
|
|
|
private static readonly TimeSpan drillingDurationLimitMax = TimeSpan.FromDays(366);
|
|
|
|
|
|
2023-10-04 15:36:00 +05:00
|
|
|
|
public WellOperationImportService(IWellOperationRepository wellOperationRepository)
|
2023-09-04 14:11:25 +05:00
|
|
|
|
{
|
|
|
|
|
this.wellOperationRepository = wellOperationRepository;
|
|
|
|
|
}
|
|
|
|
|
|
2023-10-04 15:36:00 +05:00
|
|
|
|
public async Task ImportAsync(int idWell, int idUser, int idType, SheetDto sheet, bool deleteBeforeImport, CancellationToken cancellationToken)
|
2023-09-04 14:11:25 +05:00
|
|
|
|
{
|
|
|
|
|
var validationErrors = new List<string>();
|
|
|
|
|
|
|
|
|
|
var sections = wellOperationRepository.GetSectionTypes();
|
|
|
|
|
var categories = wellOperationRepository.GetCategories(false);
|
|
|
|
|
|
|
|
|
|
var operations = new List<WellOperationDto>();
|
|
|
|
|
|
2023-10-04 15:36:00 +05:00
|
|
|
|
foreach (var row in sheet.Rows)
|
2023-09-04 14:11:25 +05:00
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
var section = sections.FirstOrDefault(s =>
|
|
|
|
|
string.Equals(s.Caption, row.Section, StringComparison.CurrentCultureIgnoreCase));
|
|
|
|
|
|
|
|
|
|
if (section is null)
|
2023-10-04 15:36:00 +05:00
|
|
|
|
throw new FileFormatException($"Лист '{sheet.Name}'. В строке '{row.Number}' не удалось определить секцию");
|
2023-09-04 14:11:25 +05:00
|
|
|
|
|
|
|
|
|
var category = categories.FirstOrDefault(c =>
|
|
|
|
|
string.Equals(c.Name, row.Category, StringComparison.CurrentCultureIgnoreCase));
|
|
|
|
|
|
|
|
|
|
if (category is null)
|
2023-10-04 15:36:00 +05:00
|
|
|
|
throw new FileFormatException($"Лист '{sheet.Name}'. В строке '{row.Number}' не удалось определить операцию");
|
2023-09-04 14:11:25 +05:00
|
|
|
|
|
|
|
|
|
if (row.DepthStart is not (>= 0d and <= 20_000d))
|
2023-10-02 09:27:20 +05:00
|
|
|
|
throw new FileFormatException(
|
2023-10-04 15:36:00 +05:00
|
|
|
|
$"Лист '{sheet.Name}'. Строка '{row.Number}' некорректная глубина на начало операции");
|
2023-09-04 14:11:25 +05:00
|
|
|
|
|
|
|
|
|
if (row.DepthEnd is not (>= 0d and <= 20_000d))
|
2023-10-02 09:27:20 +05:00
|
|
|
|
throw new FileFormatException(
|
2023-10-04 15:36:00 +05:00
|
|
|
|
$"Лист '{sheet.Name}'. Строка '{row.Number}' некорректная глубина на конец операции");
|
2023-09-04 14:11:25 +05:00
|
|
|
|
|
|
|
|
|
if (row.Date < dateLimitMin && row.Date > dateLimitMax)
|
2023-10-02 09:27:20 +05:00
|
|
|
|
throw new FileFormatException(
|
2023-10-04 15:36:00 +05:00
|
|
|
|
$"Лист '{sheet.Name}'. Строка '{row.Number}' неправильно получена дата начала операции");
|
2023-09-04 14:11:25 +05:00
|
|
|
|
|
|
|
|
|
if (operations.LastOrDefault()?.DateStart > row.Date)
|
2023-10-02 09:27:20 +05:00
|
|
|
|
throw new FileFormatException(
|
2023-10-04 15:36:00 +05:00
|
|
|
|
$"Лист '{sheet.Name}' строка '{row.Number}' дата позднее даты предыдущей операции");
|
2023-09-04 14:11:25 +05:00
|
|
|
|
|
|
|
|
|
if (row.Duration is not (>= 0d and <= 240d))
|
2023-10-04 15:36:00 +05:00
|
|
|
|
throw new FileFormatException($"Лист '{sheet.Name}'. Строка '{row.Number}' некорректная длительность операции");
|
2023-09-04 14:11:25 +05:00
|
|
|
|
|
|
|
|
|
operations.Add(new WellOperationDto
|
|
|
|
|
{
|
|
|
|
|
IdWell = idWell,
|
|
|
|
|
IdUser = idUser,
|
2023-10-04 15:36:00 +05:00
|
|
|
|
IdType = idType,
|
2023-09-04 14:11:25 +05:00
|
|
|
|
IdWellSectionType = section.Id,
|
|
|
|
|
IdCategory = category.Id,
|
|
|
|
|
CategoryInfo = row.CategoryInfo,
|
|
|
|
|
DepthStart = row.DepthStart,
|
|
|
|
|
DepthEnd = row.DepthEnd,
|
|
|
|
|
DateStart = row.Date,
|
|
|
|
|
DurationHours = row.Duration
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
catch (FileFormatException ex)
|
|
|
|
|
{
|
|
|
|
|
validationErrors.Add(ex.Message);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (operations.Any() && operations.Min(o => o.DateStart) - operations.Max(o => o.DateStart) > drillingDurationLimitMax)
|
2023-10-04 15:36:00 +05:00
|
|
|
|
validationErrors.Add($"Лист {sheet.Name} содержит диапазон дат больше {drillingDurationLimitMax}");
|
2023-09-04 14:11:25 +05:00
|
|
|
|
|
|
|
|
|
if (validationErrors.Any())
|
|
|
|
|
throw new FileFormatException(string.Join("\r\n", validationErrors));
|
|
|
|
|
|
2023-10-02 09:27:20 +05:00
|
|
|
|
if (!operations.Any())
|
2023-09-04 14:11:25 +05:00
|
|
|
|
return;
|
|
|
|
|
|
2023-10-02 09:27:20 +05:00
|
|
|
|
if (deleteBeforeImport)
|
2023-09-04 14:11:25 +05:00
|
|
|
|
{
|
|
|
|
|
var existingOperations = await wellOperationRepository.GetAsync(new WellOperationRequest
|
|
|
|
|
{
|
2023-10-04 15:36:00 +05:00
|
|
|
|
IdWell = idWell,
|
|
|
|
|
OperationType = idType
|
2023-09-04 14:11:25 +05:00
|
|
|
|
}, cancellationToken);
|
2023-10-02 09:27:20 +05:00
|
|
|
|
|
2023-09-04 14:11:25 +05:00
|
|
|
|
await wellOperationRepository.DeleteAsync(existingOperations.Select(o => o.Id), cancellationToken);
|
|
|
|
|
}
|
|
|
|
|
|
2023-10-02 09:27:20 +05:00
|
|
|
|
await wellOperationRepository.InsertRangeAsync(operations, cancellationToken);
|
2023-09-04 14:11:25 +05:00
|
|
|
|
}
|
|
|
|
|
}
|