Synchronization code

This commit is contained in:
sunkaixuan 2023-10-30 02:48:18 +08:00
parent 4766626806
commit ce31dd2289
2 changed files with 131 additions and 5 deletions

View File

@ -10,6 +10,17 @@ namespace SqlSugar
{
public NavigateType? _NavigateType { get; set; }
private void UpdateOneToMany<TChild>(string name, EntityColumnInfo nav) where TChild : class, new()
{
if (_Options?.OneToManyInsertOrUpdate == true)
{
InsertOrUpdate<TChild>(name,nav);
}
else
{
DeleteInsert<TChild>(name, nav);
}
}
private void InsertOrUpdate<TChild>(string name, EntityColumnInfo nav) where TChild : class, new()
{
List<TChild> children = new List<TChild>();
var parentEntity = _ParentEntity;
@ -42,7 +53,92 @@ namespace SqlSugar
children.AddRange(childs);
}
ids.Add(parentValue);
if (_Options?.OneToManyNoDeleteNull == true && childs == null)
if (_Options?.OneToManyNoDeleteNull == true && childs == null)
{
ids.Remove(parentValue);
}
}
if (NotAny(name))
{
DeleteMany(thisEntity, ids, thisFkColumn.DbColumnName);
if (this._Options?.OneToManyEnableLogicDelete == true)
{
var locgicColumn = thisEntity.Columns.FirstOrDefault(it => it.PropertyName.EqualCase("IsDeleted") || it.PropertyName.EqualCase("IsDelete"));
Check.ExceptionEasy(
locgicColumn == null,
thisEntity.EntityName + "Logical deletion requires the entity to have the IsDeleted property",
thisEntity.EntityName + "假删除需要实体有IsDeleted属性");
List<IConditionalModel> conditionalModels = new List<IConditionalModel>();
conditionalModels.Add(new ConditionalModel()
{
FieldName = thisFkColumn.DbColumnName,
FieldValue = string.Join(",", ids.Distinct()),
ConditionalType = ConditionalType.In,
CSharpTypeName = thisFkColumn?.PropertyInfo?.PropertyType?.Name
});
var sqlObj = _Context.Queryable<object>().SqlBuilder.ConditionalModelToSql(conditionalModels);
this._Context.Updateable<object>()
.AS(thisEntity.DbTableName)
.Where(sqlObj.Key, sqlObj.Value)
.SetColumns(locgicColumn.DbColumnName, true)
.ExecuteCommand();
}
else
{
var list=this._Context.Queryable<TChild>()
.AS(thisEntity.DbTableName)
.In(thisFkColumn.DbColumnName, ids.Distinct().ToList())
.ToList();
List<TChild> result = GetNoExistsId(list, children, thisPkColumn.PropertyName);
if (result.Any())
{
this._Context.Deleteable(result).ExecuteCommand();
}
}
_NavigateType = NavigateType.OneToMany;
InsertDatas(children, thisPkColumn);
}
else
{
this._ParentList = children.Cast<object>().ToList();
}
_NavigateType = null;
SetNewParent<TChild>(thisEntity, thisPkColumn);
}
private void DeleteInsert<TChild>(string name, EntityColumnInfo nav) where TChild : class, new()
{
List<TChild> children = new List<TChild>();
var parentEntity = _ParentEntity;
var parentList = _ParentList;
var parentNavigateProperty = parentEntity.Columns.FirstOrDefault(it => it.PropertyName == name);
var thisEntity = this._Context.EntityMaintenance.GetEntityInfo<TChild>();
var thisPkColumn = GetPkColumnByNav2(thisEntity, nav);
var thisFkColumn = GetFKColumnByNav(thisEntity, nav);
EntityColumnInfo parentPkColumn = GetParentPkColumn();
EntityColumnInfo parentNavColumn = GetParentPkNavColumn(nav);
if (parentNavColumn != null)
{
parentPkColumn = parentNavColumn;
}
if (ParentIsPk(parentNavigateProperty))
{
parentPkColumn = this._ParentEntity.Columns.FirstOrDefault(it => it.IsPrimarykey);
}
var ids = new List<object>();
foreach (var item in parentList)
{
var parentValue = parentPkColumn.PropertyInfo.GetValue(item);
var childs = parentNavigateProperty.PropertyInfo.GetValue(item) as List<TChild>;
if (childs != null)
{
foreach (var child in childs)
{
thisFkColumn.PropertyInfo.SetValue(child, parentValue, null);
}
children.AddRange(childs);
}
ids.Add(parentValue);
if (_Options?.OneToManyNoDeleteNull == true && childs == null)
{
ids.Remove(parentValue);
}
@ -54,9 +150,9 @@ namespace SqlSugar
{
var locgicColumn = thisEntity.Columns.FirstOrDefault(it => it.PropertyName.EqualCase("IsDeleted") || it.PropertyName.EqualCase("IsDelete"));
Check.ExceptionEasy(
locgicColumn==null,
thisEntity.EntityName + "Logical deletion requires the entity to have the IsDeleted property",
thisEntity.EntityName+"假删除需要实体有IsDeleted属性");
locgicColumn == null,
thisEntity.EntityName + "Logical deletion requires the entity to have the IsDeleted property",
thisEntity.EntityName + "假删除需要实体有IsDeleted属性");
List<IConditionalModel> conditionalModels = new List<IConditionalModel>();
conditionalModels.Add(new ConditionalModel()
{
@ -81,13 +177,14 @@ namespace SqlSugar
_NavigateType = NavigateType.OneToMany;
InsertDatas(children, thisPkColumn);
}
else
else
{
this._ParentList = children.Cast<object>().ToList();
}
_NavigateType = null;
SetNewParent<TChild>(thisEntity, thisPkColumn);
}
private static bool ParentIsPk(EntityColumnInfo parentNavigateProperty)
{
return parentNavigateProperty != null &&
@ -191,5 +288,33 @@ namespace SqlSugar
this._ParentEntity = entityInfo;
this._ParentPkColumn = entityColumnInfo;
}
public List<TChild> GetNoExistsId<TChild>(List<TChild> old, List<TChild> newList, string pkName)
{
List<TChild> result = new List<TChild>();
// 将newList中的主键属性转换为字符串集合
var newIds = newList.Select(item => GetPropertyValueAsString(item, pkName)).ToList();
// 获取在old中但不在newList中的主键属性值
result = old.Where(item => !newIds.Contains(GetPropertyValueAsString(item, pkName)))
.ToList();
return result;
}
// 获取对象的属性值
private string GetPropertyValueAsString<TChild>(TChild item, string propertyName)
{
var property = item.GetType().GetProperty(propertyName);
if (property != null)
{
return property.GetValue(item, null)+"";
}
else
{
throw new ArgumentException($"Property '{propertyName}' not found on type {item.GetType().Name}");
}
}
}
}

View File

@ -51,6 +51,7 @@ namespace SqlSugar
public bool OneToManyDeleteAll { get; set; }
public bool OneToManyEnableLogicDelete { get; set; }
public bool OneToManyNoDeleteNull { get; set; }
public bool OneToManyInsertOrUpdate { get; set; }
public Expression RootFunc { get; set; }
public Expression CurrentFunc { get; set; }
}