From 46e98339480d853fc78a014c34d7ff9fcaf13890 Mon Sep 17 00:00:00 2001
From: dengjunjie <dengjunjie@hnkhzn.com>
Date: 星期四, 05 十二月 2024 14:09:02 +0800
Subject: [PATCH] 产线协议

---
 项目代码/WMS/WIDESEA_WMSServer/WIDESEA_Core/AOP/SqlSugarAop.cs |  116 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++-
 1 files changed, 114 insertions(+), 2 deletions(-)

diff --git "a/\351\241\271\347\233\256\344\273\243\347\240\201/WMS/WIDESEA_WMSServer/WIDESEA_Core/AOP/SqlSugarAop.cs" "b/\351\241\271\347\233\256\344\273\243\347\240\201/WMS/WIDESEA_WMSServer/WIDESEA_Core/AOP/SqlSugarAop.cs"
index e0880c9..dac84c9 100644
--- "a/\351\241\271\347\233\256\344\273\243\347\240\201/WMS/WIDESEA_WMSServer/WIDESEA_Core/AOP/SqlSugarAop.cs"
+++ "b/\351\241\271\347\233\256\344\273\243\347\240\201/WMS/WIDESEA_WMSServer/WIDESEA_Core/AOP/SqlSugarAop.cs"
@@ -1,14 +1,21 @@
-锘縰sing SqlSugar;
+锘縰sing OfficeOpenXml.FormulaParsing.Excel.Functions.DateTime;
+using SqlSugar;
+using StackExchange.Profiling;
 using System;
 using System.Collections.Generic;
 using System.Linq;
 using System.Reflection;
 using System.Text;
 using System.Threading.Tasks;
+using WIDESEA_Core.Attributes;
 using WIDESEA_Core.Const;
+using WIDESEA_Core.DB;
 using WIDESEA_Core.DB.Models;
+using WIDESEA_Core.Enums;
 using WIDESEA_Core.Helper;
+using WIDESEA_Core.Seed;
 using WIDESEA_Core.Tenants;
+using static Org.BouncyCastle.Math.EC.ECCurve;
 
 namespace WIDESEA_Core.AOP
 {
@@ -16,6 +23,52 @@
     {
         public static void DataExecuting(object oldValue, DataFilterModel entityInfo)
         {
+            if (entityInfo.OperationType == DataFilterType.InsertByObject)
+            {
+                PropertyInfo propertyInfo = entityInfo.EntityColumnInfo.PropertyInfo;
+                CodeRuleAttribute? codeRuleAttribute = propertyInfo.GetCustomAttribute<CodeRuleAttribute>();
+                if (codeRuleAttribute != null)
+                {
+                    SqlSugarClient sugarClient = new SqlSugarClient(new ConnectionConfig
+                    {
+                        ConfigId = MainDb.CurrentDbConnId,
+                        ConnectionString = DBContext.GetMainConnectionDb().Connection,
+                        IsAutoCloseConnection = true,
+                        DbType = MainDb.DbType,
+                    });
+                    dynamic ruleConfig = sugarClient.Queryable(MainDb.CodeRuleConfig, "x").Where(nameof(CodeRuleAttribute.RuleCode), "=", codeRuleAttribute.RuleCode.ToString()).First();
+                    if (ruleConfig != null)
+                    {
+                        string code = CreateCodeByRule(ruleConfig, sugarClient);
+                        propertyInfo.SetValue(entityInfo.EntityValue, code, null);
+                    }
+                }
+
+                SequenceAttirbute? sequenceAttirbute = propertyInfo.GetCustomAttribute<SequenceAttirbute>();
+                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.EntityValue is BaseEntity baseEntity)
             {
                 // 鏂板鎿嶄綔
@@ -41,13 +94,72 @@
                             baseEntity.Modifier = App.User.UserName;
                             break;
                         case DataFilterType.InsertByObject:
-                                baseEntity.Creater = App.User.UserName;
+                            baseEntity.Creater = App.User.UserName;
                             break;
                     }
                 }
             }
         }
 
+        private static void UpdateConfigData(dynamic ruleConfig)
+        {
+            try
+            {
+
+            }
+            catch (Exception ex)
+            {
+
+            }
+        }
+
+        private static string CreateCodeByRule(dynamic ruleConfig, SqlSugarClient sugarClient)
+        {
+            string code = string.Empty;
+            DateTime dataTime = DateTime.Now;
+            DateTime now = DateTime.Now;
+            try
+            {
+                if (ruleConfig.ModifyDate != null)
+                {
+                    dataTime = Convert.ToDateTime(ruleConfig.ModifyDate);
+                }
+                else
+                {
+                    dataTime = Convert.ToDateTime(ruleConfig.CreateDate);
+                }
+
+                if (now.Year == dataTime.Year && now.Month == dataTime.Month && now.Day == dataTime.Day)
+                {
+                    now = dataTime;
+                    ruleConfig.CurrentVal = Convert.ToInt32(ruleConfig.CurrentVal) + 1;
+                }
+                else
+                {
+                    ruleConfig.CurrentVal = 1;
+                }
+
+                ruleConfig.ModifyDate = DateTime.Now;
+
+                code = ruleConfig.Format;
+                code = code.Replace($"[{CodeFormat.YYYY}]", now.Year.ToString().PadLeft(4, '0'));
+                code = code.Replace($"[{CodeFormat.MM}]", now.Month.ToString().PadLeft(2, '0'));
+                code = code.Replace($"[{CodeFormat.DD}]", now.Day.ToString().PadLeft(2, '0'));
+                code = code.Replace($"[{CodeFormat.ST}]", ruleConfig.StartStr.ToString());
+                code = code.Replace($"[{CodeFormat.NUM}]", ruleConfig.CurrentVal.ToString().PadLeft(ruleConfig.Length ?? 0, '0'));
+            }
+            catch (Exception ex)
+            {
+
+            }
+            finally
+            {
+                Dictionary<string, object> keyValuePairs = new Dictionary<string, object>() { { nameof(ruleConfig.CurrentVal), ruleConfig.CurrentVal }, { nameof(ruleConfig.Id), ruleConfig.Id }, { nameof(ruleConfig.ModifyDate), DateTime.Now } };
+                sugarClient.Updateable(keyValuePairs).AS(MainDb.CodeRuleConfig).WhereColumns(nameof(ruleConfig.Id)).ExecuteCommand();
+            }
+            return code;
+        }
+
         private static string GetWholeSql(SugarParameter[] paramArr, string sql)
         {
             foreach (var param in paramArr)

--
Gitblit v1.9.3