track/src/SJZY.Track.Repository/DataBaseContext.cs
2024-08-20 18:18:57 +08:00

67 lines
2.5 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)
{
var time = DateTime.Now;
var userId = CurrentUserRequest.CurrentUser.UserId;
if (entityInfo.PropertyName == nameof(ICreateBaicEntity.CreateTime))
entityInfo.SetValue(time);
else if (entityInfo.PropertyName == nameof(IUpdateBasicEntity.UpdateTime))
entityInfo.SetValue(time);
else if (entityInfo.PropertyName == nameof(ICreateBaicEntity.CreateUserId))
{
entityInfo.SetValue(userId);
}
else if (entityInfo.PropertyName == nameof(IUpdateBasicEntity.UpdateUserId))
{
entityInfo.SetValue(userId);
}
}
//修改
else if (entityInfo.OperationType == DataFilterType.UpdateByObject)
{
if (entityInfo.PropertyName == nameof(IUpdateBasicEntity.UpdateTime))
entityInfo.SetValue(DateTime.Now);
else if (entityInfo.PropertyName == nameof(IUpdateBasicEntity.UpdateUserId))
{
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 审计属性自动赋值
}