mirror of
https://gitee.com/dotnetchina/SqlSugar.git
synced 2025-12-17 17:41:28 +08:00
Add GridSave
This commit is contained in:
53
Src/Asp.Net/SqlSugar/Abstract/GridSave/GridSaveProvider.cs
Normal file
53
Src/Asp.Net/SqlSugar/Abstract/GridSave/GridSaveProvider.cs
Normal 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;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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);
|
||||
|
||||
@@ -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" />
|
||||
|
||||
@@ -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