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; using ClosedXML.Excel; using Mapster; namespace AsbCloudInfrastructure.Services.Parser; public abstract class ParserExcelService : IParserService where TDto : class, IValidatableObject, IId where TOptions : IParserOptionsRequest { protected readonly IServiceProvider serviceProvider; protected ParserExcelService(IServiceProvider serviceProvider) { this.serviceProvider = serviceProvider; } protected abstract string SheetName { get; } protected virtual int HeaderRowsCount => 0; protected abstract string TemplateFileName { get; } protected abstract IDictionary Cells { get; } 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; } public virtual Stream GetTemplateFile() => Assembly.GetExecutingAssembly().GetTemplateCopyStream(TemplateFileName) ?? throw new ArgumentNullException($"Файл '{TemplateFileName}' не найден"); protected virtual IDictionary ParseRow(IXLRow xlRow) { var cells = 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 = 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, 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() - 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 + 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; } }