Add RAUPRICE function. Refactoring.

This commit is contained in:
Sergey Chebotar 2021-12-03 22:25:20 +03:00
parent 915929fa9d
commit 355e2d3672
4 changed files with 69 additions and 68 deletions

View File

@ -1,10 +1,9 @@
using System; namespace Rehau.Sku.Assist
namespace Rehau.Sku.Assist
{ {
interface IProduct interface IProduct
{ {
string Id { get; } string Id { get; }
string Name { get; } string Name { get; }
string Price { get; }
} }
} }

View File

@ -1,7 +1,9 @@
using AngleSharp.Dom; using AngleSharp.Dom;
using ExcelDna.Integration;
using Newtonsoft.Json; using Newtonsoft.Json;
using System; using System;
using System.Linq; using System.Linq;
using System.Runtime.Caching;
using System.Text.RegularExpressions; using System.Text.RegularExpressions;
using System.Threading.Tasks; using System.Threading.Tasks;
@ -16,6 +18,13 @@ namespace Rehau.Sku.Assist
Series Series
} }
public enum ProductField
{
Name,
Id,
Price
}
static class SkuAssist static class SkuAssist
{ {
public static async Task<IProduct> GetProduct(string request) public static async Task<IProduct> GetProduct(string request)
@ -43,10 +52,52 @@ namespace Rehau.Sku.Assist
IProduct product = storeResponse IProduct product = storeResponse
.Ecommerce .Ecommerce
.Impressions .Impressions
.Where(i => Regex.IsMatch(i.Id, @"\d{11}", RegexOptions.None)) .Where(p => Regex.IsMatch(p.Id, @"\d{11}", RegexOptions.None))
.FirstOrDefault(); .FirstOrDefault();
return product; return product;
} }
public static object GetProduct(string request, ProductField field)
{
IProduct product;
if (MemoryCache.Default.Contains(request))
{
product = MemoryCache.Default[request] as IProduct;
}
else
{
object result = ExcelAsyncUtil.Run("RauName", new[] { request },
delegate
{
Task<IProduct> p = Task.Run(() => GetProduct(request));
return p.Result;
});
if (result == null)
return "Не найдено";
if (result.Equals(ExcelError.ExcelErrorNA))
return "Загрузка...";
product = result as IProduct;
MemoryCache.Default.Add(request, product, DateTime.Now.AddMinutes(10));
return product.Name;
}
switch (field)
{
case ProductField.Name:
return product.Name;
case ProductField.Id:
return product.Id;
case ProductField.Price:
return product.Price;
default:
return ExcelError.ExcelErrorValue;
}
}
} }
} }

View File

@ -1,6 +1,4 @@
using ExcelDna.Integration; using ExcelDna.Integration;
using System.Runtime.Caching;
using System.Threading.Tasks;
namespace Rehau.Sku.Assist namespace Rehau.Sku.Assist
{ {
@ -8,61 +6,14 @@ namespace Rehau.Sku.Assist
{ {
[ExcelFunction] [ExcelFunction]
public static object RAUNAME(string request) public static object RAUNAME(string request)
{ => SkuAssist.GetProduct(request, ProductField.Name);
if (MemoryCache.Default.Contains(request))
{
IProduct product = MemoryCache.Default[request] as IProduct;
return product.Name;
}
else
{
object result = ExcelAsyncUtil.Run("RauName", new[] { request },
delegate
{
Task<IProduct> p = Task.Run(() => SkuAssist.GetProduct(request));
return p.Result;
});
if (result == null)
return "Не найдено";
if (result.Equals(ExcelError.ExcelErrorNA))
return "Загрузка...";
IProduct product = result as IProduct;
MemoryCache.Default.Add(request, product, System.DateTime.Now.AddMinutes(10));
return product.Name;
}
}
[ExcelFunction] [ExcelFunction]
public static object RAUSKU(string request) public static object RAUSKU(string request)
{ => SkuAssist.GetProduct(request, ProductField.Id);
if (MemoryCache.Default.Contains(request))
{
IProduct product = MemoryCache.Default[request] as IProduct;
return product.Id;
}
else
{
object result = ExcelAsyncUtil.Run("RauSku", new[] { request },
delegate
{
Task<IProduct> p = Task.Run(() => SkuAssist.GetProduct(request));
return p.Result;
});
if (result == null) [ExcelFunction]
return "Не найдено"; public static object RAUPRICE(string request)
=> SkuAssist.GetProduct(request, ProductField.Price);
if (result.Equals(ExcelError.ExcelErrorNA))
return "Загрузка...";
IProduct product = result as IProduct;
MemoryCache.Default.Add(request, product, System.DateTime.Now.AddMinutes(10));
return product.Id;
}
}
} }
} }

View File

@ -2,14 +2,14 @@
namespace Rehau.Sku.Assist.Tests namespace Rehau.Sku.Assist.Tests
{ {
//[TestFixture] [TestFixture]
//public class SkuAssistTests public class SkuAssistTests
//{ {
// [Test] [Test]
// public async void BaseTest() public static void BaseTest()
// { {
// var result = await Functions.RAUNAME("160001"); var result = Functions.RAUNAME("160001");
// Assert.AreEqual("Надвижная гильза REHAU RAUTITAN РХ (11600011001)", result); Assert.AreEqual("Надвижная гильза REHAU RAUTITAN РХ (11600011001)", result);
// } }
//} }
} }