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.

DatabaseLinkBLL.cs 16 KiB

4 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490
  1. using Learun.Cache.Base;
  2. using Learun.Cache.Factory;
  3. using Learun.DataBase.Repository;
  4. using Learun.Util;
  5. using System;
  6. using System.Collections.Generic;
  7. using System.Data;
  8. namespace Learun.Application.Base.SystemModule
  9. {
  10. /// <summary>
  11. /// 版 本 Learun-ADMS-Ultimate V7.0.0 数字化智慧校园
  12. /// Copyright (c) 2013-2018 北京泉江科技有限公司
  13. /// 创建人:陈彬彬
  14. /// 日 期:2017.03.08
  15. /// 描 述:数据库连接
  16. /// </summary>
  17. public class DatabaseLinkBLL : DatabaseLinkIBLL
  18. {
  19. #region 属性
  20. private DatabaseLinkService databaseLinkService = new DatabaseLinkService();
  21. #endregion
  22. #region 缓存定义
  23. private ICache cache = CacheFactory.CaChe();
  24. private string cacheKey = "Learun_adms_databaseLink";
  25. #endregion
  26. #region 获取数据
  27. /// <summary>
  28. /// 获取列表数据
  29. /// </summary>
  30. /// <returns></returns>
  31. public List<DatabaseLinkEntity> GetList()
  32. {
  33. try
  34. {
  35. List<DatabaseLinkEntity> list = cache.Read<List<DatabaseLinkEntity>>(cacheKey, CacheId.database);
  36. if (list == null)
  37. {
  38. list = (List<DatabaseLinkEntity>)databaseLinkService.GetList();
  39. cache.Write<List<DatabaseLinkEntity>>(cacheKey, list, CacheId.database);
  40. }
  41. return list;
  42. }
  43. catch (Exception ex)
  44. {
  45. if (ex is ExceptionEx)
  46. {
  47. throw;
  48. }
  49. else
  50. {
  51. throw ExceptionEx.ThrowBusinessException(ex);
  52. }
  53. }
  54. }
  55. /// <summary>
  56. /// 获取映射数据
  57. /// </summary>
  58. /// <returns></returns>
  59. public Dictionary<string, DatabaseLinkModel> GetMap() {
  60. try
  61. {
  62. //Dictionary<string, DatabaseLinkModel> dic = cache.Read<Dictionary<string, DatabaseLinkModel>>(cacheKey + "dic", CacheId.database);
  63. //liangkun 去掉数据库缓存
  64. Dictionary<string, DatabaseLinkModel> dic = null;
  65. if (dic == null)
  66. {
  67. dic = new Dictionary<string, DatabaseLinkModel>();
  68. List<DatabaseLinkEntity> list = GetList();
  69. foreach (var item in list)
  70. {
  71. dic.Add(item.F_DatabaseLinkId, new DatabaseLinkModel()
  72. {
  73. alias = item.F_DBAlias,
  74. name = item.F_DBName
  75. });
  76. }
  77. //cache.Write(cacheKey + "dic", dic, CacheId.database);
  78. }
  79. return dic;
  80. }
  81. catch (Exception ex)
  82. {
  83. if (ex is ExceptionEx)
  84. {
  85. throw;
  86. }
  87. else
  88. {
  89. throw ExceptionEx.ThrowBusinessException(ex);
  90. }
  91. }
  92. }
  93. /// <summary>
  94. /// 获取列表数据(去掉连接串地址信息)
  95. /// </summary>
  96. /// <returns></returns>
  97. public List<DatabaseLinkEntity> GetListByNoConnection()
  98. {
  99. try
  100. {
  101. List<DatabaseLinkEntity> list = GetList();
  102. foreach (var item in list) {
  103. item.F_DbConnection = "******";
  104. }
  105. return list;
  106. }
  107. catch (Exception ex)
  108. {
  109. if (ex is ExceptionEx)
  110. {
  111. throw;
  112. }
  113. else
  114. {
  115. throw ExceptionEx.ThrowBusinessException(ex);
  116. }
  117. }
  118. }
  119. /// <summary>
  120. /// 获取列表数据
  121. /// </summary>
  122. /// <param name="keyword">关键字</param>
  123. /// <returns></returns>
  124. public List<DatabaseLinkEntity> GetListByNoConnection(string keyword)
  125. {
  126. try
  127. {
  128. List<DatabaseLinkEntity> list = GetListByNoConnection();
  129. if (!string.IsNullOrEmpty(keyword))
  130. {
  131. list = list.FindAll(t => t.F_DBName.Contains(keyword) || t.F_DBAlias.Contains(keyword) || t.F_ServerAddress.Contains(keyword));
  132. }
  133. return list;
  134. }
  135. catch (Exception ex)
  136. {
  137. if (ex is ExceptionEx)
  138. {
  139. throw;
  140. }
  141. else
  142. {
  143. throw ExceptionEx.ThrowBusinessException(ex);
  144. }
  145. }
  146. }
  147. /// <summary>
  148. /// 获取树形数据
  149. /// </summary>
  150. /// <returns></returns>
  151. public List<TreeModel> GetTreeList() {
  152. List<DatabaseLinkEntity> list = GetList();
  153. List<TreeModel> treelist = new List<TreeModel>();
  154. Dictionary<string, List<TreeModel>> dic = new Dictionary<string, List<TreeModel>>();
  155. foreach (var item in list) {
  156. TreeModel node = new TreeModel();
  157. node.id = item.F_DatabaseLinkId;
  158. node.text = item.F_DBAlias;
  159. node.value = item.F_DBName;
  160. node.complete = true;
  161. node.hasChildren = false;
  162. if (!dic.ContainsKey(item.F_ServerAddress))
  163. {
  164. TreeModel pnode = new TreeModel();
  165. pnode.id = item.F_ServerAddress;
  166. pnode.text = item.F_ServerAddress;
  167. pnode.value = "LearunServerAddress";
  168. pnode.isexpand = true;
  169. pnode.complete = true;
  170. pnode.hasChildren = true;
  171. pnode.ChildNodes = new List<TreeModel>();
  172. treelist.Add(pnode);
  173. dic.Add(item.F_ServerAddress, pnode.ChildNodes);
  174. }
  175. dic[item.F_ServerAddress].Add(node);
  176. }
  177. return treelist;
  178. }
  179. /// <summary>
  180. /// 获取树形数据
  181. /// </summary>
  182. /// <returns></returns>
  183. public List<TreeModel> GetTreeListEx()
  184. {
  185. List<DatabaseLinkEntity> list = GetList();
  186. List<TreeModel> treelist = new List<TreeModel>();
  187. Dictionary<string, List<TreeModel>> dic = new Dictionary<string, List<TreeModel>>();
  188. foreach (var item in list)
  189. {
  190. TreeModel node = new TreeModel();
  191. node.id = item.F_DatabaseLinkId;
  192. node.text = item.F_DBAlias;
  193. node.value = item.F_DBName;
  194. node.complete = false;
  195. node.isexpand = false;
  196. node.hasChildren = true;
  197. if (!dic.ContainsKey(item.F_ServerAddress))
  198. {
  199. TreeModel pnode = new TreeModel();
  200. pnode.id = item.F_ServerAddress;
  201. pnode.text = item.F_ServerAddress;
  202. pnode.value = "LearunServerAddress";
  203. pnode.isexpand = true;
  204. pnode.complete = true;
  205. pnode.hasChildren = true;
  206. pnode.ChildNodes = new List<TreeModel>();
  207. treelist.Add(pnode);
  208. dic.Add(item.F_ServerAddress, pnode.ChildNodes);
  209. }
  210. dic[item.F_ServerAddress].Add(node);
  211. }
  212. return treelist;
  213. }
  214. /// <summary>
  215. /// 获取数据连接实体
  216. /// </summary>
  217. /// <param name="databaseLinkId">主键</param>
  218. /// <returns></returns>
  219. public DatabaseLinkEntity GetEntity(string databaseLinkId) {
  220. try
  221. {
  222. List<DatabaseLinkEntity> list = GetList();
  223. return list.Find(t => t.F_DatabaseLinkId.Equals(databaseLinkId));
  224. }
  225. catch (Exception ex)
  226. {
  227. if (ex is ExceptionEx)
  228. {
  229. throw;
  230. }
  231. else
  232. {
  233. throw ExceptionEx.ThrowBusinessException(ex);
  234. }
  235. }
  236. }
  237. #endregion
  238. #region 提交数据
  239. /// <summary>
  240. /// 删除自定义查询条件
  241. /// </summary>
  242. /// <param name="keyValue">主键</param>
  243. public void VirtualDelete(string keyValue)
  244. {
  245. try
  246. {
  247. cache.Remove(cacheKey, CacheId.database);
  248. cache.Remove(cacheKey + "dic", CacheId.database);
  249. databaseLinkService.VirtualDelete(keyValue);
  250. }
  251. catch (Exception ex)
  252. {
  253. if (ex is ExceptionEx)
  254. {
  255. throw;
  256. }
  257. else
  258. {
  259. throw ExceptionEx.ThrowBusinessException(ex);
  260. }
  261. }
  262. }
  263. /// <summary>
  264. /// 保存自定义查询(新增、修改)
  265. /// </summary>
  266. /// <param name="keyValue">主键值</param>
  267. /// <param name="departmentEntity">部门实体</param>
  268. /// <returns></returns>
  269. public bool SaveEntity(string keyValue, DatabaseLinkEntity databaseLinkEntity)
  270. {
  271. try
  272. {
  273. cache.Remove(cacheKey, CacheId.database);
  274. cache.Remove(cacheKey+"dic", CacheId.database);
  275. return databaseLinkService.SaveEntity(keyValue, databaseLinkEntity);
  276. }
  277. catch (Exception ex)
  278. {
  279. if (ex is ExceptionEx)
  280. {
  281. throw;
  282. }
  283. else
  284. {
  285. throw ExceptionEx.ThrowBusinessException(ex);
  286. }
  287. }
  288. }
  289. #endregion
  290. #region 扩展方法
  291. /// <summary>
  292. /// 测试数据数据库是否能连接成功
  293. /// </summary>
  294. /// <param name="connection">连接串</param>
  295. /// <param name="dbType">数据库类型</param>
  296. /// <param name="keyValue">主键</param>
  297. public bool TestConnection(string connection, string dbType, string keyValue)
  298. {
  299. if (!string.IsNullOrEmpty(keyValue) && connection == "******") {
  300. DatabaseLinkEntity entity = GetEntity(keyValue);
  301. connection = entity.F_DbConnection;
  302. }
  303. return databaseLinkService.TestConnection(connection, dbType);
  304. }
  305. /// <summary>
  306. /// 根据指定数据库执行sql语句
  307. /// </summary>
  308. /// <param name="databaseLinkId">数据库主键</param>
  309. /// <param name="sql">sql语句</param>
  310. /// <param name="dbParameter">参数</param>
  311. public void ExecuteBySql(string databaseLinkId, string sql,object dbParameter=null)
  312. {
  313. try
  314. {
  315. DatabaseLinkEntity entity = GetEntity(databaseLinkId);
  316. databaseLinkService.ExecuteBySql(entity, sql, dbParameter);
  317. }
  318. catch (Exception ex)
  319. {
  320. if (ex is ExceptionEx)
  321. {
  322. throw;
  323. }
  324. else
  325. {
  326. throw ExceptionEx.ThrowBusinessException(ex);
  327. }
  328. }
  329. }
  330. /// <summary>
  331. /// 根据指定数据库执行存储过程
  332. /// </summary>
  333. /// <param name="databaseLinkId">数据库主键</param>
  334. /// <param name="procName">存储过程</param>
  335. /// <param name="dbParameter">参数</param>
  336. public void ExecuteByProc(string databaseLinkId, string procName, object dbParameter = null)
  337. {
  338. try
  339. {
  340. DatabaseLinkEntity entity = null;
  341. if (databaseLinkId == "systemdb")// 本地数据库
  342. {
  343. entity = new DatabaseLinkEntity()
  344. {
  345. F_DatabaseLinkId = "systemdb"
  346. };
  347. }
  348. else
  349. {
  350. entity = GetEntity(databaseLinkId);
  351. }
  352. databaseLinkService.ExecuteByProc(entity, procName, dbParameter);
  353. }
  354. catch (Exception ex)
  355. {
  356. if (ex is ExceptionEx)
  357. {
  358. throw;
  359. }
  360. else
  361. {
  362. throw ExceptionEx.ThrowBusinessException(ex);
  363. }
  364. }
  365. }
  366. /// <summary>
  367. /// 根据数据库执行sql语句,查询数据->datatable
  368. /// </summary>
  369. /// <param name="databaseLinkId">数据库主键</param>
  370. /// <param name="sql">sql语句</param>
  371. /// <param name="dbParameter">参数</param>
  372. /// <returns></returns>
  373. public DataTable FindTable(string databaseLinkId, string sql, object dbParameter = null)
  374. {
  375. try
  376. {
  377. DatabaseLinkEntity entity = GetEntity(databaseLinkId);
  378. return databaseLinkService.FindTable(entity, sql, dbParameter);
  379. }
  380. catch (Exception ex)
  381. {
  382. if (ex is ExceptionEx)
  383. {
  384. throw;
  385. }
  386. else
  387. {
  388. throw ExceptionEx.ThrowBusinessException(ex);
  389. }
  390. }
  391. }
  392. /// <summary>
  393. /// 根据数据库执行sql语句,查询数据->datatable(分页)
  394. /// </summary>
  395. /// <param name="databaseLinkId">数据库主键</param>
  396. /// <param name="sql">sql语句</param>
  397. /// <param name="dbParameter">参数</param>
  398. /// <param name="pagination">分页参数</param>
  399. /// <returns></returns>
  400. public DataTable FindTable(string databaseLinkId, string sql, object dbParameter, Pagination pagination)
  401. {
  402. try
  403. {
  404. DatabaseLinkEntity entity = GetEntity(databaseLinkId);
  405. return databaseLinkService.FindTable(entity, sql, dbParameter, pagination);
  406. }
  407. catch (Exception ex)
  408. {
  409. if (ex is ExceptionEx)
  410. {
  411. throw;
  412. }
  413. else
  414. {
  415. throw ExceptionEx.ThrowBusinessException(ex);
  416. }
  417. }
  418. }
  419. /// <summary>
  420. /// 开启事务
  421. /// </summary>
  422. /// <param name="entity">数据库连接信息</param>
  423. /// <returns></returns>
  424. public IRepository BeginTrans(string databaseLinkId)
  425. {
  426. try
  427. {
  428. DatabaseLinkEntity entity = GetEntity(databaseLinkId);
  429. return databaseLinkService.BeginTrans(entity);
  430. }
  431. catch (Exception ex)
  432. {
  433. if (ex is ExceptionEx)
  434. {
  435. throw;
  436. }
  437. else
  438. {
  439. throw ExceptionEx.ThrowServiceException(ex);
  440. }
  441. }
  442. }
  443. /// <summary>
  444. /// 根据指定数据库执行sql语句(事务)
  445. /// </summary>
  446. /// <param name="sql">sql语句</param>
  447. /// <param name="dbParameter">参数</param>
  448. /// <param name="数据库连接">db</param>
  449. public void ExecuteBySqlTrans(string sql, object dbParameter,IRepository db)
  450. {
  451. try
  452. {
  453. databaseLinkService.ExecuteBySqlTrans(sql, dbParameter, db);
  454. }
  455. catch (Exception ex)
  456. {
  457. if (ex is ExceptionEx)
  458. {
  459. throw;
  460. }
  461. else
  462. {
  463. throw ExceptionEx.ThrowServiceException(ex);
  464. }
  465. }
  466. }
  467. #endregion
  468. }
  469. }