2023-11-28 15:54:47 +05:00
|
|
|
|
using AsbCloudApp.Data.Trajectory;
|
2023-11-21 15:10:22 +05:00
|
|
|
|
using ClosedXML.Excel;
|
|
|
|
|
using System.Collections.Generic;
|
2023-11-30 09:40:51 +05:00
|
|
|
|
using System.ComponentModel.DataAnnotations;
|
2023-11-21 15:10:22 +05:00
|
|
|
|
using System.IO;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
|
2023-11-28 15:54:47 +05:00
|
|
|
|
namespace AsbCloudInfrastructure.Services.Trajectory.Import
|
2023-11-21 15:10:22 +05:00
|
|
|
|
{
|
2023-11-30 09:40:51 +05:00
|
|
|
|
public abstract class TrajectoryParserService<T>
|
|
|
|
|
where T : TrajectoryGeoDto
|
2023-11-21 15:10:22 +05:00
|
|
|
|
{
|
2023-11-30 09:40:51 +05:00
|
|
|
|
public abstract string templateFileName { get; }
|
|
|
|
|
public abstract string usingTemplateFile { get; }
|
|
|
|
|
public abstract string sheetNameTrajectory { get; }
|
|
|
|
|
public abstract int headerRowsCount { get; }
|
2023-11-21 15:10:22 +05:00
|
|
|
|
|
2023-11-30 09:40:51 +05:00
|
|
|
|
public TrajectoryParserService()
|
2023-11-21 15:10:22 +05:00
|
|
|
|
{
|
|
|
|
|
}
|
2023-11-30 09:40:51 +05:00
|
|
|
|
protected abstract T ParseRow(IXLRow row);
|
2023-11-21 15:10:22 +05:00
|
|
|
|
|
2023-11-30 09:40:51 +05:00
|
|
|
|
public IEnumerable<T> ImportAsync(Stream stream)
|
2023-11-21 15:10:22 +05:00
|
|
|
|
{
|
|
|
|
|
using var workbook = new XLWorkbook(stream, XLEventTracking.Disabled);
|
|
|
|
|
var trajectoryRows = ParseFileStream(stream);
|
|
|
|
|
|
|
|
|
|
return trajectoryRows;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private IEnumerable<T> ParseFileStream(Stream stream)
|
|
|
|
|
{
|
|
|
|
|
using var workbook = new XLWorkbook(stream, XLEventTracking.Disabled);
|
|
|
|
|
return ParseWorkbook(workbook);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private IEnumerable<T> ParseWorkbook(IXLWorkbook workbook)
|
|
|
|
|
{
|
2023-11-30 09:40:51 +05:00
|
|
|
|
var sheetTrajectory = workbook.Worksheets.FirstOrDefault(ws => ws.Name == sheetNameTrajectory);
|
2023-11-21 15:10:22 +05:00
|
|
|
|
if (sheetTrajectory is null)
|
2023-11-30 09:40:51 +05:00
|
|
|
|
throw new FileFormatException($"Книга excel не содержит листа {sheetNameTrajectory}.");
|
2023-11-21 15:10:22 +05:00
|
|
|
|
var trajectoryRows = ParseSheet(sheetTrajectory);
|
|
|
|
|
return trajectoryRows;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private IEnumerable<T> ParseSheet(IXLWorksheet sheet)
|
|
|
|
|
{
|
2023-11-28 15:54:47 +05:00
|
|
|
|
if (sheet.RangeUsed().RangeAddress.LastAddress.ColumnNumber < 6)
|
2023-11-21 15:10:22 +05:00
|
|
|
|
throw new FileFormatException($"Лист {sheet.Name} содержит меньшее количество столбцов.");
|
|
|
|
|
|
|
|
|
|
var count = sheet.RowsUsed().Count() - headerRowsCount;
|
|
|
|
|
|
|
|
|
|
if (count > 1024)
|
|
|
|
|
throw new FileFormatException($"Лист {sheet.Name} содержит слишком большое количество строк.");
|
|
|
|
|
|
|
|
|
|
if (count <= 0)
|
|
|
|
|
throw new FileFormatException($"Лист {sheet.Name} некорректного формата либо пустой");
|
|
|
|
|
|
|
|
|
|
var trajectoryRows = new List<T>(count);
|
|
|
|
|
var parseErrors = new List<string>();
|
|
|
|
|
for (int i = 0; i < count; i++)
|
|
|
|
|
{
|
|
|
|
|
var row = sheet.Row(1 + i + headerRowsCount);
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
var trajectoryRow = ParseRow(row);
|
|
|
|
|
trajectoryRows.Add(trajectoryRow);
|
|
|
|
|
}
|
|
|
|
|
catch (FileFormatException ex)
|
|
|
|
|
{
|
|
|
|
|
parseErrors.Add(ex.Message);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (parseErrors.Any())
|
|
|
|
|
throw new FileFormatException(string.Join("\r\n", parseErrors));
|
|
|
|
|
|
|
|
|
|
return trajectoryRows;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|