RhSolutions-AddIn/RhSolutions.AddIn/AddIn/RhSolutionsFunctions.cs

203 lines
5.7 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

namespace RhSolutions.AddIn;
public static class RhSolutionsFunctions
{
private static IDatabaseClient databaseClient = RhSolutionsAddIn.ServiceProvider.GetService<IDatabaseClient>();
private static ICurrencyClient currencyClient = RhSolutionsAddIn.ServiceProvider.GetRequiredService<ICurrencyClient>();
[ExcelFunction]
public static object РЕХАУ(string query)
{
ProductSku.TryParse(query, out var skus);
if (ExcelAsyncUtil.Run(nameof(РЕХАУ), query, delegate
{
return databaseClient.GetProducts(query)
.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]
public static object РЕХАУАРТИКУЛ(string query)
{
if (ProductSku.TryParse(query, out var skus))
{
return skus.First().Id;
}
else
{
return ExcelError.ExcelErrorNA;
}
}
[ExcelFunction]
public static object РЕХАУИМЯ(string query)
{
if (!ProductSku.TryParse(query, out var skus))
{
return ExcelError.ExcelErrorNA;
}
else
{
var article = skus.First().Id;
if (ExcelAsyncUtil.Run(nameof(РЕХАУИМЯ), query, 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]
public static object РЕХАУЦЕНА(string query)
{
if (!ProductSku.TryParse(query, out var skus))
{
return ExcelError.ExcelErrorNA;
}
else
{
var article = skus.First().Id;
if (ExcelAsyncUtil.Run(nameof(РЕХАУЦЕНА), query, 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]
public static object РЕХАУЦЕНАРУБ(string line, double dateField)
{
if (!ProductSku.TryParse(line, out var skus))
{
return ExcelError.ExcelErrorNA;
}
else
{
var article = skus.First().Id;
DateTime date = dateField == 0 ? DateTime.Today : DateTime.FromOADate(dateField);
if (ExcelAsyncUtil.Run(nameof(КУРСЕВРО), dateField, delegate
{
var requestResult = currencyClient.GetExchangeRate(date)
.GetAwaiter()
.GetResult();
return requestResult ?? -1m;
}) is not decimal exchangeRate)
{
return "Загрузка...";
}
else if (ExcelAsyncUtil.Run(nameof(РЕХАУЦЕНАРУБ), line, delegate
{
var product = databaseClient.GetProducts(article)
.GetAwaiter()
.GetResult()
.FirstOrDefault();
return product == null ? -1m :
product.Price * (decimal)exchangeRate * 1.2m;
}) is not decimal requestResult)
{
return "Загрузка...";
}
else
{
if (requestResult < 0)
{
return ExcelError.ExcelErrorNA;
}
else
{
return Math.Round(requestResult, 2);
}
}
}
}
[ExcelFunction]
public static object КУРСЕВРО(double dateField)
{
DateTime date = dateField == 0 ? DateTime.Today : DateTime.FromOADate(dateField);
var exchangeRate = ExcelAsyncUtil.Run(nameof(КУРСЕВРО), dateField, delegate
{
var requestResult = currencyClient.GetExchangeRate(date)
.GetAwaiter()
.GetResult();
return requestResult ?? -1m;
});
if (exchangeRate is not decimal)
{
return "Загрузка...";
}
return (decimal)exchangeRate < 0 ? ExcelError.ExcelErrorNA : exchangeRate;
}
}