2023-09-04 14:11:25 +05:00
|
|
|
using System;
|
|
|
|
using System.Collections.Generic;
|
|
|
|
using System.IO;
|
|
|
|
using System.Linq;
|
|
|
|
using AsbCloudApp.Data.WellOperationImport;
|
2023-10-04 15:36:00 +05:00
|
|
|
using AsbCloudApp.Data.WellOperationImport.Options;
|
2023-09-04 14:11:25 +05:00
|
|
|
using AsbCloudApp.Services.WellOperationImport;
|
|
|
|
using AsbCloudDb.Model;
|
|
|
|
using AsbCloudInfrastructure.Services.WellOperationImport.Constants;
|
|
|
|
using ClosedXML.Excel;
|
|
|
|
|
|
|
|
namespace AsbCloudInfrastructure.Services.WellOperationImport.FileParser;
|
|
|
|
|
2023-10-04 15:36:00 +05:00
|
|
|
public class WellOperationDefaultExcelParser : IWellOperationExcelParser<WellOperationImportDefaultOptionsDto>
|
2023-09-04 14:11:25 +05:00
|
|
|
{
|
2023-10-04 15:36:00 +05:00
|
|
|
public SheetDto Parse(Stream stream, WellOperationImportDefaultOptionsDto options)
|
2023-09-04 14:11:25 +05:00
|
|
|
{
|
|
|
|
using var workbook = new XLWorkbook(stream, XLEventTracking.Disabled);
|
|
|
|
|
|
|
|
return ParseWorkbook(workbook, options);
|
|
|
|
}
|
|
|
|
|
2023-10-04 15:36:00 +05:00
|
|
|
private static SheetDto ParseWorkbook(IXLWorkbook workbook, WellOperationImportDefaultOptionsDto options)
|
2023-09-04 14:11:25 +05:00
|
|
|
{
|
2023-10-02 09:27:20 +05:00
|
|
|
var sheetName = options.IdType == WellOperation.IdOperationTypePlan
|
|
|
|
? DefaultTemplateInfo.SheetNamePlan
|
|
|
|
: DefaultTemplateInfo.SheetNameFact;
|
2023-09-04 14:11:25 +05:00
|
|
|
|
|
|
|
var sheet = workbook.Worksheets.FirstOrDefault(ws =>
|
2023-10-02 09:27:20 +05:00
|
|
|
string.Equals(ws.Name, sheetName, StringComparison.CurrentCultureIgnoreCase))
|
2023-10-04 15:36:00 +05:00
|
|
|
?? throw new FileFormatException($"Книга excel не содержит листа '{sheetName}'");
|
2023-09-04 14:11:25 +05:00
|
|
|
|
|
|
|
return ParseSheet(sheet);
|
|
|
|
}
|
|
|
|
|
2023-10-04 15:36:00 +05:00
|
|
|
private static SheetDto ParseSheet(IXLWorksheet sheet)
|
2023-09-04 14:11:25 +05:00
|
|
|
{
|
|
|
|
if (sheet.RangeUsed().RangeAddress.LastAddress.ColumnNumber < 7)
|
|
|
|
throw new FileFormatException($"Лист {sheet.Name} содержит меньшее количество столбцов.");
|
|
|
|
|
|
|
|
var count = sheet.RowsUsed().Count() - DefaultTemplateInfo.HeaderRowsCount;
|
|
|
|
|
|
|
|
switch (count)
|
|
|
|
{
|
|
|
|
case > 1024:
|
|
|
|
throw new FileFormatException($"Лист {sheet.Name} содержит слишком большое количество операций.");
|
|
|
|
case <= 0:
|
2023-10-04 15:36:00 +05:00
|
|
|
return new SheetDto { Name = sheet.Name };
|
2023-09-04 14:11:25 +05:00
|
|
|
}
|
|
|
|
|
|
|
|
var rows = new RowDto[count];
|
|
|
|
|
|
|
|
var cellValuesErrors = new List<string>();
|
|
|
|
|
|
|
|
for (int i = 0; i < rows.Length; i++)
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
var xlRow = sheet.Row(1 + i + DefaultTemplateInfo.HeaderRowsCount);
|
|
|
|
|
|
|
|
rows[i] = ParseRow(xlRow);
|
|
|
|
}
|
|
|
|
catch (FileFormatException ex)
|
|
|
|
{
|
|
|
|
cellValuesErrors.Add(ex.Message);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (cellValuesErrors.Any())
|
|
|
|
throw new FileFormatException(string.Join("\r\n", cellValuesErrors));
|
|
|
|
|
2023-10-04 15:36:00 +05:00
|
|
|
return new SheetDto
|
|
|
|
{
|
|
|
|
Name = sheet.Name,
|
|
|
|
Rows = rows
|
|
|
|
};
|
2023-09-04 14:11:25 +05:00
|
|
|
}
|
|
|
|
|
|
|
|
private static RowDto ParseRow(IXLRow xlRow)
|
|
|
|
{
|
|
|
|
return new RowDto
|
|
|
|
{
|
|
|
|
Number = xlRow.RowNumber(),
|
2023-09-29 16:48:59 +05:00
|
|
|
Section = xlRow.Cell(DefaultTemplateInfo.ColumnSection).GetCellValue<string>(),
|
|
|
|
Category = xlRow.Cell(DefaultTemplateInfo.ColumnCategory).GetCellValue<string>(),
|
2023-10-02 09:27:20 +05:00
|
|
|
CategoryInfo = xlRow.Cell(DefaultTemplateInfo.ColumnCategoryInfo).GetCellValue<string?>(),
|
2023-09-29 16:48:59 +05:00
|
|
|
DepthStart = xlRow.Cell(DefaultTemplateInfo.ColumnDepthStart).GetCellValue<double>(),
|
|
|
|
DepthEnd = xlRow.Cell(DefaultTemplateInfo.ColumnDepthEnd).GetCellValue<double>(),
|
|
|
|
Date = xlRow.Cell(DefaultTemplateInfo.ColumnDate).GetCellValue<DateTime>(),
|
|
|
|
Duration = xlRow.Cell(DefaultTemplateInfo.ColumnDuration).GetCellValue<double>(),
|
2023-10-02 09:27:20 +05:00
|
|
|
Comment = xlRow.Cell(DefaultTemplateInfo.ColumnComment).GetCellValue<string?>()
|
2023-09-04 14:11:25 +05:00
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|