From 8e3dff1788905c203509f866921957b027cb2643 Mon Sep 17 00:00:00 2001 From: Sergey Chebotar Date: Thu, 27 Jan 2022 09:59:33 +0300 Subject: [PATCH] Extract PriceList Base Class --- src/PriceListTools/AbstractPriceListTool.cs | 10 +-- src/PriceListTools/PriceList.cs | 83 +++------------------ src/PriceListTools/SourceFile.cs | 74 ++++++++++++++++++ src/RehauSku.Assist.csproj | 3 +- 4 files changed, 90 insertions(+), 80 deletions(-) create mode 100644 src/PriceListTools/SourceFile.cs diff --git a/src/PriceListTools/AbstractPriceListTool.cs b/src/PriceListTools/AbstractPriceListTool.cs index c174f30..437450c 100644 --- a/src/PriceListTools/AbstractPriceListTool.cs +++ b/src/PriceListTools/AbstractPriceListTool.cs @@ -8,13 +8,13 @@ namespace RehauSku.PriceListTools internal abstract class AbstractPriceListTool { protected private Application ExcelApp; - protected private PriceList NewPriceList; - protected private List sourcePriceLists; + protected private SourceFile NewPriceList; + protected private List sourcePriceLists; public AbstractPriceListTool() { ExcelApp = (Application)ExcelDnaUtil.Application; - sourcePriceLists = new List(); + sourcePriceLists = new List(); } protected private void FilterByAmount() @@ -31,7 +31,7 @@ namespace RehauSku.PriceListTools try { - NewPriceList = new PriceList(wb); + NewPriceList = new SourceFile(wb); } catch (Exception ex) @@ -58,7 +58,7 @@ namespace RehauSku.PriceListTools Workbook wb = ExcelApp.Workbooks.Open(file); try { - PriceList priceList = new PriceList(wb); + SourceFile priceList = new SourceFile(wb); sourcePriceLists.Add(priceList); wb.Close(); } diff --git a/src/PriceListTools/PriceList.cs b/src/PriceListTools/PriceList.cs index 1bf9663..5ec870e 100644 --- a/src/PriceListTools/PriceList.cs +++ b/src/PriceListTools/PriceList.cs @@ -1,83 +1,18 @@ using Microsoft.Office.Interop.Excel; -using System.Collections.Generic; -using System; namespace RehauSku.PriceListTools { internal class PriceList { - private const string amountHeader = "Кол-во"; - private const string skuHeader = "Актуальный материал"; - private const string groupHeader = "Программа"; + protected const string amountHeader = "Кол-во"; + protected const string skuHeader = "Актуальный материал"; + protected const string groupHeader = "Программа"; - public readonly Worksheet Sheet; - public readonly string Name; - public Dictionary SkuAmount { get; private set; } + public Range amountCell { get; protected set; } + public Range skuCell { get; protected set; } + public Range groupCell { get; protected set; } - public readonly Range amountCell; - public readonly Range skuCell; - public readonly Range groupCell; - - //public Dictionary Map { get; private set; } - - public PriceList(Workbook workbook) - { - Sheet = workbook.ActiveSheet; - Name = workbook.Name + '\n' + Sheet.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 } не распознан"); - } - - FillSkuAmount(); - } - - private void FillSkuAmount() - { - SkuAmount = new Dictionary(); - - object[,] amountColumn = Sheet.Columns[amountCell.Column].Value2; - object[,] skuColumn = Sheet.Columns[skuCell.Column].Value2; - - for (int row = amountCell.Row + 1; row < amountColumn.GetLength(0); row++) - { - object amount = amountColumn[row, 1]; - object sku = skuColumn[row, 1]; - - if (amount != null && (double)amount != 0) - { - if (SkuAmount.ContainsKey(sku.ToString())) - SkuAmount[sku.ToString()] += (double)amount; - - else - SkuAmount.Add(sku.ToString(), (double)amount); - } - } - } - - //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 = - // } - - //} + public Worksheet Sheet { get; protected set; } + public string Name { get; protected set; } } - -} - +} \ No newline at end of file diff --git a/src/PriceListTools/SourceFile.cs b/src/PriceListTools/SourceFile.cs new file mode 100644 index 0000000..20d110d --- /dev/null +++ b/src/PriceListTools/SourceFile.cs @@ -0,0 +1,74 @@ +using Microsoft.Office.Interop.Excel; +using System.Collections.Generic; +using System; + +namespace RehauSku.PriceListTools +{ + internal class SourceFile : PriceList + { + public Dictionary SkuAmount { get; private set; } + + public SourceFile(Workbook workbook) + { + Sheet = workbook.ActiveSheet; + Name = workbook.Name + '\n' + Sheet.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 } не распознан"); + } + + CreateAmountDict(); + } + + private void CreateAmountDict() + { + SkuAmount = new Dictionary(); + + object[,] amountColumn = Sheet.Columns[amountCell.Column].Value2; + object[,] skuColumn = Sheet.Columns[skuCell.Column].Value2; + + for (int row = amountCell.Row + 1; row < amountColumn.GetLength(0); row++) + { + object amount = amountColumn[row, 1]; + object sku = skuColumn[row, 1]; + + if (amount != null && (double)amount != 0) + { + if (SkuAmount.ContainsKey(sku.ToString())) + SkuAmount[sku.ToString()] += (double)amount; + + else + SkuAmount.Add(sku.ToString(), (double)amount); + } + } + } + } + + 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/RehauSku.Assist.csproj b/src/RehauSku.Assist.csproj index 968e8c0..4d8c9d5 100644 --- a/src/RehauSku.Assist.csproj +++ b/src/RehauSku.Assist.csproj @@ -124,8 +124,9 @@ - + +