Edit amount column looking up

This commit is contained in:
Sergey Chebotar 2023-05-23 09:26:08 +03:00
parent 9d1c213979
commit 7fd139a234

View File

@ -16,7 +16,7 @@ public class GuessReader : IReader
{ {
_progressBar = new("Ищу колонки со значениями", range.Columns.Count); _progressBar = new("Ищу колонки со значениями", range.Columns.Count);
int? productColumnIndex = null; int? productColumnIndex = null;
List<(int, double)> amountColumnIndeces = new(); List<int> amountColumnIndeces = new();
for (int column = 1; column < range.Columns.Count + 1; column++) for (int column = 1; column < range.Columns.Count + 1; column++)
{ {
@ -25,28 +25,47 @@ public class GuessReader : IReader
{ {
productColumnIndex = column; productColumnIndex = column;
} }
else if (IsAmountColumn(range.Columns[column], out double average)) else if (IsAmountColumn(range.Columns[column]))
{ {
amountColumnIndeces.Add((column, average)); amountColumnIndeces.Add(column);
} }
} }
if (productColumnIndex == null || amountColumnIndeces.Count == 0) if (productColumnIndex == null)
{ {
throw new ArgumentException("Колонка с артикулом или количеством не определена"); throw new ArgumentException("Колонка с артикулом не определена");
}
if (amountColumnIndeces.Count == 0)
{
throw new ArgumentException("Колонка с количеством не определена");
} }
else if (amountColumnIndeces.Count == 1) else if (amountColumnIndeces.Count == 1)
{ {
return GetDictionaryFromColumns(range.Columns[productColumnIndex], return GetDictionaryFromColumns(range.Columns[productColumnIndex],
range.Columns[amountColumnIndeces.First().Item1]); range.Columns[amountColumnIndeces.First()]);
} }
else else
{ {
int amountColumnIndex = amountColumnIndeces int amountColumnIndex = amountColumnIndeces
.Where(i => i.Item2 < 30000 && i.Item2 > 0) .Where(i => i > productColumnIndex)
.OrderByDescending(i => i.Item2).First().Item1; .FirstOrDefault();
if (amountColumnIndex == 0)
{
amountColumnIndex = amountColumnIndeces
.OrderBy(i => i)
.Where(i => i < productColumnIndex)
.LastOrDefault();
}
if (amountColumnIndex == 0)
{
throw new ArgumentException("Колонка с количеством не определена");
}
return GetDictionaryFromColumns(range.Columns[productColumnIndex], return GetDictionaryFromColumns(range.Columns[productColumnIndex],
range.Columns[amountColumnIndex]); range.Columns[amountColumnIndex]);
} }
@ -84,9 +103,9 @@ public class GuessReader : IReader
return successCounter > 0; return successCounter > 0;
} }
private bool IsAmountColumn(Range column, out double average) private bool IsAmountColumn(Range column)
{ {
average = 0.0; int successCounter = 0;
object[,] cells = column.Value2; object[,] cells = column.Value2;
if (cells == null) if (cells == null)
@ -94,8 +113,6 @@ public class GuessReader : IReader
return false; return false;
} }
int successCounter = 0;
for (int row = 1; row < column.Rows.Count + 1; row++) for (int row = 1; row < column.Rows.Count + 1; row++)
{ {
object currentCell = cells[row, 1]; object currentCell = cells[row, 1];
@ -110,23 +127,26 @@ public class GuessReader : IReader
{ {
value = (double)currentCell; value = (double)currentCell;
} }
else if (currentCell.GetType() == typeof(string)) else if (currentCell.GetType() == typeof(string))
{ {
double.TryParse((string)currentCell, out value); double.TryParse((string)currentCell, out value);
} }
if (value != 0.0) if (value == 0)
{ {
successCounter++; continue;
average += value;
} }
}
if (successCounter > 0) if (value > 30000)
{ {
average /= successCounter; return false;
}
successCounter++;
} }
return successCounter > 0; return successCounter > 1;
} }
private Dictionary<Product, double> GetDictionaryFromColumns(Range productColumn, Range amountColumn) private Dictionary<Product, double> GetDictionaryFromColumns(Range productColumn, Range amountColumn)