From 36c4d4480d3789aface2e81371c9dd5ee84e874d Mon Sep 17 00:00:00 2001 From: Sergey Chebotar Date: Tue, 20 Dec 2022 11:53:55 +0300 Subject: Rename Abstract classes --- src/PriceListTools/AbstractPriceList.cs | 15 --- src/PriceListTools/AbstractTool.cs | 189 -------------------------------- src/PriceListTools/CombineTool.cs | 2 +- src/PriceListTools/ConvertTool.cs | 2 +- src/PriceListTools/ExportTool.cs | 2 +- src/PriceListTools/MergeTool.cs | 2 +- src/PriceListTools/PriceListBase.cs | 15 +++ src/PriceListTools/SourcePriceList.cs | 2 +- src/PriceListTools/TargetPriceList.cs | 2 +- src/PriceListTools/ToolBase.cs | 189 ++++++++++++++++++++++++++++++++ 10 files changed, 210 insertions(+), 210 deletions(-) delete mode 100644 src/PriceListTools/AbstractPriceList.cs delete mode 100644 src/PriceListTools/AbstractTool.cs create mode 100644 src/PriceListTools/PriceListBase.cs create mode 100644 src/PriceListTools/ToolBase.cs (limited to 'src/PriceListTools') diff --git a/src/PriceListTools/AbstractPriceList.cs b/src/PriceListTools/AbstractPriceList.cs deleted file mode 100644 index 2274a7f..0000000 --- a/src/PriceListTools/AbstractPriceList.cs +++ /dev/null @@ -1,15 +0,0 @@ -using Microsoft.Office.Interop.Excel; - -namespace RhSolutions.PriceListTools -{ - internal abstract class AbstractPriceList - { - public Range AmountCell { get; protected set; } - public Range SkuCell { get; protected set; } - public Range GroupCell { get; protected set; } - public Range NameCell { get; protected set; } - - public Worksheet Sheet { get; protected set; } - public string Name { get; protected set; } - } -} \ No newline at end of file diff --git a/src/PriceListTools/AbstractTool.cs b/src/PriceListTools/AbstractTool.cs deleted file mode 100644 index 3ea907b..0000000 --- a/src/PriceListTools/AbstractTool.cs +++ /dev/null @@ -1,189 +0,0 @@ -using Microsoft.Office.Interop.Excel; -using RhSolutions.Interface; -using System; -using System.Collections.Generic; -using System.Linq; -using ProgressBar = RhSolutions.Interface.ProgressBar; - -namespace RhSolutions.PriceListTools -{ - internal abstract class AbstractTool - { - protected Application ExcelApp = AddIn.Excel; - protected TargetPriceList TargetFile { get; set; } - protected ResultBar ResultBar { get; set; } - protected ProgressBar ProgressBar { get; set; } - - public abstract void FillTarget(); - - public void OpenNewPrice() - { - if (ExcelApp.Workbooks - .Cast() - .FirstOrDefault(w => w.FullName == RegistryUtil.PriceListPath) != null) - { - throw new ArgumentException("Шаблонный файл редактируется в другом месте"); - } - - Workbook wb = ExcelApp.Workbooks.Open(RegistryUtil.PriceListPath, null, true); - - try - { - TargetFile = new TargetPriceList(wb); - } - - catch (Exception exception) - { - if (wb != null) - { - wb.Close(); - } - - throw exception; - } - } - - protected void FillPositionAmountToColumns(KeyValuePair positionAmount, params int[] columns) - { - Range worksheetCells = TargetFile.Sheet.Cells; - Range skuColumn = TargetFile.SkuCell.EntireColumn; - Range oldSkuColumn = TargetFile.OldSkuCell.EntireColumn; - - int? row = GetPositionRow(skuColumn, positionAmount.Key.ProductSku, positionAmount.Key.ProductLine); - - if (row != null) - { - foreach (int column in columns) - { - Range cell = worksheetCells[row, column]; - cell.AddValue(positionAmount.Value); - } - - ResultBar.IncrementSuccess(); - return; - } - - if (TargetFile.OldSkuCell != null) - { - row = GetPositionRow(oldSkuColumn, positionAmount.Key.ProductSku, positionAmount.Key.ProductLine); - - if (row != null) - { - foreach (int column in columns) - { - Range cell = worksheetCells[row, column]; - cell.AddValue(positionAmount.Value); - } - - ResultBar.IncrementReplaced(); - return; - } - } - - string sku = positionAmount.Key.ProductSku.Substring(1, 6); - row = GetPositionRow(skuColumn, sku, positionAmount.Key.ProductLine); - - if (row != null) - { - foreach (int column in columns) - { - Range cell = worksheetCells[row, column]; - cell.AddValue(positionAmount.Value); - } - - ResultBar.IncrementReplaced(); - return; - } - - FillMissing(positionAmount, columns); - ResultBar.IncrementNotFound(); - } - - protected void FillMissing(KeyValuePair positionAmount, params int[] columns) - { - Range worksheetCells = TargetFile.Sheet.Cells; - Range worksheetRows = TargetFile.Sheet.Rows; - int skuColumn = TargetFile.SkuCell.Column; - int groupColumn = TargetFile.GroupCell.Column; - int nameColumn = TargetFile.NameCell.Column; - - int row = worksheetCells[worksheetRows.Count, skuColumn] - .End[XlDirection.xlUp] - .Row + 1; - - worksheetRows[row] - .EntireRow - .Insert(XlInsertShiftDirection.xlShiftDown, XlInsertFormatOrigin.xlFormatFromLeftOrAbove); - - Range previous = worksheetRows[row - 1]; - Range current = worksheetRows[row]; - - previous.Copy(current); - current.ClearContents(); - - worksheetCells[row, groupColumn].Value2 = positionAmount.Key.ProductLine; - worksheetCells[row, nameColumn].Value2 = positionAmount.Key.Name; - - if (TargetFile.OldSkuCell != null) - { - worksheetCells[row, skuColumn].Value2 = "Не найден"; - worksheetCells[row, TargetFile.OldSkuCell.Column].Value2 = positionAmount.Key.ProductSku; - } - - else - { - worksheetCells[row, skuColumn].Value2 = positionAmount.Key.ProductSku; - } - - foreach (int column in columns) - { - Range cell = worksheetCells[row, column]; - cell.AddValue(positionAmount.Value); - } - } - - protected int? GetPositionRow(Range range, string sku, string group) - { - Range found = range.Find(sku); - string foundGroupValue; - - if (found == null) - { - return null; - } - - int firstFoundRow = found.Row; - - if (string.IsNullOrEmpty(group)) - { - return found.Row; - } - - while (true) - { - foundGroupValue = TargetFile.Sheet.Cells[found.Row, TargetFile.GroupCell.Column].Value2.ToString(); - - if (group.Equals(foundGroupValue)) - { - return found.Row; - } - - found = range.FindNext(found); - - if (found.Row == firstFoundRow) - { - return null; - } - } - } - - protected void FilterByAmount() - { - AutoFilter filter = TargetFile.Sheet.AutoFilter; - int startColumn = filter.Range.Column; - - filter.Range.AutoFilter(TargetFile.AmountCell.Column - startColumn + 1, "<>"); - TargetFile.Sheet.Range["A1"].Activate(); - } - } -} \ No newline at end of file diff --git a/src/PriceListTools/CombineTool.cs b/src/PriceListTools/CombineTool.cs index 6959ac2..e0429bd 100644 --- a/src/PriceListTools/CombineTool.cs +++ b/src/PriceListTools/CombineTool.cs @@ -7,7 +7,7 @@ using Dialog = RhSolutions.Interface.Dialog; namespace RhSolutions.PriceListTools { - internal class CombineTool : AbstractTool + internal class CombineTool : ToolBase { private List SourceFiles { get; set; } diff --git a/src/PriceListTools/ConvertTool.cs b/src/PriceListTools/ConvertTool.cs index 57895b3..788d6ff 100644 --- a/src/PriceListTools/ConvertTool.cs +++ b/src/PriceListTools/ConvertTool.cs @@ -2,7 +2,7 @@ namespace RhSolutions.PriceListTools { - internal class ConvertTool : AbstractTool + internal class ConvertTool : ToolBase { private SourcePriceList Current { get; set; } diff --git a/src/PriceListTools/ExportTool.cs b/src/PriceListTools/ExportTool.cs index 4956faf..add072d 100644 --- a/src/PriceListTools/ExportTool.cs +++ b/src/PriceListTools/ExportTool.cs @@ -5,7 +5,7 @@ using RhSolutions.Interface; namespace RhSolutions.PriceListTools { - internal class ExportTool : AbstractTool + internal class ExportTool : ToolBase { private Dictionary PositionAmount; private readonly Range Selection; diff --git a/src/PriceListTools/MergeTool.cs b/src/PriceListTools/MergeTool.cs index b9673e7..3a945dd 100644 --- a/src/PriceListTools/MergeTool.cs +++ b/src/PriceListTools/MergeTool.cs @@ -5,7 +5,7 @@ using System.Linq; namespace RhSolutions.PriceListTools { - internal class MergeTool : AbstractTool + internal class MergeTool : ToolBase { private List SourceFiles { get; set; } diff --git a/src/PriceListTools/PriceListBase.cs b/src/PriceListTools/PriceListBase.cs new file mode 100644 index 0000000..805cd04 --- /dev/null +++ b/src/PriceListTools/PriceListBase.cs @@ -0,0 +1,15 @@ +using Microsoft.Office.Interop.Excel; + +namespace RhSolutions.PriceListTools +{ + internal abstract class PriceListBase + { + public Range AmountCell { get; protected set; } + public Range SkuCell { get; protected set; } + public Range GroupCell { get; protected set; } + public Range NameCell { get; protected set; } + + public Worksheet Sheet { get; protected set; } + public string Name { get; protected set; } + } +} \ No newline at end of file diff --git a/src/PriceListTools/SourcePriceList.cs b/src/PriceListTools/SourcePriceList.cs index 7c0001e..4e43d7a 100644 --- a/src/PriceListTools/SourcePriceList.cs +++ b/src/PriceListTools/SourcePriceList.cs @@ -7,7 +7,7 @@ using RhSolutions.Interface; namespace RhSolutions.PriceListTools { - internal class SourcePriceList : AbstractPriceList + internal class SourcePriceList : PriceListBase { public Dictionary PositionAmount { get; private set; } diff --git a/src/PriceListTools/TargetPriceList.cs b/src/PriceListTools/TargetPriceList.cs index da540ae..e360062 100644 --- a/src/PriceListTools/TargetPriceList.cs +++ b/src/PriceListTools/TargetPriceList.cs @@ -4,7 +4,7 @@ using System.Linq; namespace RhSolutions.PriceListTools { - internal class TargetPriceList : AbstractPriceList + internal class TargetPriceList : PriceListBase { public Range OldSkuCell { get; private set; } diff --git a/src/PriceListTools/ToolBase.cs b/src/PriceListTools/ToolBase.cs new file mode 100644 index 0000000..d64522d --- /dev/null +++ b/src/PriceListTools/ToolBase.cs @@ -0,0 +1,189 @@ +using Microsoft.Office.Interop.Excel; +using RhSolutions.Interface; +using System; +using System.Collections.Generic; +using System.Linq; +using ProgressBar = RhSolutions.Interface.ProgressBar; + +namespace RhSolutions.PriceListTools +{ + internal abstract class ToolBase + { + protected Application ExcelApp = AddIn.Excel; + protected TargetPriceList TargetFile { get; set; } + protected ResultBar ResultBar { get; set; } + protected ProgressBar ProgressBar { get; set; } + + public abstract void FillTarget(); + + public void OpenNewPrice() + { + if (ExcelApp.Workbooks + .Cast() + .FirstOrDefault(w => w.FullName == RegistryUtil.PriceListPath) != null) + { + throw new ArgumentException("Шаблонный файл редактируется в другом месте"); + } + + Workbook wb = ExcelApp.Workbooks.Open(RegistryUtil.PriceListPath, null, true); + + try + { + TargetFile = new TargetPriceList(wb); + } + + catch (Exception exception) + { + if (wb != null) + { + wb.Close(); + } + + throw exception; + } + } + + protected void FillPositionAmountToColumns(KeyValuePair positionAmount, params int[] columns) + { + Range worksheetCells = TargetFile.Sheet.Cells; + Range skuColumn = TargetFile.SkuCell.EntireColumn; + Range oldSkuColumn = TargetFile.OldSkuCell.EntireColumn; + + int? row = GetPositionRow(skuColumn, positionAmount.Key.ProductSku, positionAmount.Key.ProductLine); + + if (row != null) + { + foreach (int column in columns) + { + Range cell = worksheetCells[row, column]; + cell.AddValue(positionAmount.Value); + } + + ResultBar.IncrementSuccess(); + return; + } + + if (TargetFile.OldSkuCell != null) + { + row = GetPositionRow(oldSkuColumn, positionAmount.Key.ProductSku, positionAmount.Key.ProductLine); + + if (row != null) + { + foreach (int column in columns) + { + Range cell = worksheetCells[row, column]; + cell.AddValue(positionAmount.Value); + } + + ResultBar.IncrementReplaced(); + return; + } + } + + string sku = positionAmount.Key.ProductSku.Substring(1, 6); + row = GetPositionRow(skuColumn, sku, positionAmount.Key.ProductLine); + + if (row != null) + { + foreach (int column in columns) + { + Range cell = worksheetCells[row, column]; + cell.AddValue(positionAmount.Value); + } + + ResultBar.IncrementReplaced(); + return; + } + + FillMissing(positionAmount, columns); + ResultBar.IncrementNotFound(); + } + + protected void FillMissing(KeyValuePair positionAmount, params int[] columns) + { + Range worksheetCells = TargetFile.Sheet.Cells; + Range worksheetRows = TargetFile.Sheet.Rows; + int skuColumn = TargetFile.SkuCell.Column; + int groupColumn = TargetFile.GroupCell.Column; + int nameColumn = TargetFile.NameCell.Column; + + int row = worksheetCells[worksheetRows.Count, skuColumn] + .End[XlDirection.xlUp] + .Row + 1; + + worksheetRows[row] + .EntireRow + .Insert(XlInsertShiftDirection.xlShiftDown, XlInsertFormatOrigin.xlFormatFromLeftOrAbove); + + Range previous = worksheetRows[row - 1]; + Range current = worksheetRows[row]; + + previous.Copy(current); + current.ClearContents(); + + worksheetCells[row, groupColumn].Value2 = positionAmount.Key.ProductLine; + worksheetCells[row, nameColumn].Value2 = positionAmount.Key.Name; + + if (TargetFile.OldSkuCell != null) + { + worksheetCells[row, skuColumn].Value2 = "Не найден"; + worksheetCells[row, TargetFile.OldSkuCell.Column].Value2 = positionAmount.Key.ProductSku; + } + + else + { + worksheetCells[row, skuColumn].Value2 = positionAmount.Key.ProductSku; + } + + foreach (int column in columns) + { + Range cell = worksheetCells[row, column]; + cell.AddValue(positionAmount.Value); + } + } + + protected int? GetPositionRow(Range range, string sku, string group) + { + Range found = range.Find(sku); + string foundGroupValue; + + if (found == null) + { + return null; + } + + int firstFoundRow = found.Row; + + if (string.IsNullOrEmpty(group)) + { + return found.Row; + } + + while (true) + { + foundGroupValue = TargetFile.Sheet.Cells[found.Row, TargetFile.GroupCell.Column].Value2.ToString(); + + if (group.Equals(foundGroupValue)) + { + return found.Row; + } + + found = range.FindNext(found); + + if (found.Row == firstFoundRow) + { + return null; + } + } + } + + protected void FilterByAmount() + { + AutoFilter filter = TargetFile.Sheet.AutoFilter; + int startColumn = filter.Range.Column; + + filter.Range.AutoFilter(TargetFile.AmountCell.Column - startColumn + 1, "<>"); + TargetFile.Sheet.Range["A1"].Activate(); + } + } +} \ No newline at end of file -- cgit v1.2.3