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.

117 lines
4.1 KiB
C#

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

using EntrustSettle.Common.LogHelper;
using EntrustSettle.Model;
using SqlSugar;
using System;
using System.Threading.Tasks;
using Yitter.IdGenerator;
namespace EntrustSettle.Common.DB.Aop;
public static class SqlSugarAop
{
public static void OnLogExecuting(string sql, SugarParameter[] param, string path, string user, string traceId)
{
if (AppSettings.app(new string[] { "AppSettings", "SqlAOPLog", "Enabled" }).ObjToBool())
{
if (AppSettings.app(new string[] { "AppSettings", "SqlAOPLog", "LogToFile", "Enabled" }).ObjToBool())
{
Parallel.For(0, 1, e =>
{
// 完整sql语句
LogLock.OutLogAOP("SqlAOPLog", traceId,
[
$"[Path]{path}",
$"[User]{user}",
"[SQL]",
GetWholeSql(param, sql),
]);
// 参数化sql语句
//LogLock.OutLogAOP("SqlAOPLog", "", new string[] { GetParas(p), "【SQL语句】" + sql });
});
}
if (AppSettings.app(new string[] { "AppSettings", "SqlAOPLog", "LogToConsole", "Enabled" }).ObjToBool())
{
ConsoleHelper.WriteColorLine(string.Join("\r\n", new string[] { "--------", $"{DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")} " + GetWholeSql(param, sql) }), ConsoleColor.DarkCyan);
}
}
}
public static void DataExecuting(object oldValue, DataFilterModel entityInfo)
{
if (entityInfo.EntityValue is RootEntityTkey<long> rootEntity)
{
if (rootEntity.Id == 0)
{
// SqlSugar自带的雪花算法有javascript精度问题所以这里用自定义的雪花算法
//rootEntity.Id = SnowFlakeSingle.Instance.NextId();
rootEntity.Id = YitIdHelper.NextId();
}
}
if (entityInfo.EntityValue is BaseEntity baseEntity)
{
// 新增操作赋值CreateTime
if (entityInfo.OperationType == DataFilterType.InsertByObject)
{
if (baseEntity.CreateTime == DateTime.MinValue)
{
baseEntity.CreateTime = DateTime.Now;
}
}
// 更新操作赋值ModifyTime
if (entityInfo.OperationType == DataFilterType.UpdateByObject)
{
baseEntity.ModifyTime = DateTime.Now;
}
//if (App.User?.ID > 0)
if (!string.IsNullOrEmpty(App.User?.ID))
{
// 这里不需要用到租户
//if (baseEntity is ITenantEntity tenant && App.User.TenantId > 0)
//{
// if (tenant.TenantId == 0)
// {
// tenant.TenantId = App.User.TenantId;
// }
//}
switch (entityInfo.OperationType)
{
case DataFilterType.InsertByObject:
if (baseEntity.CreateBy.IsNullOrEmpty() || baseEntity.CreateId is null or "")
{
baseEntity.CreateId = App.User.ID;
baseEntity.CreateBy = App.User.Name;
}
break;
case DataFilterType.UpdateByObject:
baseEntity.ModifyId = App.User.ID;
baseEntity.ModifyBy = App.User.Name;
break;
}
}
}
}
private static string GetWholeSql(SugarParameter[] paramArr, string sql)
{
foreach (var param in paramArr)
{
sql = sql.Replace(param.ParameterName, param.Value.ObjToString());
}
return sql;
}
private static string GetParas(SugarParameter[] pars)
{
string key = "【SQL参数】";
foreach (var param in pars)
{
key += $"{param.ParameterName}:{param.Value}\n";
}
return key;
}
}