diff --git a/RhSolutions.AddIn/Services/ExcelWriterBase.cs b/RhSolutions.AddIn/Services/ExcelWriterBase.cs index fdd7604..3376563 100644 --- a/RhSolutions.AddIn/Services/ExcelWriterBase.cs +++ b/RhSolutions.AddIn/Services/ExcelWriterBase.cs @@ -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 positionAmount, params int[] columns) + private void WriteOutMissing(KeyValuePair 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 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 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() diff --git a/RhSolutions.AddIn/Services/WriterFactory.cs b/RhSolutions.AddIn/Services/WriterFactory.cs index af2da6d..04ce49b 100644 --- a/RhSolutions.AddIn/Services/WriterFactory.cs +++ b/RhSolutions.AddIn/Services/WriterFactory.cs @@ -18,4 +18,4 @@ public class WriterFactory _ => throw new ArgumentException($"Незвестный интерфейс {nameof(IWriter)}: {writerName}") }; } -} +} \ No newline at end of file