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.

DataSourceBLL.cs 11 KiB

4 years ago
4 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350
  1. using Learun.Cache.Base;
  2. using Learun.Cache.Factory;
  3. using Learun.Util;
  4. using System;
  5. using System.Collections.Generic;
  6. using System.Data;
  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 DataSourceBLL : DataSourceIBLL
  17. {
  18. private DataSourceService dataSourceService = new DataSourceService();
  19. private DatabaseLinkIBLL databaseLinkIBLL = new DatabaseLinkBLL();
  20. #region 缓存定义
  21. private ICache cache = CacheFactory.CaChe();
  22. private string cacheKey = "Learun_adms_datasource_";// +编号
  23. #endregion
  24. #region 获取数据
  25. /// <summary>
  26. /// 获取分页数据
  27. /// </summary>
  28. /// <param name="pagination">分页参数</param>
  29. /// <param name="keyword">关键字</param>
  30. /// <returns></returns>
  31. public IEnumerable<DataSourceEntity> GetPageList(Pagination pagination, string keyword)
  32. {
  33. try
  34. {
  35. return dataSourceService.GetPageList(pagination, keyword);
  36. }
  37. catch (Exception ex)
  38. {
  39. if (ex is ExceptionEx)
  40. {
  41. throw;
  42. }
  43. else
  44. {
  45. throw ExceptionEx.ThrowBusinessException(ex);
  46. }
  47. }
  48. }
  49. /// <summary>
  50. /// 获取列表数据
  51. /// </summary>
  52. /// <returns></returns>
  53. public IEnumerable<DataSourceEntity> GetList()
  54. {
  55. try
  56. {
  57. return dataSourceService.GetList();
  58. }
  59. catch (Exception ex)
  60. {
  61. if (ex is ExceptionEx)
  62. {
  63. throw;
  64. }
  65. else
  66. {
  67. throw ExceptionEx.ThrowBusinessException(ex);
  68. }
  69. }
  70. }
  71. /// <summary>
  72. /// 获取实体
  73. /// </summary>
  74. /// <param name="code">编号</param>
  75. /// <returns></returns>
  76. public DataSourceEntity GetEntityByCode(string code)
  77. {
  78. try
  79. {
  80. DataSourceEntity entity = dataSourceService.GetEntityByCode(code);
  81. return entity;
  82. }
  83. catch (Exception ex)
  84. {
  85. if (ex is ExceptionEx)
  86. {
  87. throw;
  88. }
  89. else
  90. {
  91. throw ExceptionEx.ThrowBusinessException(ex);
  92. }
  93. }
  94. }
  95. #endregion
  96. #region 提交数据
  97. /// <summary>
  98. /// 删除数据源
  99. /// </summary>
  100. /// <param name="keyValue">主键</param>
  101. public void DeleteEntity(string keyValue)
  102. {
  103. try
  104. {
  105. DataSourceEntity entity = dataSourceService.GetEntity(keyValue);
  106. cache.Remove(cacheKey + entity.F_Code, CacheId.dataSource);
  107. dataSourceService.DeleteEntity(keyValue);
  108. }
  109. catch (Exception ex)
  110. {
  111. if (ex is ExceptionEx)
  112. {
  113. throw;
  114. }
  115. else
  116. {
  117. throw ExceptionEx.ThrowBusinessException(ex);
  118. }
  119. }
  120. }
  121. /// <summary>
  122. /// 保存(新增、修改)
  123. /// </summary>
  124. /// <param name="keyValue">主键值</param>
  125. /// <param name="dataSourceEntity">数据源实体</param>
  126. /// <returns></returns>
  127. public bool SaveEntity(string keyValue, DataSourceEntity dataSourceEntity)
  128. {
  129. try
  130. {
  131. if (!string.IsNullOrEmpty(keyValue))
  132. {
  133. cache.Remove(cacheKey + dataSourceEntity.F_Code, CacheId.dataSource);
  134. }
  135. return dataSourceService.SaveEntity(keyValue, dataSourceEntity);
  136. }
  137. catch (Exception ex)
  138. {
  139. if (ex is ExceptionEx)
  140. {
  141. throw;
  142. }
  143. else
  144. {
  145. throw ExceptionEx.ThrowBusinessException(ex);
  146. }
  147. }
  148. }
  149. #endregion
  150. #region 扩展方法
  151. /// <summary>
  152. /// 获取数据源的数据
  153. /// </summary>
  154. /// <param name="code">数据源编码</param>
  155. /// <param name="strWhere">sql查询条件语句</param>
  156. /// <param name="queryJson">查询条件</param>
  157. /// <returns></returns>
  158. public DataTable GetDataTable(string code, string strWhere, string queryJson = "{}")
  159. {
  160. try
  161. {
  162. DataSourceEntity entity = GetEntityByCode(code);
  163. if (entity == null)
  164. {
  165. return new DataTable();
  166. }
  167. else
  168. {
  169. if (!string.IsNullOrEmpty(strWhere))
  170. {
  171. strWhere = " where " + strWhere;
  172. }
  173. else
  174. {
  175. strWhere = "";
  176. }
  177. var queryParam = queryJson.ToJObject();
  178. string sql = string.Format(" select * From ({0})t {1} ", entity.F_Sql, strWhere);
  179. return databaseLinkIBLL.FindTable(entity.F_DbId, sql, queryParam);
  180. }
  181. }
  182. catch (Exception ex)
  183. {
  184. if (ex is ExceptionEx)
  185. {
  186. throw;
  187. }
  188. else
  189. {
  190. throw ExceptionEx.ThrowBusinessException(ex);
  191. }
  192. }
  193. }
  194. /// <summary>
  195. /// 获取树形数据
  196. /// </summary>
  197. /// <param name="code">编码</param>
  198. /// <param name="parentId">父级ID</param>
  199. /// <param name="Id">ID</param>
  200. /// <param name="showId">显示ID</param>
  201. /// <returns></returns>
  202. public List<TreeModel> GetTree(string code, string parentId, string Id, string showId)
  203. {
  204. try
  205. {
  206. DataSourceEntity entity = GetEntityByCode(code);
  207. if (entity == null)
  208. {
  209. return new List<TreeModel>();
  210. }
  211. else
  212. {
  213. DataTable list = databaseLinkIBLL.FindTable(entity.F_DbId, entity.F_Sql, new { });
  214. List<TreeModel> treeList = new List<TreeModel>();
  215. foreach (DataRow item in list.Rows)
  216. {
  217. TreeModel node = new TreeModel
  218. {
  219. id = item[Id].ToString(),
  220. text = item[showId].ToString(),
  221. value = item[Id].ToString(),
  222. showcheck = false,
  223. checkstate = 0,
  224. isexpand = true,
  225. parentId = item[parentId].ToString()
  226. };
  227. treeList.Add(node);
  228. }
  229. return treeList.ToTree();
  230. }
  231. }
  232. catch (Exception ex)
  233. {
  234. if (ex is ExceptionEx)
  235. {
  236. throw;
  237. }
  238. else
  239. {
  240. throw ExceptionEx.ThrowBusinessException(ex);
  241. }
  242. }
  243. }
  244. /// <summary>
  245. /// 获取数据源的数据(分页)
  246. /// </summary>
  247. /// <param name="code">数据源编码</param>
  248. /// <param name="pagination">分页参数</param>
  249. /// <param name="queryJson">查询条件</param>
  250. /// <returns></returns>
  251. public DataTable GetDataTable(string code, Pagination pagination, string strWhere, string queryJson = "{}")
  252. {
  253. try
  254. {
  255. DataSourceEntity entity = GetEntityByCode(code);
  256. if (entity == null)
  257. {
  258. return new DataTable();
  259. }
  260. else
  261. {
  262. if (!string.IsNullOrEmpty(strWhere))
  263. {
  264. strWhere = " where " + strWhere;
  265. }
  266. else
  267. {
  268. strWhere = "";
  269. }
  270. var queryParam = queryJson.ToJObject();
  271. string sql = string.Format(" select t.* From ({0})t {1} ", entity.F_Sql, strWhere);
  272. return databaseLinkIBLL.FindTable(entity.F_DbId, sql, queryParam, pagination);
  273. }
  274. }
  275. catch (Exception ex)
  276. {
  277. if (ex is ExceptionEx)
  278. {
  279. throw;
  280. }
  281. else
  282. {
  283. throw ExceptionEx.ThrowBusinessException(ex);
  284. }
  285. }
  286. }
  287. /// <summary>
  288. /// 获取数据源列名
  289. /// </summary>
  290. /// <param name="code">数据源编码</param>
  291. /// <returns></returns>
  292. public List<string> GetDataColName(string code)
  293. {
  294. try
  295. {
  296. Pagination pagination = new Pagination()
  297. {
  298. rows = 200,
  299. page = 0,
  300. sord = "",
  301. sidx = ""
  302. };
  303. DataTable dt = GetDataTable(code, "");
  304. List<string> res = new List<string>();
  305. foreach (DataColumn item in dt.Columns)
  306. {
  307. if (item.ColumnName != "rownum")
  308. {
  309. res.Add(item.ColumnName);
  310. }
  311. }
  312. return res;
  313. }
  314. catch (Exception ex)
  315. {
  316. if (ex is ExceptionEx)
  317. {
  318. throw;
  319. }
  320. else
  321. {
  322. throw ExceptionEx.ThrowBusinessException(ex);
  323. }
  324. }
  325. }
  326. public string GetKeyByValue(string code, string key, string keyText, string value)
  327. {
  328. string strWhere = " " + key + " =@" + key;
  329. string queryJson = "{" + key + ":\"" + keyText + "\"}";
  330. DataTable sourceDt = GetDataTable(code, strWhere, queryJson);
  331. if (sourceDt.Rows.Count > 0)
  332. {
  333. return sourceDt.Rows[0][value].ToString();
  334. }
  335. else
  336. {
  337. return "";
  338. }
  339. }
  340. #endregion
  341. }
  342. }