BaseRepository.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372
  1. using System.Linq.Expressions;
  2. using Hotline.Repository.SqlSugar.DataPermissions;
  3. using Hotline.Repository.SqlSugar.Extensions;
  4. using SqlSugar;
  5. using XF.Domain.Entities;
  6. using XF.Domain.Repository;
  7. namespace Hotline.Repository.SqlSugar
  8. {
  9. public class BaseRepository<TEntity> : IRepository<TEntity> where TEntity : class, IEntity<string>, IHasCreationTime, IDataPermission, new()
  10. {
  11. private readonly IDataPermissionFilterBuilder _dataPermissionFilterBuilder;
  12. protected ISugarUnitOfWork<HotlineDbContext> Uow { get; }
  13. protected ISqlSugarClient Db { get; }
  14. public BaseRepository(ISugarUnitOfWork<HotlineDbContext> uow, IDataPermissionFilterBuilder dataPermissionFilterBuilder)
  15. {
  16. Uow = uow;
  17. Db = uow.Db;
  18. _dataPermissionFilterBuilder = dataPermissionFilterBuilder;
  19. }
  20. public async Task<string> AddAsync(TEntity entity, CancellationToken cancellationToken = default)
  21. {
  22. entity.InitDatePermission(_dataPermissionFilterBuilder.DataPermissionManager);
  23. var excEntity = await Db.Insertable(entity).ExecuteReturnEntityAsync();
  24. return excEntity.Id;
  25. }
  26. public async Task<string> AddAsync(TEntity entity,bool isCreator , CancellationToken cancellationToken = default)
  27. {
  28. if (isCreator)
  29. {
  30. entity.InitDatePermission(_dataPermissionFilterBuilder.DataPermissionManager);
  31. }
  32. var excEntity = await Db.Insertable(entity).ExecuteReturnEntityAsync();
  33. return excEntity.Id;
  34. }
  35. /// <summary>
  36. /// 批量插入(应用场景:小数据量,超出1万条建议另行实现)
  37. /// </summary>
  38. /// <param name="entities"></param>
  39. /// <param name="cancellationToken"></param>
  40. /// <returns></returns>
  41. public async Task AddRangeAsync(List<TEntity> entities, CancellationToken cancellationToken = default)
  42. {
  43. entities.ForEach(d => d.InitDatePermission(_dataPermissionFilterBuilder.DataPermissionManager));
  44. await Db.Insertable(entities).ExecuteCommandAsync(cancellationToken);
  45. }
  46. public async Task RemoveAsync(TEntity entity, bool? soft = false, CancellationToken cancellationToken = default)
  47. {
  48. if (soft.HasValue && soft.Value)
  49. {
  50. await Db.Deleteable(entity).IsLogic().ExecuteCommandAsync("IsDeleted", true, "DeletionTime");
  51. }
  52. else
  53. {
  54. await Db.Deleteable(entity).ExecuteCommandAsync(cancellationToken);
  55. }
  56. }
  57. public async Task RemoveAsync(string id, bool? soft = false, CancellationToken cancellationToken = default)
  58. {
  59. if (soft.HasValue && soft.Value)
  60. {
  61. await Db.Deleteable<TEntity>().In(id).IsLogic().ExecuteCommandAsync("IsDeleted", true, "DeletionTime");
  62. }
  63. else
  64. {
  65. await Db.Deleteable<TEntity>().In(id).ExecuteCommandAsync(cancellationToken);
  66. }
  67. }
  68. public async Task RemoveAsync(Expression<Func<TEntity, bool>> predicate, bool? soft, CancellationToken cancellationToken = default)
  69. {
  70. if (soft.HasValue && soft.Value)
  71. {
  72. await Db.Deleteable<TEntity>().Where(predicate).IsLogic().ExecuteCommandAsync("IsDeleted", true, "DeletionTime");
  73. }
  74. else
  75. {
  76. await Db.Deleteable<TEntity>().Where(predicate).ExecuteCommandAsync(cancellationToken);
  77. }
  78. }
  79. public async Task RemoveRangeAsync(IEnumerable<TEntity> entities, CancellationToken cancellationToken = default)
  80. {
  81. await Db.Deleteable<TEntity>(entities).ExecuteCommandAsync(cancellationToken);
  82. }
  83. public async Task RemoveRangeAsync(IEnumerable<TEntity> entities, bool? soft, CancellationToken cancellationToken = default)
  84. {
  85. if (soft.HasValue && soft.Value)
  86. {
  87. await Db.Deleteable<TEntity>(entities).IsLogic().ExecuteCommandAsync("IsDeleted", true, "DeletionTime");
  88. }
  89. else
  90. {
  91. await Db.Deleteable<TEntity>(entities).ExecuteCommandAsync(cancellationToken);
  92. }
  93. }
  94. public async Task UpdateAsync(TEntity entity, CancellationToken cancellationToken = default)
  95. {
  96. await Db.Updateable(entity)
  97. .IgnoreColumns(ignoreAllNullColumns: true)
  98. .IgnoreColumns(d => d.CreationTime)
  99. .ExecuteCommandAsync(cancellationToken);
  100. }
  101. public async Task UpdateNullAsync(TEntity entity, CancellationToken cancellationToken = default)
  102. {
  103. await Db.Updateable(entity)
  104. .IgnoreColumns(d => d.CreationTime)
  105. .ExecuteCommandAsync(cancellationToken);
  106. }
  107. public async Task UpdateRangeAsync(List<TEntity> entities, CancellationToken cancellationToken = default)
  108. {
  109. await Db.Updateable(entities)
  110. .IgnoreColumns(d => d.CreationTime)
  111. .ExecuteCommandAsync(cancellationToken);
  112. }
  113. public async Task<TEntity?> GetAsync(string id, CancellationToken cancellationToken = default)
  114. {
  115. return await Db.Queryable<TEntity>().FirstAsync(d => d.Id == id, cancellationToken);
  116. }
  117. public TEntity Get(string id)
  118. {
  119. return Db.Queryable<TEntity>().First(d => d.Id == id);
  120. }
  121. public TEntity Get(Expression<Func<TEntity, bool>> predicate)
  122. {
  123. return Db.Queryable<TEntity>().First(predicate);
  124. }
  125. public async Task<TEntity?> GetAsync(Expression<Func<TEntity, bool>> predicate, CancellationToken cancellationToken = default)
  126. {
  127. return await Db.Queryable<TEntity>().FirstAsync(predicate, cancellationToken);
  128. }
  129. public async Task<TEntity?> GetAsync(Expression<Func<TEntity, bool>> predicate, bool isDesc, Expression<Func<TEntity, object>> orderBy, CancellationToken cancellationToken = default)
  130. {
  131. if (isDesc)
  132. return await Db.Queryable<TEntity>().OrderBy(orderBy, OrderByType.Desc).FirstAsync(predicate, cancellationToken);
  133. else
  134. return await Db.Queryable<TEntity>().OrderBy(orderBy, OrderByType.Asc).FirstAsync(predicate, cancellationToken);
  135. }
  136. public async Task<List<TEntity>> QueryAsync(Expression<Func<TEntity, bool>>? predicate = null, params (bool isWhere, Expression<Func<TEntity, bool>> expression)[] whereIfs)
  137. {
  138. var query = Db.Queryable<TEntity>().Where(predicate ??= d => true);
  139. if (whereIfs.Any())
  140. {
  141. foreach (var whereIf in whereIfs)
  142. {
  143. query = query.WhereIF(whereIf.isWhere, whereIf.expression);
  144. }
  145. }
  146. return await query.ToListAsync();
  147. }
  148. public Task<bool> AnyAsync(CancellationToken cancellationToken = default) => Db.Queryable<TEntity>().AnyAsync();
  149. public Task<bool> AnyAsync(Expression<Func<TEntity, bool>> predicate, CancellationToken cancellationToken = default) =>
  150. Db.Queryable<TEntity>().AnyAsync(predicate, cancellationToken);
  151. public Task<int> CountAsync(Expression<Func<TEntity, bool>> predicate, CancellationToken cancellationToken = default) =>
  152. Db.Queryable<TEntity>().CountAsync(predicate, cancellationToken);
  153. public ISugarQueryable<TEntity> Queryable(bool permissionVerify = false, bool includeDeleted = false)
  154. {
  155. if (includeDeleted)
  156. Db.QueryFilter.Clear();
  157. var query = Db.Queryable<TEntity>();
  158. if (permissionVerify)
  159. query = query.DataPermissionFiltering(_dataPermissionFilterBuilder);
  160. return query;
  161. }
  162. public IUpdateable<TEntity> Updateable() => Db.Updateable<TEntity>();
  163. public IUpdateable<TEntity> Updateable(TEntity entity) => Db.Updateable(entity);
  164. public IUpdateable<TEntity> Updateable(List<TEntity> entities) => Db.Updateable(entities);
  165. public IDeleteable<TEntity> Removeable() => Db.Deleteable<TEntity>();
  166. public UpdateNavTaskInit<TEntity, TEntity> UpdateNav(TEntity entity) => Db.UpdateNav(entity);
  167. public UpdateNavTaskInit<TEntity, TEntity> UpdateNav(TEntity entity, UpdateNavRootOptions options) => Db.UpdateNav(entity, options);
  168. public UpdateNavTaskInit<TEntity, TEntity> UpdateNav(List<TEntity> entities) => Db.UpdateNav(entities);
  169. public UpdateNavTaskInit<TEntity, TEntity> UpdateNav(List<TEntity> entities, UpdateNavRootOptions options) => Db.UpdateNav(entities, options);
  170. public InsertNavTaskInit<TEntity, TEntity> AddNav(TEntity entity)
  171. {
  172. var dataPermissionManager = _dataPermissionFilterBuilder.DataPermissionManager;
  173. entity.InitDatePermission(dataPermissionManager);
  174. return Db.InsertNav(entity);
  175. }
  176. public InsertNavTaskInit<TEntity, TEntity> AddNav(TEntity entity, InsertNavRootOptions options)
  177. {
  178. entity.InitDatePermission(_dataPermissionFilterBuilder.DataPermissionManager);
  179. return Db.InsertNav(entity, options);
  180. }
  181. public InsertNavTaskInit<TEntity, TEntity> AddNav(List<TEntity> entities)
  182. {
  183. entities.ForEach(d => d.InitDatePermission(_dataPermissionFilterBuilder.DataPermissionManager));
  184. return Db.InsertNav(entities);
  185. }
  186. public InsertNavTaskInit<TEntity, TEntity> AddNav(List<TEntity> entities, InsertNavRootOptions options)
  187. {
  188. entities.ForEach(d => d.InitDatePermission(_dataPermissionFilterBuilder.DataPermissionManager));
  189. return Db.InsertNav(entities, options);
  190. }
  191. public DeleteNavTaskInit<TEntity, TEntity> RemoveNav(TEntity entity) => Db.DeleteNav(entity);
  192. public DeleteNavTaskInit<TEntity, TEntity> RemoveNav(List<TEntity> entities) => Db.DeleteNav(entities);
  193. public DeleteNavTaskInit<TEntity, TEntity> RemoveNav(Expression<Func<TEntity, bool>> predicate) => Db.DeleteNav(predicate);
  194. public ISugarQueryable<TEntity> UnionAll(params ISugarQueryable<TEntity>[] queryables) => Db.UnionAll(queryables);
  195. /// <summary>
  196. /// 基础分页
  197. /// </summary>
  198. /// <param name="predicate"></param>
  199. /// <param name="orderByCreator"></param>
  200. /// <param name="pageIndex"></param>
  201. /// <param name="pageSize"></param>
  202. /// <param name="permissionVerify"></param>
  203. /// <returns></returns>
  204. public async Task<(int Total, List<TEntity> Items)> QueryPagedAsync(
  205. Expression<Func<TEntity, bool>> predicate,
  206. Func<ISugarQueryable<TEntity>, ISugarQueryable<TEntity>> orderByCreator,
  207. int pageIndex,
  208. int pageSize,
  209. bool permissionVerify = false,
  210. params (bool isWhere, Expression<Func<TEntity, bool>> expression)[] whereIfs)
  211. {
  212. RefAsync<int> total = 0;
  213. var query = Db.Queryable<TEntity>().Where(predicate);
  214. if (permissionVerify)
  215. query = query.DataPermissionFiltering(_dataPermissionFilterBuilder);
  216. if (whereIfs.Any())
  217. {
  218. foreach (var whereIf in whereIfs)
  219. {
  220. query = query.WhereIF(whereIf.isWhere, whereIf.expression);
  221. }
  222. }
  223. var items = await orderByCreator(query).ToPageListAsync(pageIndex, pageSize, total);
  224. return (total.Value, items);
  225. }
  226. public async Task<(int Total, List<TEntity> Items)> QueryPagedAsync(
  227. Expression<Func<TEntity, bool>> predicate,
  228. int pageIndex,
  229. int pageSize,
  230. Func<ISugarQueryable<TEntity>, ISugarQueryable<TEntity>>? includes = null,
  231. Func<ISugarQueryable<TEntity>, ISugarQueryable<TEntity>>? orderByCreator = null,
  232. bool permissionVerify = false,
  233. params (bool isWhere, Expression<Func<TEntity, bool>> expression)[] whereIfs)
  234. {
  235. RefAsync<int> total = 0;
  236. var query = Db.Queryable<TEntity>().Where(predicate);
  237. if (permissionVerify)
  238. query = query.DataPermissionFiltering(_dataPermissionFilterBuilder);
  239. if (includes is not null)
  240. query = includes(query);
  241. if (whereIfs.Any())
  242. {
  243. foreach (var whereIf in whereIfs)
  244. {
  245. query = query.WhereIF(whereIf.isWhere, whereIf.expression);
  246. }
  247. }
  248. if (orderByCreator is not null)
  249. query = orderByCreator(query);
  250. var items = await query.ToPageListAsync(pageIndex, pageSize, total);
  251. return (total.Value, items);
  252. }
  253. public async Task<List<TEntity>> QueryExtAsync(
  254. Expression<Func<TEntity, bool>> predicate,
  255. Func<ISugarQueryable<TEntity>, ISugarQueryable<TEntity>>? includes = null,
  256. Func<ISugarQueryable<TEntity>, ISugarQueryable<TEntity>>? orderByCreator = null,
  257. bool permissionVerify = false,
  258. params (bool isWhere, Expression<Func<TEntity, bool>> expression)[] whereIfs)
  259. {
  260. var query = Db.Queryable<TEntity>().Where(predicate);
  261. if (permissionVerify)
  262. query = query.DataPermissionFiltering(_dataPermissionFilterBuilder);
  263. if (includes is not null)
  264. query = includes(query);
  265. if (whereIfs.Any())
  266. {
  267. foreach (var whereIf in whereIfs)
  268. {
  269. query = query.WhereIF(whereIf.isWhere, whereIf.expression);
  270. }
  271. }
  272. if (orderByCreator is not null)
  273. query = orderByCreator(query);
  274. return await query.ToListAsync();
  275. }
  276. public async Task<List<TEntity>> QueryExtAsync(
  277. Expression<Func<TEntity, bool>> predicate,
  278. Func<ISugarQueryable<TEntity>, ISugarQueryable<TEntity>> includes,
  279. bool permissionVerify = false)
  280. {
  281. var query = Db.Queryable<TEntity>().Where(predicate);
  282. if (permissionVerify)
  283. query = query.DataPermissionFiltering(_dataPermissionFilterBuilder);
  284. query = includes(query);
  285. return await query.ToListAsync();
  286. }
  287. public async Task<TEntity> GetExtAsync(
  288. Expression<Func<TEntity, bool>> predicate,
  289. Func<ISugarQueryable<TEntity>, ISugarQueryable<TEntity>> includes,
  290. bool permissionVerify = false)
  291. {
  292. var query = Db.Queryable<TEntity>();
  293. if (permissionVerify)
  294. query = query.DataPermissionFiltering(_dataPermissionFilterBuilder);
  295. query = includes(query);
  296. return await query.FirstAsync(predicate);
  297. }
  298. public async Task<TEntity> GetExtAsync(string id, Func<ISugarQueryable<TEntity>, ISugarQueryable<TEntity>> includes, bool permissionVerify = false)
  299. {
  300. var query = Db.Queryable<TEntity>();
  301. if (permissionVerify)
  302. query = query.DataPermissionFiltering(_dataPermissionFilterBuilder);
  303. query = includes(query);
  304. return await query.FirstAsync(d => d.Id == id);
  305. }
  306. public async Task UpdateAsync(TEntity entity, bool ignoreNullColumns = true, CancellationToken cancellationToken = default)
  307. {
  308. await Db.Updateable(entity)
  309. .IgnoreColumns(ignoreAllNullColumns: ignoreNullColumns)
  310. .IgnoreColumns(d => d.CreationTime)
  311. .ExecuteCommandAsync(cancellationToken);
  312. }
  313. }
  314. }