DictionaryItemServices.cs 15 KB

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