RhSolutions-AddIn/RhSolutions.AddIn/AddIn/RhSolutionsFunction.cs
2023-03-28 10:36:36 +03:00

49 lines
1.5 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 class RhSolutionsFunction
{
[ExcelFunction(Description = "Распознать артикул и попробовать найти его в прайс-листе")]
public static object RHSOLUTIONS([ExcelArgument(Name = "\"Строка с названием материала\"")] string line)
{
IDatabaseClient databaseClient = RhSolutionsAddIn.ServiceProvider.GetService<IDatabaseClient>();
IEnumerable<Product> requestResult = ExcelAsyncUtil.Run("Database request", line, delegate
{
return databaseClient.GetProducts(line)
.GetAwaiter()
.GetResult();
}) as IEnumerable<Product>;
Sku.TryParse(line, out var skus);
if (requestResult == null)
{
if (skus.Count() > 0)
{
return $"{skus.First()} ...";
}
else
{
return "Загрузка...";
}
}
else
{
if (requestResult.Count() == 0 && skus.Count() == 0)
{
return ExcelError.ExcelErrorNA;
}
else if (requestResult.Count() == 0)
{
return $"{skus.First()}";
}
else
{
var firstProduct = requestResult.First();
return $"{firstProduct.ProductSku} {firstProduct.Name}";
}
}
}
}