2021-11-11 16:33:40 +03:00
|
|
|
|
using ExcelDna.Integration;
|
2021-11-29 11:26:25 +03:00
|
|
|
|
using System.Runtime.Caching;
|
2021-11-14 12:27:49 +03:00
|
|
|
|
using System.Net.Http;
|
2021-11-11 16:33:40 +03:00
|
|
|
|
|
|
|
|
|
namespace Rehau.Sku.Assist
|
|
|
|
|
{
|
|
|
|
|
public class Functions : IExcelAddIn
|
|
|
|
|
{
|
2021-11-29 11:26:25 +03:00
|
|
|
|
private static HttpClient _httpClient;
|
|
|
|
|
private static ObjectCache _resultCache = MemoryCache.Default;
|
2021-11-14 12:27:49 +03:00
|
|
|
|
|
2021-11-29 11:26:25 +03:00
|
|
|
|
public void AutoClose()
|
2021-11-11 16:33:40 +03:00
|
|
|
|
{
|
|
|
|
|
}
|
2021-11-29 11:26:25 +03:00
|
|
|
|
public void AutoOpen()
|
2021-11-11 16:33:40 +03:00
|
|
|
|
{
|
2021-11-29 11:26:25 +03:00
|
|
|
|
_httpClient = new HttpClient();
|
2021-11-11 16:33:40 +03:00
|
|
|
|
}
|
|
|
|
|
|
2021-11-29 11:26:25 +03:00
|
|
|
|
[ExcelFunction]
|
|
|
|
|
public static object RAUNAME(string request)
|
2021-11-11 16:33:40 +03:00
|
|
|
|
{
|
2021-11-29 11:26:25 +03:00
|
|
|
|
string cachedResult = _resultCache[request] as string;
|
|
|
|
|
|
|
|
|
|
if (cachedResult != null)
|
|
|
|
|
{
|
|
|
|
|
return cachedResult;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
object result = ExcelAsyncUtil.Run("RAUNAME", null,
|
|
|
|
|
delegate
|
|
|
|
|
{
|
|
|
|
|
var document = SkuAssist.GetDocumentAsync(request, _httpClient).Result;
|
|
|
|
|
var product = SkuAssist.GetProductFromDocument(document);
|
|
|
|
|
return product.ToString();
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
if (result.Equals(ExcelError.ExcelErrorNA))
|
|
|
|
|
{
|
|
|
|
|
return "Загрузка...";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
_resultCache.Add(request, result, System.DateTime.Now.AddMinutes(20));
|
|
|
|
|
return result.ToString();
|
|
|
|
|
}
|
|
|
|
|
}
|
2021-11-11 16:33:40 +03:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|