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.

75 lines
3.2 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);
/*
* /
*/
Console.WriteLine("开始加载sqlsugar模块");
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);
};
db.Aop.OnDiffLogEvent = it =>
{
//操作前记录 包含: 字段描述 列名 值 表名 表描述
var editBeforeData = it.BeforeData;//插入Before为null之前还没进库
//操作后记录 包含: 字段描述 列名 值 表名 表描述
var editAfterData = it.AfterData;
var sql = it.Sql;
var parameter = it.Parameters;
var data = it.BusinessData;//这边会显示你传进来的对象
var time = it.Time;
var diffType = it.DiffType;//enum insert 、update and delete
Console.WriteLine("执行的sql:" + sql);
//Write logic
};
dbProvider.QueryFilter.AddTableFilter<ITenantId>(m => m.TenantId == user.GetTenantId());
//全局软删除过滤
dbProvider.QueryFilter.AddTableFilter<IDeleted>(m => m.Deleted == false);
}
}
}
}