diff --git a/Src/Asp.NetCore2/SqlSugar/Abstract/SaveableProvider/Storageable.cs b/Src/Asp.NetCore2/SqlSugar/Abstract/SaveableProvider/Storageable.cs index 7f6aeec69..12ce79e6d 100644 --- a/Src/Asp.NetCore2/SqlSugar/Abstract/SaveableProvider/Storageable.cs +++ b/Src/Asp.NetCore2/SqlSugar/Abstract/SaveableProvider/Storageable.cs @@ -88,7 +88,15 @@ namespace SqlSugar whereFuncs.Add(new KeyValuePair, bool>, string>(StorageType.Other, conditions, message)); return this; } - + public StorageableSplitProvider SplitTable() + { + StorageableSplitProvider result = new StorageableSplitProvider(); + result.Context = this.Context; + result.SaveInfo = this; + result.List = allDatas.Select(it=>it.Item).ToList(); + result.EntityInfo = this.Context.EntityMaintenance.GetEntityInfoWithAttr(typeof(T)); + return result; + } public IStorageable DefaultAddElseUpdate() { var column = this.Context.EntityMaintenance.GetEntityInfo().Columns.FirstOrDefault(it=>it.IsPrimarykey); diff --git a/Src/Asp.NetCore2/SqlSugar/Abstract/SaveableProvider/StorageableSplitProvider.cs b/Src/Asp.NetCore2/SqlSugar/Abstract/SaveableProvider/StorageableSplitProvider.cs new file mode 100644 index 000000000..f51dea464 --- /dev/null +++ b/Src/Asp.NetCore2/SqlSugar/Abstract/SaveableProvider/StorageableSplitProvider.cs @@ -0,0 +1,121 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Reflection; +using System.Text; +using System.Threading.Tasks; + +namespace SqlSugar +{ + public class StorageableSplitProvider where T:class,new() + { + public Storageable SaveInfo { get; internal set; } + public SqlSugarProvider Context { get; internal set; } + public List List { get; internal set; } + public EntityInfo EntityInfo { get; internal set; } + public int PageSize = 1000; + public int ExecuteCommand() + { + if (List.Count > PageSize) + { + var result = 0; + this.Context.Utilities.PageEach(List, PageSize, pageItem => + { + result+= _ExecuteCommand(pageItem); + }); + return result; + } + else + { + var list = List; + return _ExecuteCommand(list); + } + + } + + + public async Task ExecuteCommandAsync() + { + if (List.Count > PageSize) + { + var result = 0; + this.Context.Utilities.PageEach(List, PageSize, async pageItem => + { + result +=await _ExecuteCommandAsync(pageItem); + }); + return result; + } + else + { + var list = List; + return await _ExecuteCommandAsync(list); + } + } + private async Task _ExecuteCommandAsync(List list) + { + int resultValue = 0; + List groupModels; + int result; + GroupDataList(list, out groupModels, out result); + foreach (var item in groupModels.GroupBy(it => it.GroupName)) + { + var addList = item.Select(it => it.Item).ToList(); + resultValue +=await this.Context.Storageable(addList).ExecuteCommandAsync(); + } + return result; + } + private int _ExecuteCommand(List list) + { + int resultValue = 0; + List groupModels; + int result; + GroupDataList(list, out groupModels, out result); + foreach (var item in groupModels.GroupBy(it => it.GroupName)) + { + var addList = item.Select(it => it.Item).ToList(); + resultValue += this.Context.Storageable(addList).As(item.Key).ExecuteCommand(); + } + return result; + } + + private void GroupDataList(List datas, out List groupModels, out int result) + { + var attribute = typeof(T).GetCustomAttribute() as SplitTableAttribute; + Check.Exception(attribute == null, $"{typeof(T).Name} need SplitTableAttribute"); + groupModels = new List(); + var db = this.Context; + foreach (var item in datas) + { + var value = db.SplitHelper().GetValue(attribute.SplitType, item); + var tableName = db.SplitHelper().GetTableName(attribute.SplitType, value); + groupModels.Add(new GroupModel() { GroupName = tableName, Item = item }); + } + var tablenames = groupModels.Select(it => it.GroupName).Distinct().ToList(); + CreateTable(tablenames); + result = 0; + } + private void CreateTable(List tableNames) + { + var isLog = this.Context.Ado.IsEnableLogEvent; + this.Context.Ado.IsEnableLogEvent = false; + foreach (var item in tableNames) + { + if (!this.Context.DbMaintenance.IsAnyTable(item, false)) + { + if (item != null) + { + this.Context.MappingTables.Add(EntityInfo.EntityName, item); + this.Context.CodeFirst.InitTables(); + } + } + } + this.Context.Ado.IsEnableLogEvent = isLog; + this.Context.MappingTables.Add(EntityInfo.EntityName, EntityInfo.DbTableName); + } + internal class GroupModel + { + public string GroupName { get; set; } + public T Item { get; set; } + } + } +} diff --git a/Src/Asp.NetCore2/SqlSugar/Interface/IStorageable.cs b/Src/Asp.NetCore2/SqlSugar/Interface/IStorageable.cs index 8bed2d866..33769eec0 100644 --- a/Src/Asp.NetCore2/SqlSugar/Interface/IStorageable.cs +++ b/Src/Asp.NetCore2/SqlSugar/Interface/IStorageable.cs @@ -31,6 +31,7 @@ namespace SqlSugar int ExecuteSqlBulkCopy(); Task ExecuteSqlBulkCopyAsync(); IStorageable DefaultAddElseUpdate(); + StorageableSplitProvider SplitTable(); } public class StorageableInfo where T : class, new()