RhSolutions-AddIn/Source/ExcelDNA/Functions.cs
2021-12-03 19:30:35 +03:00

68 lines
2.1 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using ExcelDna.Integration;
using System.Runtime.Caching;
using System.Threading.Tasks;
namespace Rehau.Sku.Assist
{
public class Functions
{
[ExcelFunction]
public static object RAUNAME(string request)
{
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]
public static object RAUSKU(string request)
{
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)
return "Не найдено";
if (result.Equals(ExcelError.ExcelErrorNA))
return "Загрузка...";
IProduct product = result as IProduct;
MemoryCache.Default.Add(request, product, System.DateTime.Now.AddMinutes(10));
return product.Id;
}
}
}
}