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.
 
 
 
 
 
 

427 lines
14 KiB

  1. using Learun.DataBase;
  2. using Learun.DataBase.Repository;
  3. using Learun.Util;
  4. using Newtonsoft.Json.Linq;
  5. using System;
  6. using System.Collections.Generic;
  7. using System.Data;
  8. using System.Text;
  9. namespace Learun.Application.Base.SystemModule
  10. {
  11. /// <summary>
  12. /// 版 本 Learun-ADMS-Ultimate V7.0.0 数字化智慧校园
  13. /// Copyright (c) 2013-2018 北京泉江科技有限公司
  14. /// 创建人:陈彬彬
  15. /// 日 期:2017.03.08
  16. /// 描 述:数据库连接
  17. /// </summary>
  18. public class DatabaseLinkService : RepositoryFactory
  19. {
  20. #region 构造函数和属性
  21. private string fieldSql;
  22. public DatabaseLinkService()
  23. {
  24. fieldSql = @"
  25. t.F_DatabaseLinkId,
  26. t.F_ServerAddress,
  27. t.F_DBName,
  28. t.F_DBAlias,
  29. t.F_DbType,
  30. t.F_DbConnection,
  31. t.F_DESEncrypt,
  32. t.F_SortCode,
  33. t.F_DeleteMark,
  34. t.F_EnabledMark,
  35. t.F_Description,
  36. t.F_CreateDate,
  37. t.F_CreateUserId,
  38. t.F_CreateUserName,
  39. t.F_ModifyDate,
  40. t.F_ModifyUserId,
  41. t.F_ModifyUserName
  42. ";
  43. }
  44. #endregion
  45. #region 获取数据
  46. /// <summary>
  47. /// 获取自定义查询条件(公共)
  48. /// </summary>
  49. /// <param name="moduleUrl">访问的功能链接地址</param>
  50. /// <param name="userId">用户ID(用户ID为null表示公共)</param>
  51. /// <returns></returns>
  52. public IEnumerable<DatabaseLinkEntity> GetList()
  53. {
  54. try
  55. {
  56. var strSql = new StringBuilder();
  57. strSql.Append("SELECT ");
  58. strSql.Append(fieldSql);
  59. strSql.Append(" FROM LR_Base_DatabaseLink t WHERE t.F_DeleteMark = 0 ");
  60. return this.BaseRepository().FindList<DatabaseLinkEntity>(strSql.ToString());
  61. }
  62. catch (Exception ex)
  63. {
  64. if (ex is ExceptionEx)
  65. {
  66. throw;
  67. }
  68. else
  69. {
  70. throw ExceptionEx.ThrowServiceException(ex);
  71. }
  72. }
  73. }
  74. #endregion
  75. #region 提交数据
  76. /// <summary>
  77. /// 删除自定义查询条件
  78. /// </summary>
  79. /// <param name="keyValue">主键</param>
  80. public void VirtualDelete(string keyValue)
  81. {
  82. try
  83. {
  84. DatabaseLinkEntity entity = new DatabaseLinkEntity()
  85. {
  86. F_DatabaseLinkId = keyValue,
  87. F_DeleteMark = 1
  88. };
  89. this.BaseRepository().Update(entity);
  90. }
  91. catch (Exception ex)
  92. {
  93. if (ex is ExceptionEx)
  94. {
  95. throw;
  96. }
  97. else
  98. {
  99. throw ExceptionEx.ThrowServiceException(ex);
  100. }
  101. }
  102. }
  103. /// <summary>
  104. /// 保存自定义查询(新增、修改)
  105. /// </summary>
  106. /// <param name="keyValue">主键值</param>
  107. /// <param name="departmentEntity">部门实体</param>
  108. /// <returns></returns>
  109. public bool SaveEntity(string keyValue, DatabaseLinkEntity databaseLinkEntity)
  110. {
  111. try
  112. {
  113. /*测试数据库连接串"******";*/
  114. if (!string.IsNullOrEmpty(keyValue) && databaseLinkEntity.F_DbConnection == "******")
  115. {
  116. databaseLinkEntity.F_DbConnection = null;// 不更新连接串地址
  117. }
  118. else
  119. {
  120. try
  121. {
  122. databaseLinkEntity.F_ServerAddress = this.BaseRepository(databaseLinkEntity.F_DbConnection, databaseLinkEntity.F_DbType).getDbConnection().DataSource;
  123. }
  124. catch (Exception)
  125. {
  126. return false;
  127. }
  128. }
  129. if (!string.IsNullOrEmpty(keyValue))
  130. {
  131. databaseLinkEntity.Modify(keyValue);
  132. this.BaseRepository().Update(databaseLinkEntity);
  133. }
  134. else
  135. {
  136. databaseLinkEntity.Create();
  137. this.BaseRepository().Insert(databaseLinkEntity);
  138. }
  139. return true;
  140. }
  141. catch (Exception ex)
  142. {
  143. if (ex is ExceptionEx)
  144. {
  145. throw;
  146. }
  147. else
  148. {
  149. throw ExceptionEx.ThrowServiceException(ex);
  150. }
  151. }
  152. }
  153. #endregion
  154. #region 扩展方法
  155. /// <summary>
  156. /// 测试数据数据库是否能连接成功
  157. /// </summary>
  158. /// <param name="connection">连接串</param>
  159. /// <param name="dbType">数据库类型</param>
  160. public bool TestConnection(string connection, string dbType)
  161. {
  162. try
  163. {
  164. var db = this.BaseRepository(connection, dbType).BeginTrans();
  165. db.Commit();
  166. return true;
  167. }
  168. catch (Exception)
  169. {
  170. return false;
  171. }
  172. }
  173. /// <summary>
  174. /// 根据指定数据库执行sql语句
  175. /// </summary>
  176. /// <param name="entity">数据库实体</param>
  177. /// <param name="sql">sql语句</param>
  178. /// <param name="dbParameter">参数</param>
  179. public void ExecuteBySql(DatabaseLinkEntity entity, string sql, object dbParameter)
  180. {
  181. try
  182. {
  183. if (dbParameter is JObject)
  184. {
  185. var paramter = SqlHelper.JObjectToParameter((JObject)dbParameter);
  186. this.BaseRepository(entity.F_DbConnection, entity.F_DbType).ExecuteBySql(sql, paramter);
  187. }
  188. else if (dbParameter is List<FieldValueParam>)
  189. {
  190. var paramter = SqlHelper.FieldValueParamToParameter((List<FieldValueParam>)dbParameter);
  191. this.BaseRepository(entity.F_DbConnection, entity.F_DbType).ExecuteBySql(sql, paramter);
  192. }
  193. else
  194. {
  195. this.BaseRepository(entity.F_DbConnection, entity.F_DbType).ExecuteBySql(sql, dbParameter);
  196. }
  197. }
  198. catch (Exception ex)
  199. {
  200. if (ex is ExceptionEx)
  201. {
  202. throw;
  203. }
  204. else
  205. {
  206. throw ExceptionEx.ThrowServiceException(ex);
  207. }
  208. }
  209. }
  210. /// <summary>
  211. /// 根据指定数据库执行sql语句
  212. /// </summary>
  213. /// <param name="entity">数据库实体</param>
  214. /// <param name="procName">存储过程</param>
  215. /// <param name="dbParameter">参数</param>
  216. public void ExecuteByProc(DatabaseLinkEntity entity, string procName, object dbParameter)
  217. {
  218. try
  219. {
  220. if (dbParameter is JObject)
  221. {
  222. var paramter = SqlHelper.JObjectToParameter((JObject)dbParameter);
  223. if (entity.F_DatabaseLinkId == "systemdb")
  224. {
  225. this.BaseRepository().ExecuteByProc(procName, paramter);
  226. }
  227. else
  228. {
  229. this.BaseRepository(entity.F_DbConnection, entity.F_DbType).ExecuteByProc(procName, paramter);
  230. }
  231. }
  232. else if (dbParameter is List<FieldValueParam>)
  233. {
  234. var paramter = SqlHelper.FieldValueParamToParameter((List<FieldValueParam>)dbParameter);
  235. if (entity.F_DatabaseLinkId == "systemdb")
  236. {
  237. this.BaseRepository().ExecuteByProc(procName, paramter);
  238. }
  239. else
  240. {
  241. this.BaseRepository(entity.F_DbConnection, entity.F_DbType).ExecuteByProc(procName, paramter);
  242. }
  243. }
  244. else
  245. {
  246. if (entity.F_DatabaseLinkId == "systemdb")
  247. {
  248. this.BaseRepository().ExecuteByProc(procName, dbParameter);
  249. }
  250. else
  251. {
  252. this.BaseRepository(entity.F_DbConnection, entity.F_DbType).ExecuteByProc(procName, dbParameter);
  253. }
  254. }
  255. }
  256. catch (Exception ex)
  257. {
  258. if (ex is ExceptionEx)
  259. {
  260. throw;
  261. }
  262. else
  263. {
  264. throw ExceptionEx.ThrowServiceException(ex);
  265. }
  266. }
  267. }
  268. /// <summary>
  269. /// 根据数据库执行sql语句,查询数据->datatable
  270. /// </summary>
  271. /// <param name="entity">数据库实体</param>
  272. /// <param name="sql">sql语句</param>
  273. /// <param name="dbParameter">参数</param>
  274. /// <returns></returns>
  275. public DataTable FindTable(DatabaseLinkEntity entity, string sql, object dbParameter)
  276. {
  277. try
  278. {
  279. if (dbParameter is JObject)
  280. {
  281. var paramter = SqlHelper.JObjectToParameter((JObject)dbParameter);
  282. return this.BaseRepository(entity.F_DbConnection, entity.F_DbType).FindTable(sql, paramter);
  283. }
  284. else if (dbParameter is List<FieldValueParam>)
  285. {
  286. var paramter = SqlHelper.FieldValueParamToParameter((List<FieldValueParam>)dbParameter);
  287. return this.BaseRepository(entity.F_DbConnection, entity.F_DbType).FindTable(sql, paramter);
  288. }
  289. else
  290. {
  291. return this.BaseRepository(entity.F_DbConnection, entity.F_DbType).FindTable(sql, dbParameter);
  292. }
  293. }
  294. catch (Exception ex)
  295. {
  296. if (ex is ExceptionEx)
  297. {
  298. throw;
  299. }
  300. else
  301. {
  302. throw ExceptionEx.ThrowServiceException(ex);
  303. }
  304. }
  305. }
  306. /// <summary>
  307. /// 根据数据库执行sql语句,查询数据->datatable(分页)
  308. /// </summary>
  309. /// <param name="entity">数据库实体</param>
  310. /// <param name="sql">sql语句</param>
  311. /// <param name="dbParameter">参数</param>
  312. /// <param name="pagination">分页参数</param>
  313. /// <returns></returns>
  314. public DataTable FindTable(DatabaseLinkEntity entity, string sql, object dbParameter, Pagination pagination) {
  315. try
  316. {
  317. if (dbParameter is JObject)
  318. {
  319. var paramter = SqlHelper.JObjectToParameter((JObject)dbParameter);
  320. return this.BaseRepository(entity.F_DbConnection, entity.F_DbType).FindTable(sql, paramter, pagination);
  321. }
  322. else if (dbParameter is List<FieldValueParam>)
  323. {
  324. var paramter = SqlHelper.FieldValueParamToParameter((List<FieldValueParam>)dbParameter);
  325. return this.BaseRepository(entity.F_DbConnection, entity.F_DbType).FindTable(sql, paramter, pagination);
  326. }
  327. else
  328. {
  329. return this.BaseRepository(entity.F_DbConnection, entity.F_DbType).FindTable(sql, dbParameter, pagination);
  330. }
  331. }
  332. catch (Exception ex)
  333. {
  334. if (ex is ExceptionEx)
  335. {
  336. throw;
  337. }
  338. else
  339. {
  340. throw ExceptionEx.ThrowServiceException(ex);
  341. }
  342. }
  343. }
  344. #region 事务执行
  345. /// <summary>
  346. /// 开启事务
  347. /// </summary>
  348. /// <param name="entity">数据库连接信息</param>
  349. /// <returns></returns>
  350. public IRepository BeginTrans(DatabaseLinkEntity entity)
  351. {
  352. try
  353. {
  354. return this.BaseRepository(entity.F_DbConnection, entity.F_DbType).BeginTrans();
  355. }
  356. catch (Exception ex)
  357. {
  358. if (ex is ExceptionEx)
  359. {
  360. throw;
  361. }
  362. else
  363. {
  364. throw ExceptionEx.ThrowServiceException(ex);
  365. }
  366. }
  367. }
  368. /// <summary>
  369. /// 根据指定数据库执行sql语句
  370. /// </summary>
  371. /// <param name="sql">sql语句</param>
  372. /// <param name="dbParameter">参数</param>
  373. public void ExecuteBySqlTrans(string sql, object dbParameter, IRepository db)
  374. {
  375. try
  376. {
  377. if (dbParameter is JObject)
  378. {
  379. var paramter = SqlHelper.JObjectToParameter((JObject)dbParameter);
  380. if (db != null)
  381. {
  382. db.ExecuteBySql(sql, paramter);
  383. }
  384. }
  385. else if (dbParameter is List<FieldValueParam>)
  386. {
  387. var paramter = SqlHelper.FieldValueParamToParameter((List<FieldValueParam>)dbParameter);
  388. if (db != null)
  389. {
  390. db.ExecuteBySql(sql, paramter);
  391. }
  392. }
  393. else
  394. {
  395. if (db != null)
  396. {
  397. db.ExecuteBySql(sql, dbParameter);
  398. }
  399. }
  400. }
  401. catch (Exception ex)
  402. {
  403. if (ex is ExceptionEx)
  404. {
  405. throw;
  406. }
  407. else
  408. {
  409. throw ExceptionEx.ThrowServiceException(ex);
  410. }
  411. }
  412. }
  413. #endregion
  414. #endregion
  415. }
  416. }