2023-05-22 10:24:18 +03:00
|
|
|
|
using System.IO;
|
|
|
|
|
|
|
|
|
|
namespace RhSolutions.Services;
|
|
|
|
|
|
2023-05-23 07:07:16 +03:00
|
|
|
|
public class GuessReader : IReader
|
2023-05-22 10:24:18 +03:00
|
|
|
|
{
|
|
|
|
|
private readonly Application _application;
|
|
|
|
|
private ProgressBar _progressBar;
|
|
|
|
|
|
2023-05-23 07:07:16 +03:00
|
|
|
|
public GuessReader(Application application)
|
2023-05-22 10:24:18 +03:00
|
|
|
|
{
|
|
|
|
|
_application = application;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public Dictionary<Product, double> ReadProducts(Range range)
|
|
|
|
|
{
|
|
|
|
|
_progressBar = new("Ищу колонки со значениями", range.Columns.Count);
|
|
|
|
|
int? productColumnIndex = null;
|
|
|
|
|
List<(int, double)> amountColumnIndeces = new();
|
|
|
|
|
|
|
|
|
|
for (int column = 1; column < range.Columns.Count + 1; column++)
|
|
|
|
|
{
|
|
|
|
|
_progressBar.Update();
|
|
|
|
|
if (productColumnIndex == null && IsProductColumn(range.Columns[column]))
|
|
|
|
|
{
|
|
|
|
|
productColumnIndex = column;
|
|
|
|
|
}
|
|
|
|
|
else if (IsAmountColumn(range.Columns[column], out double average))
|
|
|
|
|
{
|
|
|
|
|
amountColumnIndeces.Add((column, average));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (productColumnIndex == null || amountColumnIndeces.Count == 0)
|
|
|
|
|
{
|
|
|
|
|
throw new ArgumentException("Колонка с артикулом или количеством не определена");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
else if (amountColumnIndeces.Count == 1)
|
|
|
|
|
{
|
|
|
|
|
return GetDictionaryFromColumns(range.Columns[productColumnIndex],
|
|
|
|
|
range.Columns[amountColumnIndeces.First().Item1]);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
int amountColumnIndex = amountColumnIndeces
|
|
|
|
|
.Where(i => i.Item2 < 30000 && i.Item2 > 0)
|
|
|
|
|
.OrderByDescending(i => i.Item2).First().Item1;
|
|
|
|
|
return GetDictionaryFromColumns(range.Columns[productColumnIndex],
|
|
|
|
|
range.Columns[amountColumnIndex]);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private bool IsProductColumn(Range column)
|
|
|
|
|
{
|
|
|
|
|
int successCounter = 0;
|
2023-05-23 07:33:49 +03:00
|
|
|
|
object[,] cells = column.Value2;
|
|
|
|
|
|
2023-05-23 07:55:14 +03:00
|
|
|
|
if (cells == null)
|
|
|
|
|
{
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2023-05-22 10:24:18 +03:00
|
|
|
|
for (int row = 1; row < column.Rows.Count + 1; row++)
|
|
|
|
|
{
|
2023-05-23 07:33:49 +03:00
|
|
|
|
object currentCell = cells[row, 1];
|
|
|
|
|
if (currentCell == null)
|
|
|
|
|
{
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (ProductSku.TryParse(currentCell.ToString(), out IEnumerable<ProductSku> skus))
|
2023-05-22 10:24:18 +03:00
|
|
|
|
{
|
|
|
|
|
successCounter++;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (successCounter > 2)
|
|
|
|
|
{
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return successCounter > 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private bool IsAmountColumn(Range column, out double average)
|
|
|
|
|
{
|
2023-05-23 07:55:14 +03:00
|
|
|
|
average = 0.0;
|
2023-05-22 10:24:18 +03:00
|
|
|
|
object[,] cells = column.Value2;
|
2023-05-23 07:55:14 +03:00
|
|
|
|
|
|
|
|
|
if (cells == null)
|
|
|
|
|
{
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2023-05-22 10:24:18 +03:00
|
|
|
|
int successCounter = 0;
|
|
|
|
|
|
|
|
|
|
for (int row = 1; row < column.Rows.Count + 1; row++)
|
|
|
|
|
{
|
|
|
|
|
object currentCell = cells[row, 1];
|
|
|
|
|
if (currentCell == null)
|
|
|
|
|
{
|
|
|
|
|
continue;
|
|
|
|
|
}
|
2023-05-23 07:33:49 +03:00
|
|
|
|
|
2023-05-22 10:24:18 +03:00
|
|
|
|
double value = 0.0;
|
|
|
|
|
|
|
|
|
|
if (currentCell.GetType() == typeof(double))
|
|
|
|
|
{
|
|
|
|
|
value = (double)currentCell;
|
|
|
|
|
}
|
|
|
|
|
else if (currentCell.GetType() == typeof(string))
|
|
|
|
|
{
|
|
|
|
|
double.TryParse((string)currentCell, out value);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (value != 0.0)
|
|
|
|
|
{
|
|
|
|
|
successCounter++;
|
|
|
|
|
average += value;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if (successCounter > 0)
|
|
|
|
|
{
|
|
|
|
|
average /= successCounter;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return successCounter > 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private Dictionary<Product, double> GetDictionaryFromColumns(Range productColumn, Range amountColumn)
|
|
|
|
|
{
|
|
|
|
|
Dictionary<Product, double> result = new();
|
|
|
|
|
|
|
|
|
|
for (int row = 1; row < productColumn.Rows.Count + 1; row++)
|
|
|
|
|
{
|
2023-05-23 07:33:49 +03:00
|
|
|
|
object[,] amountCells = amountColumn.Value2;
|
|
|
|
|
object currentAmountCell = amountCells[row, 1];
|
|
|
|
|
|
|
|
|
|
object[,] productCells = productColumn.Value2;
|
|
|
|
|
object currentProductCell = productCells[row, 1];
|
|
|
|
|
|
2023-05-22 10:24:18 +03:00
|
|
|
|
double amountValue = 0.0;
|
2023-05-23 07:33:49 +03:00
|
|
|
|
|
|
|
|
|
if (currentAmountCell == null || currentProductCell == null)
|
|
|
|
|
{
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (ProductSku.TryParse(currentProductCell.ToString(), out IEnumerable<ProductSku> skus))
|
2023-05-22 10:24:18 +03:00
|
|
|
|
{
|
|
|
|
|
Product p = new(skus.First())
|
|
|
|
|
{
|
|
|
|
|
Name = "Распознанный артикул"
|
|
|
|
|
};
|
2023-05-23 07:33:49 +03:00
|
|
|
|
if (currentAmountCell.GetType() == typeof(double))
|
2023-05-22 10:24:18 +03:00
|
|
|
|
{
|
2023-05-23 07:33:49 +03:00
|
|
|
|
amountValue = (double)currentAmountCell;
|
2023-05-22 10:24:18 +03:00
|
|
|
|
}
|
2023-05-23 07:33:49 +03:00
|
|
|
|
else if (currentAmountCell.GetType() == typeof(string))
|
2023-05-22 10:24:18 +03:00
|
|
|
|
{
|
2023-05-23 07:33:49 +03:00
|
|
|
|
double.TryParse((string)currentAmountCell, out amountValue);
|
2023-05-22 10:24:18 +03:00
|
|
|
|
}
|
|
|
|
|
if (result.ContainsKey(p))
|
|
|
|
|
{
|
|
|
|
|
result[p] += amountValue;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
result.Add(p, amountValue);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public List<(string, Dictionary<Product, double>)> ReadProducts(IEnumerable<Worksheet> worksheets)
|
|
|
|
|
{
|
|
|
|
|
List<(string, Dictionary<Product, double>)> result = new();
|
|
|
|
|
foreach (Worksheet worksheet in worksheets)
|
|
|
|
|
{
|
|
|
|
|
string wbName = Path.GetFileNameWithoutExtension(
|
|
|
|
|
worksheet.Parent.Name);
|
|
|
|
|
Range range = worksheet.UsedRange;
|
|
|
|
|
var productsDict = ReadProducts(range);
|
|
|
|
|
result.Add((wbName, productsDict));
|
|
|
|
|
}
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public List<(string, Dictionary<Product, double>)> ReadProducts(string[] files)
|
|
|
|
|
{
|
|
|
|
|
_progressBar = new("Открываю исходные файлы...", files.Length);
|
|
|
|
|
List<Worksheet> worksheets = new();
|
|
|
|
|
|
|
|
|
|
_application.ScreenUpdating = false;
|
|
|
|
|
foreach (string file in files)
|
|
|
|
|
{
|
|
|
|
|
Workbook wb = _application.Workbooks.Open(file);
|
|
|
|
|
worksheets.Add(wb.ActiveSheet);
|
|
|
|
|
_progressBar.Update();
|
|
|
|
|
}
|
|
|
|
|
_application.ScreenUpdating = true;
|
|
|
|
|
var result = ReadProducts(worksheets);
|
|
|
|
|
foreach (var ws in worksheets)
|
|
|
|
|
{
|
|
|
|
|
ws.Parent.Close();
|
|
|
|
|
}
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void Dispose()
|
|
|
|
|
{
|
|
|
|
|
_progressBar?.Dispose();
|
|
|
|
|
}
|
|
|
|
|
}
|