diff options
Diffstat (limited to 'src/Services')
-rw-r--r-- | src/Services/EventsUtil.cs | 34 | ||||
-rw-r--r-- | src/Services/Functions.cs | 31 | ||||
-rw-r--r-- | src/Services/RegistryUtil.cs | 74 | ||||
-rw-r--r-- | src/Services/RhDatabaseClient.cs | 45 |
4 files changed, 184 insertions, 0 deletions
diff --git a/src/Services/EventsUtil.cs b/src/Services/EventsUtil.cs new file mode 100644 index 0000000..4b1d923 --- /dev/null +++ b/src/Services/EventsUtil.cs @@ -0,0 +1,34 @@ +using Microsoft.Office.Interop.Excel; +using RhSolutions.Controllers; + +namespace RhSolutions.Services +{ + internal static class EventsUtil + { + private static readonly Application Excel = RhSolutionsAddIn.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) + { + RibbonController.RefreshControl("convert"); + } + + private static void RefreshExportButton(object sh, Range target) + { + RibbonController.RefreshControl("export"); + } + } +} diff --git a/src/Services/Functions.cs b/src/Services/Functions.cs new file mode 100644 index 0000000..22b67e0 --- /dev/null +++ b/src/Services/Functions.cs @@ -0,0 +1,31 @@ +using ExcelDna.Integration; +using System; + +namespace RhSolutions.Services +{ + 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/Services/RegistryUtil.cs b/src/Services/RegistryUtil.cs new file mode 100644 index 0000000..8b165ea --- /dev/null +++ b/src/Services/RegistryUtil.cs @@ -0,0 +1,74 @@ +using Microsoft.Win32; +using RhSolutions.Controllers; +using RhSolutions.Models; +using System; +using System.IO; +using System.Windows.Forms; + +namespace RhSolutions.Services +{ + 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/Services/RhDatabaseClient.cs b/src/Services/RhDatabaseClient.cs new file mode 100644 index 0000000..c57d4d8 --- /dev/null +++ b/src/Services/RhDatabaseClient.cs @@ -0,0 +1,45 @@ +using Newtonsoft.Json; +using System.Collections.Generic; +using System.Linq; +using System.Net; +using System.Net.Http; +using System.Threading.Tasks; + +namespace RhSolutions.Services +{ + public static class RhDatabaseClient + { + private static HttpClient httpClient = RhSolutionsAddIn.httpClient; + + public static async Task<object> 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<IEnumerable<DbProduct>>(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 |