using 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.CodeConfigEnum; 
 | 
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 
 | 
{ 
 | 
    public static class SqlSugarAop 
 | 
    { 
 | 
        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) 
 | 
                { 
 | 
                    if (propertyInfo.GetValue(entityInfo.EntityValue) == 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); 
 | 
                        } 
 | 
                    } 
 | 
                } 
 | 
  
 | 
                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.EntityValue is BaseEntity baseEntity) 
 | 
            { 
 | 
                // 新增操作 
 | 
                if (entityInfo.OperationType == DataFilterType.InsertByObject && entityInfo.PropertyName == nameof(BaseEntity.CreateDate)) 
 | 
                { 
 | 
                    baseEntity.CreateDate = DateTime.Now; 
 | 
                } 
 | 
  
 | 
                if (entityInfo.OperationType == DataFilterType.UpdateByObject && entityInfo.PropertyName == nameof(BaseEntity.ModifyDate)) 
 | 
                { 
 | 
                    baseEntity.ModifyDate = DateTime.Now; 
 | 
                } 
 | 
  
 | 
                //if (App.User?.UserId > 0) 
 | 
                { 
 | 
                    switch (entityInfo.OperationType) 
 | 
                    { 
 | 
                        case DataFilterType.UpdateByObject: 
 | 
                            baseEntity.Modifier = App.User.UserName; 
 | 
                            break; 
 | 
                        case DataFilterType.InsertByObject: 
 | 
                            baseEntity.Creater = App.User.UserName; 
 | 
                            break; 
 | 
                    } 
 | 
                } 
 | 
            } 
 | 
  
 | 
            if (entityInfo.EntityValue is IBaseHistoryEntity baseHistoryEntity) 
 | 
            { 
 | 
                if (entityInfo.OperationType == DataFilterType.InsertByObject && entityInfo.PropertyName == nameof(IBaseHistoryEntity.InsertTime)) 
 | 
                { 
 | 
                    baseHistoryEntity.InsertTime = DateTime.Now; 
 | 
                } 
 | 
            } 
 | 
        } 
 | 
  
 | 
        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($"[{CodeFormatTypeEnum.YYYY}]", now.Year.ToString().PadLeft(4, '0')); 
 | 
                code = code.Replace($"[{CodeFormatTypeEnum.MM}]", now.Month.ToString().PadLeft(2, '0')); 
 | 
                code = code.Replace($"[{CodeFormatTypeEnum.DD}]", now.Day.ToString().PadLeft(2, '0')); 
 | 
                code = code.Replace($"[{CodeFormatTypeEnum.ST}]", ruleConfig.StartStr.ToString()); 
 | 
                code = code.Replace($"[{CodeFormatTypeEnum.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; 
 | 
        } 
 | 
    } 
 | 
} 
 |