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;
}
else
{
var product = products.First();
return $"{product.Id} {product.Name}";
}
}
[ExcelFunction(Name = "РЕХАУАРТИКУЛ")] string query = string.Join(" ", strings.ToArray());
public static object SkuSearch(string query) var functionName = nameof(ProductSearch);
{ var parameters = new object[] { query };
if (ProductSku.TryParse(query, out var skus)) if (ExcelAsyncUtil.RunTask(functionName, parameters, async () =>
{ {
return skus.First().Id; return await databaseClient.GetProducts(query);
} }) is not IEnumerable<Product> products)
return ExcelError.ExcelErrorNA; {
} 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 GetProductName(string query) public static object SkuSearch(string query)
{ {
if (!ProductSku.TryParse(query, out var skus)) if (ProductSku.TryParse(query, out var skus))
{ {
return ExcelError.ExcelErrorNA; return skus.First().Id;
} }
var article = skus.First().Id; return ExcelError.ExcelErrorNA;
var functionName = nameof(GetProductName); }
var parameters = new object[] { query };
if (ExcelAsyncUtil.RunTask(functionName, parameters, async () =>
{
return await databaseClient.GetProducts(article);
}) is not IEnumerable<Product> requestResult)
{
return "Загрузка...";
}
else if (!requestResult.Any())
{
return ExcelError.ExcelErrorNA;
}
else
{
var firstProduct = requestResult.First();
return firstProduct.Name;
}
}
[ExcelFunction(Name = "РЕХАУЦЕНА")] [ExcelFunction(Name = "РЕХАУИМЯ")]
public static object GetProductPrice(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(GetProductPrice); var functionName = nameof(GetProductName);
var parameters = new object[] { article }; var parameters = new object[] { query };
if (ExcelAsyncUtil.RunTask(functionName, parameters, async () =>
{
return await databaseClient.GetProducts(article);
}) is not IEnumerable<Product> requestResult)
{
return "Загрузка...";
}
else if (!requestResult.Any())
{
return ExcelError.ExcelErrorNA;
}
else
{
var firstProduct = requestResult.First();
return firstProduct.Name;
}
}
if (ExcelAsyncUtil.RunTask(functionName, parameters, async () => [ExcelFunction(Name = "РЕХАУЦЕНА")]
{ public static object GetProductPrice(string query)
return await databaseClient.GetProducts(article); {
}) is not IEnumerable<Product> requestResult) if (!ProductSku.TryParse(query, out var skus))
{ {
return "Загрузка..."; return ExcelError.ExcelErrorNA;
} }
else if (!requestResult.Any()) var article = skus.First().Id;
{ var functionName = nameof(GetProductPrice);
return ExcelError.ExcelErrorNA; var parameters = new object[] { article };
}
else
{
var firstProduct = requestResult.First();
return Math.Round(firstProduct.Price * 1.2m, 2);
}
}
[ExcelFunction(Name = "РЕХАУЦЕНАРУБ")] if (ExcelAsyncUtil.RunTask(functionName, parameters, async () =>
public static object GetProductPriceRub(string query, double dateField) {
{ return await databaseClient.GetProducts(article);
if (!ProductSku.TryParse(query, out var skus)) }) is not IEnumerable<Product> requestResult)
{ {
return ExcelError.ExcelErrorNA; return "Загрузка...";
} }
var article = skus.First().Id; else if (!requestResult.Any())
DateTime date = dateField == 0 ? DateTime.Today : DateTime.FromOADate(dateField); {
return ExcelError.ExcelErrorNA;
}
else
{
var firstProduct = requestResult.First();
return Math.Round(firstProduct.Price * 1.2m, 2);
}
}
[ExcelFunction(Name = "РЕХАУЦЕНАРУБ")]
public static object GetProductPriceRub(string query, double dateField)
{
if (!ProductSku.TryParse(query, out var skus))
{
return ExcelError.ExcelErrorNA;
}
var article = skus.First().Id;
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);
} }
} }
} }