Refactoring tolls classes

This commit is contained in:
Sergey Chebotar 2022-01-27 10:22:30 +03:00
parent 8e3dff1788
commit 72ac236b15
9 changed files with 194 additions and 184 deletions

View File

@ -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<SourceFile> sourcePriceLists;
public AbstractPriceListTool()
{
ExcelApp = (Application)ExcelDnaUtil.Application;
sourcePriceLists = new List<SourceFile>();
}
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();
}
}
}

View File

@ -3,9 +3,9 @@ using System;
namespace RehauSku.PriceListTools namespace RehauSku.PriceListTools
{ {
internal class CombineTool : AbstractPriceListTool, IDisposable internal class CombineTool : PriceListTool
{ {
public override void FillPriceList() public override void FillTarget()
{ {
int exportedValues = 0; int exportedValues = 0;
@ -52,10 +52,5 @@ namespace RehauSku.PriceListTools
AddIn.Excel.StatusBar = $"Экспортировано {exportedValues} строк из {sourcePriceLists.Count} файлов"; AddIn.Excel.StatusBar = $"Экспортировано {exportedValues} строк из {sourcePriceLists.Count} файлов";
Forms.Dialog.SaveWorkbookAs(); Forms.Dialog.SaveWorkbookAs();
} }
public void Dispose()
{
GC.SuppressFinalize(this);
}
} }
} }

View File

@ -6,7 +6,7 @@ using System.Collections.Generic;
namespace RehauSku.PriceListTools namespace RehauSku.PriceListTools
{ {
internal class ExportTool : AbstractPriceListTool, IDisposable internal class ExportTool : PriceListTool
{ {
private Dictionary<string, double> SkuAmount { get; set; } private Dictionary<string, double> SkuAmount { get; set; }
private Range Selection; private Range Selection;
@ -25,7 +25,7 @@ namespace RehauSku.PriceListTools
else throw new Exception("Неверный диапазон"); else throw new Exception("Неверный диапазон");
} }
public override void GetSource(string[] files) public override void GetSourceLists(string[] files)
=> GetSource(); => GetSource();
private void FillSkuAmountDict() private void FillSkuAmountDict()
@ -73,7 +73,7 @@ namespace RehauSku.PriceListTools
} }
} }
public override void FillPriceList() public override void FillTarget()
{ {
if (SkuAmount.Count < 1) if (SkuAmount.Count < 1)
return; return;
@ -114,11 +114,6 @@ namespace RehauSku.PriceListTools
AddIn.Excel.StatusBar = $"Экспортировано {exportedValues} строк из {SkuAmount.Count}"; AddIn.Excel.StatusBar = $"Экспортировано {exportedValues} строк из {SkuAmount.Count}";
Forms.Dialog.SaveWorkbookAs(); Forms.Dialog.SaveWorkbookAs();
} }
public void Dispose()
{
GC.SuppressFinalize(this);
}
} }
} }

View File

@ -1,58 +1,7 @@
using Microsoft.Office.Interop.Excel; namespace RehauSku.PriceListTools
using System;
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);
}
} }
} }

View File

@ -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<Source> sourcePriceLists;
public PriceListTool()
{
ExcelApp = (Application)ExcelDnaUtil.Application;
sourcePriceLists = new List<Source>();
}
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);
}
}
}

View File

@ -1,17 +1,17 @@
using Microsoft.Office.Interop.Excel; using Microsoft.Office.Interop.Excel;
using System.Collections.Generic;
using System; using System;
using System.Collections.Generic;
namespace RehauSku.PriceListTools namespace RehauSku.PriceListTools
{ {
internal class SourceFile : PriceList internal class Source : PriceList
{ {
public Dictionary<string, double> SkuAmount { get; private set; } public Dictionary<string, double> SkuAmount { get; private set; }
public SourceFile(Workbook workbook) public Source(Workbook workbook)
{ {
Sheet = workbook.ActiveSheet; Sheet = workbook.ActiveSheet;
Name = workbook.Name + '\n' + Sheet.Name; Name = workbook.Name;
amountCell = Sheet.Cells.Find(amountHeader); amountCell = Sheet.Cells.Find(amountHeader);
skuCell = Sheet.Cells.Find(skuHeader); skuCell = Sheet.Cells.Find(skuHeader);
@ -48,27 +48,5 @@ namespace RehauSku.PriceListTools
} }
} }
} }
internal class NewFile : PriceList
{
public Dictionary<PriceListPosition, Range> 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 =
//}
}
}
} }

