aboutsummaryrefslogtreecommitdiff
path: root/RhSolutions.SkuParser.Api/Services/BsExcelParser.cs
diff options
context:
space:
mode:
authorSerghei Cebotari <serghei@cebotari.ru>2025-04-28 16:43:14 +0300
committerSerghei Cebotari <serghei@cebotari.ru>2025-04-28 16:43:14 +0300
commit1ec08919c353d1459c524442b81538fd57fe5e83 (patch)
treeca5fb19390ba3b03fc2f2395a7f2a6851bed6872 /RhSolutions.SkuParser.Api/Services/BsExcelParser.cs
parent52718a90922b569350e36baee5d4d3c3e5723fed (diff)
Refactor parsers to include logging functionality and update tests accordinglyHEADmaster
Diffstat (limited to 'RhSolutions.SkuParser.Api/Services/BsExcelParser.cs')
-rw-r--r--RhSolutions.SkuParser.Api/Services/BsExcelParser.cs123
1 files changed, 67 insertions, 56 deletions
diff --git a/RhSolutions.SkuParser.Api/Services/BsExcelParser.cs b/RhSolutions.SkuParser.Api/Services/BsExcelParser.cs
index 801e28f..7abe8fa 100644
--- a/RhSolutions.SkuParser.Api/Services/BsExcelParser.cs
+++ b/RhSolutions.SkuParser.Api/Services/BsExcelParser.cs
@@ -7,64 +7,75 @@ namespace RhSolutions.SkuParser.Api.Services;
public class BsExcelParser : ISkuParser
{
- private IConfiguration configuration;
- private const int rowsLookupCount = 20;
- private const decimal vat = 1.2M;
- public BsExcelParser(IConfiguration configuration)
- {
- this.configuration = configuration;
- }
+ private readonly IConfiguration configuration;
+ private readonly ILogger<BsExcelParser> logger;
+ private const int rowsLookupCount = 20;
+ private const decimal vat = 1.2M;
- public Dictionary<Product, double> ParseProducts(IFormFile file)
- {
- using XLWorkbook workbook = new(file.OpenReadStream());
- IXLWorksheet ws = workbook.Worksheets.FirstOrDefault(ws => ws.TabActive) ?? workbook.Worksheet(1);
- var headers = configuration.GetSection("Headers");
- ws.AutoFilter.Clear();
- var cells = configuration.GetSection("Headers")
- .GetChildren()
- .ToDictionary(x => x.Path, x => ws.Range(1, 1, rowsLookupCount, ws.LastCellUsed().Address.ColumnNumber)
- .Search(x.Value)
- .FirstOrDefault())
- .OrderBy(x => x.Value?.Address.ColumnNumber ?? int.MaxValue);
- if (cells == null || cells.Any(x => x.Value == null))
- {
- throw new DataException($"В рабочей книге отсуствует таблица с необходимыми заголовками: {file.Name}");
- }
+ public BsExcelParser(IConfiguration configuration, ILogger<BsExcelParser> logger)
+ {
+ this.configuration = configuration;
+ this.logger = logger;
+ }
- var firstCell = cells.First().Value;
- var lastCell = cells.Last().Value?.WorksheetColumn().LastCellUsed();
- var table = ws.Range(firstCell, lastCell).AsTable();
- var rows = table.DataRange.Rows();
- Dictionary<Product, double> result = new();
+ public Dictionary<Product, double> ParseProducts(IFormFile file)
+ {
+ using XLWorkbook workbook = new(file.OpenReadStream());
+ IXLWorksheet ws = workbook.Worksheets.FirstOrDefault(ws => ws.TabActive) ?? workbook.Worksheet(1);
+ var headers = configuration.GetSection("Headers");
+ ws.AutoFilter.Clear();
+ var cells = configuration.GetSection("Headers")
+ .GetChildren()
+ .ToDictionary(x => x.Path, x => ws.Range(1, 1, rowsLookupCount, ws.LastCellUsed().Address.ColumnNumber)
+ .Search(x.Value)
+ .FirstOrDefault())
+ .OrderBy(x => x.Value?.Address.ColumnNumber ?? int.MaxValue);
+ if (cells == null || cells.Any(x => x.Value == null))
+ {
+ throw new DataException($"В рабочей книге отсуствует таблица с необходимыми заголовками: {file.Name}");
+ }
- foreach (var row in rows.Where(r => r.Field(headers["Quantity"]).Value.IsNumber))
- {
- Product product = new()
- {
- Sku = row.Field(headers["Sku"]).GetString(),
- ProductLine = row.Field(headers["ProductLine"]).GetString() switch
- {
- "RAUTITAN" => ProductLine.RAUTITAN,
- "RAUTHERM S" => ProductLine.RAUTHERMS,
- _ => null
- },
- Name = row.Field(headers["Name"]).GetString(),
- Price = decimal.TryParse(row.Field(headers["Price"]).GetString(), out decimal value)
- ? Math.Round(value * vat, 2)
- : null
- };
+ var firstCell = cells.First().Value;
+ var lastCell = cells.Last().Value?.WorksheetColumn().LastCellUsed();
+ var table = ws.Range(firstCell, lastCell).AsTable();
+ var rows = table.DataRange.Rows();
+ Dictionary<Product, double> result = new();
- double quantity = double.TryParse(row.Field(headers["Quantity"]).GetString(), out double q) ? q : 0;
- if (result.ContainsKey(product))
- {
- result[product] += quantity;
- }
- else
- {
- result.Add(product, quantity);
- }
- }
- return result;
- }
+ foreach (var row in rows.Where(r => r.Field(headers["Quantity"]).Value.IsNumber))
+ {
+ Product product = new()
+ {
+ Sku = row.Field(headers["Sku"]).GetString(),
+ ProductLine = row.Field(headers["ProductLine"]).GetString() switch
+ {
+ "RAUTITAN" => ProductLine.RAUTITAN,
+ "RAUTHERM S" => ProductLine.RAUTHERMS,
+ _ => null
+ },
+ Name = row.Field(headers["Name"]).GetString(),
+ Price = decimal.TryParse(row.Field(headers["Price"]).GetString(), out decimal value)
+ ? Math.Round(value * vat, 2)
+ : null
+ };
+
+ double quantity = double.TryParse(row.Field(headers["Quantity"]).GetString(), out double q) ? q : 0;
+ if (result.ContainsKey(product))
+ {
+ result[product] += quantity;
+ }
+ else
+ {
+ result.Add(product, quantity);
+ }
+ }
+
+ logger.LogInformation("Parsed {Count} products from file {FileName}.", result.Count, file.FileName);
+ foreach (var (product, quantity) in result)
+ {
+ logger.LogInformation("Product: {Sku}, Name: {Name}, Quantity: {Quantity}, Price: {Price}, ProductLine: {ProductLine}",
+ product.Sku, product.Name, quantity, product.Price, product.ProductLine);
+ }
+
+ return result;
+ }
}