Compare commits
No commits in common. "213592b9e2e589b36b5fbd2a26d1ca28530d58ff" and "8b125e475e4f6970d1948e77fdf832c5e77f2cec" have entirely different histories.
213592b9e2
...
8b125e475e
@ -24,7 +24,7 @@ public sealed class RhSolutionsAddIn : IExcelAddIn
|
||||
.AddSingleton((Application)ExcelDnaUtil.Application)
|
||||
.AddSingleton<IAddInConfiguration, AddInConfiguration>()
|
||||
.AddSingleton<IDatabaseClient, DatabaseClient>()
|
||||
.AddSingleton<ICurrencyClient, CurrencyClient>()
|
||||
.AddTransient<ICurrencyClient, CurrencyClient>()
|
||||
.AddTransient<IFileDialog, FileDialog>();
|
||||
|
||||
Services.AddSingleton<WriterFactory>();
|
||||
|
@ -2,14 +2,12 @@
|
||||
using System.Runtime.Versioning;
|
||||
#endif
|
||||
|
||||
using Microsoft.Extensions.Caching.Memory;
|
||||
|
||||
namespace RhSolutions.AddIn;
|
||||
|
||||
#if !NET472
|
||||
[SupportedOSPlatform("windows")]
|
||||
#endif
|
||||
public class RhSolutionsFunctions
|
||||
public class RhSolutionsFunction
|
||||
{
|
||||
[ExcelFunction(Description = "Поиск артикула в базе данных")]
|
||||
public static object RHSOLUTIONS([ExcelArgument(Name = "СТРОКА", Description = "Ячейка с артикулом РЕХАУ или поисковый запрос в свободной форме")] string line)
|
||||
@ -18,7 +16,7 @@ public class RhSolutionsFunctions
|
||||
|
||||
ProductSku.TryParse(line, out var skus);
|
||||
|
||||
if (ExcelAsyncUtil.Run(nameof(RHSOLUTIONS), line, delegate
|
||||
if (ExcelAsyncUtil.Run("Database request", line, delegate
|
||||
{
|
||||
return databaseClient.GetProducts(line)
|
||||
.GetAwaiter()
|
||||
@ -82,7 +80,7 @@ public class RhSolutionsFunctions
|
||||
var article = skus.First().Id;
|
||||
IDatabaseClient databaseClient = RhSolutionsAddIn.ServiceProvider.GetService<IDatabaseClient>();
|
||||
|
||||
if (ExcelAsyncUtil.Run(nameof(РЕХАУИМЯ), line, delegate
|
||||
if (ExcelAsyncUtil.Run("Database request", line, delegate
|
||||
{
|
||||
return databaseClient.GetProducts(article)
|
||||
.GetAwaiter()
|
||||
@ -118,7 +116,7 @@ public class RhSolutionsFunctions
|
||||
var article = skus.First().Id;
|
||||
IDatabaseClient databaseClient = RhSolutionsAddIn.ServiceProvider.GetService<IDatabaseClient>();
|
||||
|
||||
if (ExcelAsyncUtil.Run(nameof(РЕХАУЦЕНА), line, delegate
|
||||
if (ExcelAsyncUtil.Run("Database request", line, delegate
|
||||
{
|
||||
return databaseClient.GetProducts(article)
|
||||
.GetAwaiter()
|
||||
@ -156,7 +154,7 @@ public class RhSolutionsFunctions
|
||||
IDatabaseClient databaseClient = RhSolutionsAddIn.ServiceProvider.GetService<IDatabaseClient>();
|
||||
ICurrencyClient currencyClient = RhSolutionsAddIn.ServiceProvider.GetRequiredService<ICurrencyClient>();
|
||||
|
||||
if (ExcelAsyncUtil.Run(nameof(РЕХАУЦЕНАРУБ), line, delegate
|
||||
if (ExcelAsyncUtil.Run("Database request", line, delegate
|
||||
{
|
||||
var product = databaseClient.GetProducts(article)
|
||||
.GetAwaiter()
|
||||
@ -192,33 +190,29 @@ public class RhSolutionsFunctions
|
||||
public static object КУРСЕВРО([ExcelArgument(Name = "ДАТА", Description = "Дата в формате Excel (необязательно)")] double dateField)
|
||||
{
|
||||
ICurrencyClient currencyClient = RhSolutionsAddIn.ServiceProvider.GetRequiredService<ICurrencyClient>();
|
||||
IMemoryCache memoryCache = RhSolutionsAddIn.ServiceProvider.GetRequiredService<IMemoryCache>();
|
||||
DateTime date = dateField == 0 ? DateTime.Today : DateTime.FromOADate(dateField);
|
||||
|
||||
if (!memoryCache.TryGetValue(date, out decimal exchangeRate))
|
||||
if (ExcelAsyncUtil.Run("Database request", dateField, delegate
|
||||
{
|
||||
var result = ExcelAsyncUtil.Run(nameof(КУРСЕВРО), dateField, delegate
|
||||
{
|
||||
var requestResult = currencyClient.GetCurrencyCourse(date)
|
||||
.GetAwaiter()
|
||||
.GetResult();
|
||||
DateTime date = dateField == 0 ? DateTime.Today : DateTime.FromOADate(dateField);
|
||||
var exchangeRate = currencyClient.GetCurrencyCourse(date)
|
||||
.GetAwaiter()
|
||||
.GetResult();
|
||||
|
||||
return requestResult ?? -1m;
|
||||
});
|
||||
|
||||
if (result is not decimal)
|
||||
return exchangeRate ?? -1m;
|
||||
}) is not decimal requestResult)
|
||||
{
|
||||
return "Загрузка...";
|
||||
}
|
||||
else
|
||||
{
|
||||
if (requestResult < 0)
|
||||
{
|
||||
return "Загрузка...";
|
||||
return ExcelError.ExcelErrorNA;
|
||||
}
|
||||
else
|
||||
{
|
||||
exchangeRate = (decimal)result;
|
||||
var cacheEntryOptions = new MemoryCacheEntryOptions()
|
||||
.SetSlidingExpiration(TimeSpan.FromHours(1));
|
||||
memoryCache.Set(date, exchangeRate, cacheEntryOptions);
|
||||
return Math.Round(requestResult, 2);
|
||||
}
|
||||
}
|
||||
|
||||
return exchangeRate < 0 ? ExcelError.ExcelErrorNA : exchangeRate;
|
||||
}
|
||||
}
|
@ -1,4 +1,5 @@
|
||||
using System.Net;
|
||||
using Microsoft.Extensions.Caching.Memory;
|
||||
using System.Net;
|
||||
using System.Net.Http;
|
||||
using System.Threading.Tasks;
|
||||
using System.Xml.Linq;
|
||||
@ -8,34 +9,43 @@ namespace RhSolutions.Services;
|
||||
public class CurrencyClient : ICurrencyClient
|
||||
{
|
||||
private readonly HttpClient _httpClient;
|
||||
private const string _requestAddress = @"https://www.cbr.ru/scripts/XML_daily.asp?date_req=";
|
||||
private readonly IMemoryCache _memoryCache;
|
||||
private const string requestAddress = @"https://www.cbr.ru/scripts/XML_daily.asp?date_req=";
|
||||
public HttpStatusCode StatusCode { get; private set; }
|
||||
|
||||
public CurrencyClient(HttpClient httpClient)
|
||||
public CurrencyClient(HttpClient httpClient, IMemoryCache memoryCache)
|
||||
{
|
||||
_httpClient = httpClient;
|
||||
_memoryCache = memoryCache;
|
||||
}
|
||||
|
||||
public async Task<decimal?> GetCurrencyCourse(DateTime date)
|
||||
{
|
||||
string request = $"{_requestAddress}{date.Date:dd/MM/yyyy}";
|
||||
HttpResponseMessage response = await _httpClient.GetAsync(request);
|
||||
try
|
||||
if (!_memoryCache.TryGetValue(date, out decimal? exchangeRate))
|
||||
{
|
||||
response.EnsureSuccessStatusCode();
|
||||
string xml = await response.Content.ReadAsStringAsync();
|
||||
XElement valCourses = XElement.Parse(xml);
|
||||
string request = $"{requestAddress}{date.Date:dd/MM/yyyy}";
|
||||
HttpResponseMessage response = await _httpClient.GetAsync(request);
|
||||
try
|
||||
{
|
||||
response.EnsureSuccessStatusCode();
|
||||
string xml = await response.Content.ReadAsStringAsync();
|
||||
XElement valCourses = XElement.Parse(xml);
|
||||
exchangeRate = decimal.Parse(valCourses.Elements("Valute")
|
||||
.Where(e => e.Element("Name").Value == "Евро")
|
||||
.FirstOrDefault()
|
||||
.Element("Value").Value);
|
||||
return exchangeRate;
|
||||
}
|
||||
catch
|
||||
{
|
||||
StatusCode = response.StatusCode;
|
||||
exchangeRate = null;
|
||||
}
|
||||
|
||||
decimal? exchangeRate = decimal.Parse(valCourses.Elements("Valute")
|
||||
.Where(e => e.Element("Name").Value == "Евро")
|
||||
.FirstOrDefault()
|
||||
.Element("Value").Value);
|
||||
return exchangeRate;
|
||||
}
|
||||
catch
|
||||
{
|
||||
StatusCode = response.StatusCode;
|
||||
return null;
|
||||
var cacheEntryOptions = new MemoryCacheEntryOptions()
|
||||
.SetSlidingExpiration(TimeSpan.FromHours(1));
|
||||
_memoryCache.Set(date, exchangeRate, cacheEntryOptions);
|
||||
}
|
||||
return exchangeRate;
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user