diff --git a/Src/Asp.Net/SqlSugar/Abstract/SaveableProvider/Storageable.cs b/Src/Asp.Net/SqlSugar/Abstract/SaveableProvider/Storageable.cs index 5cc3a0f05..c1ee038ee 100644 --- a/Src/Asp.Net/SqlSugar/Abstract/SaveableProvider/Storageable.cs +++ b/Src/Asp.Net/SqlSugar/Abstract/SaveableProvider/Storageable.cs @@ -68,6 +68,22 @@ namespace SqlSugar return this; } + public int ExecuteUpdateOrInsert() + { + var result = 0; + var x = this.ToStorage(); + result+=x.AsInsertable.ExecuteCommand(); + result += x.AsUpdateable.ExecuteCommand(); + return result; + } + public async Task ExecuteUpdateOrInsertAsync() + { + var result = 0; + var x = await Task.Run(() => this.ToStorage()); + result +=await x.AsInsertable.ExecuteCommandAsync(); + result +=await x.AsUpdateable.ExecuteCommandAsync(); + return result; + } public StorageableResult ToStorage() { if (whereFuncs == null || whereFuncs.Count == 0) @@ -148,6 +164,89 @@ namespace SqlSugar return result; } + + public async Task> ToStorageAsync() + { + if (whereFuncs == null || whereFuncs.Count == 0) + { + return await this.Saveable().ToStorageAsync(); + } + if (this.allDatas.Count == 0) + return new StorageableResult() + { + AsDeleteable = this.Context.Deleteable().AS(asname).Where(it => false), + AsInsertable = this.Context.Insertable(new List()).AS(asname), + AsUpdateable = this.Context.Updateable(new List()).AS(asname), + InsertList = new List>(), + UpdateList = new List>(), + DeleteList = new List>(), + ErrorList = new List>(), + IgnoreList = new List>(), + OtherList = new List>(), + TotalList = new List>() + }; + var pkInfos = this.Context.EntityMaintenance.GetEntityInfo().Columns.Where(it => it.IsPrimarykey); + if (whereExpression == null && !pkInfos.Any()) + { + Check.Exception(true, "Need primary key or WhereColumn"); + } + if (whereExpression == null && pkInfos.Any()) + { + await this.Context.Utilities.PageEachAsync(allDatas, 300,async item => { + var addItems =await this.Context.Queryable().AS(asname).WhereClassByPrimaryKey(item.Select(it => it.Item).ToList()).ToListAsync(); + dbDataList.AddRange(addItems); + }); + } + var pkProperties = GetPkProperties(pkInfos); + var messageList = allDatas.Select(it => new StorageableMessage() + { + Item = it.Item, + Database = dbDataList, + PkFields = pkProperties + }).ToList(); + foreach (var item in whereFuncs.OrderByDescending(it => (int)it.key)) + { + List> whereList = messageList.Where(it => it.StorageType == null).ToList(); + Func, bool> exp = item.value1; + var list = whereList.Where(exp).ToList(); + foreach (var it in list) + { + it.StorageType = item.key; + it.StorageMessage = item.value2; + } + } + var delete = messageList.Where(it => it.StorageType == StorageType.Delete).ToList(); + var update = messageList.Where(it => it.StorageType == StorageType.Update).ToList(); + var inset = messageList.Where(it => it.StorageType == StorageType.Insert).ToList(); + var error = messageList.Where(it => it.StorageType == StorageType.Error).ToList(); + var ignore = messageList.Where(it => it.StorageType == StorageType.Ignore || it.StorageType == null).ToList(); + var other = messageList.Where(it => it.StorageType == StorageType.Other).ToList(); + StorageableResult result = new StorageableResult() + { + _WhereColumnList = wherecolumnList, + _AsName = asname, + _Context = this.Context, + AsDeleteable = this.Context.Deleteable().AS(asname), + AsUpdateable = this.Context.Updateable(update.Select(it => it.Item).ToList()).AS(asname), + AsInsertable = this.Context.Insertable(inset.Select(it => it.Item).ToList()).AS(asname), + OtherList = other, + InsertList = inset, + DeleteList = delete, + UpdateList = update, + ErrorList = error, + IgnoreList = ignore, + TotalList = messageList + }; + if (this.whereExpression != null) + { + result.AsUpdateable.WhereColumns(whereExpression); + result.AsDeleteable.WhereColumns(whereExpression); + } + result.AsDeleteable.Where(delete.Select(it => it.Item).ToList()); + return result; + } + + private string[] GetPkProperties(IEnumerable pkInfos) { if (whereExpression == null) diff --git a/Src/Asp.Net/SqlSugar/Interface/IStorageable.cs b/Src/Asp.Net/SqlSugar/Interface/IStorageable.cs index beb67ce28..7fc0be13c 100644 --- a/Src/Asp.Net/SqlSugar/Interface/IStorageable.cs +++ b/Src/Asp.Net/SqlSugar/Interface/IStorageable.cs @@ -17,7 +17,10 @@ namespace SqlSugar IStorageable SplitDelete(Func, bool> conditions, string message = null); IStorageable SplitOther(Func, bool> conditions, string message = null); StorageableResult ToStorage(); + Task> ToStorageAsync(); IStorageable As(string tableName); + int ExecuteUpdateOrInsert(); + Task ExecuteUpdateOrInsertAsync(); } public class StorageableInfo where T : class, new()