diff --git a/AsbCloudInfrastructure/DependencyInjection.cs b/AsbCloudInfrastructure/DependencyInjection.cs index afab0a5a..363ec8ee 100644 --- a/AsbCloudInfrastructure/DependencyInjection.cs +++ b/AsbCloudInfrastructure/DependencyInjection.cs @@ -45,6 +45,7 @@ using AsbCloudDb.Model.WellSections; using AsbCloudInfrastructure.Services.ProcessMaps; using AsbCloudApp.Data.ProcessMapPlan; using AsbCloudApp.Requests; +using AsbCloudInfrastructure.Services.Parser; namespace AsbCloudInfrastructure { diff --git a/AsbCloudInfrastructure/Services/Parser/Data/Cell.cs b/AsbCloudInfrastructure/Services/Parser/Data/Cell.cs new file mode 100644 index 00000000..a70f004e --- /dev/null +++ b/AsbCloudInfrastructure/Services/Parser/Data/Cell.cs @@ -0,0 +1,17 @@ +using ClosedXML.Excel; + +namespace AsbCloudInfrastructure.Services.Parser.Data; + +public class Cell +{ + public Cell(int columnNumber, + XLDataType type) + { + ColumnNumber = columnNumber; + Type = type; + } + + public int ColumnNumber { get; } + + public XLDataType Type { get; } +} \ No newline at end of file diff --git a/AsbCloudInfrastructure/Services/Parser/Data/Row.cs b/AsbCloudInfrastructure/Services/Parser/Data/Row.cs new file mode 100644 index 00000000..c66ce1fd --- /dev/null +++ b/AsbCloudInfrastructure/Services/Parser/Data/Row.cs @@ -0,0 +1,17 @@ +using System.Collections.Generic; + +namespace AsbCloudInfrastructure.Services.Parser.Data; + +public class Row +{ + public Row(int number, + IDictionary cells) + { + Number = number; + Cells = cells; + } + + public int Number { get; } + + public IDictionary Cells { get; } +} \ No newline at end of file diff --git a/AsbCloudInfrastructure/Services/ParserExcelService.cs b/AsbCloudInfrastructure/Services/Parser/ParserExcelService.cs similarity index 50% rename from AsbCloudInfrastructure/Services/ParserExcelService.cs rename to AsbCloudInfrastructure/Services/Parser/ParserExcelService.cs index a9a28d9e..bb717b56 100644 --- a/AsbCloudInfrastructure/Services/ParserExcelService.cs +++ b/AsbCloudInfrastructure/Services/Parser/ParserExcelService.cs @@ -3,12 +3,15 @@ using System.Collections.Generic; using System.ComponentModel.DataAnnotations; using System.IO; using System.Linq; +using System.Reflection; using AsbCloudApp.Data; using AsbCloudApp.Requests.ParserOptions; using AsbCloudApp.Services; +using AsbCloudInfrastructure.Services.Parser.Data; using ClosedXML.Excel; +using Mapster; -namespace AsbCloudInfrastructure.Services; +namespace AsbCloudInfrastructure.Services.Parser; public abstract class ParserExcelService : IParserService where TDto : class, IValidatableObject, IId @@ -22,42 +25,79 @@ public abstract class ParserExcelService : IParserService 0; - protected abstract IDictionary PropertyColumnNumbers { get; } + protected abstract string TemplateFileName { get; } - public abstract ParserResultDto Parse(Stream file, TOptions options); + protected abstract IDictionary Cells { get; } - public abstract Stream GetTemplateFile(); + public virtual ParserResultDto Parse(Stream file, TOptions options) + { + using var workbook = new XLWorkbook(file); + var sheet = workbook.GetWorksheet(SheetName); + var dtos = ParseExcelSheet(sheet); + return dtos; + } - protected abstract TDto ParseRow(IXLRow xlRow); + public virtual Stream GetTemplateFile() => + Assembly.GetExecutingAssembly().GetTemplateCopyStream(TemplateFileName) + ?? throw new ArgumentNullException($"Файл '{TemplateFileName}' не найден"); + + + protected virtual Row ParseRow(IXLRow xlRow) + { + var cells = Cells.ToDictionary(x => x.Key, x => + { + var columnNumber = x.Value.ColumnNumber; + var type = x.Value.Type; + var cellValue = xlRow.Cell(columnNumber).GetCellValue(type); + + return (x.Value, cellValue); + }); + + var row = new Row(xlRow.RowNumber(), cells); + + return row; + } + + protected virtual TDto BuildDto(Row row) + { + var propertiesDict = row.Cells.ToDictionary(x => x.Key, x => x.Value.CellValue); + + return propertiesDict.Adapt(); + } private ValidationResultDto Validate(TDto dto, int rowNumber) { var validationResults = new List(); - + var isValid = dto.Validate(validationResults); if (isValid) { - var validDto = new ValidationResultDto() + var validDto = new ValidationResultDto { Item = dto }; return validDto; } - + + var columnsDict = Cells.ToDictionary(x => x.Key, x => x.Value.ColumnNumber); + var invalidDto = new ValidationResultDto { Item = dto, Warnings = validationResults .SelectMany(v => v.MemberNames - .Where(PropertyColumnNumbers.ContainsKey) + .Where(columnsDict.ContainsKey) .Select(m => { - var columnNumber = PropertyColumnNumbers[m]; + var columnNumber = columnsDict[m]; var errorMessage = v.ErrorMessage; - var warningMessage = string.Format(XLMessageTemplates.ProblemDetailsTemplate, SheetName, rowNumber, columnNumber, errorMessage); + var warningMessage = string.Format(XLExtentions.ProblemDetailsTemplate, SheetName, rowNumber, columnNumber, + errorMessage); var warning = new ValidationResult(warningMessage, new[] { m }); return warning; })) @@ -66,18 +106,9 @@ public abstract class ParserExcelService : IParserService ParseExcelSheet(IXLWorksheet sheet, - int columnCount, - int headerRowsCount = 0) + protected virtual ParserResultDto ParseExcelSheet(IXLWorksheet sheet) { - if (sheet.RangeUsed().RangeAddress.LastAddress.ColumnNumber < columnCount) - throw new FileFormatException(string.Format(XLMessageTemplates.FewerColumnsTemplate, SheetName)); - - var count = sheet.RowsUsed().Count() - headerRowsCount; - - if (count > 1024) - throw new FileFormatException(string.Format(XLMessageTemplates.ExceedingMaxRowLimitTemplate, SheetName)); - + var count = sheet.RowsUsed().Count() - HeaderRowsCount; if (count <= 0) return new ParserResultDto(); @@ -86,12 +117,13 @@ public abstract class ParserExcelService : IParserService { - #region Columns - - private const int columnSection = 1; - private const int columnMode = 2; - private const int columnDepthStart = 3; - private const int columnDepthEnd = 4; - private const int columnPressurePlan = 5; - private const int columnPressureLimitMax = 6; - private const int columnAxialLoadPlan = 7; - private const int columnAxialLoadLimitMax = 8; - private const int columnTopDriveTorquePlan = 9; - private const int columnTopDriveTorqueLimitMax = 10; - private const int columnTopDriveSpeedPlan = 11; - private const int columnTopDriveSpeedLimitMax = 12; - private const int columnFlowPlan = 13; - private const int columnFlowLimitMax = 14; - private const int columnRopPlan = 15; - private const int columnUsageSaub = 16; - private const int columnUsageSpin = 17; - private const int columnComment = 18; - - #endregion - private readonly IEnumerable sections; public ProcessMapPlanDrillingParser(IServiceProvider serviceProvider) @@ -44,78 +22,60 @@ public class ProcessMapPlanDrillingParser : ProcessMapPlanParser "План"; - - protected override IDictionary PropertyColumnNumbers => new Dictionary - { - { nameof(ProcessMapPlanDrillingDto.DepthStart), columnDepthStart }, - { nameof(ProcessMapPlanDrillingDto.DepthEnd), columnDepthEnd }, - { nameof(ProcessMapPlanDrillingDto.DeltaPressurePlan), columnPressurePlan }, - { nameof(ProcessMapPlanDrillingDto.DeltaPressureLimitMax), columnPressureLimitMax }, - { nameof(ProcessMapPlanDrillingDto.AxialLoadPlan), columnAxialLoadPlan }, - { nameof(ProcessMapPlanDrillingDto.AxialLoadLimitMax), columnAxialLoadLimitMax }, - { nameof(ProcessMapPlanDrillingDto.TopDriveTorquePlan), columnTopDriveTorquePlan }, - { nameof(ProcessMapPlanDrillingDto.TopDriveTorqueLimitMax), columnTopDriveTorqueLimitMax }, - { nameof(ProcessMapPlanDrillingDto.TopDriveSpeedPlan), columnTopDriveSpeedPlan }, - { nameof(ProcessMapPlanDrillingDto.TopDriveSpeedLimitMax), columnTopDriveSpeedLimitMax }, - { nameof(ProcessMapPlanDrillingDto.FlowPlan), columnFlowPlan }, - { nameof(ProcessMapPlanDrillingDto.FlowLimitMax), columnFlowLimitMax }, - { nameof(ProcessMapPlanDrillingDto.RopPlan), columnRopPlan }, - { nameof(ProcessMapPlanDrillingDto.UsageSaub), columnUsageSaub }, - { nameof(ProcessMapPlanDrillingDto.UsageSpin), columnUsageSpin }, - { nameof(ProcessMapPlanDrillingDto.Comment), columnComment } - }; - protected override string TemplateFileName => "ProcessMapPlanDrillingTemplate.xlsx"; - protected override ProcessMapPlanDrillingDto ParseRow(IXLRow row) + private const int ColumnSection = 1; + private const int ColumnMode = 2; + + protected override IDictionary Cells => new Dictionary { - var sectionCaption = row.Cell(columnSection).GetCellValue()?.Trim().ToLower(); - var modeName = row.Cell(columnMode).GetCellValue()?.Trim().ToLower(); + { nameof(ProcessMapPlanDrillingDto.Section), new Cell(ColumnSection, XLDataType.Text) }, + { nameof(ProcessMapPlanDrillingDto.Mode), new Cell(ColumnMode, XLDataType.Text) }, + { nameof(ProcessMapPlanDrillingDto.DepthStart), new Cell(3, XLDataType.Number) }, + { nameof(ProcessMapPlanDrillingDto.DepthEnd), new Cell(4, XLDataType.Number) }, + { nameof(ProcessMapPlanDrillingDto.DeltaPressurePlan), new Cell(5, XLDataType.Number) }, + { nameof(ProcessMapPlanDrillingDto.DeltaPressureLimitMax), new Cell(6, XLDataType.Number) }, + { nameof(ProcessMapPlanDrillingDto.AxialLoadPlan), new Cell(7, XLDataType.Number) }, + { nameof(ProcessMapPlanDrillingDto.AxialLoadLimitMax), new Cell(8, XLDataType.Number) }, + { nameof(ProcessMapPlanDrillingDto.TopDriveTorquePlan), new Cell(9, XLDataType.Number) }, + { nameof(ProcessMapPlanDrillingDto.TopDriveTorqueLimitMax), new Cell(10, XLDataType.Number) }, + { nameof(ProcessMapPlanDrillingDto.TopDriveSpeedPlan), new Cell(11, XLDataType.Number) }, + { nameof(ProcessMapPlanDrillingDto.TopDriveSpeedLimitMax), new Cell(12, XLDataType.Number) }, + { nameof(ProcessMapPlanDrillingDto.FlowPlan), new Cell(13, XLDataType.Number) }, + { nameof(ProcessMapPlanDrillingDto.FlowLimitMax), new Cell(14, XLDataType.Number) }, + { nameof(ProcessMapPlanDrillingDto.RopPlan), new Cell(15, XLDataType.Number) }, + { nameof(ProcessMapPlanDrillingDto.UsageSaub), new Cell(16, XLDataType.Number) }, + { nameof(ProcessMapPlanDrillingDto.UsageSpin), new Cell(17, XLDataType.Number) }, + { nameof(ProcessMapPlanDrillingDto.Comment), new Cell(18, XLDataType.Text) } + }; + + protected override ProcessMapPlanDrillingDto BuildDto(Row row) + { + var dto = base.BuildDto(row); var section = sections.FirstOrDefault(s => - string.Equals(s.Caption.Trim(), sectionCaption?.Trim(), StringComparison.CurrentCultureIgnoreCase)); + string.Equals(s.Caption.Trim(), dto.Section?.Trim(), StringComparison.CurrentCultureIgnoreCase)); if (section is null) { - var message = string.Format(XLMessageTemplates.ProblemDetailsTemplate, SheetName, row.RowNumber(), columnSection, + var message = string.Format(XLExtentions.ProblemDetailsTemplate, SheetName, row.Number, ColumnSection, "Указана некорректная секция"); throw new FileFormatException(message); } - var idMode = GetIdMode(modeName); + var idMode = GetIdMode(dto.Mode); if (idMode is null) { - var message = string.Format(XLMessageTemplates.ProblemDetailsTemplate, SheetName, row.RowNumber(), columnSection, + var message = string.Format(XLExtentions.ProblemDetailsTemplate, SheetName, row.Number, ColumnSection, "Указан некорректный режим бурения"); throw new FileFormatException(message); } - var dto = new ProcessMapPlanDrillingDto - { - IdWellSectionType = section.Id, - Section = section.Caption, - IdMode = idMode.Value, - Mode = modeName, - DepthStart = row.Cell(columnDepthStart).GetCellValue(), - DepthEnd = row.Cell(columnDepthEnd).GetCellValue(), - AxialLoadPlan = row.Cell(columnAxialLoadPlan).GetCellValue(), - AxialLoadLimitMax = row.Cell(columnAxialLoadLimitMax).GetCellValue(), - DeltaPressurePlan = row.Cell(columnPressurePlan).GetCellValue(), - DeltaPressureLimitMax = row.Cell(columnPressureLimitMax).GetCellValue(), - TopDriveTorquePlan = row.Cell(columnTopDriveTorquePlan).GetCellValue(), - TopDriveTorqueLimitMax = row.Cell(columnTopDriveTorqueLimitMax).GetCellValue(), - TopDriveSpeedPlan = row.Cell(columnTopDriveSpeedPlan).GetCellValue(), - TopDriveSpeedLimitMax = row.Cell(columnTopDriveSpeedLimitMax).GetCellValue(), - FlowPlan = row.Cell(columnFlowPlan).GetCellValue(), - FlowLimitMax = row.Cell(columnFlowLimitMax).GetCellValue(), - RopPlan = row.Cell(columnRopPlan).GetCellValue(), - UsageSaub = row.Cell(columnUsageSaub).GetCellValue(), - UsageSpin = row.Cell(columnUsageSpin).GetCellValue(), - Comment = row.Cell(columnComment).GetCellValue() ?? string.Empty - }; + dto.IdWellSectionType = section.Id; + dto.IdMode = idMode.Value; return dto; } diff --git a/AsbCloudInfrastructure/Services/ProcessMapPlan/Parser/ProcessMapPlanParser.cs b/AsbCloudInfrastructure/Services/ProcessMapPlan/Parser/ProcessMapPlanParser.cs index ff76debb..3a427738 100644 --- a/AsbCloudInfrastructure/Services/ProcessMapPlan/Parser/ProcessMapPlanParser.cs +++ b/AsbCloudInfrastructure/Services/ProcessMapPlan/Parser/ProcessMapPlanParser.cs @@ -1,10 +1,9 @@ using System; using System.IO; -using System.Reflection; using AsbCloudApp.Data; using AsbCloudApp.Data.ProcessMapPlan; using AsbCloudApp.Requests.ParserOptions; -using ClosedXML.Excel; +using AsbCloudInfrastructure.Services.Parser; namespace AsbCloudInfrastructure.Services.ProcessMapPlan.Parser; @@ -16,24 +15,7 @@ public abstract class ProcessMapPlanParser : ParserExcelService Parse(Stream file, IParserOptionsRequest options) - { - using var workbook = new XLWorkbook(file); - - var sheet = workbook.GetWorksheet(SheetName); - - var processMaps = ParseExcelSheet(sheet, ColumnCount, HeaderRowsCount); - return processMaps; - } - - public override Stream GetTemplateFile() => - Assembly.GetExecutingAssembly().GetTemplateCopyStream(TemplateFileName) - ?? throw new ArgumentNullException($"Файл '{TemplateFileName}' не найден"); + protected override int HeaderRowsCount => 2; protected static int? GetIdMode(string? modeName) => modeName?.Trim().ToLower() switch diff --git a/AsbCloudInfrastructure/Services/Trajectory/Parser/TrajectoryFactManualParser.cs b/AsbCloudInfrastructure/Services/Trajectory/Parser/TrajectoryFactManualParser.cs index e48fb575..37bad537 100644 --- a/AsbCloudInfrastructure/Services/Trajectory/Parser/TrajectoryFactManualParser.cs +++ b/AsbCloudInfrastructure/Services/Trajectory/Parser/TrajectoryFactManualParser.cs @@ -1,23 +1,15 @@ using System; using System.Collections.Generic; using AsbCloudApp.Data.Trajectory; +using AsbCloudApp.Requests.ParserOptions; +using AsbCloudInfrastructure.Services.Parser; +using AsbCloudInfrastructure.Services.Parser.Data; using ClosedXML.Excel; namespace AsbCloudInfrastructure.Services.Trajectory.Parser; -public class TrajectoryFactManualParser : TrajectoryParser +public class TrajectoryFactManualParser : ParserExcelService { - #region Columns - - private const int columnWellboreDepth = 1; - private const int columnZenithAngle = 2; - private const int columnAzimuthGeo = 3; - private const int columnAzimuthMagnetic = 4; - private const int columnVerticalDepth = 5; - private const int columnComment = 6; - - #endregion - public TrajectoryFactManualParser(IServiceProvider serviceProvider) : base(serviceProvider) { @@ -25,30 +17,17 @@ public class TrajectoryFactManualParser : TrajectoryParser protected override string SheetName => "Фактическая траектория"; + protected override int HeaderRowsCount => 2; + protected override string TemplateFileName => "TrajectoryFactManualTemplate.xlsx"; - protected override IDictionary PropertyColumnNumbers => new Dictionary + protected override IDictionary Cells => new Dictionary() { - { nameof(TrajectoryGeoFactDto.WellboreDepth), columnWellboreDepth }, - { nameof(TrajectoryGeoFactDto.ZenithAngle), columnZenithAngle }, - { nameof(TrajectoryGeoFactDto.AzimuthGeo), columnAzimuthGeo }, - { nameof(TrajectoryGeoFactDto.AzimuthMagnetic), columnAzimuthMagnetic }, - { nameof(TrajectoryGeoFactDto.VerticalDepth), columnVerticalDepth }, - { nameof(TrajectoryGeoFactDto.Comment), columnComment } + { nameof(TrajectoryGeoFactDto.WellboreDepth), new Cell(1, XLDataType.Number) }, + { nameof(TrajectoryGeoFactDto.ZenithAngle), new Cell(2, XLDataType.Number) }, + { nameof(TrajectoryGeoFactDto.AzimuthGeo), new Cell(3, XLDataType.Number) }, + { nameof(TrajectoryGeoFactDto.AzimuthMagnetic), new Cell(4, XLDataType.Number) }, + { nameof(TrajectoryGeoFactDto.VerticalDepth), new Cell(5, XLDataType.Number) }, + { nameof(TrajectoryGeoFactDto.Comment), new Cell(6, XLDataType.Text) } }; - - protected override TrajectoryGeoFactDto ParseRow(IXLRow row) - { - var dto = new TrajectoryGeoFactDto - { - WellboreDepth = row.Cell(columnWellboreDepth).GetCellValue(), - ZenithAngle = row.Cell(columnZenithAngle).GetCellValue(), - AzimuthGeo = row.Cell(columnAzimuthGeo).GetCellValue(), - AzimuthMagnetic = row.Cell(columnAzimuthMagnetic).GetCellValue(), - VerticalDepth = row.Cell(columnVerticalDepth).GetCellValue(), - Comment = row.Cell(columnComment).GetCellValue() - }; - - return dto; - } } \ No newline at end of file diff --git a/AsbCloudInfrastructure/Services/Trajectory/Parser/TrajectoryParser.cs b/AsbCloudInfrastructure/Services/Trajectory/Parser/TrajectoryParser.cs deleted file mode 100644 index c82cfc5c..00000000 --- a/AsbCloudInfrastructure/Services/Trajectory/Parser/TrajectoryParser.cs +++ /dev/null @@ -1,37 +0,0 @@ -using System; -using AsbCloudApp.Data.Trajectory; -using ClosedXML.Excel; -using System.IO; -using System.Reflection; -using AsbCloudApp.Data; -using AsbCloudApp.Requests.ParserOptions; - -namespace AsbCloudInfrastructure.Services.Trajectory.Parser; - -public abstract class TrajectoryParser : ParserExcelService - where T : TrajectoryGeoDto -{ - private const int HeaderRowsCount = 2; - private const int ColumnCount = 6; - - protected TrajectoryParser(IServiceProvider serviceProvider) - : base(serviceProvider) - { - } - - protected abstract string TemplateFileName { get; } - - public override Stream GetTemplateFile() => - Assembly.GetExecutingAssembly().GetTemplateCopyStream(TemplateFileName) - ?? throw new ArgumentNullException($"Файл '{TemplateFileName}' не найден"); - - public override ParserResultDto Parse(Stream file, IParserOptionsRequest options) - { - using var workbook = new XLWorkbook(file); - - var sheet = workbook.GetWorksheet(SheetName); - - var trajectoryRows = ParseExcelSheet(sheet, ColumnCount, HeaderRowsCount); - return trajectoryRows; - } -} \ No newline at end of file diff --git a/AsbCloudInfrastructure/Services/Trajectory/Parser/TrajectoryPlanParser.cs b/AsbCloudInfrastructure/Services/Trajectory/Parser/TrajectoryPlanParser.cs index de8db5e1..99ec5ae6 100644 --- a/AsbCloudInfrastructure/Services/Trajectory/Parser/TrajectoryPlanParser.cs +++ b/AsbCloudInfrastructure/Services/Trajectory/Parser/TrajectoryPlanParser.cs @@ -1,25 +1,15 @@ using System; using System.Collections.Generic; -using AsbCloudApp.Data; using AsbCloudApp.Data.Trajectory; +using AsbCloudApp.Requests.ParserOptions; +using AsbCloudInfrastructure.Services.Parser; +using AsbCloudInfrastructure.Services.Parser.Data; using ClosedXML.Excel; namespace AsbCloudInfrastructure.Services.Trajectory.Parser; -public class TrajectoryPlanParser : TrajectoryParser +public class TrajectoryPlanParser : ParserExcelService { - #region Columns - - private const int columnWellboreDepth = 1; - private const int columnZenithAngle = 2; - private const int columnAzimuthGeo = 3; - private const int columnAzimuthMagnetic = 4; - private const int columnVerticalDepth = 5; - private const int columnRadius = 6; - private const int columnComment = 7; - - #endregion - public TrajectoryPlanParser(IServiceProvider serviceProvider) : base(serviceProvider) { @@ -27,32 +17,18 @@ public class TrajectoryPlanParser : TrajectoryParser protected override string SheetName => "Плановая траектория"; + protected override int HeaderRowsCount => 2; + protected override string TemplateFileName => "TrajectoryPlanTemplate.xlsx"; - protected override IDictionary PropertyColumnNumbers => new Dictionary + protected override IDictionary Cells => new Dictionary { - { nameof(TrajectoryGeoPlanDto.WellboreDepth), columnWellboreDepth }, - { nameof(TrajectoryGeoPlanDto.ZenithAngle), columnZenithAngle }, - { nameof(TrajectoryGeoPlanDto.AzimuthGeo), columnAzimuthGeo }, - { nameof(TrajectoryGeoPlanDto.AzimuthMagnetic), columnAzimuthMagnetic }, - { nameof(TrajectoryGeoPlanDto.VerticalDepth), columnVerticalDepth }, - { nameof(TrajectoryGeoPlanDto.Radius), columnRadius }, - { nameof(TrajectoryGeoPlanDto.Comment), columnComment } + { nameof(TrajectoryGeoPlanDto.WellboreDepth), new Cell(1, XLDataType.Number) }, + { nameof(TrajectoryGeoPlanDto.ZenithAngle), new Cell(2, XLDataType.Number) }, + { nameof(TrajectoryGeoPlanDto.AzimuthGeo), new Cell(3, XLDataType.Number) }, + { nameof(TrajectoryGeoPlanDto.AzimuthMagnetic), new Cell(4, XLDataType.Number) }, + { nameof(TrajectoryGeoPlanDto.VerticalDepth), new Cell(5, XLDataType.Number) }, + { nameof(TrajectoryGeoPlanDto.Radius), new Cell(6, XLDataType.Number) }, + { nameof(TrajectoryGeoPlanDto.Comment), new Cell(7, XLDataType.Text) } }; - - protected override TrajectoryGeoPlanDto ParseRow(IXLRow row) - { - var dto = new TrajectoryGeoPlanDto - { - WellboreDepth = row.Cell(columnWellboreDepth).GetCellValue(), - ZenithAngle = row.Cell(columnZenithAngle).GetCellValue(), - AzimuthGeo = row.Cell(columnAzimuthGeo).GetCellValue(), - AzimuthMagnetic = row.Cell(columnAzimuthMagnetic).GetCellValue(), - VerticalDepth = row.Cell(columnVerticalDepth).GetCellValue(), - Radius = row.Cell(columnRadius).GetCellValue(), - Comment = row.Cell(columnComment).GetCellValue() - }; - - return dto; - } } \ No newline at end of file diff --git a/AsbCloudInfrastructure/XLExtentions.cs b/AsbCloudInfrastructure/XLExtentions.cs index 51545ff8..481ea891 100644 --- a/AsbCloudInfrastructure/XLExtentions.cs +++ b/AsbCloudInfrastructure/XLExtentions.cs @@ -7,9 +7,15 @@ namespace AsbCloudInfrastructure; public static class XLExtentions { + public const string ProblemDetailsTemplate = "Лист: {0}, Строка: {1}, Столбец: {2}. {3}"; + + public const string NotFoundSheetTemplate = "Книга excel не содержит листа {0}"; + + public const string InvalidValueTemplate = "Лист: {0}, Строка: {1}, Столбец: {2}. Содержит некорректное значение"; + public static IXLWorksheet GetWorksheet(this IXLWorkbook workbook, string sheetName) => workbook.Worksheets.FirstOrDefault(ws => string.Equals(ws.Name.Trim(), sheetName.Trim(), StringComparison.CurrentCultureIgnoreCase)) - ?? throw new FileFormatException(string.Format(XLMessageTemplates.NotFoundSheetTemplate, sheetName)); + ?? throw new FileFormatException(string.Format(NotFoundSheetTemplate, sheetName)); public static IXLCell SetCellValue(this IXLCell cell, T value) { @@ -28,6 +34,29 @@ public static class XLExtentions return cell; } + public static object? GetCellValue(this IXLCell cell, XLDataType type) + { + try + { + return type switch + { + XLDataType.Blank => null, + XLDataType.Boolean => cell.Value.GetBoolean(), + XLDataType.Number => cell.Value.GetNumber(), + XLDataType.Text => cell.Value.GetText(), + XLDataType.Error => cell.Value.GetError(), + XLDataType.DateTime => cell.Value.GetDateTime(), + XLDataType.TimeSpan => cell.Value.GetTimeSpan(), + _ => throw new InvalidCastException() + }; + } + catch + { + var message = string.Format(InvalidValueTemplate, cell.Worksheet.Name, cell.Address.RowNumber, cell.Address.ColumnNumber); + throw new FileFormatException(message); + } + } + public static T? GetCellValue(this IXLCell cell) { try @@ -39,8 +68,7 @@ public static class XLExtentions } catch { - var message = string.Format(XLMessageTemplates.ProblemDetailsTemplate, cell.Worksheet.Name, cell.Address.RowNumber, - cell.Address.ColumnNumber, "Содержит некорректное значение"); + var message = string.Format(InvalidValueTemplate, cell.Worksheet.Name, cell.Address.RowNumber, cell.Address.ColumnNumber); throw new FileFormatException(message); } } diff --git a/AsbCloudInfrastructure/XLMessageTemplates.cs b/AsbCloudInfrastructure/XLMessageTemplates.cs deleted file mode 100644 index edb8eba9..00000000 --- a/AsbCloudInfrastructure/XLMessageTemplates.cs +++ /dev/null @@ -1,12 +0,0 @@ -namespace AsbCloudInfrastructure; - -public static class XLMessageTemplates -{ - public const string ProblemDetailsTemplate = "Лист: {0}, Строка: {1}, Столбец: {2}. {3}"; - - public const string NotFoundSheetTemplate = "Книга excel не содержит листа {0}"; - - public const string ExceedingMaxRowLimitTemplate = "Лист {0} содержит слишком большое количество строк"; - - public const string FewerColumnsTemplate = "Лист {0} содержит меньшее количество столбцов"; -} \ No newline at end of file diff --git a/AsbCloudWebApi.IntegrationTests/Controllers/ProcessMapPlan/ProcessMapPlanDrillingControllerTest.cs b/AsbCloudWebApi.IntegrationTests/Controllers/ProcessMapPlan/ProcessMapPlanDrillingControllerTest.cs index 67844201..f06e2133 100644 --- a/AsbCloudWebApi.IntegrationTests/Controllers/ProcessMapPlan/ProcessMapPlanDrillingControllerTest.cs +++ b/AsbCloudWebApi.IntegrationTests/Controllers/ProcessMapPlan/ProcessMapPlanDrillingControllerTest.cs @@ -29,7 +29,7 @@ public class ProcessMapPlanDrillingControllerTest: BaseIntegrationTest DepthEnd = 1.5, IdMode = 1, - Mode = "ротор", + Mode = "Ротор", AxialLoadPlan = 2.718281, AxialLoadLimitMax = 3.1415926, DeltaPressurePlan = 4, diff --git a/AsbCloudWebApi.Tests/Services/Trajectory/TrajectoryParserTest.cs b/AsbCloudWebApi.Tests/Services/Trajectory/TrajectoryParserTest.cs index 2a90be08..9e2ebd7e 100644 --- a/AsbCloudWebApi.Tests/Services/Trajectory/TrajectoryParserTest.cs +++ b/AsbCloudWebApi.Tests/Services/Trajectory/TrajectoryParserTest.cs @@ -2,7 +2,7 @@ using System.Linq; using AsbCloudApp.Data.Trajectory; using AsbCloudApp.Requests.ParserOptions; -using AsbCloudInfrastructure.Services; +using AsbCloudInfrastructure.Services.Parser; using Microsoft.Extensions.DependencyInjection; using NSubstitute; using Xunit; diff --git a/AsbCloudWebApi/Controllers/ProcessMapPlan/ProcessMapPlanBaseController.cs b/AsbCloudWebApi/Controllers/ProcessMapPlan/ProcessMapPlanBaseController.cs index fb55bcf7..ca54ef62 100644 --- a/AsbCloudWebApi/Controllers/ProcessMapPlan/ProcessMapPlanBaseController.cs +++ b/AsbCloudWebApi/Controllers/ProcessMapPlan/ProcessMapPlanBaseController.cs @@ -14,7 +14,7 @@ using AsbCloudApp.Services; using System.Linq; using AsbCloudApp.Data; using AsbCloudApp.Requests.ParserOptions; -using AsbCloudInfrastructure.Services; +using AsbCloudInfrastructure.Services.Parser; using AsbCloudWebApi.Controllers.Interfaces; namespace AsbCloudWebApi.Controllers.ProcessMapPlan; diff --git a/AsbCloudWebApi/Controllers/ProcessMapPlan/ProcessMapPlanDrillingController.cs b/AsbCloudWebApi/Controllers/ProcessMapPlan/ProcessMapPlanDrillingController.cs index 8855c54f..9e9a7175 100644 --- a/AsbCloudWebApi/Controllers/ProcessMapPlan/ProcessMapPlanDrillingController.cs +++ b/AsbCloudWebApi/Controllers/ProcessMapPlan/ProcessMapPlanDrillingController.cs @@ -2,7 +2,7 @@ using AsbCloudApp.Repositories; using AsbCloudApp.Requests; using AsbCloudApp.Services; -using AsbCloudInfrastructure.Services; +using AsbCloudInfrastructure.Services.Parser; namespace AsbCloudWebApi.Controllers.ProcessMapPlan; diff --git a/AsbCloudWebApi/Controllers/Trajectory/TrajectoryEditableController.cs b/AsbCloudWebApi/Controllers/Trajectory/TrajectoryEditableController.cs index 7a04e7fc..30251923 100644 --- a/AsbCloudWebApi/Controllers/Trajectory/TrajectoryEditableController.cs +++ b/AsbCloudWebApi/Controllers/Trajectory/TrajectoryEditableController.cs @@ -11,7 +11,7 @@ using System.Threading; using System.Threading.Tasks; using AsbCloudApp.Data; using AsbCloudApp.Requests.ParserOptions; -using AsbCloudInfrastructure.Services; +using AsbCloudInfrastructure.Services.Parser; using AsbCloudWebApi.Controllers.Interfaces; namespace AsbCloudWebApi.Controllers.Trajectory diff --git a/AsbCloudWebApi/Controllers/Trajectory/TrajectoryFactManualController.cs b/AsbCloudWebApi/Controllers/Trajectory/TrajectoryFactManualController.cs index 9113acc8..2bc3c769 100644 --- a/AsbCloudWebApi/Controllers/Trajectory/TrajectoryFactManualController.cs +++ b/AsbCloudWebApi/Controllers/Trajectory/TrajectoryFactManualController.cs @@ -1,7 +1,7 @@ using AsbCloudApp.Data.Trajectory; using AsbCloudApp.Repositories; using AsbCloudApp.Services; -using AsbCloudInfrastructure.Services; +using AsbCloudInfrastructure.Services.Parser; using AsbCloudInfrastructure.Services.Trajectory.Export; using Microsoft.AspNetCore.Mvc; diff --git a/AsbCloudWebApi/Controllers/Trajectory/TrajectoryPlanController.cs b/AsbCloudWebApi/Controllers/Trajectory/TrajectoryPlanController.cs index 49e1c1ba..c22876dc 100644 --- a/AsbCloudWebApi/Controllers/Trajectory/TrajectoryPlanController.cs +++ b/AsbCloudWebApi/Controllers/Trajectory/TrajectoryPlanController.cs @@ -7,7 +7,7 @@ using Microsoft.AspNetCore.Mvc; using System.Collections.Generic; using System.Threading; using System.Threading.Tasks; -using AsbCloudInfrastructure.Services; +using AsbCloudInfrastructure.Services.Parser; namespace AsbCloudWebApi.Controllers.Trajectory {