Synchronization code

This commit is contained in:
sunkaixuan
2023-06-24 23:22:33 +08:00
parent f1b37f86ac
commit 9fb4e16863
3 changed files with 120 additions and 11 deletions

View File

@@ -1,4 +1,5 @@
using System; using Newtonsoft.Json;
using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Linq.Expressions; using System.Linq.Expressions;
@@ -9,7 +10,8 @@ namespace SqlSugar
public partial 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()
{ {
private void InsertManyToMany<TChild>(string name, EntityColumnInfo nav) where TChild : class, new() private void InsertManyToMany<TChild>(string name, EntityColumnInfo nav) where TChild : class, new()
{; {
;
var parentEntity = _ParentEntity; var parentEntity = _ParentEntity;
var parentList = _ParentList; var parentList = _ParentList;
var parentPkColumn = parentEntity.Columns.FirstOrDefault(it => it.IsPrimarykey == true); var parentPkColumn = parentEntity.Columns.FirstOrDefault(it => it.IsPrimarykey == true);
@@ -88,10 +90,62 @@ namespace SqlSugar
{ {
this._Context.Deleteable<object>().AS(mappingEntity.DbTableName).In(mappingA.DbColumnName, ids).ExecuteCommand(); this._Context.Deleteable<object>().AS(mappingEntity.DbTableName).In(mappingA.DbColumnName, ids).ExecuteCommand();
} }
if (HasMappingTemplate(mappingEntity))
{
InertMappingWithTemplate(mappingEntity, mappingA, mappingB, mappgingTables);
}
else
{
this._Context.Insertable(mappgingTables).AS(mappingEntity.DbTableName).ExecuteCommand(); this._Context.Insertable(mappgingTables).AS(mappingEntity.DbTableName).ExecuteCommand();
}
SetNewParent<TChild>(thisEntity, thisPkColumn); SetNewParent<TChild>(thisEntity, thisPkColumn);
} }
private void InertMappingWithTemplate(EntityInfo mappingEntity, EntityColumnInfo mappingA, EntityColumnInfo mappingB, List<Dictionary<string, object>> mappgingTables)
{
var template = this._navOptions?.ManyToManySaveMappingTemplate;
List<object> mappingObjects = new List<object>();
foreach (var item in mappgingTables)
{
// 序列化模板对象
var serializedTemplate = JsonConvert.SerializeObject(template);
// 反序列化模板对象,创建新的映射对象
var mappingObject = JsonConvert.DeserializeObject(serializedTemplate, template.GetType());
// 获取映射对象的所有字段
var fields = mappingEntity.Columns;
// 遍历字典中的键值对,并将值赋给映射对象的对应字段
foreach (var kvp in item)
{
var fieldName = kvp.Key;
var fieldValue = kvp.Value;
// 查找与字段名匹配的字段
var field = fields.FirstOrDefault(f => f.DbColumnName.EqualCase(fieldName));
// 如果字段存在且值的类型与字段类型匹配,则赋值给字段
if (field != null)
{
var isSetValue = field.IsPrimarykey
|| field.DbColumnName == mappingA.DbColumnName
|| field.DbColumnName == mappingB.DbColumnName;
if (isSetValue)
field.PropertyInfo.SetValue(mappingObject, fieldValue);
}
}
// 将映射对象添加到列表中
mappingObjects.Add(mappingObject);
}
this._Context.InsertableByObject(mappingObjects).ExecuteCommand();
}
private bool HasMappingTemplate(EntityInfo mappingEntity)
{
return this._navOptions?.ManyToManySaveMappingTemplate?.GetType() == mappingEntity.Type;
}
private void SetMappingTableDefaultValue(EntityColumnInfo mappingPk, Dictionary<string, object> keyValuePairs) private void SetMappingTableDefaultValue(EntityColumnInfo mappingPk, Dictionary<string, object> keyValuePairs)
{ {
if (mappingPk.UnderType == UtilConstants.LongType) if (mappingPk.UnderType == UtilConstants.LongType)

View File

@@ -1,4 +1,5 @@
using System; using Newtonsoft.Json;
using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Linq.Expressions; using System.Linq.Expressions;
@@ -89,10 +90,62 @@ namespace SqlSugar
} }
} }
this._Context.Deleteable<object>().AS(mappingEntity.DbTableName).In(mappingA.DbColumnName, ids).ExecuteCommand(); this._Context.Deleteable<object>().AS(mappingEntity.DbTableName).In(mappingA.DbColumnName, ids).ExecuteCommand();
if (HasMappingTemplate(mappingEntity))
{
InertMappingWithTemplate(mappingEntity, mappingA, mappingB, mappgingTables);
}
else
{
this._Context.Insertable(mappgingTables).AS(mappingEntity.DbTableName).ExecuteCommand(); this._Context.Insertable(mappgingTables).AS(mappingEntity.DbTableName).ExecuteCommand();
}
_ParentEntity = thisEntity; _ParentEntity = thisEntity;
} }
private bool HasMappingTemplate(EntityInfo mappingEntity)
{
return this._Options?.ManyToManySaveMappingTemplate?.GetType() == mappingEntity.Type;
}
private void InertMappingWithTemplate(EntityInfo mappingEntity, EntityColumnInfo mappingA, EntityColumnInfo mappingB, List<Dictionary<string, object>> mappgingTables)
{
var template = this._Options?.ManyToManySaveMappingTemplate;
List<object> mappingObjects = new List<object>();
foreach (var item in mappgingTables)
{
// 序列化模板对象
var serializedTemplate = JsonConvert.SerializeObject(template);
// 反序列化模板对象,创建新的映射对象
var mappingObject = JsonConvert.DeserializeObject(serializedTemplate, template.GetType());
// 获取映射对象的所有字段
var fields = mappingEntity.Columns;
// 遍历字典中的键值对,并将值赋给映射对象的对应字段
foreach (var kvp in item)
{
var fieldName = kvp.Key;
var fieldValue = kvp.Value;
// 查找与字段名匹配的字段
var field = fields.FirstOrDefault(f => f.DbColumnName.EqualCase(fieldName));
// 如果字段存在且值的类型与字段类型匹配,则赋值给字段
if (field != null)
{
var isSetValue = field.IsPrimarykey
|| field.DbColumnName == mappingA.DbColumnName
|| field.DbColumnName == mappingB.DbColumnName;
if (isSetValue)
field.PropertyInfo.SetValue(mappingObject, fieldValue);
}
}
// 将映射对象添加到列表中
mappingObjects.Add(mappingObject);
}
this._Context.InsertableByObject(mappingObjects).ExecuteCommand();
}
private void SetMappingTableDefaultValue(EntityColumnInfo mappingPk, Dictionary<string, object> keyValuePairs) private void SetMappingTableDefaultValue(EntityColumnInfo mappingPk, Dictionary<string, object> keyValuePairs)
{ {
if (mappingPk.UnderType == UtilConstants.LongType) if (mappingPk.UnderType == UtilConstants.LongType)

View File

@@ -43,6 +43,7 @@ namespace SqlSugar
{ {
public bool ManyToManyIsUpdateA { get; set; } public bool ManyToManyIsUpdateA { get; set; }
public bool ManyToManyIsUpdateB { get; set; } public bool ManyToManyIsUpdateB { get; set; }
public object ManyToManySaveMappingTemplate { get; set; }
public bool OneToManyDeleteAll { get; set; } public bool OneToManyDeleteAll { get; set; }
public bool OneToManyEnableLogicDelete { get; set; } public bool OneToManyEnableLogicDelete { get; set; }
public Expression RootFunc { get; set; } public Expression RootFunc { get; set; }
@@ -53,5 +54,6 @@ namespace SqlSugar
{ {
public bool OneToManyIfExistsNoInsert { get; set; } public bool OneToManyIfExistsNoInsert { get; set; }
public bool ManyToManyNoDeleteMap { get; set; } public bool ManyToManyNoDeleteMap { get; set; }
public object ManyToManySaveMappingTemplate { get; set; }
} }
} }