Edit DatabaseClient
This commit is contained in:
parent
19007ff43e
commit
9846a3c35e
13
RhSolutions.AddIn/AddIn/ResetBarFunction.cs
Normal file
13
RhSolutions.AddIn/AddIn/ResetBarFunction.cs
Normal file
@ -0,0 +1,13 @@
|
||||
using ExcelDna.Integration;
|
||||
|
||||
namespace RhSolutions.AddIn
|
||||
{
|
||||
public static class ResetBarFunction
|
||||
{
|
||||
[ExcelFunction]
|
||||
public static void _ResetStatusBar()
|
||||
{
|
||||
RhSolutionsAddIn.Excel.StatusBar = false;
|
||||
}
|
||||
}
|
||||
}
|
@ -1,58 +1,57 @@
|
||||
using ExcelDna.Integration;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Microsoft.Office.Interop.Excel;
|
||||
using RhSolutions.Models;
|
||||
using RhSolutions.Services;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
|
||||
namespace RhSolutions.AddIn
|
||||
{
|
||||
public class Functions
|
||||
public class RhSolutionsFunction
|
||||
{
|
||||
[ExcelFunction(Description = "Распознать артикул и попробовать найти его в прайс-листе")]
|
||||
public static object RHSOLUTIONS([ExcelArgument(Name = "\"Строка с названием материала\"")] string line)
|
||||
{
|
||||
object result;
|
||||
IDatabaseClient databaseClient = RhSolutionsAddIn.ServiceProvider.GetService<IDatabaseClient>();
|
||||
|
||||
result = ExcelAsyncUtil.Run("Database request", line, delegate
|
||||
IEnumerable<Product> requestResult = 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)
|
||||
? skus.First().ToString() : string.Empty;
|
||||
Sku.TryParse(line, out var skus);
|
||||
|
||||
if (result == null)
|
||||
if (requestResult == null)
|
||||
{
|
||||
if (string.IsNullOrEmpty(parsedSku))
|
||||
if (skus.Count() > 0)
|
||||
{
|
||||
return ExcelError.ExcelErrorNA;
|
||||
return $"{skus.First()} ...";
|
||||
}
|
||||
else
|
||||
{
|
||||
return skus.First().ToString();
|
||||
}
|
||||
}
|
||||
|
||||
if (result.Equals(ExcelError.ExcelErrorNA))
|
||||
{
|
||||
if (string.IsNullOrEmpty(parsedSku))
|
||||
{
|
||||
return "Загрузка...";
|
||||
}
|
||||
else
|
||||
{
|
||||
return skus.First().ToString();
|
||||
}
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
else
|
||||
{
|
||||
if (requestResult.Count() == 0 && skus.Count() == 0)
|
||||
{
|
||||
return ExcelError.ExcelErrorNA;
|
||||
}
|
||||
|
||||
[ExcelFunction]
|
||||
public static void _ResetStatusBar()
|
||||
{
|
||||
RhSolutionsAddIn.Excel.StatusBar = false;
|
||||
else if (requestResult.Count() == 0)
|
||||
{
|
||||
return $"{skus.First()}";
|
||||
}
|
||||
|
||||
else
|
||||
{
|
||||
var firstProduct = requestResult.First();
|
||||
return $"{firstProduct.ProductSku} {firstProduct.Name}";
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@ -1,9 +1,11 @@
|
||||
using System.Threading.Tasks;
|
||||
using RhSolutions.Models;
|
||||
using System.Collections.Generic;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace RhSolutions.Services
|
||||
{
|
||||
public interface IDatabaseClient
|
||||
{
|
||||
public Task<object> GetProduct(string line);
|
||||
public Task<IEnumerable<Product>> GetProducts(string query);
|
||||
}
|
||||
}
|
@ -1,10 +1,12 @@
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Newtonsoft.Json;
|
||||
using RhSolutions;
|
||||
using RhSolutions.AddIn;
|
||||
using RhSolutions.Models;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Net;
|
||||
using System.Net.Http;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
@ -12,14 +14,15 @@ namespace RhSolutions.Services
|
||||
{
|
||||
public class RhDatabaseClient : IDatabaseClient
|
||||
{
|
||||
private IServiceProvider provider;
|
||||
private IServiceProvider serviceProvider;
|
||||
public HttpStatusCode StatusCode { get; private set; }
|
||||
|
||||
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;
|
||||
|
||||
@ -33,30 +36,22 @@ namespace RhSolutions.Services
|
||||
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);
|
||||
|
||||
try
|
||||
{
|
||||
{
|
||||
response.EnsureSuccessStatusCode();
|
||||
string json = await response.Content.ReadAsStringAsync();
|
||||
var product = JsonConvert.DeserializeObject<IEnumerable<Product>>(json)
|
||||
.FirstOrDefault();
|
||||
|
||||
if (product == null)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
else
|
||||
{
|
||||
return $"{product.ProductSku} {product.Name}";
|
||||
}
|
||||
return JsonConvert.DeserializeObject<IEnumerable<Product>>(json) ?? Enumerable.Empty<Product>();
|
||||
}
|
||||
|
||||
catch
|
||||
{
|
||||
return $"Ошибка сервера {response.StatusCode}";
|
||||
StatusCode = response.StatusCode;
|
||||
}
|
||||
|
||||
return Enumerable.Empty<Product>();
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user