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 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
|
else
|
||||||
{
|
{
|
||||||
return skus.First().ToString();
|
if (requestResult.Count() == 0 && skus.Count() == 0)
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
|
|
||||||
[ExcelFunction]
|
|
||||||
public static void _ResetStatusBar()
|
|
||||||
{
|
{
|
||||||
RhSolutionsAddIn.Excel.StatusBar = false;
|
return ExcelError.ExcelErrorNA;
|
||||||
|
}
|
||||||
|
|
||||||
|
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
|
namespace RhSolutions.Services
|
||||||
{
|
{
|
||||||
public interface IDatabaseClient
|
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 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>();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
Loading…
Reference in New Issue
Block a user