BaseRepository.cs 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Linq.Expressions;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7. using SqlSugar;
  8. using XF.Domain;
  9. using XF.Domain.Repository;
  10. namespace CallCenter.Repository.SqlSugar
  11. {
  12. public abstract class BaseRepository<TEntity> : IRepository<TEntity> where TEntity : class, IEntity<string>, IHasCreationTime, new()
  13. {
  14. protected ISugarUnitOfWork<CallCenterDbContext> Uow { get; }
  15. protected ISqlSugarClient Db { get; }
  16. public BaseRepository(ISugarUnitOfWork<CallCenterDbContext> uow)
  17. {
  18. Uow = uow;
  19. Db = uow.Db;
  20. }
  21. public async Task<string> AddAsync(TEntity entity, CancellationToken cancellationToken = default)
  22. {
  23. var excEntity = await Db.Insertable(entity).ExecuteReturnEntityAsync();
  24. return excEntity.Id;
  25. }
  26. /// <summary>
  27. /// 批量插入(应用场景:小数据量,超出1万条建议另行实现)
  28. /// </summary>
  29. /// <param name="entities"></param>
  30. /// <param name="cancellationToken"></param>
  31. /// <returns></returns>
  32. public async Task AddRangeAsync(List<TEntity> entities, CancellationToken cancellationToken = default)
  33. {
  34. await Db.Insertable(entities).ExecuteCommandAsync();
  35. }
  36. public async Task RemoveAsync(TEntity entity, bool? soft = false, CancellationToken cancellationToken = default)
  37. {
  38. if (soft.HasValue && soft.Value)
  39. {
  40. await Db.Deleteable(entity).IsLogic().ExecuteCommandAsync("IsDeleted", true, "DeletionTime");
  41. }
  42. else
  43. {
  44. await Db.Deleteable(entity).ExecuteCommandAsync();
  45. }
  46. }
  47. public async Task RemoveAsync(string id, bool? soft = false, CancellationToken cancellationToken = default)
  48. {
  49. if (soft.HasValue && soft.Value)
  50. {
  51. await Db.Deleteable<TEntity>().In(id).IsLogic().ExecuteCommandAsync("IsDeleted", true, "DeletionTime");
  52. }
  53. else
  54. {
  55. await Db.Deleteable<TEntity>().In(id).ExecuteCommandAsync();
  56. }
  57. }
  58. public async Task RemoveAsync(Expression<Func<TEntity, bool>> predicate, bool? soft, CancellationToken cancellationToken = default)
  59. {
  60. if (soft.HasValue && soft.Value)
  61. {
  62. await Db.Deleteable<TEntity>().Where(predicate).IsLogic().ExecuteCommandAsync("IsDeleted", true, "DeletionTime");
  63. }
  64. else
  65. {
  66. await Db.Deleteable<TEntity>().Where(predicate).ExecuteCommandAsync();
  67. }
  68. }
  69. public async Task RemoveRangeAsync(IEnumerable<TEntity> entities, CancellationToken cancellationToken = default)
  70. {
  71. await Db.Deleteable<TEntity>(entities).ExecuteCommandAsync();
  72. }
  73. public async Task UpdateAsync(TEntity entity, CancellationToken cancellationToken = default)
  74. {
  75. await Db.Updateable(entity)
  76. .IgnoreColumns(ignoreAllNullColumns: true)
  77. .IgnoreColumns(d => d.CreationTime)
  78. .ExecuteCommandAsync();
  79. }
  80. public async Task UpdateRangeAsync(List<TEntity> entities, CancellationToken cancellationToken = default)
  81. {
  82. await Db.Updateable(entities)
  83. .IgnoreColumns(d => d.CreationTime)
  84. .ExecuteCommandAsync();
  85. }
  86. public async Task<TEntity?> GetAsync(string id, CancellationToken cancellationToken = default)
  87. {
  88. return await Db.Queryable<TEntity>().FirstAsync(d => d.Id == id);
  89. }
  90. public async Task<TEntity?> GetAsync(Expression<Func<TEntity, bool>> predicate, CancellationToken cancellationToken = default)
  91. {
  92. return await Db.Queryable<TEntity>().FirstAsync(predicate);
  93. }
  94. public async Task<TEntity?> GetAsync(Expression<Func<TEntity, bool>> predicate,bool isDesc, Expression<Func<TEntity, object>> orderby, CancellationToken cancellationToken = default)
  95. {
  96. if (isDesc)
  97. {
  98. return await Db.Queryable<TEntity>().OrderBy(orderby, OrderByType.Desc).FirstAsync(predicate);
  99. }
  100. else
  101. {
  102. return await Db.Queryable<TEntity>().OrderBy(orderby,OrderByType.Asc).FirstAsync(predicate);
  103. }
  104. }
  105. public async Task<List<TEntity>> QueryAsync(Expression<Func<TEntity, bool>>? predicate = null, params (bool isWhere, Expression<Func<TEntity, bool>> expression)[] whereIfs)
  106. {
  107. var query = Db.Queryable<TEntity>().Where(predicate ??= d => true);
  108. if (whereIfs.Any())
  109. {
  110. foreach (var whereIf in whereIfs)
  111. {
  112. query = query.WhereIF(whereIf.isWhere, whereIf.expression);
  113. }
  114. }
  115. return await query.ToListAsync();
  116. }
  117. public async Task<bool> AnyAsync(CancellationToken cancellationToken = default) =>
  118. await Db.Queryable<TEntity>().AnyAsync();
  119. public async Task<bool> AnyAsync(Expression<Func<TEntity, bool>> predicate, CancellationToken cancellationToken = default) =>
  120. await Db.Queryable<TEntity>().AnyAsync(predicate);
  121. public async Task<int> CountAsync(Expression<Func<TEntity, bool>> predicate, CancellationToken cancellationToken = default) =>
  122. await Db.Queryable<TEntity>().CountAsync(predicate);
  123. /// <summary>
  124. /// 基础分页
  125. /// </summary>
  126. /// <param name="predicate"></param>
  127. /// <param name="orderByCreator"></param>
  128. /// <param name="pageIndex"></param>
  129. /// <param name="pageSize"></param>
  130. /// <returns></returns>
  131. public async Task<(int Total, List<TEntity> Items)> QueryPagedAsync(
  132. Expression<Func<TEntity, bool>> predicate,
  133. Func<ISugarQueryable<TEntity>, ISugarQueryable<TEntity>> orderByCreator,
  134. int pageIndex,
  135. int pageSize,
  136. params (bool isWhere, Expression<Func<TEntity, bool>> expression)[] whereIfs)
  137. {
  138. RefAsync<int> total = 0;
  139. var query = Db.Queryable<TEntity>().Where(predicate);
  140. if (whereIfs.Any())
  141. {
  142. foreach (var whereIf in whereIfs)
  143. {
  144. query = query.WhereIF(whereIf.isWhere, whereIf.expression);
  145. }
  146. }
  147. var items = await orderByCreator(query).ToPageListAsync(pageIndex, pageSize, total);
  148. return (total.Value, items);
  149. }
  150. public async Task<List<TEntity>> QueryExtAsync(Expression<Func<TEntity, bool>> predicate, Func<ISugarQueryable<TEntity>, ISugarQueryable<TEntity>> includes)
  151. {
  152. var query = Db.Queryable<TEntity>().Where(predicate);
  153. query = includes(query);
  154. return await query.ToListAsync();
  155. }
  156. public async Task<TEntity> GetExtAsync(Expression<Func<TEntity, bool>> predicate, Func<ISugarQueryable<TEntity>, ISugarQueryable<TEntity>> includes)
  157. {
  158. var query = Db.Queryable<TEntity>();
  159. query = includes(query);
  160. return await query.FirstAsync(predicate);
  161. }
  162. public async Task<TEntity> GetExtAsync(string id, Func<ISugarQueryable<TEntity>, ISugarQueryable<TEntity>> includes)
  163. {
  164. var query = Db.Queryable<TEntity>();
  165. query = includes(query);
  166. return await query.FirstAsync(d => d.Id == id);
  167. }
  168. public async Task UpdateAsync(TEntity entity, bool ignoreNullColumns = true, CancellationToken cancellationToken = default)
  169. {
  170. await Db.Updateable(entity)
  171. .IgnoreColumns(ignoreAllNullColumns: ignoreNullColumns)
  172. .IgnoreColumns(d => d.CreationTime)
  173. .ExecuteCommandAsync();
  174. }
  175. }
  176. }