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

141 lines
4.0 KiB
C#
Raw Normal View History

2023-04-06 08:29:39 +03:00
#if !NET472
using System.Runtime.Versioning;
#endif
2021-11-11 16:33:40 +03:00
2023-04-06 08:29:39 +03:00
namespace RhSolutions.AddIn;
#if !NET472
[SupportedOSPlatform("windows")]
#endif
2023-03-28 10:36:36 +03:00
public class RhSolutionsFunction
2021-11-11 16:33:40 +03:00
{
2023-03-28 10:36:36 +03:00
[ExcelFunction(Description = "Распознать артикул и попробовать найти его в прайс-листе")]
public static object RHSOLUTIONS([ExcelArgument(Name = "\"Строка с названием материала\"")] string line)
2021-11-11 16:33:40 +03:00
{
2023-03-28 10:36:36 +03:00
IDatabaseClient databaseClient = RhSolutionsAddIn.ServiceProvider.GetService<IDatabaseClient>();
2023-04-06 08:29:39 +03:00
ProductSku.TryParse(line, out var skus);
2023-04-06 08:29:39 +03:00
if (ExcelAsyncUtil.Run("Database request", line, delegate
2022-12-19 09:13:14 +03:00
{
2023-03-28 10:36:36 +03:00
return databaseClient.GetProducts(line)
.GetAwaiter()
.GetResult();
2023-04-06 08:29:39 +03:00
}) is not IEnumerable<Product> requestResult)
2023-03-28 10:36:36 +03:00
{
2023-04-06 08:29:39 +03:00
if (skus.Any())
2022-12-19 09:13:14 +03:00
{
2023-03-28 10:36:36 +03:00
return $"{skus.First()} ...";
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-28 10:36:36 +03:00
return "Загрузка...";
}
}
2023-03-27 15:52:28 +03:00
2023-03-28 10:36:36 +03:00
else
{
2023-04-06 08:29:39 +03:00
if (!requestResult.Any() && !skus.Any())
2023-03-28 10:36:36 +03:00
{
return ExcelError.ExcelErrorNA;
}
2023-04-06 08:29:39 +03:00
else if (!requestResult.Any())
2023-03-28 10:36:36 +03:00
{
return $"{skus.First()}";
}
2023-03-27 15:52:28 +03:00
2023-03-28 10:36:36 +03:00
else
{
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
}
[ExcelFunction]
public static object РЕХАУ(string line) => RHSOLUTIONS(line);
[ExcelFunction]
public static object РЕХАУАРТИКУЛ(string line)
{
if (ProductSku.TryParse(line, out var skus))
{
return skus.First().Id;
}
else
{
return ExcelError.ExcelErrorNA;
}
}
[ExcelFunction]
public static object РЕХАУИМЯ(string line)
{
if (!ProductSku.TryParse(line, out var skus))
{
return ExcelError.ExcelErrorNA;
}
else
{
var article = skus.First().Id;
IDatabaseClient databaseClient = RhSolutionsAddIn.ServiceProvider.GetService<IDatabaseClient>();
if (ExcelAsyncUtil.Run("Database request", line, delegate
{
return databaseClient.GetProducts(article)
.GetAwaiter()
.GetResult();
}) 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 line)
{
if (!ProductSku.TryParse(line, out var skus))
{
return ExcelError.ExcelErrorNA;
}
else
{
var article = skus.First().Id;
IDatabaseClient databaseClient = RhSolutionsAddIn.ServiceProvider.GetService<IDatabaseClient>();
if (ExcelAsyncUtil.Run("Database request", line, delegate
{
return databaseClient.GetProducts(article)
.GetAwaiter()
.GetResult();
}) is not IEnumerable<Product> requestResult)
{
return "Загрузка...";
}
else
{
if (!requestResult.Any())
{
return ExcelError.ExcelErrorNA;
}
else
{
var firstProduct = requestResult.First();
return string.Format("{0:N2} €", firstProduct.Price * 1.2m);
}
}
}
}
2021-11-11 16:33:40 +03:00
}