You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

56 lines
2.3 KiB
C#

using DS.Module.Core.Data;
using DS.Module.UserModule;
using SqlSugar;
namespace DS.Module.SqlSugar
{
/// <summary>
///
/// </summary>
public static class SqlsugarAopHelper
{
/// <summary>
/// 封装AOP 配置
/// </summary>
/// <param name="db"></param>
/// <param name="user"></param>
/// <param name="dbList"></param>
/// <param name="connectConfigList"></param>
public static void AopForSqlsugar(SqlSugarClient db, IUser user, List<DbConfig> dbList, List<ConnectionConfig> connectConfigList)
{
//var connectConfigList = //new List<ConnectionConfig>();
// SqlsugarHelper.ReturnConnectionConfig(dbList);
/*
* 默认只会配置到第一个数据库,这里按照官方文档进行多数据库/多租户文档的说明进行循环配置
*/
foreach (var c in connectConfigList)
{
var dbProvider = db.GetConnectionScope((string)c.ConfigId);
// var user = services.GetService<IUser>();
//单例参数配置,所有上下文生效
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<ITenantId>(m => m.TenantId == user.GetTenantId());
//全局软删除过滤
dbProvider.QueryFilter.AddTableFilter<IDeleted>(m => m.Deleted == false);
}
}
}
}