using Learun.DataBase;
using Learun.DataBase.Repository;
using Learun.Util;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Data;
using System.Text;
namespace Learun.Application.Base.SystemModule
{
///
/// 版 本 Learun-ADMS-Ultimate V7.0.0 数字化智慧校园
/// Copyright (c) 2013-2018 北京泉江科技有限公司
/// 创建人:陈彬彬
/// 日 期:2017.03.08
/// 描 述:数据库连接
///
public class DatabaseLinkService : RepositoryFactory
{
#region 构造函数和属性
private string fieldSql;
public DatabaseLinkService()
{
fieldSql = @"
t.F_DatabaseLinkId,
t.F_ServerAddress,
t.F_DBName,
t.F_DBAlias,
t.F_DbType,
t.F_DbConnection,
t.F_DESEncrypt,
t.F_SortCode,
t.F_DeleteMark,
t.F_EnabledMark,
t.F_Description,
t.F_CreateDate,
t.F_CreateUserId,
t.F_CreateUserName,
t.F_ModifyDate,
t.F_ModifyUserId,
t.F_ModifyUserName
";
}
#endregion
#region 获取数据
///
/// 获取自定义查询条件(公共)
///
/// 访问的功能链接地址
/// 用户ID(用户ID为null表示公共)
///
public IEnumerable GetList()
{
try
{
var strSql = new StringBuilder();
strSql.Append("SELECT ");
strSql.Append(fieldSql);
strSql.Append(" FROM LR_Base_DatabaseLink t WHERE t.F_DeleteMark = 0 ");
return this.BaseRepository().FindList(strSql.ToString());
}
catch (Exception ex)
{
if (ex is ExceptionEx)
{
throw;
}
else
{
throw ExceptionEx.ThrowServiceException(ex);
}
}
}
#endregion
#region 提交数据
///
/// 删除自定义查询条件
///
/// 主键
public void VirtualDelete(string keyValue)
{
try
{
DatabaseLinkEntity entity = new DatabaseLinkEntity()
{
F_DatabaseLinkId = keyValue,
F_DeleteMark = 1
};
this.BaseRepository().Update(entity);
}
catch (Exception ex)
{
if (ex is ExceptionEx)
{
throw;
}
else
{
throw ExceptionEx.ThrowServiceException(ex);
}
}
}
///
/// 保存自定义查询(新增、修改)
///
/// 主键值
/// 部门实体
///
public bool SaveEntity(string keyValue, DatabaseLinkEntity databaseLinkEntity)
{
try
{
/*测试数据库连接串"******";*/
if (!string.IsNullOrEmpty(keyValue) && databaseLinkEntity.F_DbConnection == "******")
{
databaseLinkEntity.F_DbConnection = null;// 不更新连接串地址
}
else
{
try
{
databaseLinkEntity.F_ServerAddress = this.BaseRepository(databaseLinkEntity.F_DbConnection, databaseLinkEntity.F_DbType).getDbConnection().DataSource;
}
catch (Exception)
{
return false;
}
}
if (!string.IsNullOrEmpty(keyValue))
{
databaseLinkEntity.Modify(keyValue);
this.BaseRepository().Update(databaseLinkEntity);
}
else
{
databaseLinkEntity.Create();
this.BaseRepository().Insert(databaseLinkEntity);
}
return true;
}
catch (Exception ex)
{
if (ex is ExceptionEx)
{
throw;
}
else
{
throw ExceptionEx.ThrowServiceException(ex);
}
}
}
#endregion
#region 扩展方法
///
/// 测试数据数据库是否能连接成功
///
/// 连接串
/// 数据库类型
public bool TestConnection(string connection, string dbType)
{
try
{
var db = this.BaseRepository(connection, dbType).BeginTrans();
db.Commit();
return true;
}
catch (Exception)
{
return false;
}
}
///
/// 根据指定数据库执行sql语句
///
/// 数据库实体
/// sql语句
/// 参数
public void ExecuteBySql(DatabaseLinkEntity entity, string sql, object dbParameter)
{
try
{
if (dbParameter is JObject)
{
var paramter = SqlHelper.JObjectToParameter((JObject)dbParameter);
this.BaseRepository(entity.F_DbConnection, entity.F_DbType).ExecuteBySql(sql, paramter);
}
else if (dbParameter is List)
{
var paramter = SqlHelper.FieldValueParamToParameter((List)dbParameter);
this.BaseRepository(entity.F_DbConnection, entity.F_DbType).ExecuteBySql(sql, paramter);
}
else
{
this.BaseRepository(entity.F_DbConnection, entity.F_DbType).ExecuteBySql(sql, dbParameter);
}
}
catch (Exception ex)
{
if (ex is ExceptionEx)
{
throw;
}
else
{
throw ExceptionEx.ThrowServiceException(ex);
}
}
}
///
/// 根据指定数据库执行sql语句
///
/// 数据库实体
/// 存储过程
/// 参数
public void ExecuteByProc(DatabaseLinkEntity entity, string procName, object dbParameter)
{
try
{
if (dbParameter is JObject)
{
var paramter = SqlHelper.JObjectToParameter((JObject)dbParameter);
if (entity.F_DatabaseLinkId == "systemdb")
{
this.BaseRepository().ExecuteByProc(procName, paramter);
}
else
{
this.BaseRepository(entity.F_DbConnection, entity.F_DbType).ExecuteByProc(procName, paramter);
}
}
else if (dbParameter is List)
{
var paramter = SqlHelper.FieldValueParamToParameter((List)dbParameter);
if (entity.F_DatabaseLinkId == "systemdb")
{
this.BaseRepository().ExecuteByProc(procName, paramter);
}
else
{
this.BaseRepository(entity.F_DbConnection, entity.F_DbType).ExecuteByProc(procName, paramter);
}
}
else
{
if (entity.F_DatabaseLinkId == "systemdb")
{
this.BaseRepository().ExecuteByProc(procName, dbParameter);
}
else
{
this.BaseRepository(entity.F_DbConnection, entity.F_DbType).ExecuteByProc(procName, dbParameter);
}
}
}
catch (Exception ex)
{
if (ex is ExceptionEx)
{
throw;
}
else
{
throw ExceptionEx.ThrowServiceException(ex);
}
}
}
///
/// 根据数据库执行sql语句,查询数据->datatable
///
/// 数据库实体
/// sql语句
/// 参数
///
public DataTable FindTable(DatabaseLinkEntity entity, string sql, object dbParameter)
{
try
{
if (dbParameter is JObject)
{
var paramter = SqlHelper.JObjectToParameter((JObject)dbParameter);
return this.BaseRepository(entity.F_DbConnection, entity.F_DbType).FindTable(sql, paramter);
}
else if (dbParameter is List)
{
var paramter = SqlHelper.FieldValueParamToParameter((List)dbParameter);
return this.BaseRepository(entity.F_DbConnection, entity.F_DbType).FindTable(sql, paramter);
}
else
{
return this.BaseRepository(entity.F_DbConnection, entity.F_DbType).FindTable(sql, dbParameter);
}
}
catch (Exception ex)
{
if (ex is ExceptionEx)
{
throw;
}
else
{
throw ExceptionEx.ThrowServiceException(ex);
}
}
}
///
/// 根据数据库执行sql语句,查询数据->datatable(分页)
///
/// 数据库实体
/// sql语句
/// 参数
/// 分页参数
///
public DataTable FindTable(DatabaseLinkEntity entity, string sql, object dbParameter, Pagination pagination) {
try
{
if (dbParameter is JObject)
{
var paramter = SqlHelper.JObjectToParameter((JObject)dbParameter);
return this.BaseRepository(entity.F_DbConnection, entity.F_DbType).FindTable(sql, paramter, pagination);
}
else if (dbParameter is List)
{
var paramter = SqlHelper.FieldValueParamToParameter((List)dbParameter);
return this.BaseRepository(entity.F_DbConnection, entity.F_DbType).FindTable(sql, paramter, pagination);
}
else
{
return this.BaseRepository(entity.F_DbConnection, entity.F_DbType).FindTable(sql, dbParameter, pagination);
}
}
catch (Exception ex)
{
if (ex is ExceptionEx)
{
throw;
}
else
{
throw ExceptionEx.ThrowServiceException(ex);
}
}
}
#region 事务执行
///
/// 开启事务
///
/// 数据库连接信息
///
public IRepository BeginTrans(DatabaseLinkEntity entity)
{
try
{
return this.BaseRepository(entity.F_DbConnection, entity.F_DbType).BeginTrans();
}
catch (Exception ex)
{
if (ex is ExceptionEx)
{
throw;
}
else
{
throw ExceptionEx.ThrowServiceException(ex);
}
}
}
///
/// 根据指定数据库执行sql语句
///
/// sql语句
/// 参数
public void ExecuteBySqlTrans(string sql, object dbParameter, IRepository db)
{
try
{
if (dbParameter is JObject)
{
var paramter = SqlHelper.JObjectToParameter((JObject)dbParameter);
if (db != null)
{
db.ExecuteBySql(sql, paramter);
}
}
else if (dbParameter is List)
{
var paramter = SqlHelper.FieldValueParamToParameter((List)dbParameter);
if (db != null)
{
db.ExecuteBySql(sql, paramter);
}
}
else
{
if (db != null)
{
db.ExecuteBySql(sql, dbParameter);
}
}
}
catch (Exception ex)
{
if (ex is ExceptionEx)
{
throw;
}
else
{
throw ExceptionEx.ThrowServiceException(ex);
}
}
}
#endregion
#endregion
}
}