using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Linq.Expressions;
using Bowin.Common.Cache;
using Bowin.Common.Utility;
using EMIS.Entities;
namespace EMIS.ViewModel.CacheManage
{
public static class IdNameExt
{
///
///
///
///
///
///
public static string GetName(DictionaryItem dictionaryItem, int? value)
{
return GetDictionaryItem(dictionaryItem).Where(w => w.Value == value).Select(s => s.Name).FirstOrDefault();
}
///
///
///
///
///
public static List GetDictionaryItem(DictionaryItem dictionaryItem)
{
var dictionaryCode = dictionaryItem.ToString();
return GetDictionaryItem(dictionaryCode);
}
///
///
///
///
///
public static List GetDictionaryItem(string dictionaryCode)
{
var key = "Dictionary_" + dictionaryCode;
var cachedValue = CacheHelper.Get(key);
if (cachedValue == null)
{
using (var context = new EMISNewContext())
{
var result = context.Set()
.Where(x => x.DictionaryCode == dictionaryCode)
.OrderBy(x => x.OrderNo).ThenBy(x => x.Value)
.AsNoTracking()
.ToList();
if (result != null)
{
CacheHelper.Add(key, result);
return result;
}
else
{
return null;
}
}
}
else
{
return (List)cachedValue;
}
}
///
/// 判断是否存在字典项中
///
/// 字典编码
/// 判断的文字
public static bool IsDictionaryExist(string dictionaryCode, string dictionaryStr)
{
return IdNameExt.GetDictionaryItem(dictionaryCode).Any(x => x.Name == dictionaryStr.Trim());
}
///
///
///
///
///
///
///
private static string ObjToName(object objId, Func func)
{
if (!(objId is int))
{
if ((objId == null || default(T).Equals(objId))) return "";
}
T id;
if (objId.ToString().TryParseTo(out id))
{
return func(id);
}
else
{
return "";
}
}
///
///
///
///
///
///
///
///
///
///
///
///
private static string GetCachedValue(string keyTemplate, TPK key, Expression> pkselector,
Expression> selector) where T : class where CT : DbContext, new()
{
if (!(key is int))
{
if (key == null || default(TPK).Equals(key)) return "";
}
var cachedValue = CacheHelper.Get(string.Format(keyTemplate, key));
if (cachedValue == null)
{
ConstantExpression keyValueExp = Expression.Constant(key, pkselector.Body.Type);
//ParameterExpression param = Expression.Parameter(predicate.Parameters[0].Type);
Expression> lambda =
Expression.Lambda>(Expression.Equal(pkselector.Body, keyValueExp), pkselector.Parameters[0]);
using (var context = new CT())
{
TResult result = context.Set().Where(lambda).Select(selector).FirstOrDefault();
if (result != null)
{
CacheHelper.Add(string.Format(keyTemplate, key), result.ToString());
return result.ToString();
}
else
{
return "";
}
}
}
else
{
return cachedValue.ToString();
}
}
///
///
///
///
///
///
///
///
///
///
///
private static string GetCachedValue(string keyTemplate, TPK key, Expression> pkselector,
Expression> selector) where T : class
{
if (key == null) return "";
if (default(TPK) == null)
{
if (key == null) return "";
}
else if (default(TPK).Equals(key))
{
return "";
}
var cachedValue = CacheHelper.Get(string.Format(keyTemplate, key));
if (cachedValue == null)
{
ConstantExpression keyValueExp = Expression.Constant(key, pkselector.Body.Type);
//ParameterExpression param = Expression.Parameter(predicate.Parameters[0].Type);
Expression> lambda =
Expression.Lambda>(Expression.Equal(pkselector.Body, keyValueExp), pkselector.Parameters[0]);
using (var context = new EMISNewContext())
{
TResult result = context.Set().Where(lambda).Select(selector).FirstOrDefault();
if (result != null)
{
CacheHelper.Add(string.Format(keyTemplate, key), result.ToString());
return result.ToString();
}
else
{
return "";
}
}
}
else
{
return cachedValue.ToString();
}
}
}
}