BaseRepository.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  1. using System.Linq.Expressions;
  2. using Hotline.Repository.SqlSugar.DataPermissions;
  3. using SqlSugar;
  4. using XF.Domain.Entities;
  5. using XF.Domain.Repository;
  6. namespace Hotline.Repository.SqlSugar
  7. {
  8. public abstract class BaseRepository<TEntity> : IRepository<TEntity> where TEntity : class, IEntity<string>, IHasCreationTime, IDataPermission, new()
  9. {
  10. private readonly IDataPermissionFilterBuilder _dataPermissionFilterBuilder;
  11. protected ISugarUnitOfWork<HotlineDbContext> Uow { get; }
  12. protected ISqlSugarClient Db { get; }
  13. public BaseRepository(ISugarUnitOfWork<HotlineDbContext> uow, IDataPermissionFilterBuilder dataPermissionFilterBuilder)
  14. {
  15. Uow = uow;
  16. Db = uow.Db;
  17. _dataPermissionFilterBuilder = dataPermissionFilterBuilder;
  18. }
  19. public async Task<string> AddAsync(TEntity entity, CancellationToken cancellationToken = default)
  20. {
  21. entity.InitDatePermission(_dataPermissionFilterBuilder.DataPermissionManager);
  22. var excEntity = await Db.Insertable(entity).ExecuteReturnEntityAsync();
  23. return excEntity.Id;
  24. }
  25. /// <summary>
  26. /// 批量插入(应用场景:小数据量,超出1万条建议另行实现)
  27. /// </summary>
  28. /// <param name="entities"></param>
  29. /// <param name="cancellationToken"></param>
  30. /// <returns></returns>
  31. public async Task AddRangeAsync(List<TEntity> entities, CancellationToken cancellationToken = default)
  32. {
  33. entities.ForEach(d => d.InitDatePermission(_dataPermissionFilterBuilder.DataPermissionManager));
  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 Task<bool> AnyAsync(CancellationToken cancellationToken = default) => Db.Queryable<TEntity>().AnyAsync();
  107. public Task<bool> AnyAsync(Expression<Func<TEntity, bool>> predicate, CancellationToken cancellationToken = default) =>
  108. Db.Queryable<TEntity>().AnyAsync(predicate);
  109. public Task<int> CountAsync(Expression<Func<TEntity, bool>> predicate, CancellationToken cancellationToken = default) =>
  110. Db.Queryable<TEntity>().CountAsync(predicate);
  111. public ISugarQueryable<TEntity> Queryable(bool permissionVerify = false, bool includeDeleted = false)
  112. {
  113. if (includeDeleted)
  114. Db.QueryFilter.Clear();
  115. var query = Db.Queryable<TEntity>();
  116. if (permissionVerify)
  117. query = query.DataPermissionFiltering(_dataPermissionFilterBuilder);
  118. return query;
  119. }
  120. public UpdateNavTaskInit<TEntity, TEntity> UpdateNav(TEntity entity) => Db.UpdateNav(entity);
  121. public UpdateNavTaskInit<TEntity, TEntity> UpdateNav(TEntity entity, UpdateNavRootOptions options) => Db.UpdateNav(entity, options);
  122. public UpdateNavTaskInit<TEntity, TEntity> UpdateNav(List<TEntity> entities) => Db.UpdateNav(entities);
  123. public UpdateNavTaskInit<TEntity, TEntity> UpdateNav(List<TEntity> entities, UpdateNavRootOptions options) => Db.UpdateNav(entities, options);
  124. /// <summary>
  125. /// 基础分页
  126. /// </summary>
  127. /// <param name="predicate"></param>
  128. /// <param name="orderByCreator"></param>
  129. /// <param name="pageIndex"></param>
  130. /// <param name="pageSize"></param>
  131. /// <param name="permissionVerify"></param>
  132. /// <returns></returns>
  133. public async Task<(int Total, List<TEntity> Items)> QueryPagedAsync(
  134. Expression<Func<TEntity, bool>> predicate,
  135. Func<ISugarQueryable<TEntity>, ISugarQueryable<TEntity>> orderByCreator,
  136. int pageIndex,
  137. int pageSize,
  138. bool permissionVerify = false,
  139. params (bool isWhere, Expression<Func<TEntity, bool>> expression)[] whereIfs)
  140. {
  141. RefAsync<int> total = 0;
  142. var query = Db.Queryable<TEntity>().Where(predicate);
  143. if (permissionVerify)
  144. query = query.DataPermissionFiltering(_dataPermissionFilterBuilder);
  145. if (whereIfs.Any())
  146. {
  147. foreach (var whereIf in whereIfs)
  148. {
  149. query = query.WhereIF(whereIf.isWhere, whereIf.expression);
  150. }
  151. }
  152. var items = await orderByCreator(query).ToPageListAsync(pageIndex, pageSize, total);
  153. return (total.Value, items);
  154. }
  155. public async Task<(int Total, List<TEntity> Items)> QueryPagedAsync(
  156. Expression<Func<TEntity, bool>> predicate,
  157. int pageIndex,
  158. int pageSize,
  159. Func<ISugarQueryable<TEntity>, ISugarQueryable<TEntity>>? includes = null,
  160. Func<ISugarQueryable<TEntity>, ISugarQueryable<TEntity>>? orderByCreator = null,
  161. bool permissionVerify = false,
  162. params (bool isWhere, Expression<Func<TEntity, bool>> expression)[] whereIfs)
  163. {
  164. RefAsync<int> total = 0;
  165. var query = Db.Queryable<TEntity>().Where(predicate);
  166. if (permissionVerify)
  167. query = query.DataPermissionFiltering(_dataPermissionFilterBuilder);
  168. if (includes is not null)
  169. query = includes(query);
  170. if (whereIfs.Any())
  171. {
  172. foreach (var whereIf in whereIfs)
  173. {
  174. query = query.WhereIF(whereIf.isWhere, whereIf.expression);
  175. }
  176. }
  177. if (orderByCreator is not null)
  178. query = orderByCreator(query);
  179. var items = await query.ToPageListAsync(pageIndex, pageSize, total);
  180. return (total.Value, items);
  181. }
  182. public async Task<List<TEntity>> QueryExtAsync(
  183. Expression<Func<TEntity, bool>> predicate,
  184. Func<ISugarQueryable<TEntity>, ISugarQueryable<TEntity>>? includes = null,
  185. Func<ISugarQueryable<TEntity>, ISugarQueryable<TEntity>>? orderByCreator = null,
  186. bool permissionVerify = false,
  187. params (bool isWhere, Expression<Func<TEntity, bool>> expression)[] whereIfs)
  188. {
  189. var query = Db.Queryable<TEntity>().Where(predicate);
  190. if (permissionVerify)
  191. query = query.DataPermissionFiltering(_dataPermissionFilterBuilder);
  192. if (includes is not null)
  193. query = includes(query);
  194. if (whereIfs.Any())
  195. {
  196. foreach (var whereIf in whereIfs)
  197. {
  198. query = query.WhereIF(whereIf.isWhere, whereIf.expression);
  199. }
  200. }
  201. if (orderByCreator is not null)
  202. query = orderByCreator(query);
  203. return await query.ToListAsync();
  204. }
  205. public async Task<List<TEntity>> QueryExtAsync(
  206. Expression<Func<TEntity, bool>> predicate,
  207. Func<ISugarQueryable<TEntity>, ISugarQueryable<TEntity>> includes,
  208. bool permissionVerify = false)
  209. {
  210. var query = Db.Queryable<TEntity>().Where(predicate);
  211. if (permissionVerify)
  212. query = query.DataPermissionFiltering(_dataPermissionFilterBuilder);
  213. query = includes(query);
  214. return await query.ToListAsync();
  215. }
  216. public async Task<TEntity> GetExtAsync(
  217. Expression<Func<TEntity, bool>> predicate,
  218. Func<ISugarQueryable<TEntity>, ISugarQueryable<TEntity>> includes,
  219. bool permissionVerify = false)
  220. {
  221. var query = Db.Queryable<TEntity>();
  222. if (permissionVerify)
  223. query = query.DataPermissionFiltering(_dataPermissionFilterBuilder);
  224. query = includes(query);
  225. return await query.FirstAsync(predicate);
  226. }
  227. public async Task<TEntity> GetExtAsync(string id, Func<ISugarQueryable<TEntity>, ISugarQueryable<TEntity>> includes, bool permissionVerify = false)
  228. {
  229. var query = Db.Queryable<TEntity>();
  230. if (permissionVerify)
  231. query = query.DataPermissionFiltering(_dataPermissionFilterBuilder);
  232. query = includes(query);
  233. return await query.FirstAsync(d => d.Id == id);
  234. }
  235. public async Task UpdateAsync(TEntity entity, bool ignoreNullColumns = true, CancellationToken cancellationToken = default)
  236. {
  237. await Db.Updateable(entity)
  238. .IgnoreColumns(ignoreAllNullColumns: ignoreNullColumns)
  239. .IgnoreColumns(d => d.CreationTime)
  240. .ExecuteCommandAsync();
  241. }
  242. }
  243. }