From d4d98ec85002d10b41268b8b21ee1e34f8a6c950 Mon Sep 17 00:00:00 2001 From: Sergey Chebotar Date: Mon, 15 May 2023 06:33:24 +0300 Subject: [PATCH] =?UTF-8?q?Add=20=3D=D0=A0=D0=95=D0=A5=D0=90=D0=A3=D0=A6?= =?UTF-8?q?=D0=95=D0=9D=D0=90=D0=A0=D0=A3=D0=91()=20function?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- RhSolutions.AddIn/AddIn/RhSolutionsAddIn.cs | 1 + .../AddIn/RhSolutionsFunction.cs | 55 +++++++++++++++++-- RhSolutions.AddIn/Services/CurrencyClient.cs | 41 ++++++++++++++ RhSolutions.AddIn/Services/ICurrencyClient.cs | 9 +++ 4 files changed, 101 insertions(+), 5 deletions(-) create mode 100644 RhSolutions.AddIn/Services/CurrencyClient.cs create mode 100644 RhSolutions.AddIn/Services/ICurrencyClient.cs diff --git a/RhSolutions.AddIn/AddIn/RhSolutionsAddIn.cs b/RhSolutions.AddIn/AddIn/RhSolutionsAddIn.cs index 0301535..bf9c992 100644 --- a/RhSolutions.AddIn/AddIn/RhSolutionsAddIn.cs +++ b/RhSolutions.AddIn/AddIn/RhSolutionsAddIn.cs @@ -22,6 +22,7 @@ public sealed class RhSolutionsAddIn : IExcelAddIn .AddSingleton((Application)ExcelDnaUtil.Application) .AddSingleton() .AddSingleton() + .AddTransient() .AddTransient() .AddTransient(); diff --git a/RhSolutions.AddIn/AddIn/RhSolutionsFunction.cs b/RhSolutions.AddIn/AddIn/RhSolutionsFunction.cs index e2bfc50..c9e5407 100644 --- a/RhSolutions.AddIn/AddIn/RhSolutionsFunction.cs +++ b/RhSolutions.AddIn/AddIn/RhSolutionsFunction.cs @@ -39,12 +39,10 @@ public class RhSolutionsFunction { return ExcelError.ExcelErrorNA; } - else if (!requestResult.Any()) { return $"{skus.First()}"; } - else { var firstProduct = requestResult.First(); @@ -52,8 +50,10 @@ public class RhSolutionsFunction } } } + [ExcelFunction] public static object РЕХАУ(string line) => RHSOLUTIONS(line); + [ExcelFunction] public static object РЕХАУАРТИКУЛ(string line) { @@ -66,6 +66,7 @@ public class RhSolutionsFunction return ExcelError.ExcelErrorNA; } } + [ExcelFunction] public static object РЕХАУИМЯ(string line) { @@ -93,7 +94,6 @@ public class RhSolutionsFunction { return ExcelError.ExcelErrorNA; } - else { var firstProduct = requestResult.First(); @@ -102,6 +102,7 @@ public class RhSolutionsFunction } } } + [ExcelFunction] public static object РЕХАУЦЕНА(string line) { @@ -129,11 +130,55 @@ public class RhSolutionsFunction { return ExcelError.ExcelErrorNA; } - else { var firstProduct = requestResult.First(); - return string.Format("{0:N2} €", firstProduct.Price * 1.2m); + return Math.Round(firstProduct.Price * 1.2m, 2); + } + } + } + } + + [ExcelFunction] + public static object РЕХАУЦЕНАРУБ(string line, double dateField) + { + if (!ProductSku.TryParse(line, out var skus)) + { + return ExcelError.ExcelErrorNA; + } + else + { + var article = skus.First().Id; + IDatabaseClient databaseClient = RhSolutionsAddIn.ServiceProvider.GetService(); + ICurrencyClient currencyClient = RhSolutionsAddIn.ServiceProvider.GetRequiredService(); + + if (ExcelAsyncUtil.Run("Database request", line, delegate + { + var product = databaseClient.GetProducts(article) + .GetAwaiter() + .GetResult() + .FirstOrDefault(); + + DateTime date = dateField == 0 ? DateTime.Now : DateTime.FromOADate(dateField); + var exchangeRate = currencyClient.GetCurrencyCourse(date) + .GetAwaiter() + .GetResult(); + + return product == null ? -1m : + product.Price * exchangeRate * 1.2m; + }) is not decimal requestResult) + { + return "Загрузка..."; + } + else + { + if (requestResult < 0) + { + return ExcelError.ExcelErrorNA; + } + else + { + return Math.Round(requestResult, 2); } } } diff --git a/RhSolutions.AddIn/Services/CurrencyClient.cs b/RhSolutions.AddIn/Services/CurrencyClient.cs new file mode 100644 index 0000000..4f3b922 --- /dev/null +++ b/RhSolutions.AddIn/Services/CurrencyClient.cs @@ -0,0 +1,41 @@ +using System.Net; +using System.Net.Http; +using System.Threading.Tasks; +using System.Xml.Linq; + +namespace RhSolutions.Services; + +public class CurrencyClient : ICurrencyClient +{ + private readonly HttpClient _httpClient; + public HttpStatusCode StatusCode { get; private set; } + + public CurrencyClient(IServiceProvider serviceProvider) + { + _httpClient = serviceProvider.GetRequiredService(); + } + + public async Task GetCurrencyCourse(DateTime date) + { + string request = $"https://www.cbr.ru/scripts/XML_daily.asp?date_req={date.Date.ToString("dd/MM/yyyy")}"; + var response = await _httpClient.GetAsync(request); + + try + { + response.EnsureSuccessStatusCode(); + string xml = await response.Content.ReadAsStringAsync(); + XElement valCourses = XElement.Parse(xml); + var course = decimal.Parse(valCourses.Elements("Valute") + .Where(e => e.Element("Name").Value == "Евро") + .FirstOrDefault() + .Element("Value").Value); + return course; + } + catch + { + StatusCode = response.StatusCode; + } + + return null; + } +} \ No newline at end of file diff --git a/RhSolutions.AddIn/Services/ICurrencyClient.cs b/RhSolutions.AddIn/Services/ICurrencyClient.cs new file mode 100644 index 0000000..f982a06 --- /dev/null +++ b/RhSolutions.AddIn/Services/ICurrencyClient.cs @@ -0,0 +1,9 @@ +using Newtonsoft.Json; +using System.Threading.Tasks; + +namespace RhSolutions.Services; + +public interface ICurrencyClient +{ + public Task GetCurrencyCourse(DateTime date); +}