From 79eeb2a20668875bc9333471125ea30efe43b043 Mon Sep 17 00:00:00 2001 From: Sergey Chebotar Date: Wed, 24 May 2023 06:39:50 +0300 Subject: [PATCH] Refactoring Guess Reader --- RhSolutions.AddIn/Services/GuessReader.cs | 43 +++++++---------------- 1 file changed, 12 insertions(+), 31 deletions(-) diff --git a/RhSolutions.AddIn/Services/GuessReader.cs b/RhSolutions.AddIn/Services/GuessReader.cs index 5f8cc94..2f6c3c6 100644 --- a/RhSolutions.AddIn/Services/GuessReader.cs +++ b/RhSolutions.AddIn/Services/GuessReader.cs @@ -1,4 +1,5 @@ -using System.IO; +using Microsoft.Office.Interop.Excel; +using System.IO; namespace RhSolutions.Services; @@ -56,7 +57,6 @@ public class GuessReader : IReader if (amountColumnIndex == 0) { amountColumnIndex = amountColumnIndeces - .OrderBy(i => i) .Where(i => i < productColumnIndex) .LastOrDefault(); } @@ -121,19 +121,9 @@ public class GuessReader : IReader continue; } - double value = 0.0; + double? value = currentCell as double?; - if (currentCell.GetType() == typeof(double)) - { - value = (double)currentCell; - } - - else if (currentCell.GetType() == typeof(string)) - { - double.TryParse((string)currentCell, out value); - } - - if (value == 0) + if (value == null || value == 0) { continue; } @@ -143,7 +133,7 @@ public class GuessReader : IReader return false; } - if (++successCounter > 5) + if (++successCounter > 3) { return true; } @@ -163,37 +153,28 @@ public class GuessReader : IReader var amountCells = amountColumn.Value2; object currentAmountCell = productColumn.Rows.Count == 1 ? amountCells : amountCells[row, 1]; - var productCells = productColumn.Value2; - object currentProductCell = productColumn.Rows.Count == 1 ? productCells : productCells[row, 1]; - - double amountValue = 0.0; - - if (currentAmountCell == null || currentProductCell == null) + double? amountValue = currentAmountCell as double?; + if (amountValue == null) { continue; } + var productCells = productColumn.Value2; + object currentProductCell = productColumn.Rows.Count == 1 ? productCells : productCells[row, 1]; + if (ProductSku.TryParse(currentProductCell.ToString(), out IEnumerable skus)) { Product p = new(skus.First()) { Name = "Распознанный артикул" }; - if (currentAmountCell.GetType() == typeof(double)) - { - amountValue = (double)currentAmountCell; - } - else if (currentAmountCell.GetType() == typeof(string)) - { - double.TryParse((string)currentAmountCell, out amountValue); - } if (result.ContainsKey(p)) { - result[p] += amountValue; + result[p] += amountValue.Value; } else { - result.Add(p, amountValue); + result.Add(p, amountValue.Value); } } }