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