190 lines
5.7 KiB
C#
190 lines
5.7 KiB
C#
using System.IO;
|
||
|
||
namespace RhSolutions.Services;
|
||
|
||
public class MagicReader : IReader
|
||
{
|
||
private readonly Application _application;
|
||
private ProgressBar _progressBar;
|
||
|
||
public MagicReader(Application application)
|
||
{
|
||
_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;
|
||
for (int row = 1; row < column.Rows.Count + 1; row++)
|
||
{
|
||
if (ProductSku.TryParse(column.Cells[row, 1].Value2.ToString(), out IEnumerable<ProductSku> skus))
|
||
{
|
||
successCounter++;
|
||
}
|
||
|
||
if (successCounter > 2)
|
||
{
|
||
return true;
|
||
}
|
||
}
|
||
|
||
return successCounter > 0;
|
||
}
|
||
|
||
private bool IsAmountColumn(Range column, out double average)
|
||
{
|
||
object[,] cells = column.Value2;
|
||
int successCounter = 0;
|
||
average = 0.0;
|
||
|
||
for (int row = 1; row < column.Rows.Count + 1; row++)
|
||
{
|
||
object currentCell = cells[row, 1];
|
||
if (currentCell == null)
|
||
{
|
||
continue;
|
||
}
|
||
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++)
|
||
{
|
||
object[,] cells = amountColumn.Value2;
|
||
object currentCell = cells[row, 1];
|
||
double amountValue = 0.0;
|
||
if (ProductSku.TryParse(productColumn.Cells[row, 1].Value2.ToString(), out IEnumerable<ProductSku> skus) &&
|
||
currentCell != null)
|
||
{
|
||
Product p = new(skus.First())
|
||
{
|
||
Name = "Распознанный артикул"
|
||
};
|
||
|
||
if (currentCell.GetType() == typeof(double))
|
||
{
|
||
amountValue = (double)currentCell;
|
||
}
|
||
else if (currentCell.GetType() == typeof(string))
|
||
{
|
||
double.TryParse((string)currentCell, out amountValue);
|
||
}
|
||
|
||
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();
|
||
}
|
||
} |