245 lines
9.0 KiB
C#
245 lines
9.0 KiB
C#
#if !NET472
|
||
using System.Runtime.Versioning;
|
||
#endif
|
||
|
||
using Microsoft.Extensions.Caching.Memory;
|
||
|
||
namespace RhSolutions.AddIn;
|
||
|
||
#if !NET472
|
||
[SupportedOSPlatform("windows")]
|
||
#endif
|
||
public class RhSolutionsFunctions
|
||
{
|
||
[ExcelFunction(Description = "Поиск артикула в базе данных")]
|
||
public static object RHSOLUTIONS([ExcelArgument(Name = "СТРОКА", Description = "Ячейка с артикулом РЕХАУ или поисковый запрос в свободной форме")] string line)
|
||
{
|
||
IDatabaseClient databaseClient = RhSolutionsAddIn.ServiceProvider.GetService<IDatabaseClient>();
|
||
|
||
ProductSku.TryParse(line, out var skus);
|
||
|
||
if (ExcelAsyncUtil.Run(nameof(RHSOLUTIONS), line, delegate
|
||
{
|
||
return databaseClient.GetProducts(line)
|
||
.GetAwaiter()
|
||
.GetResult();
|
||
}) is not IEnumerable<Product> requestResult)
|
||
{
|
||
if (skus.Any())
|
||
{
|
||
return $"{skus.First()} ...";
|
||
}
|
||
else
|
||
{
|
||
return "Загрузка...";
|
||
}
|
||
}
|
||
|
||
else
|
||
{
|
||
if (!requestResult.Any() && !skus.Any())
|
||
{
|
||
return ExcelError.ExcelErrorNA;
|
||
}
|
||
else if (!requestResult.Any())
|
||
{
|
||
return $"{skus.First()}";
|
||
}
|
||
else
|
||
{
|
||
var firstProduct = requestResult.First();
|
||
return $"{firstProduct.ProductSku} {firstProduct.Name}";
|
||
}
|
||
}
|
||
}
|
||
|
||
[ExcelFunction(Description = "Поиск артикула в базе данных")]
|
||
public static object РЕХАУ([ExcelArgument(Name = "СТРОКА", Description = "Ячейка с артикулом РЕХАУ или поисковый запрос в свободной форме")] string line)
|
||
=> RHSOLUTIONS(line);
|
||
|
||
[ExcelFunction(Description = "Выделить артикул РЕХАУ из ячейки")]
|
||
public static object РЕХАУАРТИКУЛ([ExcelArgument(Name = "СТРОКА", Description = "Ячейка содержащая артикул РЕХАУ")] string line)
|
||
{
|
||
if (ProductSku.TryParse(line, out var skus))
|
||
{
|
||
return skus.First().Id;
|
||
}
|
||
else
|
||
{
|
||
return ExcelError.ExcelErrorNA;
|
||
}
|
||
}
|
||
|
||
[ExcelFunction(Description = "Поиск названия по артикулу РЕХАУ")]
|
||
public static object РЕХАУИМЯ([ExcelArgument(Name = "АРТИКУЛ", Description = "Ячейка содержащая артикул РЕХАУ")] string line)
|
||
{
|
||
if (!ProductSku.TryParse(line, out var skus))
|
||
{
|
||
return ExcelError.ExcelErrorNA;
|
||
}
|
||
else
|
||
{
|
||
var article = skus.First().Id;
|
||
IDatabaseClient databaseClient = RhSolutionsAddIn.ServiceProvider.GetService<IDatabaseClient>();
|
||
|
||
if (ExcelAsyncUtil.Run(nameof(РЕХАУИМЯ), line, delegate
|
||
{
|
||
return databaseClient.GetProducts(article)
|
||
.GetAwaiter()
|
||
.GetResult();
|
||
}) is not IEnumerable<Product> requestResult)
|
||
{
|
||
return "Загрузка...";
|
||
}
|
||
else
|
||
{
|
||
if (!requestResult.Any())
|
||
{
|
||
return ExcelError.ExcelErrorNA;
|
||
}
|
||
else
|
||
{
|
||
var firstProduct = requestResult.First();
|
||
return firstProduct.Name;
|
||
}
|
||
}
|
||
}
|
||
}
|
||
|
||
[ExcelFunction(Description = "Поиск цены артикула РЕХАУ в евро")]
|
||
public static object РЕХАУЦЕНА([ExcelArgument(Name = "АРТИКУЛ", Description = "Ячейка содержащая артикул РЕХАУ")] string line)
|
||
{
|
||
if (!ProductSku.TryParse(line, out var skus))
|
||
{
|
||
return ExcelError.ExcelErrorNA;
|
||
}
|
||
else
|
||
{
|
||
var article = skus.First().Id;
|
||
IDatabaseClient databaseClient = RhSolutionsAddIn.ServiceProvider.GetService<IDatabaseClient>();
|
||
|
||
if (ExcelAsyncUtil.Run(nameof(РЕХАУЦЕНА), line, delegate
|
||
{
|
||
return databaseClient.GetProducts(article)
|
||
.GetAwaiter()
|
||
.GetResult();
|
||
}) is not IEnumerable<Product> requestResult)
|
||
{
|
||
return "Загрузка...";
|
||
}
|
||
else
|
||
{
|
||
if (!requestResult.Any())
|
||
{
|
||
return ExcelError.ExcelErrorNA;
|
||
}
|
||
else
|
||
{
|
||
var firstProduct = requestResult.First();
|
||
return Math.Round(firstProduct.Price * 1.2m, 2);
|
||
}
|
||
}
|
||
}
|
||
}
|
||
|
||
[ExcelFunction(Description = "Поиск цены артикула РЕХАУ в рублях")]
|
||
public static object РЕХАУЦЕНАРУБ([ExcelArgument(Name = "АРТИКУЛ", Description = "Ячейка содержащая артикул РЕХАУ")] string line,
|
||
[ExcelArgument(Name = "ДАТА", Description = "Дата в формате Excel (необязательно)")] double dateField)
|
||
{
|
||
if (!ProductSku.TryParse(line, out var skus))
|
||
{
|
||
return ExcelError.ExcelErrorNA;
|
||
}
|
||
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))
|
||
{
|
||
var result = ExcelAsyncUtil.Run(nameof(КУРСЕВРО), dateField, delegate
|
||
{
|
||
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);
|
||
}
|
||
}
|
||
|
||
if (ExcelAsyncUtil.Run(nameof(РЕХАУЦЕНАРУБ), line, delegate
|
||
{
|
||
var product = databaseClient.GetProducts(article)
|
||
.GetAwaiter()
|
||
.GetResult()
|
||
.FirstOrDefault();
|
||
|
||
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);
|
||
}
|
||
}
|
||
}
|
||
}
|
||
|
||
[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 result = ExcelAsyncUtil.Run(nameof(КУРСЕВРО), dateField, delegate
|
||
{
|
||
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 exchangeRate < 0 ? ExcelError.ExcelErrorNA : exchangeRate;
|
||
}
|
||
} |