DictionaryItemServices.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Linq.Expressions;
  6. using Bowin.Common.Linq.Entity;
  7. using Bowin.Common.Linq;
  8. using EMIS.ViewModel;
  9. using EMIS.ViewModel.SystemView;
  10. using EMIS.DataLogic.SystemSetting;
  11. using EMIS.Entities;
  12. using Bowin.Common.Cache;
  13. namespace EMIS.CommonLogic.SystemSetting
  14. {
  15. public class DictionaryItemServices : BaseServices, IDictionaryItemServices
  16. {
  17. public DictionaryItemDAL DictionaryItemDAL { get; set; }
  18. /// <summary>
  19. /// 查询对应的数据字典信息View
  20. /// </summary>
  21. /// <param name="configuretView"></param>
  22. /// <param name="dictionaryCode"></param>
  23. /// <param name="isEditable"></param>
  24. /// <param name="isVisible"></param>
  25. /// <param name="pageIndex"></param>
  26. /// <param name="pageSize"></param>
  27. /// <returns></returns>
  28. public IGridResultSet<DictionaryItemView> GetDictionaryItemViewGird(ConfiguretView configuretView, string dictionaryCode, int? isEditable, int? isVisible, int pageIndex, int pageSize)
  29. {
  30. //数据字典元素
  31. Expression<Func<Sys_DictionaryItem, bool>> exp = (x => true);
  32. if (!string.IsNullOrEmpty(dictionaryCode) && dictionaryCode != "-1")
  33. {
  34. //类型代码
  35. exp = exp.And(x => x.DictionaryCode == dictionaryCode);
  36. }
  37. if (isEditable.HasValue)
  38. {
  39. //可否编辑
  40. if (isEditable == (int)CF_GeneralPurpose.IsYes)
  41. {
  42. exp = exp.And(x => x.IsEditable == true);
  43. }
  44. if (isEditable == (int)CF_GeneralPurpose.IsNo)
  45. {
  46. exp = exp.And(x => x.IsEditable != true);
  47. }
  48. }
  49. var query = DictionaryItemDAL.GetDictionaryItemViewQueryable(exp);
  50. if (isVisible.HasValue)
  51. {
  52. //可否显示
  53. if (isVisible == (int)CF_GeneralPurpose.IsYes)
  54. {
  55. query = query.Where(x => x.RecordStatus > (int)SYS_STATUS.UNUSABLE);
  56. }
  57. if (isVisible == (int)CF_GeneralPurpose.IsNo)
  58. {
  59. query = query.Where(x => x.RecordStatus <= (int)SYS_STATUS.UNUSABLE);
  60. }
  61. }
  62. //查询条件
  63. if (!string.IsNullOrEmpty(configuretView.ConditionValue))
  64. {
  65. query = query.DynamicWhere(configuretView.Attribute, configuretView.Condition, configuretView.ConditionValue.Trim());
  66. }
  67. return query.OrderBy(x => x.DictionaryCode).ThenBy(x => x.DictionaryName).ThenBy(x => x.OrderNo).ToGridResultSet<DictionaryItemView>(pageIndex, pageSize);
  68. }
  69. /// <summary>
  70. /// 查询对应的数据字典信息List
  71. /// </summary>
  72. /// <param name="configuretView"></param>
  73. /// <param name="dictionaryCode"></param>
  74. /// <param name="isEditable"></param>
  75. /// <param name="isVisible"></param>
  76. /// <returns></returns>
  77. public IList<DictionaryItemView> GetDictionaryItemViewList(ConfiguretView configuretView, string dictionaryCode, int? isEditable, int? isVisible)
  78. {
  79. //数据字典元素
  80. Expression<Func<Sys_DictionaryItem, bool>> exp = (x => true);
  81. if (!string.IsNullOrEmpty(dictionaryCode) && dictionaryCode != "-1")
  82. {
  83. //类型代码
  84. exp = exp.And(x => x.DictionaryCode == dictionaryCode);
  85. }
  86. if (isEditable.HasValue)
  87. {
  88. //可否编辑
  89. if (isEditable == (int)CF_GeneralPurpose.IsYes)
  90. {
  91. exp = exp.And(x => x.IsEditable == true);
  92. }
  93. if (isEditable == (int)CF_GeneralPurpose.IsNo)
  94. {
  95. exp = exp.And(x => x.IsEditable != true);
  96. }
  97. }
  98. var query = DictionaryItemDAL.GetDictionaryItemViewQueryable(exp);
  99. if (isVisible.HasValue)
  100. {
  101. //可否显示
  102. if (isVisible == (int)CF_GeneralPurpose.IsYes)
  103. {
  104. query = query.Where(x => x.RecordStatus > (int)SYS_STATUS.UNUSABLE);
  105. }
  106. if (isVisible == (int)CF_GeneralPurpose.IsNo)
  107. {
  108. query = query.Where(x => x.RecordStatus <= (int)SYS_STATUS.UNUSABLE);
  109. }
  110. }
  111. //查询条件
  112. if (!string.IsNullOrEmpty(configuretView.ConditionValue))
  113. {
  114. query = query.DynamicWhere(configuretView.Attribute, configuretView.Condition, configuretView.ConditionValue.Trim());
  115. }
  116. return query.OrderBy(x => x.DictionaryCode).ThenBy(x => x.DictionaryName).ThenBy(x => x.OrderNo).ToList();
  117. }
  118. /// <summary>
  119. /// 根据数据字典元素ID查询对应的数据字典信息DictionaryItemView
  120. /// </summary>
  121. /// <param name="DictionaryItemID"></param>
  122. /// <returns></returns>
  123. public DictionaryItemView GetDictionaryItemView(Guid? DictionaryItemID)
  124. {
  125. try
  126. {
  127. var dictionaryItemView = DictionaryItemDAL.GetDictionaryItemViewQueryable(x => x.DictionaryItemID == DictionaryItemID).SingleOrDefault();
  128. return dictionaryItemView;
  129. }
  130. catch (Exception ex)
  131. {
  132. throw new Exception(ex.Message);
  133. }
  134. }
  135. /// <summary>
  136. /// 编辑(新增、修改)
  137. /// </summary>
  138. /// <param name="dictionaryItemView"></param>
  139. public void DictionaryItemEdit(DictionaryItemView dictionaryItemView)
  140. {
  141. try
  142. {
  143. //查询数据库进行验证
  144. var dictionaryItemVerification = DictionaryItemDAL.DictionaryItemRepository.GetList(x => x.DictionaryItemID != dictionaryItemView.DictionaryItemID
  145. && x.DictionaryCode == dictionaryItemView.DictionaryCode && (x.Value == dictionaryItemView.Value || (x.Code == dictionaryItemView.Code && x.Name == dictionaryItemView.Name))).SingleOrDefault();
  146. if (dictionaryItemVerification == null)
  147. {
  148. //数据有误验证
  149. if (dictionaryItemView.DictionaryItemID != Guid.Empty)
  150. {
  151. var dictionaryItem = DictionaryItemDAL.DictionaryItemRepository.GetList(x => x.DictionaryItemID == dictionaryItemView.DictionaryItemID).SingleOrDefault();
  152. if (dictionaryItem == null)
  153. {
  154. throw new Exception("数据有误,请核查");
  155. }
  156. else
  157. {
  158. //表示修改
  159. dictionaryItem.OrderNo = (short)dictionaryItemView.OrderNo;
  160. //dictionaryItem.Code = dictionaryItemView.Code;
  161. dictionaryItem.Name = dictionaryItemView.Name;
  162. //dictionaryItem.IsEditable = dictionaryItemView.IsEditable;
  163. dictionaryItem.RecordStatus = dictionaryItemView.IsVisible == true ? (int)SYS_STATUS.USABLE : (int)SYS_STATUS.UNUSABLE;
  164. //SetModifyStatus(dictionaryItem);
  165. }
  166. }
  167. else
  168. {
  169. //表示新增
  170. Sys_DictionaryItem dictionaryItem = new Sys_DictionaryItem();
  171. dictionaryItem.DictionaryItemID = Guid.NewGuid();
  172. dictionaryItem.DictionaryCode = dictionaryItemView.DictionaryCode;
  173. dictionaryItem.OrderNo = (short)dictionaryItemView.OrderNo;
  174. dictionaryItem.Code = dictionaryItemView.Code;
  175. dictionaryItem.Name = dictionaryItemView.Name;
  176. dictionaryItem.Value = dictionaryItemView.Value;
  177. dictionaryItem.IsEditable = true;
  178. dictionaryItem.RecordStatus = dictionaryItemView.IsVisible == true ? (int)SYS_STATUS.USABLE : (int)SYS_STATUS.UNUSABLE;
  179. //SetNewStatus(dictionaryItem, dictionaryItemView.IsVisible == true ? (int)SYS_STATUS.USABLE : (int)SYS_STATUS.UNUSABLE);
  180. UnitOfWork.Add(dictionaryItem);
  181. }
  182. }
  183. else
  184. {
  185. throw new Exception("已存在相同的数据字典,请核查");
  186. }
  187. //事务提交
  188. UnitOfWork.Commit();
  189. //清除数据缓存
  190. var key = "Dictionary_" + dictionaryItemView.DictionaryCode;
  191. CacheHelper.Remove(key);
  192. }
  193. catch (Exception ex)
  194. {
  195. throw new Exception(ex.Message);
  196. }
  197. }
  198. /// <summary>
  199. /// 删除(只可删除可编辑的信息)
  200. /// </summary>
  201. /// <param name="dictionaryItemIDList"></param>
  202. /// <returns></returns>
  203. public bool DictionaryItemDelete(IList<Guid?> dictionaryItemIDList)
  204. {
  205. try
  206. {
  207. //查询对应的数据字典信息List
  208. var dictionaryItemList = DictionaryItemDAL.DictionaryItemRepository.GetList(x => dictionaryItemIDList.Contains(x.DictionaryItemID)).ToList();
  209. if (dictionaryItemList.Any(x => x.IsEditable != true))
  210. {
  211. throw new Exception("不能对不可编辑的信息进行删除");
  212. }
  213. UnitOfWork.Delete<Sys_DictionaryItem>(x => dictionaryItemIDList.Contains(x.DictionaryItemID));
  214. UnitOfWork.Commit();
  215. foreach (var dictionaryItem in dictionaryItemList)
  216. {
  217. //清除数据缓存
  218. var key = "Dictionary_" + dictionaryItem.DictionaryCode;
  219. CacheHelper.Remove(key);
  220. }
  221. return true;
  222. }
  223. catch (Exception ex)
  224. {
  225. throw new Exception(ex.Message);
  226. }
  227. }
  228. /// <summary>
  229. /// 数据字典下拉列表
  230. /// </summary>
  231. /// <param name="configuretView"></param>
  232. /// <param name="dictionaryCode"></param>
  233. /// <param name="pageIndex"></param>
  234. /// <param name="pageSize"></param>
  235. /// <returns></returns>
  236. public IGridResultSet<DictionaryItemView> GetDictionaryViewComboGrid(ConfiguretView configuretView, string dictionaryCode, int pageIndex, int pageSize)
  237. {
  238. var query = DictionaryItemDAL.GetDictionaryItemViewQueryable(x => x.RecordStatus > (int)SYS_STATUS.UNUSABLE);
  239. if (!string.IsNullOrEmpty(dictionaryCode))
  240. {
  241. query = query.Where(x => x.DictionaryCode == dictionaryCode);
  242. }
  243. if (!string.IsNullOrEmpty(configuretView.ConditionValue))
  244. {
  245. query = query.DynamicWhere(configuretView.Attribute, configuretView.Condition, configuretView.ConditionValue);
  246. }
  247. return query.OrderBy(x => x.DictionaryCode).ThenBy(x => x.DictionaryName).ThenBy(x => x.Value).ToGridResultSet<DictionaryItemView>(pageIndex, pageSize);
  248. }
  249. /// <summary>
  250. /// 数据字典下拉列表(市)
  251. /// </summary>
  252. /// <param name="configuretView"></param>
  253. /// <param name="pageIndex"></param>
  254. /// <param name="pageSize"></param>
  255. /// <param name="proviceCode"></param>
  256. /// <returns></returns>
  257. public IGridResultSet<DictionaryItemView> GetCityComboGrid(ConfiguretView configuretView, int pageIndex, int pageSize, string proviceCode = null)
  258. {
  259. //所属地区
  260. var dictionaryCode = typeof(CF_Region).Name;
  261. //后缀"00"表示省份
  262. var query = DictionaryItemDAL.GetDictionaryItemViewQueryable(x => x.DictionaryCode == dictionaryCode && x.Code.EndsWith("00") && !x.Code.EndsWith("0000") && x.RecordStatus > (int)SYS_STATUS.UNUSABLE);
  263. if (!string.IsNullOrEmpty(proviceCode))
  264. {
  265. query = query.Where(x => x.Code.StartsWith(proviceCode));
  266. }
  267. if (!string.IsNullOrEmpty(configuretView.ConditionValue))
  268. {
  269. query = query.DynamicWhere(configuretView.Attribute, configuretView.Condition, configuretView.ConditionValue);
  270. }
  271. return query.OrderBy(x => x.DictionaryCode).ThenBy(x => x.DictionaryName).ThenBy(x => x.Value).ToGridResultSet<DictionaryItemView>(pageIndex, pageSize);
  272. }
  273. /// <summary>
  274. /// 数据字典下拉列表(县、区)
  275. /// </summary>
  276. /// <param name="configuretView"></param>
  277. /// <param name="pageIndex"></param>
  278. /// <param name="pageSize"></param>
  279. /// <param name="cityCode"></param>
  280. /// <returns></returns>
  281. public IGridResultSet<DictionaryItemView> GetDistrictComboGrid(ConfiguretView configuretView, int pageIndex, int pageSize, string cityCode = null)
  282. {
  283. //所属地区
  284. var dictionaryCode = typeof(CF_Region).Name;
  285. //后缀"00"表示省份,中间两位表示县、区
  286. var query = DictionaryItemDAL.GetDictionaryItemViewQueryable(x => x.DictionaryCode == dictionaryCode && !x.Code.EndsWith("00") && !x.Code.EndsWith("0000") && x.RecordStatus > (int)SYS_STATUS.UNUSABLE);
  287. if (!string.IsNullOrEmpty(cityCode))
  288. {
  289. query = query.Where(x => x.Code.StartsWith(cityCode.Substring(0,4)));
  290. }
  291. if (!string.IsNullOrEmpty(configuretView.ConditionValue))
  292. {
  293. query = query.DynamicWhere(configuretView.Attribute, configuretView.Condition, configuretView.ConditionValue);
  294. }
  295. return query.OrderBy(x => x.DictionaryCode).ThenBy(x => x.DictionaryName).ThenBy(x => x.Value).ToGridResultSet<DictionaryItemView>(pageIndex, pageSize);
  296. }
  297. }
  298. }