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