|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490 |
- using Learun.Cache.Base;
- using Learun.Cache.Factory;
- using Learun.DataBase.Repository;
- using Learun.Util;
- using System;
- using System.Collections.Generic;
- using System.Data;
-
- namespace Learun.Application.Base.SystemModule
- {
- /// <summary>
- /// 版 本 Learun-ADMS-Ultimate V7.0.0 数字化智慧校园
- /// Copyright (c) 2013-2018 北京泉江科技有限公司
- /// 创建人:陈彬彬
- /// 日 期:2017.03.08
- /// 描 述:数据库连接
- /// </summary>
- public class DatabaseLinkBLL : DatabaseLinkIBLL
- {
- #region 属性
- private DatabaseLinkService databaseLinkService = new DatabaseLinkService();
- #endregion
-
- #region 缓存定义
- private ICache cache = CacheFactory.CaChe();
- private string cacheKey = "Learun_adms_databaseLink";
- #endregion
-
- #region 获取数据
- /// <summary>
- /// 获取列表数据
- /// </summary>
- /// <returns></returns>
- public List<DatabaseLinkEntity> GetList()
- {
- try
- {
- List<DatabaseLinkEntity> list = cache.Read<List<DatabaseLinkEntity>>(cacheKey, CacheId.database);
- if (list == null)
- {
- list = (List<DatabaseLinkEntity>)databaseLinkService.GetList();
- cache.Write<List<DatabaseLinkEntity>>(cacheKey, list, CacheId.database);
- }
- return list;
- }
- catch (Exception ex)
- {
- if (ex is ExceptionEx)
- {
- throw;
- }
- else
- {
- throw ExceptionEx.ThrowBusinessException(ex);
- }
- }
- }
-
- /// <summary>
- /// 获取映射数据
- /// </summary>
- /// <returns></returns>
- public Dictionary<string, DatabaseLinkModel> GetMap() {
- try
- {
- //Dictionary<string, DatabaseLinkModel> dic = cache.Read<Dictionary<string, DatabaseLinkModel>>(cacheKey + "dic", CacheId.database);
- //liangkun 去掉数据库缓存
- Dictionary<string, DatabaseLinkModel> dic = null;
- if (dic == null)
- {
- dic = new Dictionary<string, DatabaseLinkModel>();
- List<DatabaseLinkEntity> list = GetList();
- foreach (var item in list)
- {
- dic.Add(item.F_DatabaseLinkId, new DatabaseLinkModel()
- {
- alias = item.F_DBAlias,
- name = item.F_DBName
- });
- }
- //cache.Write(cacheKey + "dic", dic, CacheId.database);
- }
- return dic;
- }
- catch (Exception ex)
- {
- if (ex is ExceptionEx)
- {
- throw;
- }
- else
- {
- throw ExceptionEx.ThrowBusinessException(ex);
- }
- }
- }
-
- /// <summary>
- /// 获取列表数据(去掉连接串地址信息)
- /// </summary>
- /// <returns></returns>
- public List<DatabaseLinkEntity> GetListByNoConnection()
- {
- try
- {
- List<DatabaseLinkEntity> list = GetList();
- foreach (var item in list) {
- item.F_DbConnection = "******";
- }
- return list;
- }
- catch (Exception ex)
- {
- if (ex is ExceptionEx)
- {
- throw;
- }
- else
- {
- throw ExceptionEx.ThrowBusinessException(ex);
- }
- }
- }
- /// <summary>
- /// 获取列表数据
- /// </summary>
- /// <param name="keyword">关键字</param>
- /// <returns></returns>
- public List<DatabaseLinkEntity> GetListByNoConnection(string keyword)
- {
- try
- {
- List<DatabaseLinkEntity> list = GetListByNoConnection();
- if (!string.IsNullOrEmpty(keyword))
- {
- list = list.FindAll(t => t.F_DBName.Contains(keyword) || t.F_DBAlias.Contains(keyword) || t.F_ServerAddress.Contains(keyword));
- }
- return list;
- }
- catch (Exception ex)
- {
- if (ex is ExceptionEx)
- {
- throw;
- }
- else
- {
- throw ExceptionEx.ThrowBusinessException(ex);
- }
- }
- }
- /// <summary>
- /// 获取树形数据
- /// </summary>
- /// <returns></returns>
- public List<TreeModel> GetTreeList() {
- List<DatabaseLinkEntity> list = GetList();
- List<TreeModel> treelist = new List<TreeModel>();
- Dictionary<string, List<TreeModel>> dic = new Dictionary<string, List<TreeModel>>();
-
- foreach (var item in list) {
- TreeModel node = new TreeModel();
- node.id = item.F_DatabaseLinkId;
- node.text = item.F_DBAlias;
- node.value = item.F_DBName;
- node.complete = true;
- node.hasChildren = false;
-
- if (!dic.ContainsKey(item.F_ServerAddress))
- {
- TreeModel pnode = new TreeModel();
- pnode.id = item.F_ServerAddress;
- pnode.text = item.F_ServerAddress;
- pnode.value = "LearunServerAddress";
- pnode.isexpand = true;
- pnode.complete = true;
- pnode.hasChildren = true;
- pnode.ChildNodes = new List<TreeModel>();
- treelist.Add(pnode);
- dic.Add(item.F_ServerAddress, pnode.ChildNodes);
- }
- dic[item.F_ServerAddress].Add(node);
- }
- return treelist;
- }
- /// <summary>
- /// 获取树形数据
- /// </summary>
- /// <returns></returns>
- public List<TreeModel> GetTreeListEx()
- {
- List<DatabaseLinkEntity> list = GetList();
- List<TreeModel> treelist = new List<TreeModel>();
- Dictionary<string, List<TreeModel>> dic = new Dictionary<string, List<TreeModel>>();
- foreach (var item in list)
- {
- TreeModel node = new TreeModel();
- node.id = item.F_DatabaseLinkId;
- node.text = item.F_DBAlias;
- node.value = item.F_DBName;
- node.complete = false;
- node.isexpand = false;
- node.hasChildren = true;
-
- if (!dic.ContainsKey(item.F_ServerAddress))
- {
- TreeModel pnode = new TreeModel();
- pnode.id = item.F_ServerAddress;
- pnode.text = item.F_ServerAddress;
- pnode.value = "LearunServerAddress";
- pnode.isexpand = true;
- pnode.complete = true;
- pnode.hasChildren = true;
- pnode.ChildNodes = new List<TreeModel>();
- treelist.Add(pnode);
- dic.Add(item.F_ServerAddress, pnode.ChildNodes);
- }
- dic[item.F_ServerAddress].Add(node);
- }
- return treelist;
- }
- /// <summary>
- /// 获取数据连接实体
- /// </summary>
- /// <param name="databaseLinkId">主键</param>
- /// <returns></returns>
- public DatabaseLinkEntity GetEntity(string databaseLinkId) {
- try
- {
- List<DatabaseLinkEntity> list = GetList();
- return list.Find(t => t.F_DatabaseLinkId.Equals(databaseLinkId));
- }
- catch (Exception ex)
- {
- if (ex is ExceptionEx)
- {
- throw;
- }
- else
- {
- throw ExceptionEx.ThrowBusinessException(ex);
- }
- }
- }
-
- #endregion
-
- #region 提交数据
- /// <summary>
- /// 删除自定义查询条件
- /// </summary>
- /// <param name="keyValue">主键</param>
- public void VirtualDelete(string keyValue)
- {
- try
- {
- cache.Remove(cacheKey, CacheId.database);
- cache.Remove(cacheKey + "dic", CacheId.database);
- databaseLinkService.VirtualDelete(keyValue);
- }
- catch (Exception ex)
- {
- if (ex is ExceptionEx)
- {
- throw;
- }
- else
- {
- throw ExceptionEx.ThrowBusinessException(ex);
- }
- }
- }
- /// <summary>
- /// 保存自定义查询(新增、修改)
- /// </summary>
- /// <param name="keyValue">主键值</param>
- /// <param name="departmentEntity">部门实体</param>
- /// <returns></returns>
- public bool SaveEntity(string keyValue, DatabaseLinkEntity databaseLinkEntity)
- {
- try
- {
- cache.Remove(cacheKey, CacheId.database);
- cache.Remove(cacheKey+"dic", CacheId.database);
- return databaseLinkService.SaveEntity(keyValue, databaseLinkEntity);
- }
- catch (Exception ex)
- {
- if (ex is ExceptionEx)
- {
- throw;
- }
- else
- {
- throw ExceptionEx.ThrowBusinessException(ex);
- }
- }
-
- }
- #endregion
-
- #region 扩展方法
- /// <summary>
- /// 测试数据数据库是否能连接成功
- /// </summary>
- /// <param name="connection">连接串</param>
- /// <param name="dbType">数据库类型</param>
- /// <param name="keyValue">主键</param>
- public bool TestConnection(string connection, string dbType, string keyValue)
- {
- if (!string.IsNullOrEmpty(keyValue) && connection == "******") {
- DatabaseLinkEntity entity = GetEntity(keyValue);
- connection = entity.F_DbConnection;
- }
-
-
-
-
-
-
- return databaseLinkService.TestConnection(connection, dbType);
- }
- /// <summary>
- /// 根据指定数据库执行sql语句
- /// </summary>
- /// <param name="databaseLinkId">数据库主键</param>
- /// <param name="sql">sql语句</param>
- /// <param name="dbParameter">参数</param>
- public void ExecuteBySql(string databaseLinkId, string sql,object dbParameter=null)
- {
- try
- {
- DatabaseLinkEntity entity = GetEntity(databaseLinkId);
- databaseLinkService.ExecuteBySql(entity, sql, dbParameter);
- }
- catch (Exception ex)
- {
- if (ex is ExceptionEx)
- {
- throw;
- }
- else
- {
- throw ExceptionEx.ThrowBusinessException(ex);
- }
- }
- }
- /// <summary>
- /// 根据指定数据库执行存储过程
- /// </summary>
- /// <param name="databaseLinkId">数据库主键</param>
- /// <param name="procName">存储过程</param>
- /// <param name="dbParameter">参数</param>
- public void ExecuteByProc(string databaseLinkId, string procName, object dbParameter = null)
- {
- try
- {
- DatabaseLinkEntity entity = null;
- if (databaseLinkId == "systemdb")// 本地数据库
- {
- entity = new DatabaseLinkEntity()
- {
- F_DatabaseLinkId = "systemdb"
- };
- }
- else
- {
- entity = GetEntity(databaseLinkId);
- }
- databaseLinkService.ExecuteByProc(entity, procName, dbParameter);
- }
- catch (Exception ex)
- {
- if (ex is ExceptionEx)
- {
- throw;
- }
- else
- {
- throw ExceptionEx.ThrowBusinessException(ex);
- }
- }
- }
- /// <summary>
- /// 根据数据库执行sql语句,查询数据->datatable
- /// </summary>
- /// <param name="databaseLinkId">数据库主键</param>
- /// <param name="sql">sql语句</param>
- /// <param name="dbParameter">参数</param>
- /// <returns></returns>
- public DataTable FindTable(string databaseLinkId, string sql, object dbParameter = null)
- {
- try
- {
- DatabaseLinkEntity entity = GetEntity(databaseLinkId);
- return databaseLinkService.FindTable(entity, sql, dbParameter);
- }
- catch (Exception ex)
- {
- if (ex is ExceptionEx)
- {
- throw;
- }
- else
- {
- throw ExceptionEx.ThrowBusinessException(ex);
- }
- }
- }
- /// <summary>
- /// 根据数据库执行sql语句,查询数据->datatable(分页)
- /// </summary>
- /// <param name="databaseLinkId">数据库主键</param>
- /// <param name="sql">sql语句</param>
- /// <param name="dbParameter">参数</param>
- /// <param name="pagination">分页参数</param>
- /// <returns></returns>
- public DataTable FindTable(string databaseLinkId, string sql, object dbParameter, Pagination pagination)
- {
- try
- {
-
- DatabaseLinkEntity entity = GetEntity(databaseLinkId);
- return databaseLinkService.FindTable(entity, sql, dbParameter, pagination);
- }
- catch (Exception ex)
- {
- if (ex is ExceptionEx)
- {
- throw;
- }
- else
- {
- throw ExceptionEx.ThrowBusinessException(ex);
- }
- }
- }
-
- /// <summary>
- /// 开启事务
- /// </summary>
- /// <param name="entity">数据库连接信息</param>
- /// <returns></returns>
- public IRepository BeginTrans(string databaseLinkId)
- {
- try
- {
- DatabaseLinkEntity entity = GetEntity(databaseLinkId);
- return databaseLinkService.BeginTrans(entity);
- }
- catch (Exception ex)
- {
-
- if (ex is ExceptionEx)
- {
- throw;
- }
- else
- {
- throw ExceptionEx.ThrowServiceException(ex);
- }
- }
- }
- /// <summary>
- /// 根据指定数据库执行sql语句(事务)
- /// </summary>
- /// <param name="sql">sql语句</param>
- /// <param name="dbParameter">参数</param>
- /// <param name="数据库连接">db</param>
- public void ExecuteBySqlTrans(string sql, object dbParameter,IRepository db)
- {
- try
- {
- databaseLinkService.ExecuteBySqlTrans(sql, dbParameter, db);
- }
- catch (Exception ex)
- {
- if (ex is ExceptionEx)
- {
- throw;
- }
- else
- {
- throw ExceptionEx.ThrowServiceException(ex);
- }
- }
- }
- #endregion
- }
- }
|