diff --git a/Src/Asp.Net/SqlSugar/Abstract/ExecuteNavProvider/DeleteNavOneToMany.cs b/Src/Asp.Net/SqlSugar/Abstract/ExecuteNavProvider/DeleteNavOneToMany.cs index 54be3acd0..d1b33208d 100644 --- a/Src/Asp.Net/SqlSugar/Abstract/ExecuteNavProvider/DeleteNavOneToMany.cs +++ b/Src/Asp.Net/SqlSugar/Abstract/ExecuteNavProvider/DeleteNavOneToMany.cs @@ -4,16 +4,92 @@ using System.Linq; using System.Text; using System.Threading.Tasks; -namespace SqlSugar +namespace SqlSugar { public partial class DeleteNavProvider where T : class, new() where Root : class, new() { - private void DeleteOneToMany(string name, EntityColumnInfo nav) where TChild : class, new() { - throw new NotImplementedException(); + var parentEntity = _ParentEntity; + var prentList = _ParentList.Cast().ToList(); + var parentNavigateProperty = parentEntity.Columns.FirstOrDefault(it => it.PropertyName == name); + var thisEntity = this._Context.EntityMaintenance.GetEntityInfo(); + var thisPkColumn = GetPkColumnByNav(thisEntity, nav); + var thisFkColumn = GetFKColumnByNav(thisEntity, nav); + EntityColumnInfo parentPkColumn = GetParentPkColumn(); + EntityColumnInfo parentNavColumn = GetParentPkNavColumn(nav); + if (parentNavColumn != null) + { + parentPkColumn = parentNavColumn; + } + + if (!_IsDeletedParant) + SetContext(() => this._Context.Deleteable(prentList).ExecuteCommand()); + + var ids = _ParentList.Select(it => parentPkColumn.PropertyInfo.GetValue(it)).ToList(); + var childList = this._Context.Queryable().In(thisFkColumn.DbColumnName, ids).ToList(); + + this._ParentList = childList.Cast().ToList(); + this._ParentPkColumn = thisPkColumn; + this._IsDeletedParant = true; + + SetContext(() => this._Context.Deleteable(childList).ExecuteCommand()); + } + + private void SetContext(Action action) + { + var key = "_DeleteNavTask"; + if (this._Context.TempItems == null) + { + this._Context.TempItems = new Dictionary(); + } + if (!this._Context.TempItems.ContainsKey(key)) + { + this._Context.TempItems.Add(key, null); + } + var oldTask = this._Context.TempItems[key]; + var newTask = new List(); + if (oldTask != null) + { + newTask = (List)oldTask; + } + newTask.Add(action); + this._Context.TempItems[key] = newTask; + } + + private EntityColumnInfo GetParentPkColumn() + { + EntityColumnInfo parentPkColumn = _ParentPkColumn; + if (_ParentPkColumn == null) + { + parentPkColumn = _ParentPkColumn = this._ParentEntity.Columns.FirstOrDefault(it => it.IsPrimarykey); + } + return parentPkColumn; + } + private EntityColumnInfo GetParentPkNavColumn(EntityColumnInfo nav) + { + EntityColumnInfo result = null; + if (nav.Navigat.Name2.HasValue()) + { + result = _ParentPkColumn = this._ParentEntity.Columns.FirstOrDefault(it => it.PropertyName == nav.Navigat.Name2); + } + return result; } + private EntityColumnInfo GetPkColumnByNav(EntityInfo entity, EntityColumnInfo nav) + { + var pkColumn = entity.Columns.FirstOrDefault(it => it.IsPrimarykey == true); + if (nav.Navigat.Name2.HasValue()) + { + pkColumn = entity.Columns.FirstOrDefault(it => it.PropertyName == nav.Navigat.Name2); + } + return pkColumn; + } + private EntityColumnInfo GetFKColumnByNav(EntityInfo entity, EntityColumnInfo nav) + { + var fkColumn = entity.Columns.FirstOrDefault(it => it.PropertyName == nav.Navigat.Name); + return fkColumn; + } } } diff --git a/Src/Asp.Net/SqlSugar/Abstract/ExecuteNavProvider/DeleteNavProvider.cs b/Src/Asp.Net/SqlSugar/Abstract/ExecuteNavProvider/DeleteNavProvider.cs index 626e351b6..f38d73254 100644 --- a/Src/Asp.Net/SqlSugar/Abstract/ExecuteNavProvider/DeleteNavProvider.cs +++ b/Src/Asp.Net/SqlSugar/Abstract/ExecuteNavProvider/DeleteNavProvider.cs @@ -15,10 +15,12 @@ namespace SqlSugar public EntityInfo _ParentEntity { get; set; } public EntityColumnInfo _ParentPkColumn { get; set; } public SqlSugarProvider _Context { get; set; } + public bool _IsDeletedParant { get; set; } - public DeleteNavTask ThenInclude< TChild>(Expression> expression) + public DeleteNavProvider ThenInclude< TChild>(Expression> expression) where TChild : class, new() { + InitParentList(); var name = ExpressionTool.GetMemberName(expression); var nav = this._ParentEntity.Columns.FirstOrDefault(x => x.PropertyName == name); if (nav.Navigat == null) @@ -37,13 +39,72 @@ namespace SqlSugar { DeleteManyToMany(name, nav); } - return null; + return GetResult(); } - public DeleteNavTask ThenInclude(Expression>> expression) + public DeleteNavProvider ThenInclude(Expression>> expression) where TChild : class, new() { - throw new NotImplementedException(); + InitParentList(); + var name = ExpressionTool.GetMemberName(expression); + var nav = this._ParentEntity.Columns.FirstOrDefault(x => x.PropertyName == name); + if (nav.Navigat == null) + { + Check.ExceptionEasy($"{name} no navigate attribute", $"{this._ParentEntity.EntityName}的属性{name}没有导航属性"); + } + if (nav.Navigat.NavigatType == NavigateType.OneToOne || nav.Navigat.NavigatType == NavigateType.ManyToOne) + { + DeleteOneToOne(name, nav); + } + else if (nav.Navigat.NavigatType == NavigateType.OneToMany) + { + DeleteOneToMany(name, nav); + } + else + { + DeleteManyToMany(name, nav); + } + return GetResult(); + } + private DeleteNavProvider GetResult() where TChild : class, new() + { + return new DeleteNavProvider() + { + _Context = this._Context, + _ParentEntity = this._ParentEntity, + _ParentList = this._ParentList, + _Roots = this._Roots, + _ParentPkColumn = this._ParentPkColumn, + _RootList = this._RootList, + _IsDeletedParant=this._IsDeletedParant + }; + } + public DeleteNavProvider AsNav() + { + return new DeleteNavProvider + { + _Context = _Context, + _ParentEntity = null, + _ParentList = null, + _Roots = _Roots, + _IsDeletedParant = this._IsDeletedParant, + _ParentPkColumn = this._Context.EntityMaintenance.GetEntityInfo().Columns.First(it => it.IsPrimarykey) + }; + } + private void InitParentList() + { + this._ParentEntity = this._Context.EntityMaintenance.GetEntityInfo(); + if (_RootList == null) + { + _RootList = _ParentList = _Roots.Cast().ToList(); + } + else if (_ParentList == null) + { + _ParentList = _RootList; + var pkColumn = this._Context.EntityMaintenance.GetEntityInfo().Columns.FirstOrDefault(it => it.IsPrimarykey); + this._ParentPkColumn = pkColumn; + } + } } } diff --git a/Src/Asp.Net/SqlSugar/Abstract/ExecuteNavProvider/DeleteNavTask.cs b/Src/Asp.Net/SqlSugar/Abstract/ExecuteNavProvider/DeleteNavTask.cs index 1122b6508..d3433384c 100644 --- a/Src/Asp.Net/SqlSugar/Abstract/ExecuteNavProvider/DeleteNavTask.cs +++ b/Src/Asp.Net/SqlSugar/Abstract/ExecuteNavProvider/DeleteNavTask.cs @@ -9,7 +9,7 @@ namespace SqlSugar { public class DeleteNavTaskInit where T : class, new() where Root : class, new() { - public List Roots { get; set; } + internal List Roots { get; set; } internal SqlSugarProvider Context { get; set; } internal DeleteNavProvider deleteNavProvider { get; set; } @@ -17,7 +17,7 @@ namespace SqlSugar { this.Context = deleteNavProvider._Context; DeleteNavTask result = new DeleteNavTask(); - Func> func = () => deleteNavProvider.ThenInclude(expression); + Func> func = () => deleteNavProvider.ThenInclude(expression); result.PreFunc = func; result.Context = this.Context; return result; @@ -26,7 +26,7 @@ namespace SqlSugar { this.Context = deleteNavProvider._Context; DeleteNavTask result = new DeleteNavTask(); - Func> func = () => deleteNavProvider.ThenInclude(expression); + Func> func = () => deleteNavProvider.ThenInclude(expression); result.PreFunc = func; result.Context = this.Context; return result; @@ -35,11 +35,11 @@ namespace SqlSugar public class DeleteNavTask where T : class, new() where Root : class, new() { public SqlSugarProvider Context { get; set; } - public Func> PreFunc { get; set; } + public Func> PreFunc { get; set; } public DeleteNavTask ThenInclude(Expression> expression) where TChild : class, new() { DeleteNavTask result = new DeleteNavTask(); - Func> func = () => PreFunc().ThenInclude(expression); + Func> func = () => PreFunc().ThenInclude(expression); result.PreFunc = func; result.Context = this.Context; return result; @@ -47,7 +47,7 @@ namespace SqlSugar public DeleteNavTask ThenInclude(Expression>> expression) where TChild : class, new() { DeleteNavTask result = new DeleteNavTask(); - Func> func = () => PreFunc().ThenInclude(expression); + Func> func = () => PreFunc().ThenInclude(expression); result.PreFunc = func; result.Context = this.Context; return result; @@ -62,16 +62,19 @@ namespace SqlSugar } public bool ExecuteCommand() { + PreFunc(); + var hasTran = this.Context.Ado.Transaction != null; if (hasTran) { - PreFunc(); + ExecTasks(); } else { this.Context.Ado.UseTran(() => { - PreFunc(); + ExecTasks(); + }, ex => throw ex); } return true; @@ -89,10 +92,20 @@ namespace SqlSugar private DeleteNavTask AsNav() { DeleteNavTask result = new DeleteNavTask(); - Func> func = () => PreFunc().AsNav(); + Func> func = () => PreFunc().AsNav(); result.PreFunc = func; result.Context = this.Context; return result; } + private void ExecTasks() + { + var tasks=(List)this.Context.TempItems["_DeleteNavTask"]; + tasks.Reverse(); + foreach (var task in tasks) + { + task(); + } + this.Context.TempItems.Remove("_DeleteNavTask"); + } } } diff --git a/Src/Asp.Net/SqlSugar/Abstract/SugarProvider/SqlSugarProvider.cs b/Src/Asp.Net/SqlSugar/Abstract/SugarProvider/SqlSugarProvider.cs index bfc15ecba..bcec7d82e 100644 --- a/Src/Asp.Net/SqlSugar/Abstract/SugarProvider/SqlSugarProvider.cs +++ b/Src/Asp.Net/SqlSugar/Abstract/SugarProvider/SqlSugarProvider.cs @@ -910,8 +910,9 @@ namespace SqlSugar public DeleteNavTaskInit DeleteNav(List datas) where T : class, new() { var result = new DeleteNavTaskInit(); - result.Roots = datas; - result.Context = this; + result.deleteNavProvider = new DeleteNavProvider(); + result.deleteNavProvider._Roots = datas; + result.deleteNavProvider._Context = this; return result; } public DeleteNavTaskInit DeleteNav(Expression> whereExpression) where T : class, new()