Search for range vaules in Product search function

This commit is contained in:
Serghei Cebotari 2024-11-06 22:46:44 +03:00
parent 06e47d3135
commit a73b2441b3

View File

@ -2,142 +2,155 @@
public static class RhSolutionsFunctions public static class RhSolutionsFunctions
{ {
private static readonly IDatabaseClient databaseClient = private static readonly IDatabaseClient databaseClient =
RhSolutionsAddIn.ServiceProvider.GetService<IDatabaseClient>(); RhSolutionsAddIn.ServiceProvider.GetService<IDatabaseClient>();
private static readonly ICurrencyClient currencyClient = private static readonly ICurrencyClient currencyClient =
RhSolutionsAddIn.ServiceProvider.GetRequiredService<ICurrencyClient>(); RhSolutionsAddIn.ServiceProvider.GetRequiredService<ICurrencyClient>();
[ExcelFunction(Name = "РЕХАУ")] [ExcelFunction(Name = "РЕХАУ")]
public static object ProductSearch(string query) public static object ProductSearch(object[,] values)
{ {
var functionName = nameof(ProductSearch); List<string> strings = new();
var parameters = new object[] { query }; int rows = values.GetLength(0);
if (ExcelAsyncUtil.RunTask(functionName, parameters, async () => int columns = values.GetLength(1);
{ for (int row = 0; row < rows; row++)
return await databaseClient.GetProducts(query); {
}) is not IEnumerable<Product> products) for (int column = 0; column < columns; column++)
{ {
return "Загрузка..."; object value = values[row, column];
} strings.Add(value.ToString());
else if (!products.Any()) }
{ }
return ExcelError.ExcelErrorNA;
} string query = string.Join(" ", strings.ToArray());
else var functionName = nameof(ProductSearch);
{ var parameters = new object[] { query };
var product = products.First(); if (ExcelAsyncUtil.RunTask(functionName, parameters, async () =>
return $"{product.Id} {product.Name}"; {
} return await databaseClient.GetProducts(query);
} }) is not IEnumerable<Product> products)
{
return "Загрузка...";
}
else if (!products.Any())
{
return ExcelError.ExcelErrorNA;
}
else
{
var product = products.First();
return $"{product.Id} {product.Name}";
}
}
[ExcelFunction(Name = "РЕХАУАРТИКУЛ")] [ExcelFunction(Name = "РЕХАУАРТИКУЛ")]
public static object SkuSearch(string query) public static object SkuSearch(string query)
{ {
if (ProductSku.TryParse(query, out var skus)) if (ProductSku.TryParse(query, out var skus))
{ {
return skus.First().Id; return skus.First().Id;
} }
return ExcelError.ExcelErrorNA; return ExcelError.ExcelErrorNA;
} }
[ExcelFunction(Name = "РЕХАУИМЯ")] [ExcelFunction(Name = "РЕХАУИМЯ")]
public static object GetProductName(string query) public static object GetProductName(string query)
{ {
if (!ProductSku.TryParse(query, out var skus)) if (!ProductSku.TryParse(query, out var skus))
{ {
return ExcelError.ExcelErrorNA; return ExcelError.ExcelErrorNA;
} }
var article = skus.First().Id; var article = skus.First().Id;
var functionName = nameof(GetProductName); var functionName = nameof(GetProductName);
var parameters = new object[] { query }; var parameters = new object[] { query };
if (ExcelAsyncUtil.RunTask(functionName, parameters, async () => if (ExcelAsyncUtil.RunTask(functionName, parameters, async () =>
{ {
return await databaseClient.GetProducts(article); return await databaseClient.GetProducts(article);
}) is not IEnumerable<Product> requestResult) }) is not IEnumerable<Product> requestResult)
{ {
return "Загрузка..."; return "Загрузка...";
} }
else if (!requestResult.Any()) else if (!requestResult.Any())
{ {
return ExcelError.ExcelErrorNA; return ExcelError.ExcelErrorNA;
} }
else else
{ {
var firstProduct = requestResult.First(); var firstProduct = requestResult.First();
return firstProduct.Name; return firstProduct.Name;
} }
} }
[ExcelFunction(Name = "РЕХАУЦЕНА")] [ExcelFunction(Name = "РЕХАУЦЕНА")]
public static object GetProductPrice(string query) public static object GetProductPrice(string query)
{ {
if (!ProductSku.TryParse(query, out var skus)) if (!ProductSku.TryParse(query, out var skus))
{ {
return ExcelError.ExcelErrorNA; return ExcelError.ExcelErrorNA;
} }
var article = skus.First().Id; var article = skus.First().Id;
var functionName = nameof(GetProductPrice); var functionName = nameof(GetProductPrice);
var parameters = new object[] { article }; var parameters = new object[] { article };
if (ExcelAsyncUtil.RunTask(functionName, parameters, async () => if (ExcelAsyncUtil.RunTask(functionName, parameters, async () =>
{ {
return await databaseClient.GetProducts(article); return await databaseClient.GetProducts(article);
}) is not IEnumerable<Product> requestResult) }) is not IEnumerable<Product> requestResult)
{ {
return "Загрузка..."; return "Загрузка...";
} }
else if (!requestResult.Any()) else if (!requestResult.Any())
{ {
return ExcelError.ExcelErrorNA; return ExcelError.ExcelErrorNA;
} }
else else
{ {
var firstProduct = requestResult.First(); var firstProduct = requestResult.First();
return Math.Round(firstProduct.Price * 1.2m, 2); return Math.Round(firstProduct.Price * 1.2m, 2);
} }
} }
[ExcelFunction(Name = "РЕХАУЦЕНАРУБ")] [ExcelFunction(Name = "РЕХАУЦЕНАРУБ")]
public static object GetProductPriceRub(string query, double dateField) public static object GetProductPriceRub(string query, double dateField)
{ {
if (!ProductSku.TryParse(query, out var skus)) if (!ProductSku.TryParse(query, out var skus))
{ {
return ExcelError.ExcelErrorNA; return ExcelError.ExcelErrorNA;
} }
var article = skus.First().Id; var article = skus.First().Id;
DateTime date = dateField == 0 ? DateTime.Today : DateTime.FromOADate(dateField); DateTime date = dateField == 0 ? DateTime.Today : DateTime.FromOADate(dateField);
var functionName = nameof(GetProductPriceRub); var functionName = nameof(GetProductPriceRub);
var parameters = new object[] { date }; var parameters = new object[] { date };
if (ExcelAsyncUtil.RunTask(functionName, parameters, async () => if (ExcelAsyncUtil.RunTask(functionName, parameters, async () =>
{ {
var requestResult = await currencyClient.GetExchangeRate(date); var requestResult = await currencyClient.GetExchangeRate(date);
return requestResult ?? -1m; return requestResult ?? -1m;
}) is not decimal exchangeRate) }) is not decimal exchangeRate)
{ {
return "Загрузка..."; return "Загрузка...";
} }
parameters = new object[] { query }; parameters = new object[] { query };
if (ExcelAsyncUtil.RunTask(functionName, parameters, async () => if (ExcelAsyncUtil.RunTask(functionName, parameters, async () =>
{ {
var products = await databaseClient.GetProducts(article); var products = await databaseClient.GetProducts(article);
var product = products.FirstOrDefault(); var product = products.FirstOrDefault();
return product == null ? -1m : return product == null ? -1m :
product.Price * (decimal)exchangeRate * 1.2m; product.Price * (decimal)exchangeRate * 1.2m;
}) is not decimal requestResult) }) is not decimal requestResult)
{ {
return "Загрузка..."; return "Загрузка...";
} }
else if (requestResult < 0 || exchangeRate < 0) else if (requestResult < 0 || exchangeRate < 0)
{ {
return ExcelError.ExcelErrorNA; return ExcelError.ExcelErrorNA;
} }
else else
{ {
return Math.Round(requestResult, 2); return Math.Round(requestResult, 2);
} }
} }
} }