Browse Source

数据审计新增有序guid

zhontai 2 years ago
parent
commit
7f46ddecb4

+ 9 - 0
src/platform/ZhonTai.Admin/Core/Attributes/OrderGuidAttribute.cs

@@ -0,0 +1,9 @@
+using System;
+
+namespace ZhonTai.Admin.Core.Attributes;
+
+[AttributeUsage(AttributeTargets.Property)]
+public class OrderGuidAttribute : Attribute
+{
+    public bool Enable { get; set; } = true;
+}

+ 13 - 3
src/platform/ZhonTai.Admin/Core/Db/DbHelper.cs

@@ -144,13 +144,15 @@ public class DbHelper
     /// <param name="user"></param>
     public static void AuditValue(AuditValueEventArgs e, TimeSpan timeOffset, IUser user)
     {
-        if (e.Property.GetCustomAttribute<ServerTimeAttribute>(false) != null
-               && (e.Column.CsType == typeof(DateTime) || e.Column.CsType == typeof(DateTime?))
-               && (e.Value == null || (DateTime)e.Value == default || (DateTime?)e.Value == default))
+        //数据库时间
+        if ((e.Column.CsType == typeof(DateTime) || e.Column.CsType == typeof(DateTime?))
+        && e.Property.GetCustomAttribute<ServerTimeAttribute>(false) != null
+        && (e.Value == null || (DateTime)e.Value == default || (DateTime?)e.Value == default))
         {
             e.Value = DateTime.Now.Subtract(timeOffset);
         }
 
+        //雪花Id
         if (e.Column.CsType == typeof(long)
         && e.Property.GetCustomAttribute<SnowflakeAttribute>(false) is SnowflakeAttribute snowflakeAttribute
         && snowflakeAttribute.Enable && (e.Value == null || (long)e.Value == default || (long?)e.Value == default))
@@ -158,6 +160,14 @@ public class DbHelper
             e.Value = YitIdHelper.NextId();
         }
 
+        //有序Guid
+        if (e.Column.CsType == typeof(Guid)
+        && e.Property.GetCustomAttribute<OrderGuidAttribute>(false) is OrderGuidAttribute orderGuidAttribute
+        && orderGuidAttribute.Enable && (e.Value == null || (Guid)e.Value == default || (Guid?)e.Value == default))
+        {
+            e.Value = FreeUtil.NewMongodbId();
+        }
+
         if (user == null || user.Id <= 0)
         {
             return;