RhSolutions-AddIn/RhSolutions.AddIn/AddIn/RhSolutionsFunction.cs

57 lines
1.8 KiB
C#
Raw Normal View History

using ExcelDna.Integration;
2023-03-27 13:55:58 +03:00
using Microsoft.Extensions.DependencyInjection;
2023-03-27 15:52:28 +03:00
using RhSolutions.Models;
2022-12-20 12:41:46 +03:00
using RhSolutions.Services;
2023-03-27 15:52:28 +03:00
using System.Collections.Generic;
using System.Linq;
2021-11-11 16:33:40 +03:00
2022-12-20 12:41:46 +03:00
namespace RhSolutions.AddIn
2021-11-11 16:33:40 +03:00
{
2023-03-27 15:52:28 +03:00
public class RhSolutionsFunction
2021-11-11 16:33:40 +03:00
{
2022-12-28 15:32:26 +03:00
[ExcelFunction(Description = "Распознать артикул и попробовать найти его в прайс-листе")]
public static object RHSOLUTIONS([ExcelArgument(Name = "\"Строка с названием материала\"")] string line)
2022-12-19 09:13:14 +03:00
{
2023-03-27 13:55:58 +03:00
IDatabaseClient databaseClient = RhSolutionsAddIn.ServiceProvider.GetService<IDatabaseClient>();
2023-03-27 15:52:28 +03:00
IEnumerable<Product> requestResult = ExcelAsyncUtil.Run("Database request", line, delegate
2022-12-19 09:13:14 +03:00
{
2023-03-27 15:52:28 +03:00
return databaseClient.GetProducts(line)
.GetAwaiter()
.GetResult();
}) as IEnumerable<Product>;
2022-12-19 09:13:14 +03:00
2023-03-27 15:52:28 +03:00
Sku.TryParse(line, out var skus);
2023-03-27 15:52:28 +03:00
if (requestResult == null)
2022-12-19 09:13:14 +03:00
{
2023-03-27 15:52:28 +03:00
if (skus.Count() > 0)
{
2023-03-27 15:52:28 +03:00
return $"{skus.First()} ...";
}
else
{
2023-03-27 15:52:28 +03:00
return "Загрузка...";
}
2022-12-19 09:13:14 +03:00
}
2023-03-27 15:52:28 +03:00
else
2022-12-19 09:13:14 +03:00
{
2023-03-27 15:52:28 +03:00
if (requestResult.Count() == 0 && skus.Count() == 0)
{
2023-03-27 15:52:28 +03:00
return ExcelError.ExcelErrorNA;
}
2023-03-27 15:52:28 +03:00
else if (requestResult.Count() == 0)
{
return $"{skus.First()}";
}
else
{
2023-03-27 15:52:28 +03:00
var firstProduct = requestResult.First();
return $"{firstProduct.ProductSku} {firstProduct.Name}";
}
2022-12-19 09:13:14 +03:00
}
}
2022-12-19 09:13:14 +03:00
}
2021-11-11 16:33:40 +03:00
}