Compare commits

..

5 Commits

3 changed files with 146 additions and 132 deletions

3
.vscode/launch.json vendored
View File

@ -15,7 +15,8 @@
], ],
"cwd": "${workspaceFolder}", "cwd": "${workspaceFolder}",
"console": "internalConsole", "console": "internalConsole",
"stopAtEntry": false "stopAtEntry": false,
"requireExactSource": false
}, },
{ {
"name": ".NET Core Attach", "name": ".NET Core Attach",

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);
} }
} }
} }

View File

@ -1,6 +1,6 @@
<Project Sdk="Microsoft.NET.Sdk"> <Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup> <PropertyGroup>
<TargetFrameworks>net472</TargetFrameworks> <TargetFrameworks>net472;net6.0-windows</TargetFrameworks>
<LangVersion>10</LangVersion> <LangVersion>10</LangVersion>
<OutputType>Library</OutputType> <OutputType>Library</OutputType>
<RootNamespace>RhSolutions</RootNamespace> <RootNamespace>RhSolutions</RootNamespace>
@ -17,8 +17,8 @@
<PackageReference Include="Microsoft.Bcl.AsyncInterfaces" Version="8.0.0" /> <PackageReference Include="Microsoft.Bcl.AsyncInterfaces" Version="8.0.0" />
<PackageReference Include="Microsoft.Bcl.HashCode" Version="1.1.1" /> <PackageReference Include="Microsoft.Bcl.HashCode" Version="1.1.1" />
<PackageReference Include="Microsoft.CSharp" Version="4.7.0" /> <PackageReference Include="Microsoft.CSharp" Version="4.7.0" />
<PackageReference Include="Microsoft.Extensions.Caching.Memory" Version="8.0.0" /> <PackageReference Include="Microsoft.Extensions.Caching.Memory" Version="8.0.1" />
<PackageReference Include="Microsoft.Extensions.Http" Version="8.0.0" /> <PackageReference Include="Microsoft.Extensions.Http" Version="8.0.1" />
<PackageReference Include="netDxf" Version="2022.11.2" /> <PackageReference Include="netDxf" Version="2022.11.2" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" /> <PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
<PackageReference Include="System.Buffers" Version="4.5.1" /> <PackageReference Include="System.Buffers" Version="4.5.1" />