|
|
|
|
using Ds.Modules.DsEntity.log;
|
|
|
|
|
using DS.Module.Core.Data;
|
|
|
|
|
using DS.Module.UserModule;
|
|
|
|
|
using Newtonsoft.Json;
|
|
|
|
|
using SqlSugar;
|
|
|
|
|
using SqlSugar.IOC;
|
|
|
|
|
|
|
|
|
|
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));
|
|
|
|
|
Console.WriteLine("执行的sql:" + sql);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
//数据处理事件
|
|
|
|
|
dbProvider.Aop.DataExecuting = (oldValue, entityInfo) =>
|
|
|
|
|
{
|
|
|
|
|
// 新增操作
|
|
|
|
|
SqlsugarHelper.InsertByObjectForSqlsugar(entityInfo, user);
|
|
|
|
|
// 更新操作
|
|
|
|
|
SqlsugarHelper.UpdateByObjectForSqlsugar(entityInfo, user);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
dbProvider.Aop.OnDiffLogEvent = (it) =>
|
|
|
|
|
{
|
|
|
|
|
Console.WriteLine("执行更新前更新后:");
|
|
|
|
|
//操作前记录 包含: 字段描述 列名 值 表名 表描述
|
|
|
|
|
//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
|
|
|
|
|
// Sys_Log_DataAop
|
|
|
|
|
var log = new Sys_Log_DataAop()
|
|
|
|
|
{
|
|
|
|
|
EditBeforeData = it.BeforeData?.ToString() ?? "123",
|
|
|
|
|
EditAfterData = it.AfterData?.ToString(),
|
|
|
|
|
Aop_Sql = it.Sql,
|
|
|
|
|
Parameter = JsonConvert.SerializeObject(it.Parameters),
|
|
|
|
|
Aop_Data = it.BusinessData?.ToString() ?? "12131",
|
|
|
|
|
Aop_Time = it.Time.ToString(),
|
|
|
|
|
DiffType = it.DiffType.ToString()
|
|
|
|
|
};
|
|
|
|
|
Console.WriteLine("执行的sql:" + log);
|
|
|
|
|
//Write logic
|
|
|
|
|
DbScoped.Sugar.GetConnection(200).Insertable(log).ExecuteCommand();
|
|
|
|
|
};
|
|
|
|
|
//全局过滤租户
|
|
|
|
|
dbProvider.QueryFilter.AddTableFilter<ITenantId>(m => m.TenantId == user.GetTenantId());
|
|
|
|
|
//全局过滤机构Id
|
|
|
|
|
dbProvider.QueryFilter.AddTableFilter<IOrgId>(m => m.OrgId == user.GetOrgId());
|
|
|
|
|
//全局软删除过滤
|
|
|
|
|
dbProvider.QueryFilter.AddTableFilter<IDeleted>(m => m.Deleted == false);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|