2024-01-29 15:03:53 +05:00
|
|
|
|
using AsbCloudApp.Data.Trajectory;
|
|
|
|
|
using ClosedXML.Excel;
|
|
|
|
|
using System.IO;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using AsbCloudApp.Data;
|
2024-01-31 17:20:54 +05:00
|
|
|
|
using AsbCloudApp.Services.Parser;
|
2024-01-29 15:03:53 +05:00
|
|
|
|
|
|
|
|
|
namespace AsbCloudInfrastructure.Services.Trajectory.Parser;
|
|
|
|
|
|
2024-01-31 17:20:54 +05:00
|
|
|
|
public abstract class TrajectoryParserService<T> : IParserService<T>
|
2024-01-29 15:03:53 +05:00
|
|
|
|
where T : TrajectoryGeoDto
|
|
|
|
|
{
|
2024-01-31 17:20:54 +05:00
|
|
|
|
private const int HeaderRowsCount = 2;
|
|
|
|
|
private const int ColumnCount = 6;
|
|
|
|
|
|
|
|
|
|
protected abstract string SheetName { get; }
|
|
|
|
|
|
2024-01-29 15:03:53 +05:00
|
|
|
|
protected abstract ValidationResultDto<T> ParseRow(IXLRow row);
|
|
|
|
|
|
2024-01-31 17:20:54 +05:00
|
|
|
|
public ParserResultDto<T> Parse(Stream file)
|
2024-01-29 15:03:53 +05:00
|
|
|
|
{
|
|
|
|
|
using var workbook = new XLWorkbook(file, XLEventTracking.Disabled);
|
|
|
|
|
|
2024-01-31 17:20:54 +05:00
|
|
|
|
var sheet = workbook.Worksheets.FirstOrDefault(ws =>
|
|
|
|
|
ws.Name.ToLower().Trim() == SheetName.ToLower().Trim())
|
|
|
|
|
?? throw new FileFormatException($"Книга excel не содержит листа {SheetName}.");
|
2024-01-29 15:03:53 +05:00
|
|
|
|
|
2024-01-31 17:20:54 +05:00
|
|
|
|
var trajectoryRows = sheet.Parse(ParseRow, ColumnCount, HeaderRowsCount);
|
2024-01-29 15:03:53 +05:00
|
|
|
|
return trajectoryRows;
|
|
|
|
|
}
|
|
|
|
|
}
|