From 5687f26b0673cbfa437a3867688b37873d2720a9 Mon Sep 17 00:00:00 2001 From: sunkaixuan <610262374@qq.com> Date: Thu, 30 Jun 2022 01:12:34 +0800 Subject: [PATCH] Update nav insert --- .../ExecuteNavProvider/InsertNavProvider.cs | 41 ++++++++++++++++--- Src/Asp.Net/SqlSugar/Utilities/UtilMethods.cs | 4 ++ 2 files changed, 40 insertions(+), 5 deletions(-) diff --git a/Src/Asp.Net/SqlSugar/Abstract/ExecuteNavProvider/InsertNavProvider.cs b/Src/Asp.Net/SqlSugar/Abstract/ExecuteNavProvider/InsertNavProvider.cs index efeb31fb1..838252f32 100644 --- a/Src/Asp.Net/SqlSugar/Abstract/ExecuteNavProvider/InsertNavProvider.cs +++ b/Src/Asp.Net/SqlSugar/Abstract/ExecuteNavProvider/InsertNavProvider.cs @@ -11,7 +11,7 @@ namespace SqlSugar { public List Roots { get; set; } - public object ParentList { get; set; } + public List ParentList { get; set; } public EntityInfo ParentEntity { get; set; } public SqlSugarProvider Context { get; set; } @@ -19,7 +19,7 @@ namespace SqlSugar { if (ParentList == null) { - ParentList = GetParentList(Roots); + ParentList = GetParentList(Roots).Cast().ToList(); } var name=ExpressionTool.GetMemberName(expression); var nav = this.ParentEntity.Columns.FirstOrDefault(x => x.PropertyName == name); @@ -29,7 +29,7 @@ namespace SqlSugar } if (nav.Navigat.NavigatType == NavigateType.OneToOne || nav.Navigat.NavigatType == NavigateType.ManyToOne) { - InsertOneToOne(); + InsertOneToOne(ParentList,this.ParentEntity, name,nav); } else if (nav.Navigat.NavigatType == NavigateType.OneToMany) { @@ -52,9 +52,40 @@ namespace SqlSugar } - private void InsertOneToOne() + private void InsertOneToOne(List parentList, EntityInfo parentEntity, string name, EntityColumnInfo nav) { - + var parentColumn = parentEntity.Columns.FirstOrDefault(it => it.PropertyName==nav.Navigat.Name); + this.ParentEntity = this.Context.EntityMaintenance.GetEntityInfo(); + var pkColumn = ParentEntity.Columns.FirstOrDefault(it=>it.IsPrimarykey==true); + if (nav.Navigat.Name2.HasValue()) + { + pkColumn = ParentEntity.Columns.FirstOrDefault(it => it.PropertyName==nav.Navigat.Name2); + } + Check.Exception(pkColumn == null, $" Navigate {parentEntity.EntityName} : {name} is error ", $"导航实体 {parentEntity.EntityName} 属性 {name} 配置错误"); + List childList = new List(); + foreach (var parent in parentList) + { + var childItems=(TChild)nav.PropertyInfo.GetValue(parent); + if (childItems != null) + { + var pkValue = pkColumn.PropertyInfo.GetValue(childItems); + var pvValue = parentColumn.PropertyInfo.GetValue(parent); + if (IsDefaultValue(pvValue)) + { + pvValue = pkValue; + } + if (IsDefaultValue(pvValue)) + { + + } + } + } + parentList = childList; + } + + private static bool IsDefaultValue(object pvValue) + { + return pvValue == null || pvValue == UtilMethods.GetDefaultValue(pvValue.GetType()); } private List GetParentList(List datas) where Type : class ,new() diff --git a/Src/Asp.Net/SqlSugar/Utilities/UtilMethods.cs b/Src/Asp.Net/SqlSugar/Utilities/UtilMethods.cs index 3ee26c61e..365f6fc22 100644 --- a/Src/Asp.Net/SqlSugar/Utilities/UtilMethods.cs +++ b/Src/Asp.Net/SqlSugar/Utilities/UtilMethods.cs @@ -216,6 +216,10 @@ namespace SqlSugar Type type = Nullable.GetUnderlyingType(oldType); return type == null ? oldType : type; } + public static object GetDefaultValue(Type type) + { + return type.IsValueType ? Activator.CreateInstance(type) : null; + } public static string ReplaceSqlParameter(string itemSql, SugarParameter itemParameter, string newName) { itemSql = Regex.Replace(itemSql, string.Format(@"{0} ", "\\" + itemParameter.ParameterName), newName + " ", RegexOptions.IgnoreCase);