StorageableSplitProvider.cs 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Linq.Expressions;
  5. using System.Reflection;
  6. using System.Text;
  7. using System.Threading.Tasks;
  8. namespace SqlSugar
  9. {
  10. public class StorageableSplitProvider<T> where T:class,new()
  11. {
  12. internal Storageable<T> SaveInfo { get; set; }
  13. internal SqlSugarProvider Context { get; set; }
  14. internal List<T> List { get; set; }
  15. internal EntityInfo EntityInfo { get; set; }
  16. internal Expression<Func<T, object>> whereExpression { get; set; }
  17. internal int pageSize = 1000;
  18. internal Action<int> ActionCallBack =null;
  19. public StorageableSplitProvider<T> PageSize(int size, Action<int> ActionCallBack = null)
  20. {
  21. this.pageSize = size;
  22. return this;
  23. }
  24. public int ExecuteCommand()
  25. {
  26. if (List.Count > pageSize)
  27. {
  28. var result = 0;
  29. this.Context.Utilities.PageEach(List, pageSize, pageItem =>
  30. {
  31. result+= _ExecuteCommand(pageItem);
  32. });
  33. return result;
  34. }
  35. else
  36. {
  37. var list = List;
  38. return _ExecuteCommand(list);
  39. }
  40. }
  41. public int ExecuteSqlBulkCopy()
  42. {
  43. if (List.Count > pageSize)
  44. {
  45. var result = 0;
  46. this.Context.Utilities.PageEach(List, pageSize, pageItem =>
  47. {
  48. result += _ExecuteSqlBulkCopy(pageItem);
  49. });
  50. return result;
  51. }
  52. else
  53. {
  54. var list = List;
  55. return _ExecuteSqlBulkCopy(list);
  56. }
  57. }
  58. public async Task<int> ExecuteCommandAsync()
  59. {
  60. if (List.Count > pageSize)
  61. {
  62. var result = 0;
  63. await this.Context.Utilities.PageEachAsync(List, pageSize, async pageItem =>
  64. {
  65. result +=await _ExecuteCommandAsync(pageItem);
  66. if (ActionCallBack != null)
  67. {
  68. ActionCallBack(result);
  69. }
  70. });
  71. return result;
  72. }
  73. else
  74. {
  75. var list = List;
  76. return await _ExecuteCommandAsync(list);
  77. }
  78. }
  79. public async Task<int> ExecuteSqlBulkCopyAsync()
  80. {
  81. if (List.Count > pageSize)
  82. {
  83. var result = 0;
  84. await this.Context.Utilities.PageEachAsync(List, pageSize, async pageItem =>
  85. {
  86. result += await _ExecuteSqlBulkCopyAsync(pageItem);
  87. if (ActionCallBack != null)
  88. {
  89. ActionCallBack(result);
  90. }
  91. });
  92. return result;
  93. }
  94. else
  95. {
  96. var list = List;
  97. return await _ExecuteSqlBulkCopyAsync(list);
  98. }
  99. }
  100. private async Task<int> _ExecuteCommandAsync(List<T> list)
  101. {
  102. int resultValue = 0;
  103. List<GroupModel> groupModels;
  104. int result;
  105. GroupDataList(list, out groupModels, out result);
  106. foreach (var item in groupModels.GroupBy(it => it.GroupName))
  107. {
  108. var addList = item.Select(it => it.Item).ToList();
  109. resultValue +=await this.Context.Storageable(addList).As(item.Key).WhereColumns(whereExpression).ExecuteCommandAsync();
  110. if (ActionCallBack != null)
  111. {
  112. ActionCallBack(resultValue);
  113. }
  114. }
  115. return result;
  116. }
  117. private int _ExecuteCommand(List<T> list)
  118. {
  119. int resultValue = 0;
  120. List<GroupModel> groupModels;
  121. int result;
  122. GroupDataList(list, out groupModels, out result);
  123. foreach (var item in groupModels.GroupBy(it => it.GroupName))
  124. {
  125. var addList = item.Select(it => it.Item).ToList();
  126. resultValue += this.Context.Storageable(addList).As(item.Key).WhereColumns(whereExpression).ExecuteCommand();
  127. }
  128. return result;
  129. }
  130. private async Task<int> _ExecuteSqlBulkCopyAsync(List<T> list)
  131. {
  132. int resultValue = 0;
  133. List<GroupModel> groupModels;
  134. int result;
  135. GroupDataList(list, out groupModels, out result);
  136. foreach (var item in groupModels.GroupBy(it => it.GroupName))
  137. {
  138. var addList = item.Select(it => it.Item).ToList();
  139. resultValue += await this.Context.Storageable(addList).As(item.Key).WhereColumns(whereExpression).ExecuteSqlBulkCopyAsync();
  140. if (ActionCallBack != null)
  141. {
  142. ActionCallBack(resultValue);
  143. }
  144. }
  145. return result;
  146. }
  147. private int _ExecuteSqlBulkCopy(List<T> list)
  148. {
  149. int resultValue = 0;
  150. List<GroupModel> groupModels;
  151. int result;
  152. GroupDataList(list, out groupModels, out result);
  153. foreach (var item in groupModels.GroupBy(it => it.GroupName))
  154. {
  155. var addList = item.Select(it => it.Item).ToList();
  156. resultValue += this.Context.Storageable(addList).As(item.Key).WhereColumns(whereExpression).ExecuteSqlBulkCopy();
  157. }
  158. return result;
  159. }
  160. private void GroupDataList(List<T> datas, out List<GroupModel> groupModels, out int result)
  161. {
  162. var attribute = typeof(T).GetCustomAttribute<SplitTableAttribute>() as SplitTableAttribute;
  163. Check.Exception(attribute == null, $"{typeof(T).Name} need SplitTableAttribute");
  164. groupModels = new List<GroupModel>();
  165. var db = this.Context;
  166. foreach (var item in datas)
  167. {
  168. var value = db.SplitHelper<T>().GetValue(attribute.SplitType, item);
  169. var tableName = db.SplitHelper<T>().GetTableName(attribute.SplitType, value);
  170. groupModels.Add(new GroupModel() { GroupName = tableName, Item = item });
  171. }
  172. var tablenames = groupModels.Select(it => it.GroupName).Distinct().ToList();
  173. CreateTable(tablenames);
  174. result = 0;
  175. }
  176. private void CreateTable(List<string> tableNames)
  177. {
  178. var isLog = this.Context.Ado.IsEnableLogEvent;
  179. this.Context.Ado.IsEnableLogEvent = false;
  180. foreach (var item in tableNames)
  181. {
  182. if (!this.Context.DbMaintenance.IsAnyTable(item, false))
  183. {
  184. if (item != null)
  185. {
  186. this.Context.MappingTables.Add(EntityInfo.EntityName, item);
  187. this.Context.CodeFirst.InitTables<T>();
  188. }
  189. }
  190. }
  191. this.Context.Ado.IsEnableLogEvent = isLog;
  192. this.Context.MappingTables.Add(EntityInfo.EntityName, EntityInfo.DbTableName);
  193. }
  194. internal class GroupModel
  195. {
  196. public string GroupName { get; set; }
  197. public T Item { get; set; }
  198. }
  199. }
  200. }