From 1ccfa84e4530bed4222f1bd3f3b9310d28bab777 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=A1=D1=82=D0=B5=D0=BF=D0=B0=D0=BD=D0=BE=D0=B2=20=D0=94?= =?UTF-8?q?=D0=BC=D0=B8=D1=82=D1=80=D0=B8=D0=B9?= Date: Fri, 22 Mar 2024 07:37:06 +0300 Subject: [PATCH] =?UTF-8?q?=D0=A0=D0=B5=D1=84=D0=B0=D1=82=D0=BA=D0=BE?= =?UTF-8?q?=D1=80=D0=B8=D0=BD=D0=B3=20=D0=B8=D0=BD=D1=84=D1=80=D0=B0=D1=81?= =?UTF-8?q?=D1=82=D1=80=D1=83=D0=BA=D1=82=D1=83=D1=80=D1=8B=20=D1=8D=D0=BA?= =?UTF-8?q?=D1=81=D0=BF=D0=BE=D1=80=D1=82=D0=B0/=D0=B8=D0=BC=D0=BF=D0=BE?= =?UTF-8?q?=D1=80=D1=82=D0=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Services/{ => Export}/IExportService.cs | 12 +- .../Services/Export/IExportServiceFactory.cs | 20 +++ .../Services/Parsers/IParserFactory.cs | 23 +++ .../Services/{ => Parsers}/IParserService.cs | 11 +- .../ExcelServices/ExcelExportService.cs | 76 ++++++++++ .../Services/ExcelServices/ExcelParser.cs | 134 ++++++++++++++++++ .../ExcelServices/ExcelWellRelatedParser.cs | 23 +++ .../ExcelServices/ExportExcelService.cs | 3 +- .../ExcelServices/ParserExcelService.cs | 3 +- .../Report/ProcessMapReportDrillingService.cs | 3 +- .../ScheduleReportService.cs | 5 +- AsbCloudInfrastructure/XLExtentions.cs | 10 +- ...ProcessMapReportDataSaubStatServiceTest.cs | 1 + .../ProcessMapPlanBaseController.cs | 2 + .../TrajectoryEditableController.cs | 1 + 15 files changed, 316 insertions(+), 11 deletions(-) rename AsbCloudApp/Services/{ => Export}/IExportService.cs (70%) create mode 100644 AsbCloudApp/Services/Export/IExportServiceFactory.cs create mode 100644 AsbCloudApp/Services/Parsers/IParserFactory.cs rename AsbCloudApp/Services/{ => Parsers}/IParserService.cs (76%) create mode 100644 AsbCloudInfrastructure/Services/ExcelServices/ExcelExportService.cs create mode 100644 AsbCloudInfrastructure/Services/ExcelServices/ExcelParser.cs create mode 100644 AsbCloudInfrastructure/Services/ExcelServices/ExcelWellRelatedParser.cs diff --git a/AsbCloudApp/Services/IExportService.cs b/AsbCloudApp/Services/Export/IExportService.cs similarity index 70% rename from AsbCloudApp/Services/IExportService.cs rename to AsbCloudApp/Services/Export/IExportService.cs index b921fec5..ddfd50f9 100644 --- a/AsbCloudApp/Services/IExportService.cs +++ b/AsbCloudApp/Services/Export/IExportService.cs @@ -3,12 +3,12 @@ using System.Threading; using System.Threading.Tasks; using AsbCloudApp.Requests.ExportOptions; -namespace AsbCloudApp.Services; +namespace AsbCloudApp.Services.Export; /// /// Экспорт данных /// -public interface IExportService +public interface IExportService : IExportService where TOptions : IExportOptionsRequest { /// @@ -18,4 +18,12 @@ public interface IExportService /// /// Task<(string FileName, Stream File)> ExportAsync(TOptions options, CancellationToken token); +} + +/// +/// Экспорт данных +/// +public interface IExportService +{ + } \ No newline at end of file diff --git a/AsbCloudApp/Services/Export/IExportServiceFactory.cs b/AsbCloudApp/Services/Export/IExportServiceFactory.cs new file mode 100644 index 00000000..fb6b5fc9 --- /dev/null +++ b/AsbCloudApp/Services/Export/IExportServiceFactory.cs @@ -0,0 +1,20 @@ +using AsbCloudApp.Requests.ExportOptions; + +namespace AsbCloudApp.Services.Export; + +/// +/// Фабрика создания сервисов для экспорта +/// +/// +public interface IExportServiceFactory + where TId : struct +{ + /// + /// Создать сервис экспорта + /// + /// + /// + /// + IExportService CreateExportService(TId id) + where TOptions : IExportOptionsRequest; +} \ No newline at end of file diff --git a/AsbCloudApp/Services/Parsers/IParserFactory.cs b/AsbCloudApp/Services/Parsers/IParserFactory.cs new file mode 100644 index 00000000..e0fc1aa2 --- /dev/null +++ b/AsbCloudApp/Services/Parsers/IParserFactory.cs @@ -0,0 +1,23 @@ +using AsbCloudApp.Data; +using AsbCloudApp.Requests.ParserOptions; + +namespace AsbCloudApp.Services.Parsers; + +/// +/// Фабрика для создания сервиса парсинга +/// +/// +/// +public interface IParserFactory + where TId : struct + where TDto : class, IId +{ + /// + /// Создать парсер + /// + /// + /// + /// + IParserService CreateParser(TId id) + where TOptions : IParserOptionsRequest; +} \ No newline at end of file diff --git a/AsbCloudApp/Services/IParserService.cs b/AsbCloudApp/Services/Parsers/IParserService.cs similarity index 76% rename from AsbCloudApp/Services/IParserService.cs rename to AsbCloudApp/Services/Parsers/IParserService.cs index 89212ba7..672c9e2b 100644 --- a/AsbCloudApp/Services/IParserService.cs +++ b/AsbCloudApp/Services/Parsers/IParserService.cs @@ -2,14 +2,14 @@ using System.IO; using AsbCloudApp.Data; using AsbCloudApp.Requests.ParserOptions; -namespace AsbCloudApp.Services; +namespace AsbCloudApp.Services.Parsers; /// /// Сервис парсинга /// /// /// -public interface IParserService +public interface IParserService : IParserService where TDto : class, IId where TOptions : IParserOptionsRequest { @@ -26,4 +26,11 @@ public interface IParserService /// /// Stream GetTemplateFile(); +} + +/// +/// Сервис парсинга +/// +public interface IParserService +{ } \ No newline at end of file diff --git a/AsbCloudInfrastructure/Services/ExcelServices/ExcelExportService.cs b/AsbCloudInfrastructure/Services/ExcelServices/ExcelExportService.cs new file mode 100644 index 00000000..74e2d5fb --- /dev/null +++ b/AsbCloudInfrastructure/Services/ExcelServices/ExcelExportService.cs @@ -0,0 +1,76 @@ +using System; +using System.Collections.Generic; +using System.IO; +using System.Linq; +using System.Reflection; +using System.Threading; +using System.Threading.Tasks; +using AsbCloudApp.Requests.ExportOptions; +using AsbCloudApp.Services.Export; +using AsbCloudInfrastructure.Services.ExcelServices.Templates; +using ClosedXML.Excel; +using Mapster; + +namespace AsbCloudInfrastructure.Services.ExcelServices; + +public abstract class ExcelExportService : IExportService + where TOptions : IExportOptionsRequest + where TTemplate : class, ITemplateParameters, new() +{ + protected TTemplate TemplateParameters => new(); + + protected abstract Task BuildFileNameAsync(TOptions options, CancellationToken token); + + protected abstract Task> GetDtosAsync(TOptions options, CancellationToken token); + + public async Task<(string FileName, Stream File)> ExportAsync(TOptions options, CancellationToken token) + { + var dtos = await GetDtosAsync(options, token); + + var fileName = await BuildFileNameAsync(options, token); + var file = BuildFile(dtos); + return (fileName, file); + } + + private Stream BuildFile(IEnumerable dtos) + { + using var template = GetTemplateFile(); + using var workbook = new XLWorkbook(template); + AddDtosToWorkbook(workbook, dtos); + + var memoryStream = new MemoryStream(); + workbook.SaveAs(memoryStream, new SaveOptions { }); + memoryStream.Seek(0, SeekOrigin.Begin); + return memoryStream; + } + + private void AddDtosToWorkbook(XLWorkbook workbook, IEnumerable dtos) + { + var dtosToArray = dtos.ToArray(); + + if (!dtosToArray.Any()) + return; + + var sheet = workbook.GetWorksheet(TemplateParameters.SheetName); + for (var i = 0; i < dtosToArray.Length; i++) + { + var row = sheet.Row(1 + i + TemplateParameters.HeaderRowsCount); + AddRow(row, dtosToArray[i]); + } + } + + private void AddRow(IXLRow xlRow, TDto dto) + { + var properties = dto.Adapt>(); + + foreach (var (name, cellValue) in properties) + { + if (TemplateParameters.Cells.TryGetValue(name, out var cell)) + xlRow.Cell(cell.ColumnNumber).SetCellValue(cellValue); + } + } + + private Stream GetTemplateFile() => + Assembly.GetExecutingAssembly().GetTemplateCopyStream(TemplateParameters.FileName) + ?? throw new ArgumentNullException($"Файл '{TemplateParameters.FileName}' не найден"); +} \ No newline at end of file diff --git a/AsbCloudInfrastructure/Services/ExcelServices/ExcelParser.cs b/AsbCloudInfrastructure/Services/ExcelServices/ExcelParser.cs new file mode 100644 index 00000000..924e7477 --- /dev/null +++ b/AsbCloudInfrastructure/Services/ExcelServices/ExcelParser.cs @@ -0,0 +1,134 @@ +using System; +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.Parsers; +using AsbCloudInfrastructure.Services.ExcelServices.Templates; +using ClosedXML.Excel; +using Mapster; + +namespace AsbCloudInfrastructure.Services.ExcelServices; + +public abstract class ExcelParser : IParserService + where TDto : class, IValidatableObject, IId + where TOptions : IParserOptionsRequest + where TTemplate : class, ITemplateParameters, new() +{ + protected TTemplate TemplateParameters => new(); + + public virtual ParserResultDto Parse(Stream file, TOptions options) + { + using var workbook = new XLWorkbook(file); + var sheet = workbook.GetWorksheet(TemplateParameters.SheetName); + var dtos = ParseExcelSheet(sheet); + return dtos; + } + + public virtual Stream GetTemplateFile() => + Assembly.GetExecutingAssembly().GetTemplateCopyStream(TemplateParameters.FileName) + ?? throw new ArgumentNullException($"Файл '{TemplateParameters.FileName}' не найден"); + + + protected virtual IDictionary ParseRow(IXLRow xlRow) + { + var cells = TemplateParameters.Cells.ToDictionary(x => x.Key, x => + { + var columnNumber = x.Value.ColumnNumber; + var xlCell = xlRow.Cell(columnNumber); + var cellValue = x.Value.GetValueFromCell(xlCell); + return cellValue; + }); + + return cells; + } + + protected virtual TDto BuildDto(IDictionary row, int rowNumber) + { + var dto = row.Adapt(); + return dto; + } + + private ValidationResultDto Validate(TDto dto, int rowNumber) + { + var validationResults = new List(); + + var isValid = dto.Validate(validationResults); + + if (isValid) + { + var validDto = new ValidationResultDto + { + Item = dto + }; + + return validDto; + } + + var columnsDict = TemplateParameters.Cells.ToDictionary(x => x.Key, x => x.Value.ColumnNumber); + + var invalidDto = new ValidationResultDto + { + Item = dto, + Warnings = validationResults + .SelectMany(v => v.MemberNames + .Where(columnsDict.ContainsKey) + .Select(m => + { + var columnNumber = columnsDict[m]; + var errorMessage = v.ErrorMessage; + var warningMessage = string.Format(XLExtentions.ProblemDetailsTemplate, + TemplateParameters.SheetName, + rowNumber, + columnNumber, + errorMessage); + var warning = new ValidationResult(warningMessage, new[] { m }); + return warning; + })) + }; + + return invalidDto; + } + + protected virtual ParserResultDto ParseExcelSheet(IXLWorksheet sheet) + { + var count = sheet.RowsUsed().Count() - TemplateParameters.HeaderRowsCount; + if (count <= 0) + return new ParserResultDto(); + + var valiationResults = new List>(count); + var warnings = new List(); + + for (var i = 0; i < count; i++) + { + var xlRow = sheet.Row(1 + i + TemplateParameters.HeaderRowsCount); + var rowNumber = xlRow.RowNumber(); + + try + { + var row = ParseRow(xlRow); + var dto = BuildDto(row, rowNumber); + var validationResult = Validate(dto, rowNumber); + valiationResults.Add(validationResult); + } + catch (FileFormatException ex) + { + var warning = new ValidationResult(ex.Message); + warnings.Add(warning); + } + } + + var parserResult = new ParserResultDto + { + Item = valiationResults + }; + + if (warnings.Any()) + parserResult.Warnings = warnings; + + return parserResult; + } +} \ No newline at end of file diff --git a/AsbCloudInfrastructure/Services/ExcelServices/ExcelWellRelatedParser.cs b/AsbCloudInfrastructure/Services/ExcelServices/ExcelWellRelatedParser.cs new file mode 100644 index 00000000..55485f77 --- /dev/null +++ b/AsbCloudInfrastructure/Services/ExcelServices/ExcelWellRelatedParser.cs @@ -0,0 +1,23 @@ +using System.ComponentModel.DataAnnotations; +using System.IO; +using AsbCloudApp.Data; +using AsbCloudApp.Requests.ParserOptions; +using AsbCloudInfrastructure.Services.ExcelServices.Templates; + +namespace AsbCloudInfrastructure.Services.ExcelServices; + +public abstract class ExcelWellRelatedParser : ExcelParser + where TDto : class, IValidatableObject, IId, IWellRelated + where TOptions : WellRelatedParserRequest + where TTemplate : class, ITemplateParameters, new() +{ + public override ParserResultDto Parse(Stream file, TOptions options) + { + var result = base.Parse(file, options); + + foreach (var dto in result.Item) + dto.Item.IdWell = options.IdWell; + + return result; + } +} \ No newline at end of file diff --git a/AsbCloudInfrastructure/Services/ExcelServices/ExportExcelService.cs b/AsbCloudInfrastructure/Services/ExcelServices/ExportExcelService.cs index 086eba97..cc068ba0 100644 --- a/AsbCloudInfrastructure/Services/ExcelServices/ExportExcelService.cs +++ b/AsbCloudInfrastructure/Services/ExcelServices/ExportExcelService.cs @@ -6,13 +6,14 @@ using System.Reflection; using System.Threading; using System.Threading.Tasks; using AsbCloudApp.Requests.ExportOptions; -using AsbCloudApp.Services; +using AsbCloudApp.Services.Export; using AsbCloudInfrastructure.Services.ExcelServices.Templates; using ClosedXML.Excel; using Mapster; namespace AsbCloudInfrastructure.Services.ExcelServices; +[Obsolete] public abstract class ExportExcelService : IExportService where TOptions : IExportOptionsRequest { diff --git a/AsbCloudInfrastructure/Services/ExcelServices/ParserExcelService.cs b/AsbCloudInfrastructure/Services/ExcelServices/ParserExcelService.cs index 64e0259c..a74cab70 100644 --- a/AsbCloudInfrastructure/Services/ExcelServices/ParserExcelService.cs +++ b/AsbCloudInfrastructure/Services/ExcelServices/ParserExcelService.cs @@ -6,13 +6,14 @@ using System.Linq; using System.Reflection; using AsbCloudApp.Data; using AsbCloudApp.Requests.ParserOptions; -using AsbCloudApp.Services; +using AsbCloudApp.Services.Parsers; using AsbCloudInfrastructure.Services.ExcelServices.Templates; using ClosedXML.Excel; using Mapster; namespace AsbCloudInfrastructure.Services.ExcelServices; +[Obsolete] public abstract class ParserExcelService : IParserService where TDto : class, IValidatableObject, IId where TOptions : IParserOptionsRequest diff --git a/AsbCloudInfrastructure/Services/ProcessMaps/Report/ProcessMapReportDrillingService.cs b/AsbCloudInfrastructure/Services/ProcessMaps/Report/ProcessMapReportDrillingService.cs index 5ee331d4..3a0d43d4 100644 --- a/AsbCloudInfrastructure/Services/ProcessMaps/Report/ProcessMapReportDrillingService.cs +++ b/AsbCloudInfrastructure/Services/ProcessMaps/Report/ProcessMapReportDrillingService.cs @@ -13,6 +13,7 @@ using System.Collections.Generic; using System.Linq; using System.Threading; using System.Threading.Tasks; +using AsbCloudApp.Data.WellOperation; namespace AsbCloudInfrastructure.Services.ProcessMaps.Report; @@ -57,7 +58,7 @@ public class ProcessMapReportDrillingService : IProcessMapReportDrillingService var requestWellOperationFact = new WellOperationRequest() { - IdWell = idWell, + IdsWell = new[] { idWell }, OperationType = WellOperation.IdOperationTypeFact, GeDepth = geDepth, LeDepth = leDepth diff --git a/AsbCloudInfrastructure/Services/WellOperationService/ScheduleReportService.cs b/AsbCloudInfrastructure/Services/WellOperationService/ScheduleReportService.cs index 39ad3cd6..96ef7b85 100644 --- a/AsbCloudInfrastructure/Services/WellOperationService/ScheduleReportService.cs +++ b/AsbCloudInfrastructure/Services/WellOperationService/ScheduleReportService.cs @@ -9,6 +9,7 @@ using System.IO; using System.Linq; using System.Threading; using System.Threading.Tasks; +using AsbCloudApp.Data.WellOperation; namespace AsbCloudInfrastructure.Services.WellOperationService { @@ -89,7 +90,7 @@ namespace AsbCloudInfrastructure.Services.WellOperationService { var row = sheet.Row(i + headerRowsCount); SetCell(row, columnRowNumber, $"{i}"); - SetCell(row, columnCaption, $"{tvdItem.CategoryName} {tvdItem.CategoryInfo}".Trim()); + SetCell(row, columnCaption, $"{tvdItem.OperationCategoryName} {tvdItem.CategoryInfo}".Trim()); SetCell(row, columnWellDepthStart, tvdItem.DepthStart); SetCell(row, columnWellDepthEnd, tvdItem.DepthEnd); SetCell(row, columnDuration, tvdItem.DurationHours); @@ -150,7 +151,7 @@ namespace AsbCloudInfrastructure.Services.WellOperationService var row = sheet.Row(1 + i + headerRowsCount); SetCell(row, columnRowNumber, $"{1 + i}"); - SetCell(row, columnCaption, $"{operation.CategoryName} {operation.CategoryInfo}".Trim()); + SetCell(row, columnCaption, $"{operation.OperationCategoryName} {operation.CategoryInfo}".Trim()); SetCell(row, columnWellDepthStartPlan, tvdItem.Plan?.DepthStart); SetCell(row, columnWellDepthStartFact, tvdItem.Fact?.DepthStart); diff --git a/AsbCloudInfrastructure/XLExtentions.cs b/AsbCloudInfrastructure/XLExtentions.cs index e9cf11f5..b542c1d1 100644 --- a/AsbCloudInfrastructure/XLExtentions.cs +++ b/AsbCloudInfrastructure/XLExtentions.cs @@ -17,10 +17,16 @@ public static class XLExtentions public static IXLCell SetCellValue(this IXLCell cell, T value, string? format = null) { - if (typeof(T) == typeof(DateTime)) + if (value is DateTime || value is DateTimeOffset) { cell.Style.DateFormat.Format = format ?? "DD.MM.YYYY HH:MM:SS"; - } + + if (value is DateTimeOffset dateTimeOffset) + { + cell.Value = XLCellValue.FromObject(dateTimeOffset.DateTime); + return cell; + } + } cell.Value = XLCellValue.FromObject(value); diff --git a/AsbCloudWebApi.Tests/Services/ProcessMaps/ProcessMapReportDataSaubStatServiceTest.cs b/AsbCloudWebApi.Tests/Services/ProcessMaps/ProcessMapReportDataSaubStatServiceTest.cs index 68751e00..a5cba844 100644 --- a/AsbCloudWebApi.Tests/Services/ProcessMaps/ProcessMapReportDataSaubStatServiceTest.cs +++ b/AsbCloudWebApi.Tests/Services/ProcessMaps/ProcessMapReportDataSaubStatServiceTest.cs @@ -5,6 +5,7 @@ using System.Threading; using System.Threading.Tasks; using AsbCloudApp.Data; using AsbCloudApp.Data.ProcessMaps; +using AsbCloudApp.Data.WellOperation; using AsbCloudApp.Repositories; using AsbCloudApp.Requests; using AsbCloudApp.Services; diff --git a/AsbCloudWebApi/Controllers/ProcessMaps/ProcessMapPlanBaseController.cs b/AsbCloudWebApi/Controllers/ProcessMaps/ProcessMapPlanBaseController.cs index a7d1763e..f4df65cc 100644 --- a/AsbCloudWebApi/Controllers/ProcessMaps/ProcessMapPlanBaseController.cs +++ b/AsbCloudWebApi/Controllers/ProcessMaps/ProcessMapPlanBaseController.cs @@ -16,6 +16,8 @@ using AsbCloudApp.Requests.ParserOptions; using AsbCloudApp.Data.ProcessMaps; using System.ComponentModel.DataAnnotations; using AsbCloudApp.Requests.ExportOptions; +using AsbCloudApp.Services.Export; +using AsbCloudApp.Services.Parsers; namespace AsbCloudWebApi.Controllers.ProcessMaps; diff --git a/AsbCloudWebApi/Controllers/Trajectory/TrajectoryEditableController.cs b/AsbCloudWebApi/Controllers/Trajectory/TrajectoryEditableController.cs index 56a08c1e..2a734d21 100644 --- a/AsbCloudWebApi/Controllers/Trajectory/TrajectoryEditableController.cs +++ b/AsbCloudWebApi/Controllers/Trajectory/TrajectoryEditableController.cs @@ -10,6 +10,7 @@ using System.Threading; using System.Threading.Tasks; using AsbCloudApp.Data; using AsbCloudApp.Requests.ParserOptions; +using AsbCloudApp.Services.Parsers; using AsbCloudInfrastructure.Services.Trajectory.Export; namespace AsbCloudWebApi.Controllers.Trajectory