From c6016a671b2f6a031cd3940d47dcc7de60acd007 Mon Sep 17 00:00:00 2001 From: 2881099 <2881099@qq.com> Date: Fri, 2 Sep 2022 18:18:52 +0800 Subject: [PATCH] =?UTF-8?q?-=20=E4=BC=98=E5=8C=96=20JsonMap=20+=20AuditVal?= =?UTF-8?q?ue=20=E6=83=85=E5=86=B5=EF=BC=9B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../FreeSql.Extensions.JsonMap/JsonMapCore.cs | 6 ++- FreeSql.Repository/AggregateRootRepository.cs | 11 ++--- .../AggregateRootRepositorySync.cs | 47 +++++++++++++++++-- 3 files changed, 52 insertions(+), 12 deletions(-) diff --git a/Extensions/FreeSql.Extensions.JsonMap/JsonMapCore.cs b/Extensions/FreeSql.Extensions.JsonMap/JsonMapCore.cs index fb901249..25ab7407 100644 --- a/Extensions/FreeSql.Extensions.JsonMap/JsonMapCore.cs +++ b/Extensions/FreeSql.Extensions.JsonMap/JsonMapCore.cs @@ -39,7 +39,11 @@ public static class FreeSqlJsonMapCoreExtensions { FreeSql.Internal.Utils.GetDataReaderValueBlockExpressionSwitchTypeFullName.Add((LabelTarget returnTarget, Expression valueExp, Type type) => { - if (_dicTypes.ContainsKey(type)) return Expression.Return(returnTarget, Expression.TypeAs(Expression.Call(MethodJsonConvertDeserializeObject, Expression.Convert(valueExp, typeof(string)), Expression.Constant(type)), type)); + if (_dicTypes.ContainsKey(type)) return Expression.IfThenElse( + Expression.TypeIs(valueExp, type), + Expression.Return(returnTarget, valueExp), + Expression.Return(returnTarget, Expression.TypeAs(Expression.Call(MethodJsonConvertDeserializeObject, Expression.Convert(valueExp, typeof(string)), Expression.Constant(type)), type)) + ); return null; }); } diff --git a/FreeSql.Repository/AggregateRootRepository.cs b/FreeSql.Repository/AggregateRootRepository.cs index 6e4440d6..b9d2f527 100644 --- a/FreeSql.Repository/AggregateRootRepository.cs +++ b/FreeSql.Repository/AggregateRootRepository.cs @@ -37,13 +37,12 @@ namespace FreeSql public IUnitOfWork UnitOfWork { get => _repository.UnitOfWork; set => _repository.UnitOfWork = value; } public DbContextOptions DbContextOptions { - get => _repository.DbContextOptions; set + get => _repository.DbContextOptions; + set { - if (value != null) - { - _repository.DbContextOptions = value; - _repository.DbContextOptions.EnableCascadeSave = false; - } + if (value == null) throw new ArgumentNullException(nameof(DbContextOptions)); + _repository.DbContextOptions = value; + _repository.DbContextOptions.EnableCascadeSave = false; } } public void AsType(Type entityType) => _repository.AsType(entityType); diff --git a/FreeSql.Repository/AggregateRootRepositorySync.cs b/FreeSql.Repository/AggregateRootRepositorySync.cs index 6b261031..857ff1b6 100644 --- a/FreeSql.Repository/AggregateRootRepositorySync.cs +++ b/FreeSql.Repository/AggregateRootRepositorySync.cs @@ -36,7 +36,7 @@ namespace FreeSql var repos = new Dictionary(); try { - var ret = InsertAggregateRootStatic(_repository, GetChildRepository, entitys); + var ret = InsertAggregateRootStatic(_repository, GetChildRepository, entitys, out var affrows); Attach(ret); return ret; } @@ -46,10 +46,18 @@ namespace FreeSql _repository.FlushState(); } } - protected static List InsertAggregateRootStatic(IBaseRepository rootRepository, Func> getChildRepository, IEnumerable rootEntitys) { + protected static List InsertAggregateRootStatic(IBaseRepository rootRepository, Func> getChildRepository, IEnumerable rootEntitys, out int affrows) where T1 : class { Dictionary> ignores = new Dictionary>(); Dictionary> repos = new Dictionary>(); - return LocalInsertAggregateRoot(rootRepository, rootEntitys); + var localAffrows = 0; + try + { + return LocalInsertAggregateRoot(rootRepository, rootEntitys); + } + finally + { + affrows = localAffrows; + } bool LocalCanAggregateRoot(Type entityType, object entity, bool isadd) { @@ -71,7 +79,7 @@ namespace FreeSql } return false; } - List LocalInsertAggregateRoot(IBaseRepository repository, IEnumerable entitys) where T1 : class + List LocalInsertAggregateRoot(IBaseRepository repository, IEnumerable entitys) where T2 : class { var table = repository.Orm.CodeFirst.GetTableByEntity(repository.EntityType); if (table.Primarys.Any(col => col.Attribute.IsIdentity)) @@ -80,6 +88,7 @@ namespace FreeSql repository.Orm.ClearEntityPrimaryValueWithIdentity(repository.EntityType, entity); } var ret = repository.Insert(entitys); + localAffrows += ret.Count; foreach (var entity in entitys) LocalCanAggregateRoot(repository.EntityType, entity, true); foreach (var prop in table.Properties.Values) @@ -175,7 +184,35 @@ namespace FreeSql AggregateRootUtils.CompareEntityValue(Orm, EntityType, state.Value, entity, null, insertLog, updateLog, deleteLog); Attach(entity); } - return insertLog.Count + updateLog.Count + deleteLog.Count; + var affrows = 0; + + DisposeChildRepositorys(); + var insertLogDict = insertLog.GroupBy(a => a.Item1).ToDictionary(a => a.Key, a => insertLog.Where(b => b.Item1 == a.Key).Select(b => b.Item2).ToArray()); + foreach (var il in insertLogDict) + { + InsertAggregateRootStatic(GetChildRepository(il.Key), GetChildRepository, il.Value, out var affrowsOut); + affrows += affrowsOut; + } + DisposeChildRepositorys(); + + var deleteLogDict = deleteLog.GroupBy(a => a.Item1).ToDictionary(a => a.Key, a => deleteLog.Where(b => b.Item1 == a.Key).Select(b => b.Item2).ToArray()); + foreach (var dl in deleteLogDict) + affrows += Orm.Delete().AsType(dl.Key).WhereDynamic(dl.Value).ExecuteAffrows(); + + var updateLogDict = updateLog.GroupBy(a => a.Item1).ToDictionary(a => a.Key, a => updateLog.Where(b => b.Item1 == a.Key).Select(b => + NativeTuple.Create(b.Item2, b.Item3, string.Join(",", b.Item4.OrderBy(c => c)), b.Item4)).ToArray()); + var updateLogDict2 = updateLogDict.ToDictionary(a => a.Key, a => a.Value.ToDictionary(b => b.Item3, b => a.Value.Where(c => c.Item3 == b.Item3).ToArray())); + foreach (var dl in updateLogDict2) + { + foreach (var dl2 in dl.Value) + { + affrows += Orm.Update().AsType(dl.Key) + .SetSource(dl2.Value.Select(a => a.Item2).ToArray()) + .UpdateColumns(dl2.Value.First().Item4.ToArray()) + .ExecuteAffrows(); + } + } + return affrows; } protected virtual int DeleteAggregateRoot(IEnumerable entitys, List deletedOutput = null) {