Add GridSave

This commit is contained in:
sunkaixuan
2023-09-28 19:25:00 +08:00
parent 171950f439
commit 17b4d456d4
7 changed files with 75 additions and 0 deletions

View File

@@ -0,0 +1,53 @@
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; }
public bool ExecuteCommand()
{
var deleteList = GetDeleteList();
this.Context.Deleteable(deleteList).PageSize(1000).ExecuteCommand();
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();
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;
}
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;
}
}
}

View File

@@ -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());

View File

@@ -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
}
}

View File

@@ -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);

View File

@@ -122,6 +122,7 @@
<Compile Include="Abstract\FastestProvider\Setting.cs" />
<Compile Include="Abstract\FastestProvider\SplitFastest.cs" />
<Compile Include="Abstract\FilterProvider\FilterProvider.cs" />
<Compile Include="Abstract\GridSave\GridSaveProvider.cs" />
<Compile Include="Abstract\InsertableProvider\InsertablePage.cs" />
<Compile Include="Abstract\InsertableProvider\InsertableHelper.cs" />
<Compile Include="Abstract\InsertableProvider\InsertMethodInfo.cs" />

View File

@@ -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);

View File

@@ -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);
}
}
}