RhSolutions-AddIn/Source/ExcelDNA/Functions.cs

68 lines
2.1 KiB
C#
Raw Normal View History

using ExcelDna.Integration;
using System.Runtime.Caching;
2021-12-03 19:30:35 +03:00
using System.Threading.Tasks;
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
{
2021-12-03 19:30:35 +03:00
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));
return product.Name;
}
}
[ExcelFunction]
public static object RAUSKU(string request)
{
if (MemoryCache.Default.Contains(request))
{
2021-12-03 19:30:35 +03:00
IProduct product = MemoryCache.Default[request] as IProduct;
return product.Id;
2021-12-03 14:15:16 +03:00
}
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));
2021-12-03 19:30:35 +03:00
return product.Id;
}
2021-11-11 16:33:40 +03:00
}
}
}