2023-04-06 08:29:39 +03:00
|
|
|
|
using System.IO;
|
|
|
|
|
#if !NET472
|
|
|
|
|
using System.Runtime.Versioning;
|
|
|
|
|
using RhSolutions.Tools;
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
namespace RhSolutions.Services;
|
|
|
|
|
|
|
|
|
|
#if !NET472
|
|
|
|
|
[SupportedOSPlatform("windows")]
|
|
|
|
|
#endif
|
|
|
|
|
public class RhExcelReader : IExcelReader, IDisposable
|
|
|
|
|
{
|
|
|
|
|
private ProgressBar _progressBar;
|
|
|
|
|
private readonly Dictionary<string, string> headers;
|
|
|
|
|
private readonly Application _application;
|
|
|
|
|
|
|
|
|
|
public RhExcelReader(Application application, IAddInConfiguration configuration)
|
|
|
|
|
{
|
|
|
|
|
_application = application;
|
|
|
|
|
headers = configuration.GetPriceListHeaders();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public Dictionary<Product, double> ReadProducts(Range range)
|
|
|
|
|
{
|
|
|
|
|
object[,] cells = range.Value2;
|
|
|
|
|
Dictionary<Product, double> readResult = new();
|
|
|
|
|
|
|
|
|
|
for (int row = 1; row <= range.Rows.Count; row++)
|
|
|
|
|
{
|
|
|
|
|
if (cells[row, 1] == null || cells[row, 2] == null)
|
|
|
|
|
continue;
|
|
|
|
|
|
|
|
|
|
string currentSku = null;
|
|
|
|
|
double? currentAmount = null;
|
|
|
|
|
|
|
|
|
|
for (int column = 1; column <= 2; column++)
|
|
|
|
|
{
|
|
|
|
|
object currentCell = cells[row, column];
|
|
|
|
|
|
|
|
|
|
if (Sku.TryParse(currentCell.ToString(), out var validSku))
|
|
|
|
|
{
|
|
|
|
|
currentSku = validSku.FirstOrDefault().ToString() ?? null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
else if (currentCell.GetType() == typeof(string)
|
|
|
|
|
&& double.TryParse(currentCell.ToString(), out _))
|
|
|
|
|
{
|
|
|
|
|
currentAmount = double.Parse((string)currentCell);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
else if (currentCell.GetType() == typeof(double))
|
|
|
|
|
{
|
|
|
|
|
currentAmount = (double)currentCell;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (currentSku == null || currentAmount == null)
|
|
|
|
|
{
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Product product = new() { ProductSku = currentSku };
|
|
|
|
|
|
|
|
|
|
if (readResult.ContainsKey(product))
|
|
|
|
|
{
|
|
|
|
|
readResult[product] += currentAmount.Value;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
readResult.Add(product, currentAmount.Value);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return readResult;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public List<(string, Dictionary<Product, double>)>
|
|
|
|
|
ReadProducts(IEnumerable<Worksheet> worksheets)
|
|
|
|
|
{
|
|
|
|
|
List<(string, Dictionary<Product, double>)> result = new();
|
|
|
|
|
foreach (Worksheet worksheet in worksheets)
|
|
|
|
|
{
|
2023-04-06 08:49:12 +03:00
|
|
|
|
if (!worksheet.IsValidSource())
|
2023-04-06 08:29:39 +03:00
|
|
|
|
{
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
string wbName = Path.GetFileNameWithoutExtension(
|
|
|
|
|
worksheet.Parent.Name);
|
|
|
|
|
|
|
|
|
|
Range AmountCell = worksheet.Cells.Find(headers["Amount"]),
|
|
|
|
|
SkuCell = worksheet.Cells.Find(headers["Sku"]),
|
|
|
|
|
ProductLineCelll = worksheet.Cells.Find(headers["ProductLine"]),
|
|
|
|
|
NameCell = worksheet.Cells.Find(headers["Name"]);
|
|
|
|
|
var lastRowIndex = worksheet.Cells[worksheet.Rows.Count, AmountCell.Column]
|
|
|
|
|
.End[XlDirection.xlUp].Row;
|
|
|
|
|
|
|
|
|
|
Dictionary<Product, double> readResult = new();
|
|
|
|
|
|
|
|
|
|
for (int row = AmountCell.Row + 1; row <= lastRowIndex; row++)
|
|
|
|
|
{
|
|
|
|
|
double? amount = worksheet.Cells[row, AmountCell.Column].Value2 as double?;
|
|
|
|
|
|
|
|
|
|
if (amount != null && amount.Value != 0)
|
|
|
|
|
{
|
|
|
|
|
object programLine = worksheet.Cells[row, ProductLineCelll.Column].Value2;
|
|
|
|
|
object name = worksheet.Cells[row, NameCell.Column].Value2;
|
|
|
|
|
object sku = worksheet.Cells[row, SkuCell.Column].Value2;
|
|
|
|
|
|
|
|
|
|
if (programLine == null || name == null || sku == null)
|
|
|
|
|
continue;
|
|
|
|
|
|
|
|
|
|
if (!Sku.TryParse(sku.ToString(), out _))
|
|
|
|
|
continue;
|
|
|
|
|
|
|
|
|
|
Product p = new()
|
|
|
|
|
{
|
|
|
|
|
ProductSku = sku.ToString(),
|
|
|
|
|
ProductLine = programLine.ToString(),
|
|
|
|
|
Name = name.ToString()
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
if (readResult.ContainsKey(p))
|
|
|
|
|
{
|
|
|
|
|
readResult[p] += amount.Value;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
readResult.Add(p, amount.Value);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
result.Add((wbName, readResult));
|
|
|
|
|
}
|
|
|
|
|
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()
|
|
|
|
|
{
|
2023-04-07 07:25:03 +03:00
|
|
|
|
_progressBar?.Dispose();
|
2023-04-06 08:29:39 +03:00
|
|
|
|
}
|
|
|
|
|
}
|