IdNameExt.cs 5.8 KB

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