mirror of
https://gitee.com/dotnetchina/SqlSugar.git
synced 2025-09-19 01:58:13 +08:00
Synchronization code
This commit is contained in:
@@ -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();
|
||||
|
@@ -0,0 +1,94 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace SqlSugar
|
||||
{
|
||||
public class GridSaveProvider<T> where T : class, new()
|
||||
{
|
||||
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();
|
||||
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();
|
||||
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;
|
||||
}
|
||||
|
||||
public List<T> GetDeleteList()
|
||||
{
|
||||
string[] primaryKeys = this.Context.EntityMaintenance.GetEntityInfo<T>().Columns.Where(it => it.IsPrimarykey).Select(it => it.PropertyName).ToArray();
|
||||
var saveListDictionary = this.SaveList.ToDictionary(item => CreateCompositeKey(primaryKeys, item));
|
||||
var deleteList = this.OldList.Where(oldItem =>
|
||||
{
|
||||
var compositeKey = CreateCompositeKey(primaryKeys, oldItem);
|
||||
return !saveListDictionary.ContainsKey(compositeKey);
|
||||
}).ToList();
|
||||
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() ?? "");
|
||||
return string.Join("|", keyValues);
|
||||
}
|
||||
|
||||
private object GetPropertyValue(object obj, string propertyName)
|
||||
{
|
||||
var property = obj.GetType().GetProperty(propertyName);
|
||||
return property != null ? property.GetValue(obj) : null;
|
||||
}
|
||||
}
|
||||
}
|
@@ -1006,6 +1006,14 @@ namespace SqlSugar
|
||||
#endregion
|
||||
|
||||
#region Saveable
|
||||
public GridSaveProvider<T> GridSave<T>(List<T> oldList, List<T> saveList) where T : class, new()
|
||||
{
|
||||
GridSaveProvider<T> result = new GridSaveProvider<T>();
|
||||
result.Context = this;
|
||||
result.OldList = oldList;
|
||||
result.SaveList = saveList;
|
||||
return result;
|
||||
}
|
||||
public IStorageable<T> Storageable<T>(T[] dataList) where T : class, new()
|
||||
{
|
||||
return Storageable(dataList?.ToList());
|
||||
|
@@ -827,6 +827,10 @@ namespace SqlSugar
|
||||
{
|
||||
return ScopedContext.QueryableByObject(entityType, shortName);
|
||||
}
|
||||
public GridSaveProvider<T> GridSave<T>(List<T> oldList, List<T> saveList) where T : class, new()
|
||||
{
|
||||
return ScopedContext.GridSave(oldList, saveList);
|
||||
}
|
||||
#endregion
|
||||
}
|
||||
}
|
@@ -146,6 +146,7 @@ namespace SqlSugar
|
||||
#endregion
|
||||
|
||||
#region Saveable
|
||||
GridSaveProvider<T> GridSave<T>(List<T> oldList,List<T> saveList) where T : class, new();
|
||||
IStorageable<T> Storageable<T>(T[] dataList) where T : class, new();
|
||||
StorageableDataTable Storageable(List<Dictionary<string, object>> dictionaryList, string tableName);
|
||||
StorageableDataTable Storageable(Dictionary<string, object> dictionary, string tableName);
|
||||
|
@@ -535,6 +535,10 @@ namespace SqlSugar
|
||||
#endregion
|
||||
|
||||
#region Saveable
|
||||
public GridSaveProvider<T> GridSave<T>(List<T> oldList, List<T> saveList) where T : class, new()
|
||||
{
|
||||
return this.Context.GridSave(oldList, saveList);
|
||||
}
|
||||
public StorageableDataTable Storageable(DataTable data)
|
||||
{
|
||||
return this.Context.Storageable(data);
|
||||
|
@@ -894,5 +894,9 @@ namespace SqlSugar
|
||||
{
|
||||
return ScopedContext.QueryableByObject(entityType, shortName);
|
||||
}
|
||||
public GridSaveProvider<T> GridSave<T>(List<T> oldList, List<T> saveList) where T : class, new()
|
||||
{
|
||||
return ScopedContext.GridSave(oldList, saveList);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user