RhSolutions-AddIn/Source/ExcelDNA/Functions.cs

70 lines
2.3 KiB
C#
Raw Normal View History

using ExcelDna.Integration;
2021-11-29 15:50:24 +03:00
using System.Threading.Tasks;
using System.Runtime.Caching;
2021-11-11 16:33:40 +03:00
namespace Rehau.Sku.Assist
{
2021-11-29 15:50:24 +03:00
public class Functions
2021-11-11 16:33:40 +03:00
{
[ExcelFunction]
public static object RAUNAME(string request)
2021-11-11 16:33:40 +03:00
{
if (MemoryCache.Default.Contains(request))
2021-12-03 14:15:16 +03:00
{
IProduct product = MemoryCache.Default[request] as IProduct;
return product.Name;
}
else
{
object result = ExcelAsyncUtil.Run("Rauname", new[] { request },
delegate
{
2021-12-03 14:15:16 +03:00
Task<IProduct> p = Task.Run(() => SkuAssist.GetProduct(request));
return p.Result;
});
2021-12-03 14:15:16 +03:00
if (result == null)
return "Не найдено";
if (result.Equals(ExcelError.ExcelErrorNA))
return "Загрузка...";
2021-12-03 13:24:31 +03:00
2021-12-03 14:15:16 +03:00
IProduct product = result as IProduct;
MemoryCache.Default.Add(request, product, System.DateTime.Now.AddMinutes(10));
//MemoryCache.Default.Add(product.Name, product, System.DateTime.Now.AddMinutes(10));
return product.Name;
}
}
[ExcelFunction]
public static object RAUSKU(string request)
{
if (MemoryCache.Default.Contains(request))
{
IProduct result = MemoryCache.Default[request] as IProduct;
return result.Sku;
}
else
{
object result = ExcelAsyncUtil.Run("RauSku", new[] { request },
delegate
{
Task<IProduct> p = Task.Run(() => SkuAssist.GetProduct(request));
return p.Result;
});
if (result == null)
2021-12-03 13:24:31 +03:00
return "Не найдено";
2021-12-03 14:15:16 +03:00
if (result.Equals(ExcelError.ExcelErrorNA))
return "Загрузка...";
IProduct product = result as IProduct;
MemoryCache.Default.Add(request, product, System.DateTime.Now.AddMinutes(10));
//MemoryCache.Default.Add(product.Sku, product, System.DateTime.Now.AddMinutes(10));
return product.Sku;
}
2021-11-11 16:33:40 +03:00
}
}
}