Update db.GridSave

This commit is contained in:
sunkaixuan 2023-09-28 19:53:55 +08:00
parent 17b4d456d4
commit 469e53b5c5
2 changed files with 58 additions and 3 deletions

View File

@ -59,7 +59,21 @@ namespace SqlSugar
result.NavContext = UpdateNavProvider.NavContext;
return result;
}
public UpdateNavMethodInfo IncludesAllFirstLayer(params string[] ignoreColumns)
{
if (ignoreColumns == null)
{
ignoreColumns = new string[] { };
}
var navColumns = this.Context.EntityMaintenance.GetEntityInfo<Root>().Columns.Where(it=> !ignoreColumns.Contains(it.PropertyName) || !ignoreColumns.Any(z=>z.EqualCase(it.DbColumnName))).Where(it => it.Navigat != null).ToList();
var updateNavs = this;
UpdateNavMethodInfo methodInfo = updateNavs.IncludeByNameString(navColumns[0].PropertyName);
foreach (var item in navColumns.Skip(1))
{
methodInfo = methodInfo.IncludeByNameString(item.PropertyName);
}
return methodInfo;
}
public UpdateNavMethodInfo IncludeByNameString(string navMemberName, UpdateNavOptions updateNavOptions=null)
{
UpdateNavMethodInfo result = new UpdateNavMethodInfo();

View File

@ -11,18 +11,45 @@ namespace SqlSugar
internal SqlSugarProvider Context { get; set; }
internal List<T> OldList { get; set; }
internal List<T> SaveList { get; set; }
internal bool IsIncluesFirstAll { get; set; }
internal string[] IgnoreColumnsSaveInclues { get; set; }
public bool ExecuteCommand()
{
var deleteList = GetDeleteList();
this.Context.Deleteable(deleteList).PageSize(1000).ExecuteCommand();
this.Context.Storageable(SaveList).PageSize(1000).ExecuteCommand();
if (IsIncludesSave())
{
this.Context.Utilities.PageEach(SaveList, 1000, pageList =>
{
var options = new UpdateNavRootOptions() { IsInsertRoot = true };
this.Context.UpdateNav(pageList, options)
.IncludesAllFirstLayer(IgnoreColumnsSaveInclues).ExecuteCommand();
});
}
else
{
this.Context.Storageable(SaveList).PageSize(1000).ExecuteCommand();
}
return true;
}
public async Task<bool> ExecuteCommandAsync()
{
var deleteList= GetDeleteList();
await this.Context.Deleteable(deleteList).PageSize(1000).ExecuteCommandAsync();
await this.Context.Storageable(SaveList).PageSize(1000).ExecuteCommandAsync();
if (IsIncludesSave())
{
await this.Context.Utilities.PageEachAsync(SaveList, 1000, async pageList =>
{
var options = new UpdateNavRootOptions() { IsInsertRoot = true };
await this.Context.UpdateNav(pageList, options)
.IncludesAllFirstLayer(IgnoreColumnsSaveInclues).ExecuteCommandAsync();
});
}
else
{
await this.Context.Storageable(SaveList).PageSize(1000).ExecuteCommandAsync();
}
return true;
}
@ -38,6 +65,20 @@ namespace SqlSugar
return deleteList;
}
public GridSaveProvider<T> IncludesAllFirstLayer(params string [] ignoreColumns)
{
this.IsIncluesFirstAll = true;
IgnoreColumnsSaveInclues = ignoreColumns;
return this;
}
private bool IsIncludesSave()
{
return IsIncluesFirstAll && this.Context.EntityMaintenance.GetEntityInfo<T>().Columns.Any(it=>it.Navigat!=null);
}
private string CreateCompositeKey(string[] propertyNames, object obj)
{
var keyValues = propertyNames.Select(propertyName => GetPropertyValue(obj, propertyName)?.ToString() ?? "");