diff options
Diffstat (limited to 'Source/AddIn')
-rw-r--r-- | Source/AddIn/AddIn.cs | 47 | ||||
-rw-r--r-- | Source/AddIn/Functions.cs | 56 | ||||
-rw-r--r-- | Source/AddIn/MemoryCacheUtil.cs | 37 | ||||
-rw-r--r-- | Source/AddIn/RegistryUtil.cs | 76 |
4 files changed, 216 insertions, 0 deletions
diff --git a/Source/AddIn/AddIn.cs b/Source/AddIn/AddIn.cs new file mode 100644 index 0000000..67cdcc8 --- /dev/null +++ b/Source/AddIn/AddIn.cs @@ -0,0 +1,47 @@ +using ExcelDna.Integration; +using ExcelDna.IntelliSense; +using ExcelDna.Registration; +using System.Net.Http; +using System.Runtime.Caching; + + +namespace RehauSku +{ + public enum ResponseOrder + { + Default, + Relevance, + Name, + Price, + Series + } + + public class AddIn : IExcelAddIn + { + public static HttpClient httpClient; + public static MemoryCache memoryCache; + + public void AutoOpen() + { + httpClient = new HttpClient(); + memoryCache = new MemoryCache("RehauSku"); + RegisterFunctions(); + IntelliSenseServer.Install(); + RegistryUtil.Initialize(); + } + + public void AutoClose() + { + IntelliSenseServer.Uninstall(); + RegistryUtil.Uninitialize(); + memoryCache.Dispose(); + } + + void RegisterFunctions() + { + ExcelRegistration.GetExcelFunctions() + .ProcessAsyncRegistrations(nativeAsyncIfAvailable: false) + .RegisterFunctions(); + } + } +} diff --git a/Source/AddIn/Functions.cs b/Source/AddIn/Functions.cs new file mode 100644 index 0000000..618d17d --- /dev/null +++ b/Source/AddIn/Functions.cs @@ -0,0 +1,56 @@ +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; + } + } + } +}
\ No newline at end of file diff --git a/Source/AddIn/MemoryCacheUtil.cs b/Source/AddIn/MemoryCacheUtil.cs new file mode 100644 index 0000000..1d42e14 --- /dev/null +++ b/Source/AddIn/MemoryCacheUtil.cs @@ -0,0 +1,37 @@ +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<IProduct> 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 diff --git a/Source/AddIn/RegistryUtil.cs b/Source/AddIn/RegistryUtil.cs new file mode 100644 index 0000000..40d0ec2 --- /dev/null +++ b/Source/AddIn/RegistryUtil.cs @@ -0,0 +1,76 @@ +using Microsoft.Win32; +using System.IO; +using RehauSku.Forms; +using System.Windows.Forms; + +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() + { + _RootKey.Close(); + + } + + public static bool IsPriceListPathEmpty() + { + return string.IsNullOrEmpty(_priceListPath); + } + + public static string PriceListPath + { + get + { + if (IsPriceListPathEmpty() || !File.Exists(_priceListPath)) + { + MessageBox.Show("Прайс-лист отсутствует или неверный файл прайс-листа", "Укажите файл прайс-листа", MessageBoxButtons.OK, MessageBoxIcon.Warning); + string fileName = Dialog.GetFilePath(); + _priceListPath = fileName; + _RootKey.SetValue("PriceListPath", fileName); + return _priceListPath; + } + + else + { + return _priceListPath; + } + } + + set + { + _priceListPath = value; + _RootKey.SetValue("PriceListPath", value); + } + } + + 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; + } + } + } + } +} |