Refactoring functions
This commit is contained in:
parent
fe58c854ea
commit
7b2a5955d8
@ -1,26 +1,18 @@
|
||||
#if !NET472
|
||||
using System.Runtime.Versioning;
|
||||
#endif
|
||||
namespace RhSolutions.AddIn;
|
||||
|
||||
using Microsoft.Extensions.Caching.Memory;
|
||||
|
||||
namespace RhSolutions.AddIn;
|
||||
|
||||
#if !NET472
|
||||
[SupportedOSPlatform("windows")]
|
||||
#endif
|
||||
public class RhSolutionsFunctions
|
||||
public static class RhSolutionsFunctions
|
||||
{
|
||||
private static IDatabaseClient databaseClient = RhSolutionsAddIn.ServiceProvider.GetService<IDatabaseClient>();
|
||||
private static ICurrencyClient currencyClient = RhSolutionsAddIn.ServiceProvider.GetRequiredService<ICurrencyClient>();
|
||||
|
||||
[ExcelFunction(Description = "Поиск артикула в базе данных")]
|
||||
public static object RHSOLUTIONS([ExcelArgument(Name = "СТРОКА", Description = "Ячейка с артикулом РЕХАУ или поисковый запрос в свободной форме")] string line)
|
||||
public static object РЕХАУ([ExcelArgument(Name = "СТРОКА", Description = "Ячейка с артикулом РЕХАУ или поисковый запрос в свободной форме")] string query)
|
||||
{
|
||||
IDatabaseClient databaseClient = RhSolutionsAddIn.ServiceProvider.GetService<IDatabaseClient>();
|
||||
ProductSku.TryParse(query, out var skus);
|
||||
|
||||
ProductSku.TryParse(line, out var skus);
|
||||
|
||||
if (ExcelAsyncUtil.Run(nameof(RHSOLUTIONS), line, delegate
|
||||
if (ExcelAsyncUtil.Run(nameof(РЕХАУ), query, delegate
|
||||
{
|
||||
return databaseClient.GetProducts(line)
|
||||
return databaseClient.GetProducts(query)
|
||||
.GetAwaiter()
|
||||
.GetResult();
|
||||
}) is not IEnumerable<Product> requestResult)
|
||||
@ -53,14 +45,10 @@ public class RhSolutionsFunctions
|
||||
}
|
||||
}
|
||||
|
||||
[ExcelFunction(Description = "Поиск артикула в базе данных")]
|
||||
public static object РЕХАУ([ExcelArgument(Name = "СТРОКА", Description = "Ячейка с артикулом РЕХАУ или поисковый запрос в свободной форме")] string line)
|
||||
=> RHSOLUTIONS(line);
|
||||
|
||||
[ExcelFunction(Description = "Выделить артикул РЕХАУ из ячейки")]
|
||||
public static object РЕХАУАРТИКУЛ([ExcelArgument(Name = "СТРОКА", Description = "Ячейка содержащая артикул РЕХАУ")] string line)
|
||||
public static object РЕХАУАРТИКУЛ([ExcelArgument(Name = "СТРОКА", Description = "Ячейка содержащая артикул РЕХАУ")] string query)
|
||||
{
|
||||
if (ProductSku.TryParse(line, out var skus))
|
||||
if (ProductSku.TryParse(query, out var skus))
|
||||
{
|
||||
return skus.First().Id;
|
||||
}
|
||||
@ -71,18 +59,17 @@ public class RhSolutionsFunctions
|
||||
}
|
||||
|
||||
[ExcelFunction(Description = "Поиск названия по артикулу РЕХАУ")]
|
||||
public static object РЕХАУИМЯ([ExcelArgument(Name = "АРТИКУЛ", Description = "Ячейка содержащая артикул РЕХАУ")] string line)
|
||||
public static object РЕХАУИМЯ([ExcelArgument(Name = "АРТИКУЛ", Description = "Ячейка содержащая артикул РЕХАУ")] string query)
|
||||
{
|
||||
if (!ProductSku.TryParse(line, out var skus))
|
||||
if (!ProductSku.TryParse(query, out var skus))
|
||||
{
|
||||
return ExcelError.ExcelErrorNA;
|
||||
}
|
||||
else
|
||||
{
|
||||
var article = skus.First().Id;
|
||||
IDatabaseClient databaseClient = RhSolutionsAddIn.ServiceProvider.GetService<IDatabaseClient>();
|
||||
|
||||
if (ExcelAsyncUtil.Run(nameof(РЕХАУИМЯ), line, delegate
|
||||
if (ExcelAsyncUtil.Run(nameof(РЕХАУИМЯ), query, delegate
|
||||
{
|
||||
return databaseClient.GetProducts(article)
|
||||
.GetAwaiter()
|
||||
@ -107,18 +94,17 @@ public class RhSolutionsFunctions
|
||||
}
|
||||
|
||||
[ExcelFunction(Description = "Поиск цены артикула РЕХАУ в евро")]
|
||||
public static object РЕХАУЦЕНА([ExcelArgument(Name = "АРТИКУЛ", Description = "Ячейка содержащая артикул РЕХАУ")] string line)
|
||||
public static object РЕХАУЦЕНА([ExcelArgument(Name = "АРТИКУЛ", Description = "Ячейка содержащая артикул РЕХАУ")] string query)
|
||||
{
|
||||
if (!ProductSku.TryParse(line, out var skus))
|
||||
if (!ProductSku.TryParse(query, out var skus))
|
||||
{
|
||||
return ExcelError.ExcelErrorNA;
|
||||
}
|
||||
else
|
||||
{
|
||||
var article = skus.First().Id;
|
||||
IDatabaseClient databaseClient = RhSolutionsAddIn.ServiceProvider.GetService<IDatabaseClient>();
|
||||
|
||||
if (ExcelAsyncUtil.Run(nameof(РЕХАУЦЕНА), line, delegate
|
||||
if (ExcelAsyncUtil.Run(nameof(РЕХАУЦЕНА), query, delegate
|
||||
{
|
||||
return databaseClient.GetProducts(article)
|
||||
.GetAwaiter()
|
||||
@ -152,37 +138,22 @@ public class RhSolutionsFunctions
|
||||
}
|
||||
else
|
||||
{
|
||||
IDatabaseClient databaseClient = RhSolutionsAddIn.ServiceProvider.GetService<IDatabaseClient>();
|
||||
ICurrencyClient currencyClient = RhSolutionsAddIn.ServiceProvider.GetRequiredService<ICurrencyClient>();
|
||||
IMemoryCache memoryCache = RhSolutionsAddIn.ServiceProvider.GetRequiredService<IMemoryCache>();
|
||||
var article = skus.First().Id;
|
||||
DateTime date = dateField == 0 ? DateTime.Today : DateTime.FromOADate(dateField);
|
||||
|
||||
if (!memoryCache.TryGetValue(date, out decimal exchangeRate))
|
||||
if (ExcelAsyncUtil.Run(nameof(КУРСЕВРО), dateField, delegate
|
||||
{
|
||||
var result = ExcelAsyncUtil.Run(nameof(КУРСЕВРО), dateField, delegate
|
||||
{
|
||||
var requestResult = currencyClient.GetExchangeRate(date)
|
||||
.GetAwaiter()
|
||||
.GetResult();
|
||||
var requestResult = currencyClient.GetExchangeRate(date)
|
||||
.GetAwaiter()
|
||||
.GetResult();
|
||||
|
||||
return requestResult ?? -1m;
|
||||
});
|
||||
|
||||
if (result is not decimal)
|
||||
{
|
||||
return "Загрузка...";
|
||||
}
|
||||
else
|
||||
{
|
||||
exchangeRate = (decimal)result;
|
||||
var cacheEntryOptions = new MemoryCacheEntryOptions()
|
||||
.SetSlidingExpiration(TimeSpan.FromHours(1));
|
||||
memoryCache.Set(date, exchangeRate, cacheEntryOptions);
|
||||
}
|
||||
return requestResult ?? -1m;
|
||||
}) is not decimal exchangeRate)
|
||||
{
|
||||
return "Загрузка...";
|
||||
}
|
||||
|
||||
if (ExcelAsyncUtil.Run(nameof(РЕХАУЦЕНАРУБ), line, delegate
|
||||
else if (ExcelAsyncUtil.Run(nameof(РЕХАУЦЕНАРУБ), line, delegate
|
||||
{
|
||||
var product = databaseClient.GetProducts(article)
|
||||
.GetAwaiter()
|
||||
@ -190,7 +161,7 @@ public class RhSolutionsFunctions
|
||||
.FirstOrDefault();
|
||||
|
||||
return product == null ? -1m :
|
||||
product.Price * exchangeRate * 1.2m;
|
||||
product.Price * (decimal)exchangeRate * 1.2m;
|
||||
}) is not decimal requestResult)
|
||||
{
|
||||
return "Загрузка...";
|
||||
@ -212,34 +183,22 @@ public class RhSolutionsFunctions
|
||||
[ExcelFunction(Description = "Получить курс евро по ЦБ")]
|
||||
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))
|
||||
var exchangeRate = ExcelAsyncUtil.Run(nameof(КУРСЕВРО), dateField, delegate
|
||||
{
|
||||
var result = ExcelAsyncUtil.Run(nameof(КУРСЕВРО), dateField, delegate
|
||||
{
|
||||
var requestResult = currencyClient.GetExchangeRate(date)
|
||||
.GetAwaiter()
|
||||
.GetResult();
|
||||
var requestResult = currencyClient.GetExchangeRate(date)
|
||||
.GetAwaiter()
|
||||
.GetResult();
|
||||
|
||||
return requestResult ?? -1m;
|
||||
});
|
||||
return requestResult ?? -1m;
|
||||
});
|
||||
|
||||
if (result is not decimal)
|
||||
{
|
||||
return "Загрузка...";
|
||||
}
|
||||
else
|
||||
{
|
||||
exchangeRate = (decimal)result;
|
||||
var cacheEntryOptions = new MemoryCacheEntryOptions()
|
||||
.SetSlidingExpiration(TimeSpan.FromHours(1));
|
||||
memoryCache.Set(date, exchangeRate, cacheEntryOptions);
|
||||
}
|
||||
if (exchangeRate is not decimal)
|
||||
{
|
||||
return "Загрузка...";
|
||||
}
|
||||
|
||||
return exchangeRate < 0 ? ExcelError.ExcelErrorNA : exchangeRate;
|
||||
return (decimal)exchangeRate < 0 ? ExcelError.ExcelErrorNA : exchangeRate;
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user