ExcelWriterBase Refactoring

This commit is contained in:
Sergey Chebotar 2023-06-20 08:14:01 +03:00
parent 7b8cab3e9e
commit f89a3eb765
2 changed files with 52 additions and 52 deletions

View File

@ -9,12 +9,14 @@ namespace RhSolutions.Services
protected ResultBar _resultBar;
protected ProgressBar _progressBar;
protected Worksheet _worksheet;
protected Range _amountCell;
protected Range _nameCell;
protected Range _oldSkuCell;
protected Range _programLineCell;
protected Range _skuCell;
protected Worksheet _worksheet;
protected bool _appendValues = true;
public void Dispose()
{
@ -50,7 +52,7 @@ namespace RhSolutions.Services
{
foreach (var kvp in products.First().Item2)
{
FillPositionAmountToColumns(kvp, _amountCell.Column);
FillAmounts(kvp, _amountCell.Column);
_progressBar.Update();
}
FilterByAmount();
@ -71,7 +73,7 @@ namespace RhSolutions.Services
foreach (var kvp in product.Item2)
{
FillPositionAmountToColumns(kvp, _amountCell.Column - 1, _amountCell.Column);
FillAmounts(kvp, _amountCell.Column - 1, _amountCell.Column);
_progressBar.Update();
}
}
@ -81,14 +83,14 @@ namespace RhSolutions.Services
}
}
private void FillMissing(KeyValuePair<Product, double> positionAmount, params int[] columns)
private void WriteOutMissing(KeyValuePair<Product, double> productAmount, params int[] columns)
{
Range worksheetCells = _worksheet.Cells;
Range worksheetRows = _worksheet.Rows;
int skuColumn = _skuCell.Column;
int groupColumn = _programLineCell.Column;
int nameColumn = _nameCell.Column;
Product product = positionAmount.Key;
Product product = productAmount.Key;
int row = worksheetCells[worksheetRows.Count, skuColumn]
.End[XlDirection.xlUp]
@ -115,74 +117,72 @@ namespace RhSolutions.Services
foreach (int column in columns)
{
Range cell = worksheetCells[row, column];
cell.AddValue(positionAmount.Value);
cell.AddValue(productAmount.Value);
}
}
private void FillPositionAmountToColumns(KeyValuePair<Product, double> positionAmount, params int[] columns)
private void FillCells(double amount, int row, int[] columns)
{
Range worksheetCells = _worksheet.Cells;
Range skuColumn = _skuCell.EntireColumn;
foreach (int column in columns)
{
Range cell = _worksheet.Cells[row, column];
if (_appendValues)
{
cell.AddValue(amount);
}
else
{
cell.Value2 = amount;
}
}
}
int? row = GetPositionRow(skuColumn, positionAmount.Key.ProductSku, positionAmount.Key.ProductLines.FirstOrDefault());
private void FillAmounts(KeyValuePair<Product, double> positionAmount, params int[] columns)
{
Range range = _skuCell.EntireColumn;
ProductSku sku = positionAmount.Key.ProductSku;
string productLine = positionAmount.Key.ProductLines.FirstOrDefault();
int? row = GetPositionRow(range, sku, productLine);
if (row != null)
{
foreach (int column in columns)
{
Range cell = worksheetCells[row, column];
cell.AddValue(positionAmount.Value);
}
FillCells(positionAmount.Value, row.Value, columns);
_resultBar.IncrementSuccess();
return;
}
if (_oldSkuCell != null)
else
{
row = GetPositionRow(_oldSkuCell.EntireColumn, positionAmount.Key.ProductSku, positionAmount.Key.ProductLines.FirstOrDefault());
if (_oldSkuCell != null)
{
range = _oldSkuCell.EntireColumn;
row = GetPositionRow(range, sku, productLine);
}
if (row == null)
{
range = _skuCell.EntireColumn;
row = GetPositionRow(range, sku, productLine, true);
}
if (row != null)
{
Range nameCell = worksheetCells[row, _nameCell.Column];
Range nameCell = _worksheet.Cells[row, _nameCell.Column];
if (!Regex.IsMatch(nameCell.Value2, @"арт. \d{11}"))
{
nameCell.AddValue($"(замена арт. {positionAmount.Key.ProductSku})");
}
foreach (int column in columns)
{
Range cell = worksheetCells[row, column];
cell.AddValue(positionAmount.Value);
nameCell.AddValue($"(замена арт. {sku})");
}
FillCells(positionAmount.Value, row.Value, columns);
_resultBar.IncrementReplaced();
return;
}
else
{
WriteOutMissing(positionAmount, columns);
_resultBar.IncrementNotFound();
}
}
row = GetPositionRow(skuColumn, positionAmount.Key.ProductSku, positionAmount.Key.ProductLines.FirstOrDefault(), true);
if (row != null)
{
Range nameCell = worksheetCells[row, _nameCell.Column];
if (!Regex.IsMatch(nameCell.Value2, @"арт. \d{11}"))
{
nameCell.AddValue($"(замена арт. {positionAmount.Key.ProductSku})");
}
foreach (int column in columns)
{
Range cell = worksheetCells[row, column];
cell.AddValue(positionAmount.Value);
}
_resultBar.IncrementReplaced();
return;
}
FillMissing(positionAmount, columns);
_resultBar.IncrementNotFound();
}
private void FilterByAmount()

View File

@ -18,4 +18,4 @@ public class WriterFactory
_ => throw new ArgumentException($"Незвестный интерфейс {nameof(IWriter)}: {writerName}")
};
}
}
}