Compare commits

..

No commits in common. "a065c4c6990e959742faa1bf8bb7ef960146978b" and "6d3f2bf55c9242fb2eabb573fb6945a2a61d3065" have entirely different histories.

3 changed files with 132 additions and 146 deletions

3
.vscode/launch.json vendored
View File

@ -15,8 +15,7 @@
], ],
"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,155 +2,142 @@
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(object[,] values) public static object ProductSearch(string query)
{ {
List<string> strings = new(); var functionName = nameof(ProductSearch);
int rows = values.GetLength(0); var parameters = new object[] { query };
int columns = values.GetLength(1); if (ExcelAsyncUtil.RunTask(functionName, parameters, async () =>
for (int row = 0; row < rows; row++) {
{ return await databaseClient.GetProducts(query);
for (int column = 0; column < columns; column++) }) is not IEnumerable<Product> products)
{ {
object value = values[row, column]; return "Загрузка...";
strings.Add(value.ToString()); }
} else if (!products.Any())
} {
return ExcelError.ExcelErrorNA;
string query = string.Join(" ", strings.ToArray()); }
var functionName = nameof(ProductSearch); else
var parameters = new object[] { query }; {
if (ExcelAsyncUtil.RunTask(functionName, parameters, async () => var product = products.First();
{ 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);
} }
} }
} }

View File

@ -1,6 +1,6 @@
<Project Sdk="Microsoft.NET.Sdk"> <Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup> <PropertyGroup>
<TargetFrameworks>net472;net6.0-windows</TargetFrameworks> <TargetFrameworks>net472</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.1" /> <PackageReference Include="Microsoft.Extensions.Caching.Memory" Version="8.0.0" />
<PackageReference Include="Microsoft.Extensions.Http" Version="8.0.1" /> <PackageReference Include="Microsoft.Extensions.Http" Version="8.0.0" />
<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" />