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

143 lines
4.4 KiB
C#
Raw Permalink 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 readonly IDatabaseClient databaseClient =
RhSolutionsAddIn.ServiceProvider.GetService<IDatabaseClient>();
private static readonly ICurrencyClient currencyClient =
RhSolutionsAddIn.ServiceProvider.GetRequiredService<ICurrencyClient>();
[ExcelFunction(Name = "РЕХАУ")]
public static object ProductSearch(string query)
{
var functionName = nameof(ProductSearch);
var parameters = new object[] { query };
if (ExcelAsyncUtil.RunTask(functionName, parameters, async () =>
{
return await databaseClient.GetProducts(query);
}) is not IEnumerable<Product> products)
{
return "Загрузка...";
}
else if (!products.Any())
{
return ExcelError.ExcelErrorNA;
}
else
{
var product = products.First();
return $"{product.Id} {product.Name}";
}
}
[ExcelFunction(Name = "РЕХАУАРТИКУЛ")]
public static object SkuSearch(string query)
{
if (ProductSku.TryParse(query, out var skus))
{
return skus.First().Id;
}
return ExcelError.ExcelErrorNA;
}
[ExcelFunction(Name = "РЕХАУИМЯ")]
public static object GetProductName(string query)
{
if (!ProductSku.TryParse(query, out var skus))
{
return ExcelError.ExcelErrorNA;
}
var article = skus.First().Id;
var functionName = nameof(GetProductName);
var parameters = new object[] { query };
if (ExcelAsyncUtil.RunTask(functionName, parameters, async () =>
{
return await databaseClient.GetProducts(article);
}) is not IEnumerable<Product> requestResult)
{
return "Загрузка...";
}
else if (!requestResult.Any())
{
return ExcelError.ExcelErrorNA;
}
else
{
var firstProduct = requestResult.First();
return firstProduct.Name;
}
}
[ExcelFunction(Name = "РЕХАУЦЕНА")]
public static object GetProductPrice(string query)
{
if (!ProductSku.TryParse(query, out var skus))
{
return ExcelError.ExcelErrorNA;
}
var article = skus.First().Id;
var functionName = nameof(GetProductPrice);
var parameters = new object[] { article };
if (ExcelAsyncUtil.RunTask(functionName, parameters, async () =>
{
return await databaseClient.GetProducts(article);
}) 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(Name = "РЕХАУЦЕНАРУБ")]
public static object GetProductPriceRub(string query, double dateField)
{
if (!ProductSku.TryParse(query, out var skus))
{
return ExcelError.ExcelErrorNA;
}
var article = skus.First().Id;
DateTime date = dateField == 0 ? DateTime.Today : DateTime.FromOADate(dateField);
var functionName = nameof(GetProductPriceRub);
var parameters = new object[] { date };
if (ExcelAsyncUtil.RunTask(functionName, parameters, async () =>
{
var requestResult = await currencyClient.GetExchangeRate(date);
return requestResult ?? -1m;
}) is not decimal exchangeRate)
{
return "Загрузка...";
}
parameters = new object[] { query };
if (ExcelAsyncUtil.RunTask(functionName, parameters, async () =>
{
var products = await databaseClient.GetProducts(article);
var product = products.FirstOrDefault();
return product == null ? -1m :
product.Price * (decimal)exchangeRate * 1.2m;
}) is not decimal requestResult)
{
return "Загрузка...";
}
else if (requestResult < 0 || exchangeRate < 0)
{
return ExcelError.ExcelErrorNA;
}
else
{
return Math.Round(requestResult, 2);
}
}
}