123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172 |
- using System;
- using System.Collections.Generic;
- using System.Data.Entity;
- using System.Linq;
- using System.Web;
- using System.Linq.Expressions;
- using Bowin.Common.Utility;
- using EMISOnline.Entities;
- using Bowin.Common.Cache;
- using EMISOnline.ViewModel;
- namespace EMISOnline.ViewModel.Cache
- {
- 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<Sys_DictionaryItem> GetDictionaryItem(DictionaryItem dictionaryItem)
- {
- var dictionaryCode = dictionaryItem.ToString();
- return GetDictionaryItem(dictionaryCode);
- }
- public static List<Sys_DictionaryItem> GetDictionaryItem(string dictionaryCode)
- {
- var key = "Dictionary_" + dictionaryCode;
- var cachedValue = CacheHelper.Get(key);
- if (cachedValue == null)
- {
- using (var context = new EMISOnlineContextContainer())
- {
- var result = context.Set<Sys_DictionaryItem>()
- .Where(x => x.DictionaryCode == dictionaryCode)
- .OrderBy(x => x.OrderNo)
- .AsNoTracking()
- .ToList();
- if (result != null)
- {
- CacheHelper.Add(key, result);
- return result;
- }
- else
- {
- return null;
- }
- }
- }
- else
- {
- return (List<Sys_DictionaryItem>)cachedValue;
- }
- }
- /// <summary>
- /// 判断是否存在字典项中
- /// </summary>
- /// <param name="dictionaryCode">字典编码</param>
- /// <returns name="dictionaryStr">判断的文字</returns>
- public static bool IsDictionaryExist(string dictionaryCode, string dictionaryStr)
- {
- return IdNameExt.GetDictionaryItem(dictionaryCode)
- .Any(x => x.Name == dictionaryStr.Trim());
- }
- private static string ObjToName<T>(object objId, Func<T, string> func)
- {
- if (!(objId is int))
- {
- if ((objId == null || default(T).Equals(objId))) return "";
- }
- T id;
- if (objId.ToString().TryParseTo<T>(out id))
- {
- return func(id);
- }
- else
- {
- return "";
- }
- }
- private static string GetCachedValue<CT, T, TPK, TResult>(string keyTemplate,
- TPK key,
- Expression<Func<T, TPK>> pkselector,
- Expression<Func<T, TResult>> 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<Func<T, bool>> lambda =
- Expression.Lambda<Func<T, bool>>(Expression.Equal(pkselector.Body, keyValueExp), pkselector.Parameters[0]);
- using (var context = new CT())
- {
- TResult result = context.Set<T>().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<T, TPK, TResult>(string keyTemplate,
- TPK key,
- Expression<Func<T, TPK>> pkselector,
- Expression<Func<T, TResult>> 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<Func<T, bool>> lambda =
- Expression.Lambda<Func<T, bool>>(Expression.Equal(pkselector.Body, keyValueExp), pkselector.Parameters[0]);
- using (var context = new EMISOnlineContextContainer())
- {
- TResult result = context.Set<T>().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();
- }
- }
- }
- }
|