using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using WIDESEA_Core.Attributes;
using WIDESEA_Core.Const;
using WIDESEA_Core.DB;
using WIDESEA_Core.DB.Models;
using WIDESEA_Core.Helper;
using WIDESEA_Core.HttpContextUser;
using WIDESEA_Core.Seed;
using WIDESEA_Core.Tenants;
namespace WIDESEA_Core.AOP
{
public static class SqlSugarAop
{
///
/// 数据执行拦截器,用于处理实体操作时的公共字段赋值和序列生成
///
/// 旧值
/// 实体过滤模型,包含操作类型、属性信息等
///
/// 功能包括:
/// 1. 自动设置BaseEntity的CreateDate/ModifyDate时间戳
/// 2. 处理带[SequenceAttribute]标记的字段序列生成
/// 3. 设置操作人信息(用户或System)
/// 4. 处理IBaseHistoryEntity的时间戳字段
/// 支持新增(InsertByObject)和更新(UpdateByObject)操作
///
public static void DataExecuting(object oldValue, DataFilterModel entityInfo)
{
if (entityInfo.EntityValue is BaseEntity baseEntity)
{
// 新增操作
if (entityInfo.OperationType == DataFilterType.InsertByObject)
{
if (entityInfo.PropertyName == nameof(BaseEntity.CreateDate))
{
baseEntity.CreateDate = DateTime.Now;
}
PropertyInfo propertyInfo = entityInfo.EntityColumnInfo.PropertyInfo;
SequenceAttribute? sequenceAttirbute = propertyInfo.GetCustomAttribute();
if (sequenceAttirbute != null)
{
if (propertyInfo.GetValue(entityInfo.EntityValue)?.ObjToInt() <= 0)
{
SqlSugarClient sugarClient = new SqlSugarClient(new ConnectionConfig
{
ConfigId = MainDb.CurrentDbConnId,
ConnectionString = DBContext.GetMainConnectionDb().Connection,
IsAutoCloseConnection = true,
DbType = DBContext.DbType,
});
int count = sugarClient.Ado.GetScalar($"SELECT COUNT(*) FROM sys.sequences WHERE name = '{sequenceAttirbute.SequenceName}'").ObjToInt();
if (count == 0)
{
string sql = $"CREATE SEQUENCE {sequenceAttirbute.SequenceName} AS [int] START WITH {sequenceAttirbute.StartWith} INCREMENT BY {sequenceAttirbute.Increment} MINVALUE {sequenceAttirbute.SeqMinValue} MAXVALUE {sequenceAttirbute.SeqMaxValue} {(sequenceAttirbute.IsCycle ? "CYCLE" : "")} CACHE";
sugarClient.Ado.ExecuteCommand(sql);
}
int seq = sugarClient.Ado.GetScalar($"SELECT NEXT VALUE FOR {sequenceAttirbute.SequenceName}").ObjToInt();
propertyInfo.SetValue(entityInfo.EntityValue, seq, null);
}
}
}
else if (entityInfo.OperationType == DataFilterType.UpdateByObject)
{
baseEntity.ModifyDate = DateTime.Now;
}
try
{
if (App.User?.UserId > 0)
{
switch (entityInfo.OperationType)
{
case DataFilterType.UpdateByObject:
baseEntity.Modifier = App.User.UserName;
break;
case DataFilterType.InsertByObject:
baseEntity.Creater = App.User.UserName;
break;
}
}
else
{
switch (entityInfo.OperationType)
{
case DataFilterType.UpdateByObject:
baseEntity.Modifier = "System";
break;
case DataFilterType.InsertByObject:
baseEntity.Creater = "System";
break;
}
}
}
catch (NullReferenceException)
{
switch (entityInfo.OperationType)
{
case DataFilterType.UpdateByObject:
baseEntity.Modifier = "System";
break;
case DataFilterType.InsertByObject:
baseEntity.Creater = "System";
break;
}
}
}
if (entityInfo.EntityValue is IBaseHistoryEntity baseHistoryEntity)
{
if (entityInfo.OperationType == DataFilterType.InsertByObject && entityInfo.PropertyName == nameof(IBaseHistoryEntity.InsertTime))
{
baseHistoryEntity.InsertTime = DateTime.Now;
}
}
}
}
}