SplitFastest.cs 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Reflection;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7. namespace SqlSugar
  8. {
  9. public class SplitFastest<T>where T:class,new()
  10. {
  11. public FastestProvider<T> FastestProvider { get; set; }
  12. public SqlSugarProvider Context { get { return this.FastestProvider.context; } }
  13. public EntityInfo EntityInfo { get { return this.Context.EntityMaintenance.GetEntityInfo<T>(); } }
  14. public int BulkCopy(List<T> datas)
  15. {
  16. List<GroupModel> groupModels;
  17. int result;
  18. GroupDataList(datas, out groupModels, out result);
  19. foreach (var item in groupModels.GroupBy(it => it.GroupName))
  20. {
  21. CreateTable(item.Key);
  22. var addList = item.Select(it => it.Item).ToList();
  23. result += FastestProvider.AS(item.Key).BulkCopy(addList);
  24. this.Context.MappingTables.Add(EntityInfo.EntityName, EntityInfo.DbTableName);
  25. }
  26. return result;
  27. }
  28. public async Task<int> BulkCopyAsync(List<T> datas)
  29. {
  30. List<GroupModel> groupModels;
  31. int result;
  32. GroupDataList(datas, out groupModels, out result);
  33. foreach (var item in groupModels.GroupBy(it => it.GroupName))
  34. {
  35. CreateTable(item.Key);
  36. var addList = item.Select(it => it.Item).ToList();
  37. result +=await FastestProvider.AS(item.Key).BulkCopyAsync(addList);
  38. this.Context.MappingTables.Add(EntityInfo.EntityName, EntityInfo.DbTableName);
  39. }
  40. return result;
  41. }
  42. public int BulkUpdate(List<T> datas)
  43. {
  44. List<GroupModel> groupModels;
  45. int result;
  46. GroupDataList(datas, out groupModels, out result);
  47. foreach (var item in groupModels.GroupBy(it => it.GroupName))
  48. {
  49. CreateTable(item.Key);
  50. var addList = item.Select(it => it.Item).ToList();
  51. result += FastestProvider.AS(item.Key).BulkUpdate(addList);
  52. this.Context.MappingTables.Add(EntityInfo.EntityName, EntityInfo.DbTableName);
  53. }
  54. return result;
  55. }
  56. public async Task<int> BulkUpdateAsync(List<T> datas)
  57. {
  58. List<GroupModel> groupModels;
  59. int result;
  60. GroupDataList(datas, out groupModels, out result);
  61. foreach (var item in groupModels.GroupBy(it => it.GroupName))
  62. {
  63. CreateTable(item.Key);
  64. var addList = item.Select(it => it.Item).ToList();
  65. result += await FastestProvider.AS(item.Key).BulkUpdateAsync(addList);
  66. this.Context.MappingTables.Add(EntityInfo.EntityName, EntityInfo.DbTableName);
  67. }
  68. return result;
  69. }
  70. public int BulkUpdate(List<T> datas,string [] wherColumns,string [] updateColumns)
  71. {
  72. List<GroupModel> groupModels;
  73. int result;
  74. GroupDataList(datas, out groupModels, out result);
  75. foreach (var item in groupModels.GroupBy(it => it.GroupName))
  76. {
  77. var addList = item.Select(it => it.Item).ToList();
  78. result += FastestProvider.AS(item.Key).BulkUpdate(addList,wherColumns,updateColumns); ;
  79. }
  80. return result;
  81. }
  82. public async Task<int> BulkUpdateAsync(List<T> datas, string[] wherColumns, string[] updateColumns)
  83. {
  84. List<GroupModel> groupModels;
  85. int result;
  86. GroupDataList(datas, out groupModels, out result);
  87. foreach (var item in groupModels.GroupBy(it => it.GroupName))
  88. {
  89. var addList = item.Select(it => it.Item).ToList();
  90. result += await FastestProvider.AS(item.Key).BulkUpdateAsync(addList, wherColumns, updateColumns); ;
  91. }
  92. return result;
  93. }
  94. private void CreateTable(string tableName)
  95. {
  96. var isLog = this.Context.Ado.IsEnableLogEvent;
  97. this.Context.Ado.IsEnableLogEvent = false;
  98. if (!this.Context.DbMaintenance.IsAnyTable(tableName, false))
  99. {
  100. this.Context.MappingTables.Add(EntityInfo.EntityName, tableName);
  101. this.Context.CodeFirst.InitTables<T>();
  102. }
  103. this.Context.Ado.IsEnableLogEvent = isLog;
  104. }
  105. private void GroupDataList(List<T> datas, out List<GroupModel> groupModels, out int result)
  106. {
  107. var attribute = typeof(T).GetCustomAttribute<SplitTableAttribute>() as SplitTableAttribute;
  108. Check.Exception(attribute == null, $"{typeof(T).Name} need SplitTableAttribute");
  109. groupModels = new List<GroupModel>();
  110. var db = FastestProvider.context;
  111. var hasSplitField = typeof(T).GetProperties().Any(it => it.GetCustomAttribute<SplitFieldAttribute>() != null);
  112. foreach (var item in datas)
  113. {
  114. if (groupModels.Count > 0 && !hasSplitField)
  115. groupModels.Add(new GroupModel() { GroupName = groupModels[0].GroupName, Item = item });
  116. else
  117. {
  118. var value = db.SplitHelper<T>().GetValue(attribute.SplitType, item);
  119. var tableName = db.SplitHelper<T>().GetTableName(attribute.SplitType, value);
  120. groupModels.Add(new GroupModel() { GroupName = tableName, Item = item });
  121. }
  122. }
  123. result = 0;
  124. }
  125. internal class GroupModel
  126. {
  127. public string GroupName { get; set; }
  128. public T Item { get; set; }
  129. }
  130. }
  131. }