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
{
///
/// 版 本 Learun-ADMS-Ultimate V7.0.0 数字化智慧校园
/// Copyright (c) 2013-2018 北京泉江科技有限公司
/// 创建人:陈彬彬
/// 日 期:2017.03.08
/// 描 述:数据库连接
///
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 获取数据
///
/// 获取列表数据
///
///
public List GetList()
{
try
{
List list = cache.Read>(cacheKey, CacheId.database);
if (list == null)
{
list = (List)databaseLinkService.GetList();
cache.Write>(cacheKey, list, CacheId.database);
}
return list;
}
catch (Exception ex)
{
if (ex is ExceptionEx)
{
throw;
}
else
{
throw ExceptionEx.ThrowBusinessException(ex);
}
}
}
///
/// 获取映射数据
///
///
public Dictionary GetMap() {
try
{
//Dictionary dic = cache.Read>(cacheKey + "dic", CacheId.database);
//liangkun 去掉数据库缓存
Dictionary dic = null;
if (dic == null)
{
dic = new Dictionary();
List 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);
}
}
}
///
/// 获取列表数据(去掉连接串地址信息)
///
///
public List GetListByNoConnection()
{
try
{
List 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);
}
}
}
///
/// 获取列表数据
///
/// 关键字
///
public List GetListByNoConnection(string keyword)
{
try
{
List 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);
}
}
}
///
/// 获取树形数据
///
///
public List GetTreeList() {
List list = GetList();
List treelist = new List();
Dictionary> dic = new Dictionary>();
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();
treelist.Add(pnode);
dic.Add(item.F_ServerAddress, pnode.ChildNodes);
}
dic[item.F_ServerAddress].Add(node);
}
return treelist;
}
///
/// 获取树形数据
///
///
public List GetTreeListEx()
{
List list = GetList();
List treelist = new List();
Dictionary> dic = new Dictionary>();
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();
treelist.Add(pnode);
dic.Add(item.F_ServerAddress, pnode.ChildNodes);
}
dic[item.F_ServerAddress].Add(node);
}
return treelist;
}
///
/// 获取数据连接实体
///
/// 主键
///
public DatabaseLinkEntity GetEntity(string databaseLinkId) {
try
{
List 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 提交数据
///
/// 删除自定义查询条件
///
/// 主键
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);
}
}
}
///
/// 保存自定义查询(新增、修改)
///
/// 主键值
/// 部门实体
///
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 扩展方法
///
/// 测试数据数据库是否能连接成功
///
/// 连接串
/// 数据库类型
/// 主键
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);
}
///
/// 根据指定数据库执行sql语句
///
/// 数据库主键
/// sql语句
/// 参数
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);
}
}
}
///
/// 根据指定数据库执行存储过程
///
/// 数据库主键
/// 存储过程
/// 参数
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);
}
}
}
///
/// 根据数据库执行sql语句,查询数据->datatable
///
/// 数据库主键
/// sql语句
/// 参数
///
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);
}
}
}
///
/// 根据数据库执行sql语句,查询数据->datatable(分页)
///
/// 数据库主键
/// sql语句
/// 参数
/// 分页参数
///
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);
}
}
}
///
/// 开启事务
///
/// 数据库连接信息
///
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);
}
}
}
///
/// 根据指定数据库执行sql语句(事务)
///
/// sql语句
/// 参数
/// db
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
}
}