From 72ac236b15603e84f18ec346749186b6cb2c2bdf Mon Sep 17 00:00:00 2001 From: Sergey Chebotar Date: Thu, 27 Jan 2022 10:22:30 +0300 Subject: [PATCH] Refactoring tolls classes --- src/PriceListTools/AbstractPriceListTool.cs | 83 ----------- src/PriceListTools/CombineTool.cs | 9 +- src/PriceListTools/ExportTool.cs | 11 +- src/PriceListTools/MergeTool.cs | 55 +------- src/PriceListTools/PriceListTool.cs | 129 ++++++++++++++++++ .../{SourceFile.cs => Source.cs} | 30 +--- src/PriceListTools/Target.cs | 46 +++++++ src/RehauSku.Assist.csproj | 5 +- src/Ribbon/RibbonController.cs | 10 +- 9 files changed, 194 insertions(+), 184 deletions(-) delete mode 100644 src/PriceListTools/AbstractPriceListTool.cs create mode 100644 src/PriceListTools/PriceListTool.cs rename src/PriceListTools/{SourceFile.cs => Source.cs} (63%) create mode 100644 src/PriceListTools/Target.cs diff --git a/src/PriceListTools/AbstractPriceListTool.cs b/src/PriceListTools/AbstractPriceListTool.cs deleted file mode 100644 index 437450c..0000000 --- a/src/PriceListTools/AbstractPriceListTool.cs +++ /dev/null @@ -1,83 +0,0 @@ -using ExcelDna.Integration; -using Microsoft.Office.Interop.Excel; -using System; -using System.Collections.Generic; - -namespace RehauSku.PriceListTools -{ - internal abstract class AbstractPriceListTool - { - protected private Application ExcelApp; - protected private SourceFile NewPriceList; - protected private List sourcePriceLists; - - public AbstractPriceListTool() - { - ExcelApp = (Application)ExcelDnaUtil.Application; - sourcePriceLists = new List(); - } - - protected private void FilterByAmount() - { - AutoFilter filter = NewPriceList.Sheet.AutoFilter; - - filter.Range.AutoFilter(NewPriceList.amountCell.Column, "<>"); - NewPriceList.Sheet.Range["A1"].Activate(); - } - - public void OpenNewPrice(string path) - { - Workbook wb = ExcelApp.Workbooks.Open(path); - - try - { - NewPriceList = new SourceFile(wb); - } - - catch (Exception ex) - { - System.Windows.Forms.MessageBox.Show - (ex.Message, - "Ошибка открытия шаблонного прайс-листа", - System.Windows.Forms.MessageBoxButtons.OK, - System.Windows.Forms.MessageBoxIcon.Information); - wb.Close(); - } - } - - public virtual void GetSource() - { - throw new NotImplementedException(); - } - - public virtual void GetSource(string[] files) - { - ExcelApp.ScreenUpdating = false; - foreach (string file in files) - { - Workbook wb = ExcelApp.Workbooks.Open(file); - try - { - SourceFile priceList = new SourceFile(wb); - sourcePriceLists.Add(priceList); - wb.Close(); - } - catch (Exception ex) - { - System.Windows.Forms.MessageBox.Show - (ex.Message, - "Ошибка открытия исходного прайс-листа", - System.Windows.Forms.MessageBoxButtons.OK, - System.Windows.Forms.MessageBoxIcon.Information); - wb.Close(); - } - } - ExcelApp.ScreenUpdating = true; - } - - public virtual void FillPriceList() - { - throw new NotImplementedException(); - } - } -} \ No newline at end of file diff --git a/src/PriceListTools/CombineTool.cs b/src/PriceListTools/CombineTool.cs index 2e5995c..39d3b98 100644 --- a/src/PriceListTools/CombineTool.cs +++ b/src/PriceListTools/CombineTool.cs @@ -3,9 +3,9 @@ using System; namespace RehauSku.PriceListTools { - internal class CombineTool : AbstractPriceListTool, IDisposable + internal class CombineTool : PriceListTool { - public override void FillPriceList() + public override void FillTarget() { int exportedValues = 0; @@ -52,10 +52,5 @@ namespace RehauSku.PriceListTools AddIn.Excel.StatusBar = $"Экспортировано {exportedValues} строк из {sourcePriceLists.Count} файлов"; Forms.Dialog.SaveWorkbookAs(); } - - public void Dispose() - { - GC.SuppressFinalize(this); - } } } diff --git a/src/PriceListTools/ExportTool.cs b/src/PriceListTools/ExportTool.cs index 2dc673b..b36fd13 100644 --- a/src/PriceListTools/ExportTool.cs +++ b/src/PriceListTools/ExportTool.cs @@ -6,7 +6,7 @@ using System.Collections.Generic; namespace RehauSku.PriceListTools { - internal class ExportTool : AbstractPriceListTool, IDisposable + internal class ExportTool : PriceListTool { private Dictionary SkuAmount { get; set; } private Range Selection; @@ -25,7 +25,7 @@ namespace RehauSku.PriceListTools else throw new Exception("Неверный диапазон"); } - public override void GetSource(string[] files) + public override void GetSourceLists(string[] files) => GetSource(); private void FillSkuAmountDict() @@ -73,7 +73,7 @@ namespace RehauSku.PriceListTools } } - public override void FillPriceList() + public override void FillTarget() { if (SkuAmount.Count < 1) return; @@ -114,11 +114,6 @@ namespace RehauSku.PriceListTools AddIn.Excel.StatusBar = $"Экспортировано {exportedValues} строк из {SkuAmount.Count}"; Forms.Dialog.SaveWorkbookAs(); } - - public void Dispose() - { - GC.SuppressFinalize(this); - } } } diff --git a/src/PriceListTools/MergeTool.cs b/src/PriceListTools/MergeTool.cs index 0e98b95..51884dd 100644 --- a/src/PriceListTools/MergeTool.cs +++ b/src/PriceListTools/MergeTool.cs @@ -1,58 +1,7 @@ -using Microsoft.Office.Interop.Excel; -using System; - -namespace RehauSku.PriceListTools +namespace RehauSku.PriceListTools { - internal class MergeTool : AbstractPriceListTool, IDisposable + internal class MergeTool : PriceListTool { - public override void FillPriceList() - { - int exportedValues = 0; - ExcelApp.ScreenUpdating = false; - - foreach (var sheet in sourcePriceLists) - { - if (sheet.SkuAmount.Count == 0) - continue; - - foreach (var kvp in sheet.SkuAmount) - { - Range cell = NewPriceList.Sheet.Columns[NewPriceList.skuCell.Column].Find(kvp.Key); - - if (cell == null) - { - System.Windows.Forms.MessageBox.Show - ($"Артикул {kvp.Key} отсутствует в таблице заказов {RegistryUtil.PriceListPath}", - "Отсутствует позиция в конечной таблице заказов", - System.Windows.Forms.MessageBoxButtons.OK, - System.Windows.Forms.MessageBoxIcon.Information); - } - - else - { - Range sumCell = NewPriceList.Sheet.Cells[cell.Row, NewPriceList.amountCell.Column]; - - if (sumCell.Value2 == null) - sumCell.Value2 = kvp.Value; - else - sumCell.Value2 += kvp.Value; - - exportedValues++; - } - } - } - - FilterByAmount(); - ExcelApp.ScreenUpdating = true; - - AddIn.Excel.StatusBar = $"Экспортировано {exportedValues} строк из {sourcePriceLists.Count} файлов"; - Forms.Dialog.SaveWorkbookAs(); - } - - public void Dispose() - { - GC.SuppressFinalize(this); - } } } diff --git a/src/PriceListTools/PriceListTool.cs b/src/PriceListTools/PriceListTool.cs new file mode 100644 index 0000000..5ccd31a --- /dev/null +++ b/src/PriceListTools/PriceListTool.cs @@ -0,0 +1,129 @@ +using ExcelDna.Integration; +using Microsoft.Office.Interop.Excel; +using System; +using System.Collections.Generic; + +namespace RehauSku.PriceListTools +{ + internal abstract class PriceListTool : IDisposable + { + protected private Application ExcelApp; + protected private Target NewPriceList; + protected private List sourcePriceLists; + + public PriceListTool() + { + ExcelApp = (Application)ExcelDnaUtil.Application; + sourcePriceLists = new List(); + } + + public void OpenNewPrice(string path) + { + Workbook wb = ExcelApp.Workbooks.Open(path); + + try + { + NewPriceList = new Target(wb); + } + + catch (Exception ex) + { + System.Windows.Forms.MessageBox.Show + (ex.Message, + "Ошибка открытия шаблонного прайс-листа", + System.Windows.Forms.MessageBoxButtons.OK, + System.Windows.Forms.MessageBoxIcon.Information); + wb.Close(); + } + } + + public virtual void GetSource() + { + throw new NotImplementedException(); + } + + public virtual void GetSourceLists(string[] files) + { + ExcelApp.ScreenUpdating = false; + foreach (string file in files) + { + Workbook wb = ExcelApp.Workbooks.Open(file); + try + { + Source priceList = new Source(wb); + sourcePriceLists.Add(priceList); + wb.Close(); + } + catch (Exception ex) + { + System.Windows.Forms.MessageBox.Show + (ex.Message, + "Ошибка открытия исходного прайс-листа", + System.Windows.Forms.MessageBoxButtons.OK, + System.Windows.Forms.MessageBoxIcon.Information); + wb.Close(); + } + } + ExcelApp.ScreenUpdating = true; + } + + public virtual void FillTarget() + { + ExcelApp.ScreenUpdating = false; + FillAmountColumn(); + FilterByAmount(); + ExcelApp.ScreenUpdating = true; + + Forms.Dialog.SaveWorkbookAs(); + } + + protected private void FillAmountColumn() + { + int exportedValues = 0; + foreach (var sheet in sourcePriceLists) + { + if (sheet.SkuAmount.Count == 0) + continue; + + foreach (var kvp in sheet.SkuAmount) + { + Range cell = NewPriceList.Sheet.Columns[NewPriceList.skuCell.Column].Find(kvp.Key); + + if (cell == null) + { + System.Windows.Forms.MessageBox.Show + ($"Артикул {kvp.Key} отсутствует в таблице заказов {RegistryUtil.PriceListPath}", + "Отсутствует позиция в конечной таблице заказов", + System.Windows.Forms.MessageBoxButtons.OK, + System.Windows.Forms.MessageBoxIcon.Information); + } + + else + { + Range sumCell = NewPriceList.Sheet.Cells[cell.Row, NewPriceList.amountCell.Column]; + + if (sumCell.Value2 == null) + sumCell.Value2 = kvp.Value; + else + sumCell.Value2 += kvp.Value; + + exportedValues++; + } + } + } + } + + protected private void FilterByAmount() + { + AutoFilter filter = NewPriceList.Sheet.AutoFilter; + + filter.Range.AutoFilter(NewPriceList.amountCell.Column, "<>"); + NewPriceList.Sheet.Range["A1"].Activate(); + } + + public void Dispose() + { + GC.SuppressFinalize(this); + } + } +} \ No newline at end of file diff --git a/src/PriceListTools/SourceFile.cs b/src/PriceListTools/Source.cs similarity index 63% rename from src/PriceListTools/SourceFile.cs rename to src/PriceListTools/Source.cs index 20d110d..40ac257 100644 --- a/src/PriceListTools/SourceFile.cs +++ b/src/PriceListTools/Source.cs @@ -1,17 +1,17 @@ using Microsoft.Office.Interop.Excel; -using System.Collections.Generic; using System; +using System.Collections.Generic; namespace RehauSku.PriceListTools { - internal class SourceFile : PriceList + internal class Source : PriceList { public Dictionary SkuAmount { get; private set; } - public SourceFile(Workbook workbook) + public Source(Workbook workbook) { Sheet = workbook.ActiveSheet; - Name = workbook.Name + '\n' + Sheet.Name; + Name = workbook.Name; amountCell = Sheet.Cells.Find(amountHeader); skuCell = Sheet.Cells.Find(skuHeader); @@ -48,27 +48,5 @@ namespace RehauSku.PriceListTools } } } - - internal class NewFile : PriceList - { - public Dictionary Map { get; private set; } - - public void CreateMap() - { - Range amountCell = Sheet.Cells.Find(amountHeader); - Range skuCell = Sheet.Cells.Find(skuHeader); - Range groupCell = Sheet.Cells.Find(groupHeader); - - //headerRowNumber = amountCell.Row; - //skuColumnNumber = skuCell.Column; - //amountColumnNumber = amountCell.Column; - //groupColumnNumber = groupCell.Column; - - //for (int row = headerRowNumber + 1; row < skuCell.Rows.Count; row++) - //{ - // string sku = - //} - } - } } diff --git a/src/PriceListTools/Target.cs b/src/PriceListTools/Target.cs new file mode 100644 index 0000000..b9eb856 --- /dev/null +++ b/src/PriceListTools/Target.cs @@ -0,0 +1,46 @@ +using Microsoft.Office.Interop.Excel; +using System; +using System.Collections.Generic; + +namespace RehauSku.PriceListTools +{ + internal class Target : PriceList + { + public Dictionary Map { get; private set; } + + public Target(Workbook workbook) + { + Sheet = workbook.ActiveSheet; + Name = workbook.Name; + + amountCell = Sheet.Cells.Find(amountHeader); + skuCell = Sheet.Cells.Find(skuHeader); + groupCell = Sheet.Cells.Find(groupHeader); + + if (amountCell == null || skuCell == null || groupCell == null) + { + throw new ArgumentException($"Лист { Name } не распознан"); + } + + CreateMap(); + } + + private void CreateMap() + { + Range amountCell = Sheet.Cells.Find(amountHeader); + Range skuCell = Sheet.Cells.Find(skuHeader); + Range groupCell = Sheet.Cells.Find(groupHeader); + + //headerRowNumber = amountCell.Row; + //skuColumnNumber = skuCell.Column; + //amountColumnNumber = amountCell.Column; + //groupColumnNumber = groupCell.Column; + + //for (int row = headerRowNumber + 1; row < skuCell.Rows.Count; row++) + //{ + // string sku = + //} + } + } +} + diff --git a/src/RehauSku.Assist.csproj b/src/RehauSku.Assist.csproj index 4d8c9d5..74d6df8 100644 --- a/src/RehauSku.Assist.csproj +++ b/src/RehauSku.Assist.csproj @@ -122,11 +122,12 @@ - + - + + diff --git a/src/Ribbon/RibbonController.cs b/src/Ribbon/RibbonController.cs index ed59541..d17426b 100644 --- a/src/Ribbon/RibbonController.cs +++ b/src/Ribbon/RibbonController.cs @@ -42,10 +42,10 @@ namespace RehauSku.Ribbon string[] files = Dialog.GetMultiplyFiles(); if (files.Length != 0) { - mergeTool.GetSource(files); + mergeTool.GetSourceLists(files); string exportFile = RegistryUtil.PriceListPath; mergeTool.OpenNewPrice(exportFile); - mergeTool.FillPriceList(); + mergeTool.FillTarget(); } } } @@ -57,10 +57,10 @@ namespace RehauSku.Ribbon string[] files = Dialog.GetMultiplyFiles(); if (files.Length != 0) { - combineTool.GetSource(files); + combineTool.GetSourceLists(files); string exportFile = RegistryUtil.PriceListPath; combineTool.OpenNewPrice(exportFile); - combineTool.FillPriceList(); + combineTool.FillTarget(); } } } @@ -74,7 +74,7 @@ namespace RehauSku.Ribbon exportTool.GetSource(); string exportFile = RegistryUtil.PriceListPath; exportTool.OpenNewPrice(exportFile); - exportTool.FillPriceList(); + exportTool.FillTarget(); } } catch (Exception ex)