You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

289 lines
9.5 KiB

  1. using Learun.Application.Organization;
  2. using Learun.Cache.Base;
  3. using Learun.Cache.Factory;
  4. using Learun.Util;
  5. using System;
  6. using System.Collections.Generic;
  7. namespace Learun.Application.Base.SystemModule
  8. {
  9. /// <summary>
  10. /// 版 本 Learun-ADMS-Ultimate V7.0.0 数字化智慧校园
  11. /// Copyright (c) 2013-2018 北京泉江科技有限公司
  12. /// 创建人:陈彬彬
  13. /// 日 期:2017.03.08
  14. /// 描 述:自定义查询
  15. /// </summary>
  16. public class CustmerQueryBLL : CustmerQueryIBLL
  17. {
  18. #region 属性
  19. private CustmerQueryService custmerQueryService = new CustmerQueryService();
  20. #endregion
  21. #region 缓存定义
  22. private ICache cache = CacheFactory.CaChe();
  23. private string cacheKey = "learun_adms_custmerQuery_";// +功能地址 + 用户主键(可无)
  24. #endregion
  25. #region 获取数据
  26. /// <summary>
  27. /// 获取自定义查询(公共)分页数据
  28. /// </summary>
  29. /// <param name="pagination">分页参数</param>
  30. /// <param name="keyword">关键字</param>
  31. /// <returns></returns>
  32. public IEnumerable<CustmerQueryEntity> GetPageList(Pagination pagination, string keyword)
  33. {
  34. try
  35. {
  36. return custmerQueryService.GetPageList(pagination, keyword);
  37. }
  38. catch (Exception ex)
  39. {
  40. if (ex is ExceptionEx)
  41. {
  42. throw;
  43. }
  44. else
  45. {
  46. throw ExceptionEx.ThrowServiceException(ex);
  47. }
  48. }
  49. }
  50. /// <summary>
  51. /// 获取自定义查询条件
  52. /// </summary>
  53. /// <param name="moduleUrl">访问的功能链接地址</param>
  54. /// <param name="userId">用户ID(用户ID为null表示公共)</param>
  55. /// <returns></returns>
  56. public List<CustmerQueryEntity> GetList(string moduleUrl, string userId)
  57. {
  58. try
  59. {
  60. string key = cacheKey + moduleUrl;
  61. if (!string.IsNullOrEmpty(userId))
  62. {
  63. key += '_' + userId;
  64. }
  65. List<CustmerQueryEntity> list = cache.Read<List<CustmerQueryEntity>>(key, CacheId.custmerQuery);
  66. if (list == null)
  67. {
  68. list = (List<CustmerQueryEntity>)custmerQueryService.GetList(moduleUrl, userId);
  69. cache.Write<List<CustmerQueryEntity>>(key, list, CacheId.custmerQuery);
  70. }
  71. return list;
  72. }
  73. catch (Exception ex)
  74. {
  75. if (ex is ExceptionEx)
  76. {
  77. throw;
  78. }
  79. else
  80. {
  81. throw ExceptionEx.ThrowBusinessException(ex);
  82. }
  83. }
  84. }
  85. /// <summary>
  86. /// 获取自定义查询条件(用于具体使用)
  87. /// </summary>
  88. /// <param name="moduleUrl"></param>
  89. /// <param name="userId"></param>
  90. /// <returns></returns>
  91. public List<CustmerQueryEntity> GetCustmerList(string moduleUrl, string userId)
  92. {
  93. try
  94. {
  95. List<CustmerQueryEntity> list = GetList(moduleUrl, userId);
  96. List<CustmerQueryEntity> publiclist = GetList(moduleUrl, "");
  97. publiclist.AddRange(list);
  98. return publiclist;
  99. }
  100. catch (Exception ex)
  101. {
  102. if (ex is ExceptionEx)
  103. {
  104. throw;
  105. }
  106. else
  107. {
  108. throw ExceptionEx.ThrowBusinessException(ex);
  109. }
  110. }
  111. }
  112. #endregion
  113. #region 提交数据
  114. /// <summary>
  115. /// 删除自定义查询条件
  116. /// </summary>
  117. /// <param name="keyValue">主键</param>
  118. public void DeleteEntity(string keyValue)
  119. {
  120. try
  121. {
  122. CustmerQueryEntity custmerQueryEntity = custmerQueryService.GetEntity(keyValue);
  123. string key = cacheKey + custmerQueryEntity.F_ModuleUrl;
  124. if (!string.IsNullOrEmpty(custmerQueryEntity.F_UserId))
  125. {
  126. key += '_' + custmerQueryEntity.F_UserId;
  127. }
  128. custmerQueryService.DeleteEntity(keyValue);
  129. cache.Remove(key, CacheId.custmerQuery);
  130. }
  131. catch (Exception ex)
  132. {
  133. if (ex is ExceptionEx)
  134. {
  135. throw;
  136. }
  137. else
  138. {
  139. throw ExceptionEx.ThrowBusinessException(ex);
  140. }
  141. }
  142. }
  143. /// <summary>
  144. /// 保存自定义查询(新增、修改)
  145. /// </summary>
  146. /// <param name="keyValue">主键值</param>
  147. /// <param name="departmentEntity">部门实体</param>
  148. /// <returns></returns>
  149. public void SaveEntity(string keyValue, CustmerQueryEntity custmerQueryEntity)
  150. {
  151. try
  152. {
  153. string key = cacheKey + custmerQueryEntity.F_ModuleUrl;
  154. if (!string.IsNullOrEmpty(custmerQueryEntity.F_UserId))
  155. {
  156. key += '_' + custmerQueryEntity.F_UserId;
  157. }
  158. custmerQueryService.SaveEntity(keyValue, custmerQueryEntity);
  159. cache.Remove(key, CacheId.custmerQuery);
  160. }
  161. catch (Exception ex)
  162. {
  163. if (ex is ExceptionEx)
  164. {
  165. throw;
  166. }
  167. else
  168. {
  169. throw ExceptionEx.ThrowBusinessException(ex);
  170. }
  171. }
  172. }
  173. #endregion
  174. #region 扩展方法
  175. /// <summary>
  176. /// 将条件转化成sql语句
  177. /// </summary>
  178. /// <param name="queryJson">查询条件</param>
  179. /// <param name="formula">公式,没有就默认采用and连接</param>
  180. /// <returns></returns>
  181. public string ConditionToSql(string queryJson, string formula, UserEntity userEntity)
  182. {
  183. try
  184. {
  185. string strSql = "";
  186. List<CustmerQueryModel> list = queryJson.ToObject<List<CustmerQueryModel>>();
  187. if (string.IsNullOrEmpty(formula))
  188. {
  189. for (int i = 1; i < list.Count + 1; i++)
  190. {
  191. strSql = formula.Replace("" + i, "{@learun" + i + "learun@}");
  192. }
  193. }
  194. else
  195. {
  196. for (int i = 1; i < list.Count + 1; i++)
  197. {
  198. if (strSql != "")
  199. {
  200. strSql += " AND ";
  201. }
  202. strSql += " {@learun" + i + "learun@} ";
  203. }
  204. }
  205. int num = 1;
  206. foreach (var item in list)
  207. {
  208. string strone =" " + item.field;
  209. string value = getValue(item.type, item.value, userEntity);
  210. switch (item.condition)
  211. {
  212. case 1:// 等于
  213. strone += " = " + value;
  214. break;
  215. case 6:// 不等于
  216. strone += " != " + value;
  217. break;
  218. case 7:// 包含
  219. strone += " like %" + value+"%";
  220. break;
  221. case 8:// 不包含
  222. strone += " not like %" + value + "%";
  223. break;
  224. default:
  225. break;
  226. }
  227. strone += " ";
  228. strSql.Replace("{@learun" + num + "learun@}", strone);
  229. num++;
  230. }
  231. return strSql;
  232. }
  233. catch (Exception ex)
  234. {
  235. if (ex is ExceptionEx)
  236. {
  237. throw;
  238. }
  239. else
  240. {
  241. throw ExceptionEx.ThrowBusinessException(ex);
  242. }
  243. }
  244. }
  245. /// <summary>
  246. /// 获取数据
  247. /// </summary>
  248. /// <param name="type">数据类型</param>
  249. /// <param name="value">数据值</param>
  250. /// <returns></returns>
  251. private string getValue(int type, string value, UserEntity userEntity)
  252. {
  253. string text = "";
  254. switch (type)
  255. {
  256. case 1:// 文本
  257. text = value;
  258. break;
  259. case 4:// 当前账号
  260. text = userEntity.F_UserId;
  261. break;
  262. case 5:// 当前公司
  263. text = userEntity.F_CompanyId;
  264. break;
  265. case 6:// 当前部门
  266. text = userEntity.F_DepartmentId;
  267. break;
  268. case 7:// 当前岗位
  269. //text = userEntity.F_PostId;
  270. break;
  271. default:
  272. text = value;
  273. break;
  274. }
  275. return text;
  276. }
  277. #endregion
  278. }
  279. }