The development of insert nav

This commit is contained in:
sunkaixuan
2022-07-01 02:16:52 +08:00
parent d2d0a10de9
commit 346a2595c3
7 changed files with 164 additions and 74 deletions

View File

@@ -7,112 +7,97 @@ using System.Threading.Tasks;
namespace SqlSugar
{
public class InsertNavProvider<Root,T> where T : class,new() where Root:class,new()
public partial class InsertNavProvider<Root,T> where T : class,new() where Root:class,new()
{
public List<Root> Roots { get; set; }
public List<object> ParentList { get; set; }
public EntityInfo ParentEntity { get; set; }
public SqlSugarProvider Context { get; set; }
public List<Root> _Roots { get; set; }
public List<object> _ParentList { get; set; }
public EntityInfo _ParentEntity { get; set; }
public SqlSugarProvider _Context { get; set; }
public InsertNavProvider<Root,TChild> ThenInclude<TChild>(Expression<Func<T,TChild>> expression) where TChild : class, new()
public InsertNavProvider<Root, Root> AsNav()
{
if (ParentList == null)
return new InsertNavProvider<Root, Root> {
_Context = _Context,
_ParentEntity = null,
_ParentList=null,
_Roots= _Roots
};
}
public InsertNavProvider<Root, TChild> ThenInclude<TChild>(Expression<Func<T, TChild>> 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)
{
ParentList = GetParentList(Roots).Cast<object>().ToList();
}
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}没有导航属性");
Check.ExceptionEasy($"{name} no navigate attribute", $"{this._ParentEntity.EntityName}的属性{name}没有导航属性");
}
if (nav.Navigat.NavigatType == NavigateType.OneToOne || nav.Navigat.NavigatType == NavigateType.ManyToOne)
{
InsertOneToOne<TChild>(ParentList,this.ParentEntity, name,nav);
InsertOneToOne<TChild>(name, nav);
}
else if (nav.Navigat.NavigatType == NavigateType.OneToMany)
{
InsertOneToMany();
InsertOneToMany<TChild>(name, nav);
}
else
else
{
InsertManyToMany();
InsertManyToMany<TChild>(name, nav);
}
return null;
return GetResult<TChild>();
}
private void InsertManyToMany()
{
}
private void InsertOneToMany()
{
}
private void InsertOneToOne<TChild>(List<object> parentList, EntityInfo parentEntity, string name, EntityColumnInfo nav)
{
var parentColumn = parentEntity.Columns.FirstOrDefault(it => it.PropertyName==nav.Navigat.Name);
this.ParentEntity = this.Context.EntityMaintenance.GetEntityInfo<TChild>();
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<object> childList = new List<object>();
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 void InitParentList()
{
if (_ParentList == null)
{
_ParentList = GetParentList(_Roots).Cast<object>().ToList();
}
}
private InsertNavProvider<Root, TChild> GetResult<TChild>() where TChild : class, new()
{
return new InsertNavProvider<Root, TChild>()
{
_Context = this._Context,
_ParentEntity = this._ParentEntity,
_ParentList = this._ParentList,
_Roots = this._Roots
};
}
private List<Type> GetParentList<Type>(List<Type> datas) where Type : class ,new()
{
List<Type> result = new List<Type>();
this.Context.InitMappingInfo<Type>();
var entity = this.Context.EntityMaintenance.GetEntityInfo<Type>();
this._Context.InitMappingInfo<Type>();
var entity = this._Context.EntityMaintenance.GetEntityInfo<Type>();
var isIdentity = entity.Columns.Where(it=>it.IsIdentity).Any();
if (isIdentity)
{
foreach (var item in datas)
{
this.Context.Insertable(datas).ExecuteCommandIdentityIntoEntity();
}
InsertIdentity(datas);
}
else
{
this.Context.Insertable(datas).ExecuteCommand();
this._Context.Insertable(datas).ExecuteCommand();
}
this.ParentEntity = entity;
this._ParentEntity = entity;
result = datas;
return result;
}
public InsertNavProvider<Root,Root> AsNav()
private void InsertIdentity<Type>(List<Type> datas) where Type : class, new()
{
return null;
foreach (var item in datas)
{
this._Context.Insertable(item).ExecuteCommandIdentityIntoEntity();
}
}
}
}

View File

@@ -0,0 +1,16 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;
namespace SqlSugar
{
public partial class InsertNavProvider<Root, T> where T : class, new() where Root : class, new()
{
private void InsertManyToMany<TChild>(string name, EntityColumnInfo nav) where TChild : class, new()
{
}
}
}

View File

