track/src/SJZY.Track.Repository/DataBaseContext.cs
2024-03-01 15:07:40 +08:00

77 lines
2.8 KiB
C#

using SJZY.UserCenter.HttpApi.Services;
#pragma warning disable 1591
namespace SJZY.Track.Repository;
/// <summary>
/// Sqlsugar 上下文对象
/// </summary>
public class DataBaseContext
{
#region 审计属性自动赋值
public static void OnExcutingAction(object oldValue, DataFilterModel entityInfo)
{
//过滤掉 "v_" 开头的表
if (entityInfo.EntityColumnInfo.DbTableName.StartsWith("v_", StringComparison.OrdinalIgnoreCase))
throw new Exception("非法的操作,不允许编辑映射表");
//新增
if (entityInfo.OperationType == DataFilterType.InsertByObject)
{
if (entityInfo.PropertyName == nameof(ICreateBaicEntity.CreateTime))
entityInfo.SetValue(DateTime.Now);
if (entityInfo.PropertyName == nameof(ICreateBaicEntity.CreateUserId))
{
if (oldValue == null || (int)oldValue == 0)
{
try
{
entityInfo.SetValue(CurrentUserRequest.CurrentUser?.UserId);
}
catch
{
}
}
//entityInfo.SetValue(CurrentUserRequest.CurrentUser.UserId);
}
}
//修改
else if (entityInfo.OperationType == DataFilterType.UpdateByObject)
{
if (entityInfo.PropertyName == nameof(IUpdateBasicEntity.UpdateTime))
entityInfo.SetValue(DateTime.Now);
else if (entityInfo.PropertyName == nameof(IUpdateBasicEntity.UpdateUserId))
{
if (oldValue == null || (int)oldValue == 1)
{
try
{
entityInfo.SetValue(CurrentUserRequest.CurrentUser.UserId);
}
catch
{
}
}
//entityInfo.SetValue(CurrentUserRequest.CurrentUser.UserId);
}
//删除
else if (entityInfo.PropertyName == nameof(IDeleteBasicEntity.IsDeleted))
{
var currentType = entityInfo.EntityValue.GetType();
bool.TryParse(
currentType.GetProperty(entityInfo.PropertyName)?.GetValue(entityInfo.EntityValue)?.ToString(),
out bool isdelete);
if (isdelete)
{
//currentType.GetProperty(nameof(IDeleteBasicEntity.DeleteUserId))?.SetValue(entityInfo.EntityValue, 5);
currentType.GetProperty(nameof(IDeleteBasicEntity.DeleteTime))
?.SetValue(entityInfo.EntityValue, DateTime.Now);
}
}
}
}
#endregion 审计属性自动赋值
}