BaseRepository.cs 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  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<List<TEntity>> QueryAsync(Expression<Func<TEntity, bool>>? predicate = null, params (bool isWhere, Expression<Func<TEntity, bool>> expression)[] whereIfs)
  95. {
  96. var query = Db.Queryable<TEntity>().Where(predicate ??= d => true);
  97. if (whereIfs.Any())
  98. {
  99. foreach (var whereIf in whereIfs)
  100. {
  101. query = query.WhereIF(whereIf.isWhere, whereIf.expression);
  102. }
  103. }
  104. return await query.ToListAsync();
  105. }
  106. public async Task<bool> AnyAsync(CancellationToken cancellationToken = default) =>
  107. await Db.Queryable<TEntity>().AnyAsync();
  108. public async Task<bool> AnyAsync(Expression<Func<TEntity, bool>> predicate, CancellationToken cancellationToken = default) =>
  109. await Db.Queryable<TEntity>().AnyAsync(predicate);
  110. public async Task<int> CountAsync(Expression<Func<TEntity, bool>> predicate, CancellationToken cancellationToken = default) =>
  111. await Db.Queryable<TEntity>().CountAsync(predicate);
  112. /// <summary>
  113. /// 基础分页
  114. /// </summary>
  115. /// <param name="predicate"></param>
  116. /// <param name="orderByCreator"></param>
  117. /// <param name="pageIndex"></param>
  118. /// <param name="pageSize"></param>
  119. /// <returns></returns>
  120. public async Task<(int Total, List<TEntity> Items)> QueryPagedAsync(
  121. Expression<Func<TEntity, bool>> predicate,
  122. Func<ISugarQueryable<TEntity>, ISugarQueryable<TEntity>> orderByCreator,
  123. int pageIndex,
  124. int pageSize,
  125. params (bool isWhere, Expression<Func<TEntity, bool>> expression)[] whereIfs)
  126. {
  127. RefAsync<int> total = 0;
  128. var query = Db.Queryable<TEntity>().Where(predicate);
  129. if (whereIfs.Any())
  130. {
  131. foreach (var whereIf in whereIfs)
  132. {
  133. query = query.WhereIF(whereIf.isWhere, whereIf.expression);
  134. }
  135. }
  136. var items = await orderByCreator(query).ToPageListAsync(pageIndex, pageSize, total);
  137. return (total.Value, items);
  138. }
  139. public async Task<List<TEntity>> QueryExtAsync(Expression<Func<TEntity, bool>> predicate, Func<ISugarQueryable<TEntity>, ISugarQueryable<TEntity>> includes)
  140. {
  141. var query = Db.Queryable<TEntity>().Where(predicate);
  142. query = includes(query);
  143. return await query.ToListAsync();
  144. }
  145. public async Task<TEntity> GetExtAsync(Expression<Func<TEntity, bool>> predicate, Func<ISugarQueryable<TEntity>, ISugarQueryable<TEntity>> includes)
  146. {
  147. var query = Db.Queryable<TEntity>();
  148. query = includes(query);
  149. return await query.FirstAsync(predicate);
  150. }
  151. public async Task<TEntity> GetExtAsync(string id, Func<ISugarQueryable<TEntity>, ISugarQueryable<TEntity>> includes)
  152. {
  153. var query = Db.Queryable<TEntity>();
  154. query = includes(query);
  155. return await query.FirstAsync(d => d.Id == id);
  156. }
  157. public async Task UpdateAsync(TEntity entity, bool ignoreNullColumns = true, CancellationToken cancellationToken = default)
  158. {
  159. await Db.Updateable(entity)
  160. .IgnoreColumns(ignoreAllNullColumns: ignoreNullColumns)
  161. .IgnoreColumns(d => d.CreationTime)
  162. .ExecuteCommandAsync();
  163. }
  164. }
  165. }