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

166 lines
5.0 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.

using ExcelDna.Registration.Utils;
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]
public static object РЕХАУ(string query)
{
var functionName = nameof(РЕХАУ);
var parameters = new object[] { query };
if (AsyncTaskUtil.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]
public static object РЕХАУАРТИКУЛ(string query)
{
if (ProductSku.TryParse(query, out var skus))
{
return skus.First().Id;
}
return ExcelError.ExcelErrorNA;
}
[ExcelFunction]
public static object РЕХАУИМЯ(string query)
{
if (!ProductSku.TryParse(query, out var skus))
{
return ExcelError.ExcelErrorNA;
}
var article = skus.First().Id;
var functionName = nameof(РЕХАУИМЯ);
var parameters = new object[] { query };
if (AsyncTaskUtil.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]
public static object РЕХАУЦЕНА(string query)
{
if (!ProductSku.TryParse(query, out var skus))
{
return ExcelError.ExcelErrorNA;
}
var article = skus.First().Id;
var functionName = nameof(РЕХАУЦЕНА);
var parameters = new object[] { article };
if (AsyncTaskUtil.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]
public static object РЕХАУЦЕНАРУБ(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(РЕХАУЦЕНАРУБ);
var parameters = new object[] { date };
if (AsyncTaskUtil.RunTask(functionName, parameters, async () =>
{
var requestResult = await currencyClient.GetExchangeRate(date);
return requestResult ?? -1m;
}) is not decimal exchangeRate)
{
return "Загрузка...";
}
parameters = new object[] { query };
if (AsyncTaskUtil.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);
}
}
[ExcelFunction]
public static object КУРСЕВРО(double dateField)
{
DateTime date = dateField == 0 ? DateTime.Today : DateTime.FromOADate(dateField);
var functionName = nameof(КУРСЕВРО);
var parameters = new object[] { date };
var exchangeRate = AsyncTaskUtil.RunTask(functionName, parameters, async () =>
{
var requestResult = await currencyClient.GetExchangeRate(date);
return requestResult ?? -1m;
});
if (exchangeRate is not decimal)
{
return "Загрузка...";
}
return (decimal)exchangeRate < 0 ? ExcelError.ExcelErrorNA : exchangeRate;
}
}