Edit DatabaseClient

This commit is contained in:
Sergey Chebotar 2023-03-27 15:52:28 +03:00
parent 19007ff43e
commit 9846a3c35e
4 changed files with 58 additions and 49 deletions

View File

@ -0,0 +1,13 @@
using ExcelDna.Integration;
namespace RhSolutions.AddIn
{
public static class ResetBarFunction
{
[ExcelFunction]
public static void _ResetStatusBar()
{
RhSolutionsAddIn.Excel.StatusBar = false;
}
}
}

View File

@ -1,58 +1,57 @@
using ExcelDna.Integration; using ExcelDna.Integration;
using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.DependencyInjection;
using Microsoft.Office.Interop.Excel; using RhSolutions.Models;
using RhSolutions.Services; using RhSolutions.Services;
using System.Collections.Generic;
using System.Linq; using System.Linq;
namespace RhSolutions.AddIn namespace RhSolutions.AddIn
{ {
public class Functions public class RhSolutionsFunction
{ {
[ExcelFunction(Description = "Распознать артикул и попробовать найти его в прайс-листе")] [ExcelFunction(Description = "Распознать артикул и попробовать найти его в прайс-листе")]
public static object RHSOLUTIONS([ExcelArgument(Name = "\"Строка с названием материала\"")] string line) public static object RHSOLUTIONS([ExcelArgument(Name = "\"Строка с названием материала\"")] string line)
{ {
object result;
IDatabaseClient databaseClient = RhSolutionsAddIn.ServiceProvider.GetService<IDatabaseClient>(); IDatabaseClient databaseClient = RhSolutionsAddIn.ServiceProvider.GetService<IDatabaseClient>();
IEnumerable<Product> requestResult = ExcelAsyncUtil.Run("Database request", line, delegate
result = ExcelAsyncUtil.Run("Database request", line, delegate
{ {
return databaseClient.GetProduct(line).GetAwaiter().GetResult(); return databaseClient.GetProducts(line)
}); .GetAwaiter()
.GetResult();
}) as IEnumerable<Product>;
string parsedSku = Sku.TryParse(line, out var skus) Sku.TryParse(line, out var skus);
? skus.First().ToString() : string.Empty;
if (result == null) if (requestResult == null)
{ {
if (string.IsNullOrEmpty(parsedSku)) if (skus.Count() > 0)
{ {
return ExcelError.ExcelErrorNA; return $"{skus.First()} ...";
} }
else else
{
return skus.First().ToString();
}
}
if (result.Equals(ExcelError.ExcelErrorNA))
{
if (string.IsNullOrEmpty(parsedSku))
{ {
return "Загрузка..."; return "Загрузка...";
} }
else
{
return skus.First().ToString();
}
} }
return result; else
} {
if (requestResult.Count() == 0 && skus.Count() == 0)
{
return ExcelError.ExcelErrorNA;
}
[ExcelFunction] else if (requestResult.Count() == 0)
public static void _ResetStatusBar() {
{ return $"{skus.First()}";
RhSolutionsAddIn.Excel.StatusBar = false; }
else
{
var firstProduct = requestResult.First();
return $"{firstProduct.ProductSku} {firstProduct.Name}";
}
}
} }
} }
} }

View File

@ -1,9 +1,11 @@
using System.Threading.Tasks; using RhSolutions.Models;
using System.Collections.Generic;
using System.Threading.Tasks;
namespace RhSolutions.Services namespace RhSolutions.Services
{ {
public interface IDatabaseClient public interface IDatabaseClient
{ {
public Task<object> GetProduct(string line); public Task<IEnumerable<Product>> GetProducts(string query);
} }
} }

View File

@ -1,10 +1,12 @@
using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.DependencyInjection;
using Newtonsoft.Json; using Newtonsoft.Json;
using RhSolutions;
using RhSolutions.AddIn; using RhSolutions.AddIn;
using RhSolutions.Models; using RhSolutions.Models;
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Net;
using System.Net.Http; using System.Net.Http;
using System.Threading.Tasks; using System.Threading.Tasks;
@ -12,14 +14,15 @@ namespace RhSolutions.Services
{ {
public class RhDatabaseClient : IDatabaseClient public class RhDatabaseClient : IDatabaseClient
{ {
private IServiceProvider provider; private IServiceProvider serviceProvider;
public HttpStatusCode StatusCode { get; private set; }
public RhDatabaseClient(IServiceProvider provider) public RhDatabaseClient(IServiceProvider provider)
{ {
this.provider = provider; this.serviceProvider = provider;
} }
public async Task<object> GetProduct(string line) public async Task<IEnumerable<Product>> GetProducts(string line)
{ {
string request; string request;
@ -33,30 +36,22 @@ namespace RhSolutions.Services
request = @"https://rh.cebotari.ru/api/search?query=" + line; request = @"https://rh.cebotari.ru/api/search?query=" + line;
} }
var client = provider.GetRequiredService<HttpClient>(); var client = serviceProvider.GetRequiredService<HttpClient>();
var response = await client.GetAsync(request); var response = await client.GetAsync(request);
try try
{ {
response.EnsureSuccessStatusCode(); response.EnsureSuccessStatusCode();
string json = await response.Content.ReadAsStringAsync(); string json = await response.Content.ReadAsStringAsync();
var product = JsonConvert.DeserializeObject<IEnumerable<Product>>(json) return JsonConvert.DeserializeObject<IEnumerable<Product>>(json) ?? Enumerable.Empty<Product>();
.FirstOrDefault();
if (product == null)
{
return null;
}
else
{
return $"{product.ProductSku} {product.Name}";
}
} }
catch catch
{ {
return $"Ошибка сервера {response.StatusCode}"; StatusCode = response.StatusCode;
} }
return Enumerable.Empty<Product>();
} }
} }
} }