RhSolutions-AddIn/RhSolutions.AddIn/Services/GuessReader.cs

242 lines
7.0 KiB
C#
Raw Normal View History

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)
{
2023-05-23 15:02:14 +03:00
_progressBar = new("Поиск возможных пар артикул-количество...", range.Columns.Count);
2023-05-22 10:24:18 +03:00
int? productColumnIndex = null;
2023-05-23 09:26:08 +03:00
List<int> amountColumnIndeces = new();
2023-05-22 10:24:18 +03:00
for (int column = 1; column < range.Columns.Count + 1; column++)
{
_progressBar.Update();
if (productColumnIndex == null && IsProductColumn(range.Columns[column]))
{
productColumnIndex = column;
}
2023-05-23 09:26:08 +03:00
else if (IsAmountColumn(range.Columns[column]))
2023-05-22 10:24:18 +03:00
{
2023-05-23 09:26:08 +03:00
amountColumnIndeces.Add(column);
2023-05-22 10:24:18 +03:00
}
}
2023-05-23 09:26:08 +03:00
if (productColumnIndex == null)
2023-05-22 10:24:18 +03:00
{
2023-05-23 09:26:08 +03:00
throw new ArgumentException("Колонка с артикулом не определена");
}
if (amountColumnIndeces.Count == 0)
{
throw new ArgumentException("Колонка с количеством не определена");
2023-05-22 10:24:18 +03:00
}
else if (amountColumnIndeces.Count == 1)
{
return GetDictionaryFromColumns(range.Columns[productColumnIndex],
2023-05-23 09:26:08 +03:00
range.Columns[amountColumnIndeces.First()]);
2023-05-22 10:24:18 +03:00
}
else
{
int amountColumnIndex = amountColumnIndeces
2023-05-23 09:26:08 +03:00
.Where(i => i > productColumnIndex)
.FirstOrDefault();
if (amountColumnIndex == 0)
{
amountColumnIndex = amountColumnIndeces
.OrderBy(i => i)
.Where(i => i < productColumnIndex)
.LastOrDefault();
}
if (amountColumnIndex == 0)
{
throw new ArgumentException("Колонка с количеством не определена");
}
2023-05-22 10:24:18 +03:00
return GetDictionaryFromColumns(range.Columns[productColumnIndex],
range.Columns[amountColumnIndex]);
}
}
private bool IsProductColumn(Range column)
{
int successCounter = 0;
2023-05-23 15:02:14 +03:00
var cells = column.Value2;
2023-05-23 07:33:49 +03:00
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 15:02:14 +03:00
{
object currentCell = column.Rows.Count == 1 ? cells : cells[row, 1];
2023-05-23 07:33:49 +03:00
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;
}
2023-05-23 09:26:08 +03:00
private bool IsAmountColumn(Range column)
2023-05-22 10:24:18 +03:00
{
2023-05-23 09:26:08 +03:00
int successCounter = 0;
2023-05-23 15:02:14 +03:00
var 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 15:02:14 +03:00
object currentCell = column.Rows.Count == 1 ? cells : cells[row, 1];
2023-05-22 10:24:18 +03:00
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;
}
2023-05-23 09:26:08 +03:00
2023-05-22 10:24:18 +03:00
else if (currentCell.GetType() == typeof(string))
{
double.TryParse((string)currentCell, out value);
}
2023-05-23 09:26:08 +03:00
if (value == 0)
2023-05-22 10:24:18 +03:00
{
2023-05-23 09:26:08 +03:00
continue;
2023-05-22 10:24:18 +03:00
}
2023-05-23 09:26:08 +03:00
if (value > 30000)
{
return false;
}
2023-05-23 15:02:56 +03:00
if (++successCounter > 5)
2023-05-23 15:02:14 +03:00
{
return true;
}
2023-05-22 10:24:18 +03:00
}
2023-05-23 15:02:14 +03:00
return successCounter > 0;
2023-05-22 10:24:18 +03:00
}
private Dictionary<Product, double> GetDictionaryFromColumns(Range productColumn, Range amountColumn)
{
Dictionary<Product, double> result = new();
2023-05-23 15:02:14 +03:00
_progressBar = new("Заполняю словарь значений...", productColumn.Rows.Count);
2023-05-22 10:24:18 +03:00
for (int row = 1; row < productColumn.Rows.Count + 1; row++)
{
2023-05-23 15:02:14 +03:00
_progressBar.Update();
var amountCells = amountColumn.Value2;
object currentAmountCell = productColumn.Rows.Count == 1 ? amountCells : amountCells[row, 1];
2023-05-23 07:33:49 +03:00
2023-05-23 15:02:14 +03:00
var productCells = productColumn.Value2;
object currentProductCell = productColumn.Rows.Count == 1 ? productCells : productCells[row, 1];
2023-05-23 07:33:49 +03:00
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();
}
}