InsertablePage.cs 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. namespace SqlSugar
  7. {
  8. public class InsertablePage<T> where T:class,new()
  9. {
  10. public int PageSize { get; set; }
  11. public SqlSugarProvider Context { get; set; }
  12. public T[] DataList { get; set; }
  13. public string TableName { get; internal set; }
  14. public List<string> InsertColumns { get; internal set; }
  15. public bool IsEnableDiffLogEvent { get; internal set; }
  16. public DiffLogModel DiffModel { get; internal set; }
  17. public bool IsOffIdentity { get; internal set; }
  18. public bool IsInsertColumnsNull { get; internal set; }
  19. public int ExecuteCommand()
  20. {
  21. if (DataList.Count() == 1 && DataList.First() == null)
  22. {
  23. return 0;
  24. }
  25. if (PageSize == 0) { PageSize = 1000; }
  26. var result = 0;
  27. var isNoTran = this.Context.Ado.IsNoTran();
  28. try
  29. {
  30. if (isNoTran)
  31. {
  32. this.Context.Ado.BeginTran();
  33. }
  34. this.Context.Utilities.PageEach(DataList, PageSize, pageItem =>
  35. {
  36. result += this.Context.Insertable(pageItem).AS(TableName).IgnoreColumnsNull(this.IsInsertColumnsNull).OffIdentity(IsOffIdentity).EnableDiffLogEventIF(IsEnableDiffLogEvent, DiffModel).InsertColumns(InsertColumns.ToArray()).ExecuteCommand();
  37. });
  38. if (isNoTran)
  39. {
  40. this.Context.Ado.CommitTran();
  41. }
  42. }
  43. catch (Exception)
  44. {
  45. if (isNoTran)
  46. {
  47. this.Context.Ado.RollbackTran();
  48. }
  49. throw;
  50. }
  51. return result;
  52. }
  53. public async Task<int> ExecuteCommandAsync()
  54. {
  55. if (DataList.Count() == 1 && DataList.First() == null)
  56. {
  57. return 0;
  58. }
  59. if (PageSize == 0) { PageSize = 1000; }
  60. var result = 0;
  61. var isNoTran = this.Context.Ado.IsNoTran();
  62. try
  63. {
  64. if (isNoTran)
  65. {
  66. await this.Context.Ado.BeginTranAsync();
  67. }
  68. await this.Context.Utilities.PageEachAsync(DataList, PageSize, async pageItem =>
  69. {
  70. result +=await this.Context.Insertable(pageItem).AS(TableName).IgnoreColumnsNull(this.IsInsertColumnsNull).OffIdentity(IsOffIdentity).EnableDiffLogEventIF(IsEnableDiffLogEvent, DiffModel).InsertColumns(InsertColumns.ToArray()).ExecuteCommandAsync();
  71. });
  72. if (isNoTran)
  73. {
  74. await this.Context.Ado.CommitTranAsync();
  75. }
  76. }
  77. catch (Exception)
  78. {
  79. if (isNoTran)
  80. {
  81. await this.Context.Ado.RollbackTranAsync();
  82. }
  83. throw;
  84. }
  85. return result;
  86. }
  87. public List<long> ExecuteReturnSnowflakeIdList()
  88. {
  89. if (DataList.Count() == 1 && DataList.First() == null)
  90. {
  91. return new List<long>();
  92. }
  93. if (PageSize == 0) { PageSize = 1000; }
  94. var result = new List<long>();
  95. var isNoTran = this.Context.Ado.IsNoTran();
  96. try
  97. {
  98. if (isNoTran)
  99. {
  100. this.Context.Ado.BeginTran();
  101. }
  102. this.Context.Utilities.PageEach(DataList, PageSize, pageItem =>
  103. {
  104. result.AddRange(this.Context.Insertable(pageItem).AS(TableName).OffIdentity(IsOffIdentity).EnableDiffLogEventIF(IsEnableDiffLogEvent, DiffModel).InsertColumns(InsertColumns.ToArray()).ExecuteReturnSnowflakeIdList());
  105. });
  106. if (isNoTran)
  107. {
  108. this.Context.Ado.CommitTran();
  109. }
  110. }
  111. catch (Exception)
  112. {
  113. if (isNoTran)
  114. {
  115. this.Context.Ado.RollbackTran();
  116. }
  117. throw;
  118. }
  119. return result;
  120. }
  121. public async Task<List<long>> ExecuteReturnSnowflakeIdListAsync()
  122. {
  123. if (DataList.Count() == 1 && DataList.First() == null)
  124. {
  125. return new List<long>();
  126. }
  127. if (PageSize == 0) { PageSize = 1000; }
  128. var result = new List<long>();
  129. var isNoTran = this.Context.Ado.IsNoTran();
  130. try
  131. {
  132. if (isNoTran)
  133. {
  134. await this.Context.Ado.BeginTranAsync();
  135. }
  136. await this.Context.Utilities.PageEachAsync(DataList, PageSize, async pageItem =>
  137. {
  138. result.AddRange(await this.Context.Insertable(pageItem).AS(TableName).OffIdentity(IsOffIdentity).EnableDiffLogEventIF(IsEnableDiffLogEvent, DiffModel).InsertColumns(InsertColumns.ToArray()).ExecuteReturnSnowflakeIdListAsync());
  139. });
  140. if (isNoTran)
  141. {
  142. await this.Context.Ado.CommitTranAsync();
  143. }
  144. }
  145. catch (Exception)
  146. {
  147. if (isNoTran)
  148. {
  149. await this.Context.Ado.RollbackTranAsync();
  150. }
  151. throw;
  152. }
  153. return result;
  154. }
  155. public InsertablePage<T> IgnoreColumnsNull(bool isIgnoreNull = true)
  156. {
  157. this.PageSize = 1;
  158. this.IsInsertColumnsNull = isIgnoreNull;
  159. return this;
  160. }
  161. }
  162. }