2023-09-04 14:11:25 +05:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.IO;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using AsbCloudApp.Data;
|
|
|
|
|
using AsbCloudApp.Data.WellOperationImport;
|
|
|
|
|
using AsbCloudApp.Repositories;
|
2024-02-06 11:41:51 +05:00
|
|
|
|
using AsbCloudApp.Services;
|
2023-09-04 14:11:25 +05:00
|
|
|
|
using AsbCloudApp.Services.WellOperationImport;
|
|
|
|
|
|
|
|
|
|
namespace AsbCloudInfrastructure.Services.WellOperationImport;
|
|
|
|
|
|
|
|
|
|
public class WellOperationImportService : IWellOperationImportService
|
|
|
|
|
{
|
2024-02-06 11:41:51 +05:00
|
|
|
|
private readonly IWellService wellService;
|
2023-09-04 14:11:25 +05:00
|
|
|
|
private readonly IWellOperationRepository wellOperationRepository;
|
2024-02-22 13:34:05 +05:00
|
|
|
|
private readonly IWellOperationCategoryRepository wellOperationCategoryRepository;
|
|
|
|
|
private static readonly DateTime dateLimitMin = new(2001, 1, 1, 0, 0, 0);
|
2023-09-04 14:11:25 +05:00
|
|
|
|
private static readonly DateTime dateLimitMax = new(2099, 1, 1, 0, 0, 0);
|
|
|
|
|
private static readonly TimeSpan drillingDurationLimitMax = TimeSpan.FromDays(366);
|
|
|
|
|
|
2024-02-22 13:34:05 +05:00
|
|
|
|
public WellOperationImportService(
|
|
|
|
|
IWellService wellService,
|
|
|
|
|
IWellOperationRepository wellOperationRepository,
|
|
|
|
|
IWellOperationCategoryRepository wellOperationCategoryRepository
|
|
|
|
|
)
|
2023-09-04 14:11:25 +05:00
|
|
|
|
{
|
2024-02-06 11:41:51 +05:00
|
|
|
|
this.wellService = wellService;
|
2023-09-04 14:11:25 +05:00
|
|
|
|
this.wellOperationRepository = wellOperationRepository;
|
2024-02-22 13:34:05 +05:00
|
|
|
|
this.wellOperationCategoryRepository = wellOperationCategoryRepository;
|
|
|
|
|
}
|
2023-09-04 14:11:25 +05:00
|
|
|
|
|
2023-11-17 11:19:04 +05:00
|
|
|
|
public IEnumerable<WellOperationDto> Import(int idWell, int idUser, int idType, SheetDto sheet)
|
2023-09-04 14:11:25 +05:00
|
|
|
|
{
|
|
|
|
|
var validationErrors = new List<string>();
|
|
|
|
|
|
|
|
|
|
var sections = wellOperationRepository.GetSectionTypes();
|
2024-02-22 13:34:05 +05:00
|
|
|
|
var categories = wellOperationCategoryRepository.Get(false);
|
2023-09-04 14:11:25 +05:00
|
|
|
|
|
2023-11-17 11:19:04 +05:00
|
|
|
|
var wellOperations = new List<WellOperationDto>();
|
2024-02-06 11:41:51 +05:00
|
|
|
|
|
|
|
|
|
var rows = sheet.Rows.OrderBy(r => r.Date);
|
|
|
|
|
|
|
|
|
|
var prevRow = new RowDto();
|
|
|
|
|
|
|
|
|
|
foreach (var row in 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}' неправильно получена дата начала операции");
|
2024-02-06 11:41:51 +05:00
|
|
|
|
|
|
|
|
|
if (prevRow.Date > 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
|
|
|
|
|
2024-02-06 11:41:51 +05:00
|
|
|
|
var timezone = wellService.GetTimezone(idWell);
|
2024-02-07 09:21:03 +05:00
|
|
|
|
var timezoneOffset = TimeSpan.FromHours(timezone.Hours);
|
|
|
|
|
|
2024-02-06 11:41:51 +05:00
|
|
|
|
var wellOperation = new WellOperationDto
|
2023-09-04 14:11:25 +05:00
|
|
|
|
{
|
|
|
|
|
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,
|
2024-02-22 13:34:05 +05:00
|
|
|
|
WellSectionTypeName = section.Caption,
|
2023-09-04 14:11:25 +05:00
|
|
|
|
IdCategory = category.Id,
|
2024-02-22 13:34:05 +05:00
|
|
|
|
CategoryName = category.Name,
|
2023-09-04 14:11:25 +05:00
|
|
|
|
CategoryInfo = row.CategoryInfo,
|
|
|
|
|
DepthStart = row.DepthStart,
|
|
|
|
|
DepthEnd = row.DepthEnd,
|
2024-02-07 09:21:03 +05:00
|
|
|
|
DateStart = new DateTimeOffset(row.Date, timezoneOffset),
|
2023-12-21 12:02:29 +05:00
|
|
|
|
DurationHours = row.Duration,
|
|
|
|
|
Comment = row.Comment
|
2024-02-06 11:41:51 +05:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
wellOperations.Add(wellOperation);
|
|
|
|
|
|
|
|
|
|
prevRow = row;
|
2023-09-04 14:11:25 +05:00
|
|
|
|
}
|
|
|
|
|
catch (FileFormatException ex)
|
|
|
|
|
{
|
|
|
|
|
validationErrors.Add(ex.Message);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-11-17 11:19:04 +05:00
|
|
|
|
if (wellOperations.Any() && wellOperations.Min(o => o.DateStart) - wellOperations.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-11-17 11:19:04 +05:00
|
|
|
|
|
|
|
|
|
return wellOperations;
|
2023-09-04 14:11:25 +05:00
|
|
|
|
}
|
|
|
|
|
}
|