From 73569a43644309d0342817580bcfd86c1face5b8 Mon Sep 17 00:00:00 2001 From: Sergey Chebotar Date: Tue, 20 Dec 2022 12:27:47 +0300 Subject: Namespace refactoring --- src/AddIn.cs | 31 ++++++ src/AddIn/AddIn.cs | 30 ------ src/AddIn/EventsUtil.cs | 33 ------ src/AddIn/Functions.cs | 31 ------ src/AddIn/RegistryUtil.cs | 73 ------------- src/AddIn/RhDatabaseClient.cs | 45 -------- src/AddIn/Sku.cs | 67 ------------ src/AddIn/SkuExtensions.cs | 12 --- src/AddIn/WorksheetExtensions.cs | 41 ------- src/Controllers/CombineTool.cs | 56 ++++++++++ src/Controllers/ConvertTool.cs | 30 ++++++ src/Controllers/ExportTool.cs | 99 +++++++++++++++++ src/Controllers/MergeTool.cs | 46 ++++++++ src/Controllers/RibbonController.cs | 137 ++++++++++++++++++++++++ src/Controllers/ToolBase.cs | 189 +++++++++++++++++++++++++++++++++ src/Interface/Dialog.cs | 40 ------- src/Interface/ProgressBar.cs | 22 ---- src/Interface/ResultBar.cs | 45 -------- src/Interface/RibbonController.cs | 137 ------------------------ src/Interface/StatusbarBase.cs | 25 ----- src/Models/Dialog.cs | 40 +++++++ src/Models/PriceListBase.cs | 15 +++ src/Models/PriceListHeaders.cs | 11 ++ src/Models/Product.cs | 35 ++++++ src/Models/ProgressBar.cs | 22 ++++ src/Models/ResultBar.cs | 45 ++++++++ src/Models/Sku.cs | 67 ++++++++++++ src/Models/SkuExtensions.cs | 12 +++ src/Models/SourcePriceList.cs | 114 ++++++++++++++++++++ src/Models/StatusbarBase.cs | 25 +++++ src/Models/TargetPriceList.cs | 39 +++++++ src/Models/WorksheetExtensions.cs | 41 +++++++ src/PriceListTools/CombineTool.cs | 56 ---------- src/PriceListTools/ConvertTool.cs | 30 ------ src/PriceListTools/ExportTool.cs | 99 ----------------- src/PriceListTools/MergeTool.cs | 46 -------- src/PriceListTools/PriceListBase.cs | 15 --- src/PriceListTools/PriceListHeaders.cs | 11 -- src/PriceListTools/Product.cs | 35 ------ src/PriceListTools/SourcePriceList.cs | 115 -------------------- src/PriceListTools/TargetPriceList.cs | 39 ------- src/PriceListTools/ToolBase.cs | 189 --------------------------------- src/RhSolutions-AddIn.dna | 2 +- src/RhSolutions.csproj | 50 ++++----- src/Services/EventsUtil.cs | 34 ++++++ src/Services/Functions.cs | 31 ++++++ src/Services/RegistryUtil.cs | 74 +++++++++++++ src/Services/RhDatabaseClient.cs | 45 ++++++++ 48 files changed, 1264 insertions(+), 1262 deletions(-) create mode 100644 src/AddIn.cs delete mode 100644 src/AddIn/AddIn.cs delete mode 100644 src/AddIn/EventsUtil.cs delete mode 100644 src/AddIn/Functions.cs delete mode 100644 src/AddIn/RegistryUtil.cs delete mode 100644 src/AddIn/RhDatabaseClient.cs delete mode 100644 src/AddIn/Sku.cs delete mode 100644 src/AddIn/SkuExtensions.cs delete mode 100644 src/AddIn/WorksheetExtensions.cs create mode 100644 src/Controllers/CombineTool.cs create mode 100644 src/Controllers/ConvertTool.cs create mode 100644 src/Controllers/ExportTool.cs create mode 100644 src/Controllers/MergeTool.cs create mode 100644 src/Controllers/RibbonController.cs create mode 100644 src/Controllers/ToolBase.cs delete mode 100644 src/Interface/Dialog.cs delete mode 100644 src/Interface/ProgressBar.cs delete mode 100644 src/Interface/ResultBar.cs delete mode 100644 src/Interface/RibbonController.cs delete mode 100644 src/Interface/StatusbarBase.cs create mode 100644 src/Models/Dialog.cs create mode 100644 src/Models/PriceListBase.cs create mode 100644 src/Models/PriceListHeaders.cs create mode 100644 src/Models/Product.cs create mode 100644 src/Models/ProgressBar.cs create mode 100644 src/Models/ResultBar.cs create mode 100644 src/Models/Sku.cs create mode 100644 src/Models/SkuExtensions.cs create mode 100644 src/Models/SourcePriceList.cs create mode 100644 src/Models/StatusbarBase.cs create mode 100644 src/Models/TargetPriceList.cs create mode 100644 src/Models/WorksheetExtensions.cs delete mode 100644 src/PriceListTools/CombineTool.cs delete mode 100644 src/PriceListTools/ConvertTool.cs delete mode 100644 src/PriceListTools/ExportTool.cs delete mode 100644 src/PriceListTools/MergeTool.cs delete mode 100644 src/PriceListTools/PriceListBase.cs delete mode 100644 src/PriceListTools/PriceListHeaders.cs delete mode 100644 src/PriceListTools/Product.cs delete mode 100644 src/PriceListTools/SourcePriceList.cs delete mode 100644 src/PriceListTools/TargetPriceList.cs delete mode 100644 src/PriceListTools/ToolBase.cs create mode 100644 src/Services/EventsUtil.cs create mode 100644 src/Services/Functions.cs create mode 100644 src/Services/RegistryUtil.cs create mode 100644 src/Services/RhDatabaseClient.cs (limited to 'src') diff --git a/src/AddIn.cs b/src/AddIn.cs new file mode 100644 index 0000000..8c44821 --- /dev/null +++ b/src/AddIn.cs @@ -0,0 +1,31 @@ +using ExcelDna.Integration; +using ExcelDna.IntelliSense; +using Microsoft.Office.Interop.Excel; +using RhSolutions.Services; +using System.Net.Http; +using System.Runtime.Caching; + +namespace RhSolutions +{ + class RhSolutionsAddIn : IExcelAddIn + { + public static Application Excel; + public static HttpClient httpClient; + + public void AutoOpen() + { + Excel = (Application)ExcelDnaUtil.Application; + httpClient = new HttpClient(); + IntelliSenseServer.Install(); + RegistryUtil.Initialize(); + EventsUtil.Initialize(); + } + + public void AutoClose() + { + IntelliSenseServer.Uninstall(); + RegistryUtil.Uninitialize(); + EventsUtil.Uninitialize(); + } + } +} diff --git a/src/AddIn/AddIn.cs b/src/AddIn/AddIn.cs deleted file mode 100644 index b8bfd21..0000000 --- a/src/AddIn/AddIn.cs +++ /dev/null @@ -1,30 +0,0 @@ -using ExcelDna.Integration; -using ExcelDna.IntelliSense; -using Microsoft.Office.Interop.Excel; -using System.Net.Http; -using System.Runtime.Caching; - -namespace RhSolutions -{ - class AddIn : IExcelAddIn - { - public static Application Excel; - public static HttpClient httpClient; - - public void AutoOpen() - { - Excel = (Application)ExcelDnaUtil.Application; - httpClient = new HttpClient(); - IntelliSenseServer.Install(); - RegistryUtil.Initialize(); - EventsUtil.Initialize(); - } - - public void AutoClose() - { - IntelliSenseServer.Uninstall(); - RegistryUtil.Uninitialize(); - EventsUtil.Uninitialize(); - } - } -} diff --git a/src/AddIn/EventsUtil.cs b/src/AddIn/EventsUtil.cs deleted file mode 100644 index 3ceccfd..0000000 --- a/src/AddIn/EventsUtil.cs +++ /dev/null @@ -1,33 +0,0 @@ -using Microsoft.Office.Interop.Excel; - -namespace RhSolutions -{ - internal static class EventsUtil - { - private static readonly Application Excel = AddIn.Excel; - - public static void Initialize() - { - Excel.SheetSelectionChange += RefreshExportButton; - Excel.SheetActivate += RefreshConvertButton; - Excel.WorkbookActivate += RefreshConvertButton; - } - - public static void Uninitialize() - { - Excel.SheetSelectionChange -= RefreshExportButton; - Excel.SheetActivate -= RefreshConvertButton; - Excel.WorkbookActivate -= RefreshConvertButton; - } - - private static void RefreshConvertButton(object sh) - { - Interface.RibbonController.RefreshControl("convert"); - } - - private static void RefreshExportButton(object sh, Range target) - { - Interface.RibbonController.RefreshControl("export"); - } - } -} diff --git a/src/AddIn/Functions.cs b/src/AddIn/Functions.cs deleted file mode 100644 index 5bcfd45..0000000 --- a/src/AddIn/Functions.cs +++ /dev/null @@ -1,31 +0,0 @@ -using ExcelDna.Integration; -using System; - -namespace RhSolutions -{ - public class Functions - { - [ExcelFunction(Description = "Запрос в удаленную базу данных")] - public static object RHSOLUTIONS([ExcelArgument(Name = "Запрос")] string line) - { - object result; - - result = ExcelAsyncUtil.Run("Database request", line, delegate - { - return RhDatabaseClient.GetProduct(line).GetAwaiter().GetResult(); - }); - - if (result == null) - { - return ExcelError.ExcelErrorNA; - } - - if (result.Equals(ExcelError.ExcelErrorNA)) - { - return "Загрузка..."; - } - - return result; - } - } -} \ No newline at end of file diff --git a/src/AddIn/RegistryUtil.cs b/src/AddIn/RegistryUtil.cs deleted file mode 100644 index e848462..0000000 --- a/src/AddIn/RegistryUtil.cs +++ /dev/null @@ -1,73 +0,0 @@ -using Microsoft.Win32; -using RhSolutions.Interface; -using System; -using System.IO; -using System.Windows.Forms; - -namespace RhSolutions -{ - static class RegistryUtil - { - private static string priceListPath; - private static RegistryKey RootKey { get; set; } - - public static void Initialize() - { - RootKey = Registry.CurrentUser.CreateSubKey(@"SOFTWARE\REHAU\SkuAssist"); - priceListPath = RootKey.GetValue("PriceListPath") as string; - } - - public static void Uninitialize() - { - RootKey.Close(); - } - - public static string PriceListPath - { - get - { - if (string.IsNullOrEmpty(priceListPath) || !File.Exists(priceListPath)) - { - DialogResult result = MessageBox.Show("Прайс-лист отсутствует или неверный файл шаблона прайс-листа. " + - "Укажите файл шаблона прайс-листа.", - "Нет файла шаблона", - MessageBoxButtons.OK, MessageBoxIcon.Warning); - - if (result == DialogResult.OK) - { - string fileName = Dialog.GetFilePath(); - - if (string.IsNullOrEmpty(fileName)) - { - throw new Exception("Нет файла шаблона"); - } - - priceListPath = fileName; - RootKey.SetValue("PriceListPath", fileName); - return priceListPath; - } - - else - throw new Exception("Нет файла шаблона"); - } - - else - { - return priceListPath; - } - } - - set - { - priceListPath = value; - RootKey.SetValue("PriceListPath", value); - RibbonController.RefreshControl("setPriceList"); - } - } - - public static string GetPriceListName() - { - return Path.GetFileName(priceListPath); - } - } -} diff --git a/src/AddIn/RhDatabaseClient.cs b/src/AddIn/RhDatabaseClient.cs deleted file mode 100644 index 3a2de5c..0000000 --- a/src/AddIn/RhDatabaseClient.cs +++ /dev/null @@ -1,45 +0,0 @@ -using Newtonsoft.Json; -using System.Collections.Generic; -using System.Linq; -using System.Net; -using System.Net.Http; -using System.Threading.Tasks; - -namespace RhSolutions -{ - public static class RhDatabaseClient - { - private static HttpClient httpClient = AddIn.httpClient; - - public static async Task GetProduct(string line) - { - string request = @"https://rh.cebotari.ru/api/search?query=" + line; - - ServicePointManager.SecurityProtocol = - SecurityProtocolType.Tls12 | - SecurityProtocolType.Tls11 | - SecurityProtocolType.Tls; - - string response = await httpClient.GetStringAsync(request); - - var products = JsonConvert.DeserializeObject>(response); - - var product = products.FirstOrDefault(); - - if (product == null) - { - return null; - } - else - { - return $"{product.productSku} {product.name}"; - } - } - - private class DbProduct - { - public string productSku { get; set; } - public string name { get; set; } - } - } -} \ No newline at end of file diff --git a/src/AddIn/Sku.cs b/src/AddIn/Sku.cs deleted file mode 100644 index aaf5937..0000000 --- a/src/AddIn/Sku.cs +++ /dev/null @@ -1,67 +0,0 @@ -using System.Text.RegularExpressions; - -namespace RhSolutions -{ - internal class Sku - { - public string Article { get; private set; } - public string Variant { get; private set; } - - public Sku(string article, string variant) - { - Article = article; - Variant = variant; - } - - public static bool TryParse(string line, out Sku rehauSku) - { - Match match; - match = Regex.Match(line, @"\b[1]\d{6}[1]\d{3}\b"); - if (match.Success) - { - string sku = match.Value.Substring(1, 6); - string variant = match.Value.Substring(8, 3); - rehauSku = new Sku(sku, variant); - return true; - } - - match = Regex.Match(line, @"\b\d{6}\D\d{3}\b"); - if (match.Success) - { - string sku = match.Value.Substring(0, 6); - string variant = match.Value.Substring(7, 3); - rehauSku = new Sku(sku, variant); - return true; - } - - match = Regex.Match(line, @"\b\d{9}\b"); - if (match.Success) - { - string sku = match.Value.Substring(0, 6); - string variant = match.Value.Substring(6, 3); - rehauSku = new Sku(sku, variant); - return true; - } - - match = Regex.Match(line, @"\b\d{6}\b"); - if (match.Success) - { - string sku = match.Value.Substring(0, 6); - string variant = "001"; - rehauSku = new Sku(sku, variant); - return true; - } - - else - { - rehauSku = null; - return false; - } - } - - public override string ToString() - { - return $"1{Article}1{Variant}"; - } - } -} \ No newline at end of file diff --git a/src/AddIn/SkuExtensions.cs b/src/AddIn/SkuExtensions.cs deleted file mode 100644 index 2e97406..0000000 --- a/src/AddIn/SkuExtensions.cs +++ /dev/null @@ -1,12 +0,0 @@ -using System.Text.RegularExpressions; - -namespace RhSolutions -{ - static class SkuExtensions - { - public static bool IsRehauSku(this string line) - { - return Regex.IsMatch(line, @"^[1]\d{6}[1]\d{3}$"); - } - } -} \ No newline at end of file diff --git a/src/AddIn/WorksheetExtensions.cs b/src/AddIn/WorksheetExtensions.cs deleted file mode 100644 index b07e2c1..0000000 --- a/src/AddIn/WorksheetExtensions.cs +++ /dev/null @@ -1,41 +0,0 @@ -using Microsoft.Office.Interop.Excel; -using RhSolutions.PriceListTools; -using System.Linq; - -namespace RhSolutions -{ - public static class WorksheetExtensions - { - public static bool IsRehauSource(this Worksheet worksheet) - { - Range amountCell; - Range skuCell; - Range groupCell; - Range nameCell; - - Range[] cells = new[] - { - amountCell = worksheet.Cells.Find(PriceListHeaders.Amount), - skuCell = worksheet.Cells.Find(PriceListHeaders.Sku), - groupCell = worksheet.Cells.Find(PriceListHeaders.Group), - nameCell = worksheet.Cells.Find(PriceListHeaders.Name) - }; - - return cells.All(x => x != null); - } - - public static void AddValue(this Range range, double value) - { - if (range.Value2 == null) - { - range.Value2 = value; - } - - else - { - range.Value2 += value; - } - } - } -} - diff --git a/src/Controllers/CombineTool.cs b/src/Controllers/CombineTool.cs new file mode 100644 index 0000000..75c0f51 --- /dev/null +++ b/src/Controllers/CombineTool.cs @@ -0,0 +1,56 @@ +using Microsoft.Office.Interop.Excel; +using RhSolutions.Models; +using System; +using System.Collections.Generic; +using System.Linq; +using Dialog = RhSolutions.Models.Dialog; + +namespace RhSolutions.Controllers +{ + internal class CombineTool : ToolBase + { + private List SourceFiles { get; set; } + + public CombineTool() + { + string[] files = Dialog.GetMultiplyFiles(); + + if (files != null) + { + SourceFiles = SourcePriceList.GetSourceLists(files); + } + + else + { + throw new Exception("Не выбраны файлы"); + } + } + + public override void FillTarget() + { + using (ProgressBar = new ProgressBar("Заполняю строки...", SourceFiles.Sum(file => file.PositionAmount.Count))) + using (ResultBar = new ResultBar()) + { + foreach (SourcePriceList source in SourceFiles) + { + TargetFile.Sheet.Columns[TargetFile.AmountCell.Column] + .EntireColumn + .Insert(XlInsertShiftDirection.xlShiftToRight, XlInsertFormatOrigin.xlFormatFromRightOrBelow); + + Range newColumnHeader = TargetFile.Sheet.Cells[TargetFile.AmountCell.Row, TargetFile.AmountCell.Column - 1]; + newColumnHeader.Value2 = $"{source.Name}"; + newColumnHeader.WrapText = true; + + foreach (var kvp in source.PositionAmount) + { + FillPositionAmountToColumns(kvp, TargetFile.AmountCell.Column - 1, TargetFile.AmountCell.Column); + ProgressBar.Update(); + } + } + + FilterByAmount(); + ResultBar.Update(); + } + } + } +} diff --git a/src/Controllers/ConvertTool.cs b/src/Controllers/ConvertTool.cs new file mode 100644 index 0000000..5b2cd4d --- /dev/null +++ b/src/Controllers/ConvertTool.cs @@ -0,0 +1,30 @@ +using RhSolutions.Models; + +namespace RhSolutions.Controllers +{ + internal class ConvertTool : ToolBase + { + private SourcePriceList Current { get; set; } + + public ConvertTool() + { + Current = new SourcePriceList(ExcelApp.ActiveWorkbook); + } + + public override void FillTarget() + { + using (ProgressBar = new ProgressBar("Заполняю строки...", Current.PositionAmount.Count)) + using (ResultBar = new ResultBar()) + { + foreach (var kvp in Current.PositionAmount) + { + FillPositionAmountToColumns(kvp, TargetFile.AmountCell.Column); + ProgressBar.Update(); + } + + FilterByAmount(); + ResultBar.Update(); + } + } + } +} \ No newline at end of file diff --git a/src/Controllers/ExportTool.cs b/src/Controllers/ExportTool.cs new file mode 100644 index 0000000..6d8c348 --- /dev/null +++ b/src/Controllers/ExportTool.cs @@ -0,0 +1,99 @@ +using Microsoft.Office.Interop.Excel; +using System; +using System.Collections.Generic; +using RhSolutions.Models; + +namespace RhSolutions.Controllers +{ + internal class ExportTool : ToolBase + { + private Dictionary PositionAmount; + private readonly Range Selection; + + public ExportTool() + { + Selection = ExcelApp.Selection; + GetSelected(); + + if (PositionAmount.Count == 0) + { + throw new Exception("В выделенном диапазоне не найдены позиции для экспорта"); + } + } + + public override void FillTarget() + { + using (ProgressBar = new ProgressBar("Заполняю строки...", PositionAmount.Count)) + using (ResultBar = new ResultBar()) + { + foreach (var kvp in PositionAmount) + { + FillPositionAmountToColumns(kvp, TargetFile.AmountCell.Column); + ProgressBar.Update(); + } + + FilterByAmount(); + ResultBar.Update(); + } + } + + private void GetSelected() + { + object[,] cells = Selection.Value2; + PositionAmount = new Dictionary(); + + int rowsCount = Selection.Rows.Count; + + for (int row = 1; row <= rowsCount; row++) + { + if (cells[row, 1] == null || cells[row, 2] == null) + continue; + + string sku = null; + double? amount = null; + + for (int column = 1; column <= 2; column++) + { + object current = cells[row, column]; + + if (Sku.TryParse(current.ToString(), out Sku rauSku)) + { + sku = rauSku.ToString(); + } + + else if (current.GetType() == typeof(string) + && double.TryParse(current.ToString(), out _)) + { + amount = double.Parse((string)current); + } + + else if (current.GetType() == typeof(double)) + { + amount = (double)current; + } + } + + if (sku == null || amount == null) + { + continue; + } + + Product position = new Product + { + ProductSku = sku + }; + + if (PositionAmount.ContainsKey(position)) + { + PositionAmount[position] += amount.Value; + } + + else + { + PositionAmount.Add(position, amount.Value); + } + } + } + } +} + diff --git a/src/Controllers/MergeTool.cs b/src/Controllers/MergeTool.cs new file mode 100644 index 0000000..dec8ff7 --- /dev/null +++ b/src/Controllers/MergeTool.cs @@ -0,0 +1,46 @@ +using RhSolutions.Models; +using System; +using System.Collections.Generic; +using System.Linq; + +namespace RhSolutions.Controllers +{ + internal class MergeTool : ToolBase + { + private List SourceFiles { get; set; } + + public MergeTool() + { + string[] files = Dialog.GetMultiplyFiles(); + + if (files != null) + { + SourceFiles = SourcePriceList.GetSourceLists(files); + } + + else + { + throw new Exception("Не выбраны файлы"); + } + } + + public override void FillTarget() + { + using (ProgressBar = new ProgressBar("Заполняю строки...", SourceFiles.Sum(x => x.PositionAmount.Count))) + using (ResultBar = new ResultBar()) + { + foreach (SourcePriceList source in SourceFiles) + { + foreach (var kvp in source.PositionAmount) + { + FillPositionAmountToColumns(kvp, TargetFile.AmountCell.Column); + ProgressBar.Update(); + } + } + + FilterByAmount(); + ResultBar.Update(); + } + } + } +} diff --git a/src/Controllers/RibbonController.cs b/src/Controllers/RibbonController.cs new file mode 100644 index 0000000..87c62e1 --- /dev/null +++ b/src/Controllers/RibbonController.cs @@ -0,0 +1,137 @@ +using ExcelDna.Integration.CustomUI; +using Microsoft.Office.Interop.Excel; +using RhSolutions.Services; +using System; +using System.IO; +using System.Reflection; +using System.Runtime.InteropServices; +using System.Windows.Forms; + +namespace RhSolutions.Controllers +{ + [ComVisible(true)] + public class RibbonController : ExcelRibbon + { + private static IRibbonUI ribbonUi; + + public override string GetCustomUI(string RibbonID) + { + return @" + + + + + +