using DS.Module.Core.Data;
using DS.Module.UserModule;
using SqlSugar;
namespace DS.Module.SqlSugar
{
///
///
///
public static class SqlsugarAopHelper
{
///
/// 封装AOP 配置
///
///
///
///
///
public static void AopForSqlsugar(SqlSugarClient db, IUser user, List dbList, List connectConfigList)
{
//var connectConfigList = //new List();
// SqlsugarHelper.ReturnConnectionConfig(dbList);
/*
* 默认只会配置到第一个数据库,这里按照官方文档进行多数据库/多租户文档的说明进行循环配置
*/
foreach (var c in connectConfigList)
{
var dbProvider = db.GetConnectionScope((string)c.ConfigId);
// var user = services.GetService();
//单例参数配置,所有上下文生效
dbProvider.Ado.CommandTimeOut = 30;
dbProvider.Aop.OnLogExecuting = (sql, pars) =>
{
//Logger.Log(LogLevel.Info,
// DateTime.Now.ToString() + "\r\n" +
// UtilMethods.GetSqlString(c.DbType, sql, pars));
};
dbProvider.Aop.DataExecuting = (oldValue, entityInfo) =>
{
// 新增操作
SqlsugarHelper.InsertByObjectForSqlsugar(entityInfo, user);
// 更新操作
SqlsugarHelper.UpdateByObjectForSqlsugar(entityInfo, user);
};
dbProvider.Aop.OnError = (exp) => //执行SQL 错误事件
{
// Logger.Error(DateTime.Now.ToString() + "\r\n" + exp.Sql + "\r\n" + exp.Parametres);
};
dbProvider.QueryFilter.AddTableFilter(m => m.TenantId == user.GetTenantId());
//全局软删除过滤
dbProvider.QueryFilter.AddTableFilter(m => m.Deleted == false);
}
}
}
}