View File

@ -0,0 +1,46 @@
using Microsoft.Office.Interop.Excel;
using System;
using System.Collections.Generic;
namespace RehauSku.PriceListTools
{
internal class Target : PriceList
{
public Dictionary<PriceListPosition, Range> 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 =
//}
}
}
}

View File

@ -122,11 +122,12 @@
<Compile Include="Assistant\RequestModifier.cs" /> <Compile Include="Assistant\RequestModifier.cs" />
<Compile Include="Assistant\SkuExtensions.cs" /> <Compile Include="Assistant\SkuExtensions.cs" />
<Compile Include="PriceListTools\CombineTool.cs" /> <Compile Include="PriceListTools\CombineTool.cs" />
<Compile Include="PriceListTools\AbstractPriceListTool.cs" /> <Compile Include="PriceListTools\PriceListTool.cs" />
<Compile Include="PriceListTools\MergeTool.cs" /> <Compile Include="PriceListTools\MergeTool.cs" />
<Compile Include="PriceListTools\PriceList.cs" /> <Compile Include="PriceListTools\PriceList.cs" />
<Compile Include="PriceListTools\PriceListPosition.cs" /> <Compile Include="PriceListTools\PriceListPosition.cs" />
<Compile Include="PriceListTools\SourceFile.cs" /> <Compile Include="PriceListTools\Source.cs" />
<Compile Include="PriceListTools\Target.cs" />
<Compile Include="Ribbon\RibbonController.cs" /> <Compile Include="Ribbon\RibbonController.cs" />
<Compile Include="Assistant\HttpClientUtil.cs" /> <Compile Include="Assistant\HttpClientUtil.cs" />
<Compile Include="Assistant\StoreResponse.cs" /> <Compile Include="Assistant\StoreResponse.cs" />

View File

@ -42,10 +42,10 @@ namespace RehauSku.Ribbon
string[] files = Dialog.GetMultiplyFiles(); string[] files = Dialog.GetMultiplyFiles();
if (files.Length != 0) if (files.Length != 0)
{ {
mergeTool.GetSource(files); mergeTool.GetSourceLists(files);
string exportFile = RegistryUtil.PriceListPath; string exportFile = RegistryUtil.PriceListPath;
mergeTool.OpenNewPrice(exportFile); mergeTool.OpenNewPrice(exportFile);
mergeTool.FillPriceList(); mergeTool.FillTarget();
} }
} }
} }
@ -57,10 +57,10 @@ namespace RehauSku.Ribbon
string[] files = Dialog.GetMultiplyFiles(); string[] files = Dialog.GetMultiplyFiles();
if (files.Length != 0) if (files.Length != 0)
{ {
combineTool.GetSource(files); combineTool.GetSourceLists(files);
string exportFile = RegistryUtil.PriceListPath; string exportFile = RegistryUtil.PriceListPath;
combineTool.OpenNewPrice(exportFile); combineTool.OpenNewPrice(exportFile);
combineTool.FillPriceList(); combineTool.FillTarget();
} }
} }
} }
@ -74,7 +74,7 @@ namespace RehauSku.Ribbon
exportTool.GetSource(); exportTool.GetSource();
string exportFile = RegistryUtil.PriceListPath; string exportFile = RegistryUtil.PriceListPath;
exportTool.OpenNewPrice(exportFile); exportTool.OpenNewPrice(exportFile);
exportTool.FillPriceList(); exportTool.FillTarget();
} }
} }
catch (Exception ex) catch (Exception ex)