Refactoring Guess Reader

This commit is contained in:
Sergey Chebotar 2023-05-24 06:39:50 +03:00
parent a9aa1f30c5
commit 79eeb2a206

View File

@ -1,4 +1,5 @@
using System.IO; using Microsoft.Office.Interop.Excel;
using System.IO;
namespace RhSolutions.Services; namespace RhSolutions.Services;
@ -56,7 +57,6 @@ public class GuessReader : IReader
if (amountColumnIndex == 0) if (amountColumnIndex == 0)
{ {
amountColumnIndex = amountColumnIndeces amountColumnIndex = amountColumnIndeces
.OrderBy(i => i)
.Where(i => i < productColumnIndex) .Where(i => i < productColumnIndex)
.LastOrDefault(); .LastOrDefault();
} }
@ -121,19 +121,9 @@ public class GuessReader : IReader
continue; continue;
} }
double value = 0.0; double? value = currentCell as double?;
if (currentCell.GetType() == typeof(double)) if (value == null || value == 0)
{
value = (double)currentCell;
}
else if (currentCell.GetType() == typeof(string))
{
double.TryParse((string)currentCell, out value);
}
if (value == 0)
{ {
continue; continue;
} }
@ -143,7 +133,7 @@ public class GuessReader : IReader
return false; return false;
} }
if (++successCounter > 5) if (++successCounter > 3)
{ {
return true; return true;
} }
@ -163,37 +153,28 @@ public class GuessReader : IReader
var amountCells = amountColumn.Value2; var amountCells = amountColumn.Value2;
object currentAmountCell = productColumn.Rows.Count == 1 ? amountCells : amountCells[row, 1]; object currentAmountCell = productColumn.Rows.Count == 1 ? amountCells : amountCells[row, 1];
var productCells = productColumn.Value2; double? amountValue = currentAmountCell as double?;
object currentProductCell = productColumn.Rows.Count == 1 ? productCells : productCells[row, 1]; if (amountValue == null)
double amountValue = 0.0;
if (currentAmountCell == null || currentProductCell == null)
{ {
continue; continue;
} }
var productCells = productColumn.Value2;
object currentProductCell = productColumn.Rows.Count == 1 ? productCells : productCells[row, 1];
if (ProductSku.TryParse(currentProductCell.ToString(), out IEnumerable<ProductSku> skus)) if (ProductSku.TryParse(currentProductCell.ToString(), out IEnumerable<ProductSku> skus))
{ {
Product p = new(skus.First()) Product p = new(skus.First())
{ {
Name = "Распознанный артикул" 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)) if (result.ContainsKey(p))
{ {
result[p] += amountValue; result[p] += amountValue.Value;
} }
else else
{ {
result.Add(p, amountValue); result.Add(p, amountValue.Value);
} }
} }
} }