IdNameExt.cs 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Data.Entity;
  4. using System.Linq;
  5. using System.Web;
  6. using System.Linq.Expressions;
  7. using Bowin.Common.Cache;
  8. using Bowin.Common.Utility;
  9. using EMIS.Entities;
  10. namespace EMIS.ViewModel.CacheManage
  11. {
  12. public static class IdNameExt
  13. {
  14. /// <summary>
  15. ///
  16. /// </summary>
  17. /// <param name="dictionaryItem"></param>
  18. /// <param name="value"></param>
  19. /// <returns></returns>
  20. public static string GetName(DictionaryItem dictionaryItem, int? value)
  21. {
  22. return GetDictionaryItem(dictionaryItem).Where(w => w.Value == value).Select(s => s.Name).FirstOrDefault();
  23. }
  24. /// <summary>
  25. ///
  26. /// </summary>
  27. /// <param name="dictionaryItem"></param>
  28. /// <returns></returns>
  29. public static List<Sys_DictionaryItem> GetDictionaryItem(DictionaryItem dictionaryItem)
  30. {
  31. var dictionaryCode = dictionaryItem.ToString();
  32. return GetDictionaryItem(dictionaryCode);
  33. }
  34. /// <summary>
  35. ///
  36. /// </summary>
  37. /// <param name="dictionaryCode"></param>
  38. /// <returns></returns>
  39. public static List<Sys_DictionaryItem> GetDictionaryItem(string dictionaryCode)
  40. {
  41. var key = "Dictionary_" + dictionaryCode;
  42. var cachedValue = CacheHelper.Get(key);
  43. if (cachedValue == null)
  44. {
  45. using (var context = new EMISNewContext())
  46. {
  47. var result = context.Set<Sys_DictionaryItem>()
  48. .Where(x => x.DictionaryCode == dictionaryCode)
  49. .OrderBy(x => x.OrderNo).ThenBy(x => x.Value)
  50. .AsNoTracking()
  51. .ToList();
  52. if (result != null)
  53. {
  54. CacheHelper.Add(key, result);
  55. return result;
  56. }
  57. else
  58. {
  59. return null;
  60. }
  61. }
  62. }
  63. else
  64. {
  65. return (List<Sys_DictionaryItem>)cachedValue;
  66. }
  67. }
  68. /// <summary>
  69. /// 判断是否存在字典项中
  70. /// </summary>
  71. /// <param name="dictionaryCode">字典编码</param>
  72. /// <returns name="dictionaryStr">判断的文字</returns>
  73. public static bool IsDictionaryExist(string dictionaryCode, string dictionaryStr)
  74. {
  75. return IdNameExt.GetDictionaryItem(dictionaryCode).Any(x => x.Name == dictionaryStr.Trim());
  76. }
  77. /// <summary>
  78. ///
  79. /// </summary>
  80. /// <typeparam name="T"></typeparam>
  81. /// <param name="objId"></param>
  82. /// <param name="func"></param>
  83. /// <returns></returns>
  84. private static string ObjToName<T>(object objId, Func<T, string> func)
  85. {
  86. if (!(objId is int))
  87. {
  88. if ((objId == null || default(T).Equals(objId))) return "";
  89. }
  90. T id;
  91. if (objId.ToString().TryParseTo<T>(out id))
  92. {
  93. return func(id);
  94. }
  95. else
  96. {
  97. return "";
  98. }
  99. }
  100. /// <summary>
  101. ///
  102. /// </summary>
  103. /// <typeparam name="CT"></typeparam>
  104. /// <typeparam name="T"></typeparam>
  105. /// <typeparam name="TPK"></typeparam>
  106. /// <typeparam name="TResult"></typeparam>
  107. /// <param name="keyTemplate"></param>
  108. /// <param name="key"></param>
  109. /// <param name="pkselector"></param>
  110. /// <param name="selector"></param>
  111. /// <returns></returns>
  112. private static string GetCachedValue<CT, T, TPK, TResult>(string keyTemplate, TPK key, Expression<Func<T, TPK>> pkselector,
  113. Expression<Func<T, TResult>> selector) where T : class where CT : DbContext, new()
  114. {
  115. if (!(key is int))
  116. {
  117. if (key == null || default(TPK).Equals(key)) return "";
  118. }
  119. var cachedValue = CacheHelper.Get(string.Format(keyTemplate, key));
  120. if (cachedValue == null)
  121. {
  122. ConstantExpression keyValueExp = Expression.Constant(key, pkselector.Body.Type);
  123. //ParameterExpression param = Expression.Parameter(predicate.Parameters[0].Type);
  124. Expression<Func<T, bool>> lambda =
  125. Expression.Lambda<Func<T, bool>>(Expression.Equal(pkselector.Body, keyValueExp), pkselector.Parameters[0]);
  126. using (var context = new CT())
  127. {
  128. TResult result = context.Set<T>().Where(lambda).Select(selector).FirstOrDefault();
  129. if (result != null)
  130. {
  131. CacheHelper.Add(string.Format(keyTemplate, key), result.ToString());
  132. return result.ToString();
  133. }
  134. else
  135. {
  136. return "";
  137. }
  138. }
  139. }
  140. else
  141. {
  142. return cachedValue.ToString();
  143. }
  144. }
  145. /// <summary>
  146. ///
  147. /// </summary>
  148. /// <typeparam name="T"></typeparam>
  149. /// <typeparam name="TPK"></typeparam>
  150. /// <typeparam name="TResult"></typeparam>
  151. /// <param name="keyTemplate"></param>
  152. /// <param name="key"></param>
  153. /// <param name="pkselector"></param>
  154. /// <param name="selector"></param>
  155. /// <returns></returns>
  156. private static string GetCachedValue<T, TPK, TResult>(string keyTemplate, TPK key, Expression<Func<T, TPK>> pkselector,
  157. Expression<Func<T, TResult>> selector) where T : class
  158. {
  159. if (key == null) return "";
  160. if (default(TPK) == null)
  161. {
  162. if (key == null) return "";
  163. }
  164. else if (default(TPK).Equals(key))
  165. {
  166. return "";
  167. }
  168. var cachedValue = CacheHelper.Get(string.Format(keyTemplate, key));
  169. if (cachedValue == null)
  170. {
  171. ConstantExpression keyValueExp = Expression.Constant(key, pkselector.Body.Type);
  172. //ParameterExpression param = Expression.Parameter(predicate.Parameters[0].Type);
  173. Expression<Func<T, bool>> lambda =
  174. Expression.Lambda<Func<T, bool>>(Expression.Equal(pkselector.Body, keyValueExp), pkselector.Parameters[0]);
  175. using (var context = new EMISNewContext())
  176. {
  177. TResult result = context.Set<T>().Where(lambda).Select(selector).FirstOrDefault();
  178. if (result != null)
  179. {
  180. CacheHelper.Add(string.Format(keyTemplate, key), result.ToString());
  181. return result.ToString();
  182. }
  183. else
  184. {
  185. return "";
  186. }
  187. }
  188. }
  189. else
  190. {
  191. return cachedValue.ToString();
  192. }
  193. }
  194. }
  195. }