using System; using System.Collections.Generic; using System.Linq; using System.Linq.Expressions; using System.Reflection; using System.Text; using System.Threading.Tasks; namespace SqlSugar { public class StorageableSplitProvider where T:class,new() { internal Storageable SaveInfo { get; set; } internal SqlSugarProvider Context { get; set; } internal List List { get; set; } internal EntityInfo EntityInfo { get; set; } internal Expression> whereExpression { get; set; } internal int pageSize = 1000; internal Action ActionCallBack =null; public StorageableSplitProvider PageSize(int size, Action ActionCallBack = null) { this.pageSize = size; return this; } 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 int ExecuteSqlBulkCopy() { if (List.Count > pageSize) { var result = 0; this.Context.Utilities.PageEach(List, pageSize, pageItem => { result += _ExecuteSqlBulkCopy(pageItem); }); return result; } else { var list = List; return _ExecuteSqlBulkCopy(list); } } public async Task ExecuteCommandAsync() { if (List.Count > pageSize) { var result = 0; await this.Context.Utilities.PageEachAsync(List, pageSize, async pageItem => { result +=await _ExecuteCommandAsync(pageItem); if (ActionCallBack != null) { ActionCallBack(result); } }); return result; } else { var list = List; return await _ExecuteCommandAsync(list); } } public async Task ExecuteSqlBulkCopyAsync() { if (List.Count > pageSize) { var result = 0; await this.Context.Utilities.PageEachAsync(List, pageSize, async pageItem => { result += await _ExecuteSqlBulkCopyAsync(pageItem); if (ActionCallBack != null) { ActionCallBack(result); } }); return result; } else { var list = List; return await _ExecuteSqlBulkCopyAsync(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).As(item.Key).WhereColumns(whereExpression).ExecuteCommandAsync(); if (ActionCallBack != null) { ActionCallBack(resultValue); } } return resultValue; } 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).WhereColumns(whereExpression).ExecuteCommand(); } return resultValue; } private async Task _ExecuteSqlBulkCopyAsync(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).As(item.Key).WhereColumns(whereExpression).ExecuteSqlBulkCopyAsync(); if (ActionCallBack != null) { ActionCallBack(resultValue); } } return resultValue; } private int _ExecuteSqlBulkCopy(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).WhereColumns(whereExpression).ExecuteSqlBulkCopy(); } return resultValue; } 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; } } } }