using ExcelDna.Integration; using Microsoft.Extensions.DependencyInjection; using RhSolutions.Models; using RhSolutions.Services; using System.Collections.Generic; using System.Linq; namespace RhSolutions.AddIn { public class RhSolutionsFunction { [ExcelFunction(Description = "Распознать артикул и попробовать найти его в прайс-листе")] public static object RHSOLUTIONS([ExcelArgument(Name = "\"Строка с названием материала\"")] string line) { IDatabaseClient databaseClient = RhSolutionsAddIn.ServiceProvider.GetService(); IEnumerable requestResult = ExcelAsyncUtil.Run("Database request", line, delegate { return databaseClient.GetProducts(line) .GetAwaiter() .GetResult(); }) as IEnumerable; 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}"; } } } } }