From 538d83257a71a0795071d104343ac3b1e35a1569 Mon Sep 17 00:00:00 2001 From: Sergey Chebotar Date: Mon, 4 Jul 2022 09:08:02 +0300 Subject: Add SKU Parser Function --- src/AddIn/Functions.cs | 13 ++++++++++ src/AddIn/RehauSku.cs | 67 ++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 80 insertions(+) create mode 100644 src/AddIn/RehauSku.cs (limited to 'src/AddIn') diff --git a/src/AddIn/Functions.cs b/src/AddIn/Functions.cs index 618d17d..867e246 100644 --- a/src/AddIn/Functions.cs +++ b/src/AddIn/Functions.cs @@ -52,5 +52,18 @@ namespace RehauSku return null; } } + + [ExcelFunction(Description = "Получение корректного артикула из строки")] + public static object GETRAUSKU([ExcelArgument(Name = "\"Строка\"", Description = "строка, содержащая актикул")] string line) + { + RauSku rausku; + + if (RauSku.TryParse(line, out rausku)) + { + return rausku.ToString(); + } + + else return ExcelError.ExcelErrorNA; + } } } \ No newline at end of file diff --git a/src/AddIn/RehauSku.cs b/src/AddIn/RehauSku.cs new file mode 100644 index 0000000..40e5d30 --- /dev/null +++ b/src/AddIn/RehauSku.cs @@ -0,0 +1,67 @@ +using System.Text.RegularExpressions; + +namespace RehauSku +{ + internal class RauSku + { + public string Sku { get; private set; } + public string Variant { get; private set; } + + public RauSku(string sku, string variant) + { + Sku = sku; + Variant = variant; + } + + public static bool TryParse(string line, out RauSku 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 RauSku(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 RauSku(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 RauSku(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 RauSku(sku, variant); + return true; + } + + else + { + rehauSku = null; + return false; + } + } + + public override string ToString() + { + return $"1{Sku}1{Variant}"; + } + } +} \ No newline at end of file -- cgit v1.2.3 From 10e6a108f974b3e2541ec322f99e60ec5ab7d9f7 Mon Sep 17 00:00:00 2001 From: Sergey Chebotar Date: Mon, 4 Jul 2022 09:21:44 +0300 Subject: Remove Response Order setting --- src/AddIn/AddIn.cs | 9 --------- src/AddIn/Functions.cs | 4 +--- src/AddIn/RegistryUtil.cs | 20 -------------------- 3 files changed, 1 insertion(+), 32 deletions(-) (limited to 'src/AddIn') diff --git a/src/AddIn/AddIn.cs b/src/AddIn/AddIn.cs index b532bfb..035e50f 100644 --- a/src/AddIn/AddIn.cs +++ b/src/AddIn/AddIn.cs @@ -7,15 +7,6 @@ using System.Runtime.Caching; namespace RehauSku { - enum ResponseOrder - { - Default, - Relevance, - Name, - Price, - Series - } - class AddIn : IExcelAddIn { public static HttpClient httpClient; diff --git a/src/AddIn/Functions.cs b/src/AddIn/Functions.cs index 867e246..efdec66 100644 --- a/src/AddIn/Functions.cs +++ b/src/AddIn/Functions.cs @@ -56,9 +56,7 @@ namespace RehauSku [ExcelFunction(Description = "Получение корректного артикула из строки")] public static object GETRAUSKU([ExcelArgument(Name = "\"Строка\"", Description = "строка, содержащая актикул")] string line) { - RauSku rausku; - - if (RauSku.TryParse(line, out rausku)) + if (RauSku.TryParse(line, out RauSku rausku)) { return rausku.ToString(); } diff --git a/src/AddIn/RegistryUtil.cs b/src/AddIn/RegistryUtil.cs index a13e941..54e071e 100644 --- a/src/AddIn/RegistryUtil.cs +++ b/src/AddIn/RegistryUtil.cs @@ -9,14 +9,12 @@ namespace RehauSku static class RegistryUtil { private static string priceListPath; - private static int? storeResponseOrder; private static RegistryKey RootKey { get; set; } public static void Initialize() { RootKey = Registry.CurrentUser.CreateSubKey(@"SOFTWARE\REHAU\SkuAssist"); priceListPath = RootKey.GetValue("PriceListPath") as string; - storeResponseOrder = RootKey.GetValue("StoreResponseOrder") as int?; } public static void Uninitialize() @@ -71,23 +69,5 @@ namespace RehauSku { return Path.GetFileName(priceListPath); } - - public static ResponseOrder StoreResponseOrder - { - get - { - if (storeResponseOrder == null) - { - RootKey.SetValue("StoreResponseOrder", (int)ResponseOrder.Default); - storeResponseOrder = (int)ResponseOrder.Default; - return (ResponseOrder)storeResponseOrder.Value; - } - - else - { - return (ResponseOrder)storeResponseOrder.Value; - } - } - } } } -- cgit v1.2.3 From dca3481e9a91e1c9d5b86e1508b0a2993088e759 Mon Sep 17 00:00:00 2001 From: Sergey Chebotar Date: Mon, 4 Jul 2022 11:33:29 +0300 Subject: Remove Store Functions --- src/AddIn/AddIn.cs | 5 ----- src/AddIn/Functions.cs | 49 -------------------------------------------- src/AddIn/MemoryCacheUtil.cs | 37 --------------------------------- 3 files changed, 91 deletions(-) delete mode 100644 src/AddIn/MemoryCacheUtil.cs (limited to 'src/AddIn') diff --git a/src/AddIn/AddIn.cs b/src/AddIn/AddIn.cs index 035e50f..1606624 100644 --- a/src/AddIn/AddIn.cs +++ b/src/AddIn/AddIn.cs @@ -9,14 +9,10 @@ namespace RehauSku { class AddIn : IExcelAddIn { - public static HttpClient httpClient; - public static MemoryCache memoryCache; public static Application Excel; public void AutoOpen() { - httpClient = new HttpClient(); - memoryCache = new MemoryCache("RehauSku"); Excel = (Application)ExcelDnaUtil.Application; RegisterFunctions(); IntelliSenseServer.Install(); @@ -29,7 +25,6 @@ namespace RehauSku IntelliSenseServer.Uninstall(); RegistryUtil.Uninitialize(); EventsUtil.Uninitialize(); - memoryCache.Dispose(); } void RegisterFunctions() diff --git a/src/AddIn/Functions.cs b/src/AddIn/Functions.cs index efdec66..c202a3b 100644 --- a/src/AddIn/Functions.cs +++ b/src/AddIn/Functions.cs @@ -1,58 +1,9 @@ using ExcelDna.Integration; -using RehauSku.Assistant; namespace RehauSku { public class Functions { - [ExcelFunction(description: "Получение названия первого продукта в поиске")] - public static object RAUNAME([ExcelArgument(Name = "\"Запрос\"", Description = "в свободной форме или ячейка с запросом")] string request) - => MakeRequest(request, ProductField.Name); - - [ExcelFunction(Description = "Получение артикула первого продукта в поиске")] - public static object RAUSKU([ExcelArgument(Name = "\"Запрос\"", Description = "в свободной форме или ячейка с запросом")] string request) - => MakeRequest(request, ProductField.Id); - - [ExcelFunction(Description = "Получение цены первого продукта в поиске")] - public static object RAUPRICE([ExcelArgument(Name = "\"Запрос\"", Description = "в свободной форме или ячейка с запросом")] string request) - => MakeRequest(request, ProductField.Price); - - private static object MakeRequest(string request, ProductField field) - { - object result; - - if (request.IsCached()) - result = request.GetFromCache(); - - else - { - result = ExcelAsyncUtil.Run("Request", request, delegate - { - return request.RequestAndCache().GetAwaiter().GetResult(); - }); - } - - if (result == null) - return "Не найдено :("; - - if (result.Equals(ExcelError.ExcelErrorNA)) - return "Загрузка..."; - - IProduct product = result as IProduct; - - switch (field) - { - case ProductField.Name: - return product.Name; - case ProductField.Id: - return product.Id; - case ProductField.Price: - return double.Parse(product.Price, System.Globalization.CultureInfo.InvariantCulture); - default: - return null; - } - } - [ExcelFunction(Description = "Получение корректного артикула из строки")] public static object GETRAUSKU([ExcelArgument(Name = "\"Строка\"", Description = "строка, содержащая актикул")] string line) { diff --git a/src/AddIn/MemoryCacheUtil.cs b/src/AddIn/MemoryCacheUtil.cs deleted file mode 100644 index 1d42e14..0000000 --- a/src/AddIn/MemoryCacheUtil.cs +++ /dev/null @@ -1,37 +0,0 @@ -using System; -using System.Runtime.Caching; -using System.Threading.Tasks; -using RehauSku.Assistant; - -namespace RehauSku -{ - static class MemoryCacheUtil - { - public static bool IsCached(this string request) - { - return AddIn.memoryCache.Contains(request); - } - - public static IProduct GetFromCache(this string request) - { - return AddIn.memoryCache[request] as IProduct; - } - - public static async Task RequestAndCache(this string request) - { - IProduct product = await SkuAssist.GetProductAsync(request); - - if (product == null) - return null; - - AddIn.memoryCache.Add(request, product, DateTime.Now.AddMinutes(10)); - return product; - } - - public static void ClearCache() - { - AddIn.memoryCache.Dispose(); - AddIn.memoryCache = new MemoryCache("RehauSku"); - } - } -} \ No newline at end of file -- cgit v1.2.3