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.
 
 
 
 
 
 

396 lines
12 KiB

  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="strWhere">sql查询条件语句</param>
  199. /// <param name="queryJson">查询条件</param>
  200. /// <returns></returns>
  201. public DataTable GetDataTable(DataSourceEntity entity, string strWhere, string queryJson = "{}")
  202. {
  203. try
  204. {
  205. if (entity == null)
  206. {
  207. return new DataTable();
  208. }
  209. else
  210. {
  211. if (!string.IsNullOrEmpty(strWhere))
  212. {
  213. strWhere = " where " + strWhere;
  214. }
  215. else
  216. {
  217. strWhere = "";
  218. }
  219. var queryParam = queryJson.ToJObject();
  220. string sql = string.Format(" select * From ({0})t {1} ", entity.F_Sql, strWhere);
  221. return databaseLinkIBLL.FindTable(entity.F_DbId, sql, queryParam);
  222. }
  223. }
  224. catch (Exception ex)
  225. {
  226. if (ex is ExceptionEx)
  227. {
  228. throw;
  229. }
  230. else
  231. {
  232. throw ExceptionEx.ThrowBusinessException(ex);
  233. }
  234. }
  235. }
  236. /// <summary>
  237. /// 获取树形数据
  238. /// </summary>
  239. /// <param name="code">编码</param>
  240. /// <param name="parentId">父级ID</param>
  241. /// <param name="Id">ID</param>
  242. /// <param name="showId">显示ID</param>
  243. /// <returns></returns>
  244. public List<TreeModel> GetTree(string code, string parentId, string Id, string showId)
  245. {
  246. try
  247. {
  248. DataSourceEntity entity = GetEntityByCode(code);
  249. if (entity == null)
  250. {
  251. return new List<TreeModel>();
  252. }
  253. else
  254. {
  255. DataTable list = databaseLinkIBLL.FindTable(entity.F_DbId, entity.F_Sql, new { });
  256. List<TreeModel> treeList = new List<TreeModel>();
  257. foreach (DataRow item in list.Rows)
  258. {
  259. TreeModel node = new TreeModel
  260. {
  261. id = item[Id].ToString(),
  262. text = item[showId].ToString(),
  263. value = item[Id].ToString(),
  264. showcheck = false,
  265. checkstate = 0,
  266. isexpand = true,
  267. parentId = item[parentId].ToString()
  268. };
  269. treeList.Add(node);
  270. }
  271. return treeList.ToTree();
  272. }
  273. }
  274. catch (Exception ex)
  275. {
  276. if (ex is ExceptionEx)
  277. {
  278. throw;
  279. }
  280. else
  281. {
  282. throw ExceptionEx.ThrowBusinessException(ex);
  283. }
  284. }
  285. }
  286. /// <summary>
  287. /// 获取数据源的数据(分页)
  288. /// </summary>
  289. /// <param name="code">数据源编码</param>
  290. /// <param name="pagination">分页参数</param>
  291. /// <param name="queryJson">查询条件</param>
  292. /// <returns></returns>
  293. public DataTable GetDataTable(string code, Pagination pagination, string strWhere, string queryJson = "{}")
  294. {
  295. try
  296. {
  297. DataSourceEntity entity = GetEntityByCode(code);
  298. if (entity == null)
  299. {
  300. return new DataTable();
  301. }
  302. else
  303. {
  304. if (!string.IsNullOrEmpty(strWhere))
  305. {
  306. strWhere = " where " + strWhere;
  307. }
  308. else
  309. {
  310. strWhere = "";
  311. }
  312. var queryParam = queryJson.ToJObject();
  313. string sql = string.Format(" select t.* From ({0})t {1} ", entity.F_Sql, strWhere);
  314. return databaseLinkIBLL.FindTable(entity.F_DbId, sql, queryParam, pagination);
  315. }
  316. }
  317. catch (Exception ex)
  318. {
  319. if (ex is ExceptionEx)
  320. {
  321. throw;
  322. }
  323. else
  324. {
  325. throw ExceptionEx.ThrowBusinessException(ex);
  326. }
  327. }
  328. }
  329. /// <summary>
  330. /// 获取数据源列名
  331. /// </summary>
  332. /// <param name="code">数据源编码</param>
  333. /// <returns></returns>
  334. public List<string> GetDataColName(string code)
  335. {
  336. try
  337. {
  338. Pagination pagination = new Pagination()
  339. {
  340. rows = 200,
  341. page = 0,
  342. sord = "",
  343. sidx = ""
  344. };
  345. DataTable dt = GetDataTable(code, "");
  346. List<string> res = new List<string>();
  347. foreach (DataColumn item in dt.Columns)
  348. {
  349. if (item.ColumnName != "rownum")
  350. {
  351. res.Add(item.ColumnName);
  352. }
  353. }
  354. return res;
  355. }
  356. catch (Exception ex)
  357. {
  358. if (ex is ExceptionEx)
  359. {
  360. throw;
  361. }
  362. else
  363. {
  364. throw ExceptionEx.ThrowBusinessException(ex);
  365. }
  366. }
  367. }
  368. public string GetKeyByValue(string code, string key, string keyText, string value)
  369. {
  370. string strWhere = " " + key + " =@" + key;
  371. string queryJson = "{" + key + ":\"" + keyText + "\"}";
  372. DataTable sourceDt = GetDataTable(code, strWhere, queryJson);
  373. if (sourceDt.Rows.Count > 0)
  374. {
  375. return sourceDt.Rows[0][value].ToString();
  376. }
  377. else
  378. {
  379. return "";
  380. }
  381. }
  382. #endregion
  383. }
  384. }