using FreeSql.Aop;
using System.Reflection;
using Yitter.IdGenerator;
using Znyc.CloudCar.Auth.HttpContextUser;
using Znyc.CloudCar.Model;
using Znyc.CloudCar.Utility.Extensions;
namespace Znyc.CloudCar.Core.Db
{
public class DbHelper
{
///
/// 审计数据
///
///
///
public static void AuditValue(AuditValueEventArgs e, IHttpContextUser httpContextUser)
{
if (e.Column.CsType == typeof(long)
&& e.Property.GetCustomAttribute(false) != null
&& (e.Value.IsNull() || (long)e.Value == default || (long?)e.Value == default))
{
e.Value = YitIdHelper.NextId();
}
if (httpContextUser.IsNull() || httpContextUser.Id <= 0)
{
return;
}
if (e.AuditValueType == AuditValueType.Insert)
{
switch (e.Property.Name)
{
case "CreatedUserId":
if (e.Value.IsNull() || (long)e.Value == default || (long?)e.Value == default)
{
e.Value = httpContextUser.Id;
}
break;
case "CreatedTime":
e.Value = DateTime.Now;
break;
}
}
else if (e.AuditValueType == AuditValueType.Update)
{
switch (e.Property.Name)
{
case "ModifiedUserId":
e.Value = httpContextUser.Id;
break;
case "ModifiedTime":
e.Value = DateTime.Now;
break;
}
}
}
}
}