Kaynağa Gözat

fixup! 😀refactor: 优化枚举转字典任务

喵你个旺呀 1 yıl önce
ebeveyn
işleme
7952b84c27

+ 3 - 1
Admin.NET/Admin.NET.Core/Job/EnumToDictJob.cs

@@ -4,6 +4,8 @@
 //
 // 不得利用本项目从事危害国家安全、扰乱社会秩序、侵犯他人合法权益等法律法规禁止的活动!任何基于本项目二次开发而产生的一切法律纠纷和责任,我们不承担任何责任!
 
+using System.Security.Cryptography;
+
 namespace Admin.NET.Core;
 
 /// <summary>
@@ -77,7 +79,7 @@ public class EnumToDictJob : IJob
         {
             var dictType = new SysDictType
             {
-                Id = 900000000000 + Math.Abs(type.TypeName.GetHashCode()),
+                Id = 900000000000 + CommonUtil.GetFixedHashCode(type.TypeName),
                 Code = type.TypeName,
                 Name = type.TypeDescribe,
                 Remark = type.TypeRemark

+ 23 - 0
Admin.NET/Admin.NET.Core/Utils/CommonUtil.cs

@@ -17,6 +17,29 @@ namespace Admin.NET.Core;
 /// </summary>
 public static class CommonUtil
 {
+    /// <summary>
+    /// 根据字符串获取固定整型哈希值
+    /// </summary>
+    /// <param name="str"></param>
+    /// <returns></returns>
+    public static int GetFixedHashCode(string str)
+    {
+        if (string.IsNullOrWhiteSpace(str)) return 0;
+        unchecked
+        {
+            int hash1 = (5381 << 16) + 5381;
+            int hash2 = hash1;
+            for (int i = 0; i < str.Length; i += 2)
+            {
+                hash1 = ((hash1 << 5) + hash1) ^ str[i];
+                if (i == str.Length - 1)
+                    break;
+                hash2 = ((hash2 << 5) + hash2) ^ str[i + 1];
+            }
+            return Math.Abs(hash1 + (hash2 * 1566083941));
+        }
+    }
+    
     /// <summary>
     /// 生成百分数
     /// </summary>