123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212 |
- 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
- {
- /// <summary>
- ///
- /// </summary>
- /// <param name="dictionaryItem"></param>
- /// <param name="value"></param>
- /// <returns></returns>
- public static string GetName(DictionaryItem dictionaryItem, int? value)
- {
- return GetDictionaryItem(dictionaryItem).Where(w => w.Value == value).Select(s => s.Name).FirstOrDefault();
- }
- /// <summary>
- ///
- /// </summary>
- /// <param name="dictionaryItem"></param>
- /// <returns></returns>
- public static List<Sys_DictionaryItem> GetDictionaryItem(DictionaryItem dictionaryItem)
- {
- var dictionaryCode = dictionaryItem.ToString();
- return GetDictionaryItem(dictionaryCode);
- }
- /// <summary>
- ///
- /// </summary>
- /// <param name="dictionaryCode"></param>
- /// <returns></returns>
- public static List<Sys_DictionaryItem> GetDictionaryItem(string dictionaryCode)
- {
- var key = "Dictionary_" + dictionaryCode;
- var cachedValue = CacheHelper.Get(key);
- if (cachedValue == null)
- {
- using (var context = new EMISNewContext())
- {
- var result = context.Set<Sys_DictionaryItem>()
- .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<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());
- }
- /// <summary>
- ///
- /// </summary>
- /// <typeparam name="T"></typeparam>
- /// <param name="objId"></param>
- /// <param name="func"></param>
- /// <returns></returns>
- 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 "";
- }
- }
- /// <summary>
- ///
- /// </summary>
- /// <typeparam name="CT"></typeparam>
- /// <typeparam name="T"></typeparam>
- /// <typeparam name="TPK"></typeparam>
- /// <typeparam name="TResult"></typeparam>
- /// <param name="keyTemplate"></param>
- /// <param name="key"></param>
- /// <param name="pkselector"></param>
- /// <param name="selector"></param>
- /// <returns></returns>
- 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();
- }
- }
- /// <summary>
- ///
- /// </summary>
- /// <typeparam name="T"></typeparam>
- /// <typeparam name="TPK"></typeparam>
- /// <typeparam name="TResult"></typeparam>
- /// <param name="keyTemplate"></param>
- /// <param name="key"></param>
- /// <param name="pkselector"></param>
- /// <param name="selector"></param>
- /// <returns></returns>
- 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 EMISNewContext())
- {
- 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();
- }
- }
- }
- }
|