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;
|
|
|
|
|
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-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();
|
|
|
|
|
var categories = wellOperationRepository.GetCategories(false);
|
|
|
|
|
|
2023-11-17 11:19:04 +05:00
|
|
|
|
var wellOperations = new List<WellOperationDto>();
|
2023-09-04 14:11:25 +05:00
|
|
|
|
|
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
|
|
|
|
|
2023-11-17 11:19:04 +05:00
|
|
|
|
if (wellOperations.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
|
|
|
|
|
2023-11-17 11:19:04 +05:00
|
|
|
|
wellOperations.Add(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,
|
|
|
|
|
IdCategory = category.Id,
|
|
|
|
|
CategoryInfo = row.CategoryInfo,
|
|
|
|
|
DepthStart = row.DepthStart,
|
|
|
|
|
DepthEnd = row.DepthEnd,
|
|
|
|
|
DateStart = row.Date,
|
2023-12-21 12:02:29 +05:00
|
|
|
|
DurationHours = row.Duration,
|
|
|
|
|
Comment = row.Comment
|
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
|
|
|
|
}
|
|
|
|
|
}
|