From 7ff298c6834275b63b612af49651673689a39660 Mon Sep 17 00:00:00 2001
From: hutongqing <hutongqing@hnkhzn.com>
Date: 星期三, 27 十一月 2024 11:33:18 +0800
Subject: [PATCH] 更新

---
 WIDESEAWCS_Server/WIDESEAWCS_Core/AOP/SqlSugarAop.cs |   51 ++++++++++++++++++++++++++++-----------------------
 1 files changed, 28 insertions(+), 23 deletions(-)

diff --git a/WIDESEAWCS_Server/WIDESEAWCS_Core/AOP/SqlSugarAop.cs b/WIDESEAWCS_Server/WIDESEAWCS_Core/AOP/SqlSugarAop.cs
index 5fde8d1..e69682a 100644
--- a/WIDESEAWCS_Server/WIDESEAWCS_Core/AOP/SqlSugarAop.cs
+++ b/WIDESEAWCS_Server/WIDESEAWCS_Core/AOP/SqlSugarAop.cs
@@ -5,10 +5,13 @@
 using System.Reflection;
 using System.Text;
 using System.Threading.Tasks;
+using WIDESEAWCS_Core.Attributes;
 using WIDESEAWCS_Core.Const;
+using WIDESEAWCS_Core.DB;
 using WIDESEAWCS_Core.DB.Models;
 using WIDESEAWCS_Core.Helper;
 using WIDESEAWCS_Core.HttpContextUser;
+using WIDESEAWCS_Core.Seed;
 using WIDESEAWCS_Core.Tenants;
 
 namespace WIDESEAWCS_Core.AOP
@@ -26,9 +29,33 @@
                     {
                         baseEntity.CreateDate = DateTime.Now;
                     }
+
+                    PropertyInfo propertyInfo = entityInfo.EntityColumnInfo.PropertyInfo;
+                    SequenceAttribute? sequenceAttirbute = propertyInfo.GetCustomAttribute<SequenceAttribute>();
+                    if (sequenceAttirbute != null)
+                    {
+                        if (propertyInfo.GetValue(entityInfo.EntityValue)?.ObjToInt() <= 0)
+                        {
+                            SqlSugarClient sugarClient = new SqlSugarClient(new ConnectionConfig
+                            {
+                                ConfigId = MainDb.CurrentDbConnId,
+                                ConnectionString = DBContext.GetMainConnectionDb().Connection,
+                                IsAutoCloseConnection = true,
+                                DbType = MainDb.DbType,
+                            });
+                            int count = sugarClient.Ado.GetScalar($"SELECT COUNT(*) FROM sys.sequences WHERE name = '{sequenceAttirbute.SequenceName}'").ObjToInt();
+                            if (count == 0)
+                            {
+                                string sql = $"CREATE SEQUENCE {sequenceAttirbute.SequenceName} AS [int] START WITH {sequenceAttirbute.StartWith} INCREMENT BY {sequenceAttirbute.Increment} MINVALUE {sequenceAttirbute.SeqMinValue} MAXVALUE {sequenceAttirbute.SeqMaxValue} {(sequenceAttirbute.IsCycle ? "CYCLE" : "")} CACHE";
+                                sugarClient.Ado.ExecuteCommand(sql);
+                            }
+                            int seq = sugarClient.Ado.GetScalar($"SELECT NEXT VALUE FOR {sequenceAttirbute.SequenceName}").ObjToInt();
+                            propertyInfo.SetValue(entityInfo.EntityValue, seq, null);
+                        }
+                    }
                 }
 
-                if (entityInfo.OperationType == DataFilterType.UpdateByObject)
+                else if (entityInfo.OperationType == DataFilterType.UpdateByObject)
                 {
                     baseEntity.ModifyDate = DateTime.Now;
                 }
@@ -72,29 +99,7 @@
                             break;
                     }
                 }
-                
             }
-        }
-
-        private static string GetWholeSql(SugarParameter[] paramArr, string sql)
-        {
-            foreach (var param in paramArr)
-            {
-                sql = sql.Replace(param.ParameterName, $@"'{param.Value.ObjToString()}'");
-            }
-
-            return sql;
-        }
-
-        private static string GetParas(SugarParameter[] pars)
-        {
-            string key = "銆怱QL鍙傛暟銆戯細";
-            foreach (var param in pars)
-            {
-                key += $"{param.ParameterName}:{param.Value}\n";
-            }
-
-            return key;
         }
     }
 }

--
Gitblit v1.9.3