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

145 lines
4.4 KiB
C#
Raw Normal View History

using ExcelDna.Registration.Utils;
namespace RhSolutions.AddIn;
2021-11-11 16:33:40 +03:00
2024-01-18 23:16:38 +03:00
public static class RhSolutionsFunctions
2021-11-11 16:33:40 +03:00
{
private static readonly IDatabaseClient databaseClient =
RhSolutionsAddIn.ServiceProvider.GetService<IDatabaseClient>();
private static readonly ICurrencyClient currencyClient =
RhSolutionsAddIn.ServiceProvider.GetRequiredService<ICurrencyClient>();
2024-01-18 23:16:38 +03:00
2024-02-14 23:20:08 +03:00
[ExcelFunction(Name = "РЕХАУ")]
public static object ProductSearch(string query)
2021-11-11 16:33:40 +03:00
{
2024-02-14 23:20:08 +03:00
var functionName = nameof(ProductSearch);
var parameters = new object[] { query };
if (AsyncTaskUtil.RunTask(functionName, parameters, async () =>
2022-12-19 09:13:14 +03:00
{
return await databaseClient.GetProducts(query);
}) is not IEnumerable<Product> products)
2023-03-28 10:36:36 +03:00
{
return "Загрузка...";
}
else if (!products.Any())
{
return ExcelError.ExcelErrorNA;
2023-03-28 10:36:36 +03:00
}
else
{
var product = products.First();
return $"{product.Id} {product.Name}";
}
2022-12-19 09:13:14 +03:00
}
2024-02-14 23:20:08 +03:00
[ExcelFunction(Name = "РЕХАУАРТИКУЛ")]
public static object SkuSearch(string query)
{
2024-01-18 23:16:38 +03:00
if (ProductSku.TryParse(query, out var skus))
{
return skus.First().Id;
}
return ExcelError.ExcelErrorNA;
}
2024-02-14 23:20:08 +03:00
[ExcelFunction(Name = "РЕХАУИМЯ")]
public static object GetProductName(string query)
{
2024-01-18 23:16:38 +03:00
if (!ProductSku.TryParse(query, out var skus))
{
return ExcelError.ExcelErrorNA;
}
var article = skus.First().Id;
2024-02-14 23:20:08 +03:00
var functionName = nameof(GetProductName);
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;
}
}
2024-02-14 23:20:08 +03:00
[ExcelFunction(Name = "РЕХАУЦЕНА")]
public static object GetProductPrice(string query)
{
2024-01-18 23:16:38 +03:00
if (!ProductSku.TryParse(query, out var skus))
{
return ExcelError.ExcelErrorNA;
}
var article = skus.First().Id;
2024-02-14 23:20:08 +03:00
var functionName = nameof(GetProductPrice);
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);
}
}
2024-02-14 23:20:08 +03:00
[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);
2024-02-14 23:20:08 +03:00
var functionName = nameof(GetProductPriceRub);
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);
}
}
2021-11-11 16:33:40 +03:00
}