对比新文件 |
| | |
| | | 锘縰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 |
| | | { |
| | | 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) |
| | | { |
| | | 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) |
| | | { |
| | | // 鏂板鎿嶄綔 |
| | | if (entityInfo.OperationType == DataFilterType.InsertByObject) |
| | | { |
| | | if (entityInfo.PropertyName == nameof(BaseEntity.CreateDate)) |
| | | { |
| | | baseEntity.CreateDate = DateTime.Now; |
| | | } |
| | | } |
| | | |
| | | if (entityInfo.OperationType == DataFilterType.UpdateByObject) |
| | | { |
| | | 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; |
| | | } |
| | | } |
| | | } |
| | | } |
| | | |
| | | 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) |
| | | { |
| | | 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; |
| | | } |
| | | } |
| | | } |