@@ -0,0 +1,24 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;
namespace SqlSugar
{
public partial class InsertNavProvider<Root, T> where T : class, new() where Root : class, new()
{
private void InsertOneToMany<TChild>(string name, EntityColumnInfo nav) where TChild : class, new()
{
var parentEntity = _ParentEntity;
var parentList = _ParentList;
var parentColumn = parentEntity.Columns.FirstOrDefault(it => it.PropertyName == nav.Navigat.Name);
this._ParentEntity = this._Context.EntityMaintenance.GetEntityInfo<TChild>();
foreach (var item in parentList)
{
}
}
}
}

View File

@@ -0,0 +1,62 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;
namespace SqlSugar
{
public partial class InsertNavProvider<Root, T> where T : class, new() where Root : class, new()
{
private void InsertOneToOne<TChild>(string name, EntityColumnInfo nav) where TChild : class, new()
{
var parentEntity = _ParentEntity;
var parentList = _ParentList;
var parentColumn = parentEntity.Columns.FirstOrDefault(it => it.PropertyName == nav.Navigat.Name);
this._ParentEntity = this._Context.EntityMaintenance.GetEntityInfo<TChild>();
EntityColumnInfo pkColumn = GetOneTwoOneChildPkColumn(nav);
Check.Exception(pkColumn == null, $" Navigate {parentEntity.EntityName} : {name} is error ", $"导航实体 {parentEntity.EntityName} 属性 {name} 配置错误");
List<TChild> childList = new List<TChild>();
foreach (var parent in parentList)
{
var navPropertyValue = parentColumn.PropertyInfo.GetValue(parent);
var childItem = (TChild)nav.PropertyInfo.GetValue(parent);
if (childItem != null)
{
if (IsDefaultValue(navPropertyValue))
{
var pkValue = pkColumn.PropertyInfo.GetValue(childItem);
if (IsDefaultValue(navPropertyValue))
{
navPropertyValue = pkValue;
}
}
pkColumn.PropertyInfo.SetValue(childItem, navPropertyValue);
childList.Add(childItem);
}
}
var x = this._Context.Storageable(childList).WhereColumns(new string[] { pkColumn.PropertyName }).ToStorage();
if (pkColumn.IsIdentity)
{
InsertIdentity(x.InsertList.Select(it => it.Item).ToList());
}
else
{
x.AsInsertable.ExecuteCommand();
}
this._ParentList = childList.Cast<object>().ToList();
}
private EntityColumnInfo GetOneTwoOneChildPkColumn(EntityColumnInfo nav)
{
var pkColumn = _ParentEntity.Columns.FirstOrDefault(it => it.IsPrimarykey == true);
if (nav.Navigat.Name2.HasValue())
{
pkColumn = _ParentEntity.Columns.FirstOrDefault(it => it.PropertyName == nav.Navigat.Name2);
}
return pkColumn;
}
}
}

View File

@@ -296,7 +296,7 @@ namespace SqlSugar
public IStorageable<T> WhereColumns(string [] columns)
{
var list = columns.Select(it=>this.Context.EntityMaintenance.GetDbColumnName<T>(it)).ToList();
var list = columns.Select(it=>this.Context.EntityMaintenance.GetPropertyName<T>(it)).ToList();
var exp=ExpressionBuilderHelper.CreateNewFields<T>(this.Context.EntityMaintenance.GetEntityInfo<T>(), list);
return this.WhereColumns(exp);
}

View File

@@ -897,8 +897,8 @@ namespace SqlSugar
public InsertNavProvider<T, T> InsertNav<T>(List<T> datas) where T : class, new()
{
var result = new InsertNavProvider<T, T>();
result.Roots = datas;
result.Context = this;
result._Roots = datas;
result._Context = this;
return result;
}
public DeleteNavProvider<T, T> DeleteNav<T>(T data)

View File

@@ -89,6 +89,9 @@
<Compile Include="Abstract\DeleteProvider\SplitTableDeleteByObjectProvider.cs" />
<Compile Include="Abstract\DeleteProvider\SplitTableDeleteProvider.cs" />
<Compile Include="Abstract\EntityMaintenance\EntityMaintenance.cs" />
<Compile Include="Abstract\ExecuteNavProvider\InsertNavProviderOneToMany.cs" />
<Compile Include="Abstract\ExecuteNavProvider\InsertNavProviderManyToMany.cs" />
<Compile Include="Abstract\ExecuteNavProvider\InsertNavProviderOneToOne.cs" />
<Compile Include="Abstract\ExecuteNavProvider\DeleteNavProvider.cs" />
<Compile Include="Abstract\ExecuteNavProvider\UpdateInsert.cs" />
<Compile Include="Abstract\ExecuteNavProvider\InsertNavProvider.cs" />