SqlSugarRepositoryExtensions.cs 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  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 Hotline.Share;
  8. using Hotline.Share.Requests;
  9. using Hotline.Share.Tools;
  10. using Microsoft.AspNetCore.Http;
  11. using SqlSugar;
  12. using XF.Domain.Entities;
  13. namespace Hotline.Repository.SqlSugar.Extensions
  14. {
  15. public static class SqlSugarRepositoryExtensions
  16. {
  17. public static async Task<(int Total, List<TEntity> Items)> ToPagedListAsync<TEntity>(this ISugarQueryable<TEntity> query, int pageIndex, int pageSize, CancellationToken cancellationToken = default)
  18. where TEntity : class, new()
  19. {
  20. RefAsync<int> total = 0;
  21. var items = await query.ToPageListAsync(pageIndex, pageSize, total, cancellationToken);
  22. return (total.Value, items);
  23. }
  24. public static async Task<(int Total, List<TEntity> Items)> ToPagedListAsync<TEntity>(this ISugarQueryable<TEntity> query, PagedRequest dto, CancellationToken cancellationToken = default)
  25. where TEntity : class, new()
  26. {
  27. RefAsync<int> total = 0;
  28. var items = await query.ToPageListAsync(dto.PageIndex, dto.PageSize, total, cancellationToken);
  29. return (total.Value, items);
  30. }
  31. /// <summary>
  32. /// 分批次查询固定数量
  33. /// </summary>
  34. /// <returns></returns>
  35. public static Task<List<TEntity>> ToFixedListAsync<TEntity>(this ISugarQueryable<TEntity> query, IQueryFixedDto dto, CancellationToken cancellationToken)
  36. where TEntity : class, new()
  37. {
  38. if (dto.QueryCount == 0) dto.QueryCount = 50;
  39. return query.Skip(dto.QueryIndex * dto.QueryCount).Take(dto.QueryCount).ToListAsync(cancellationToken);
  40. }
  41. public static Task<List<TEntity>> ToFixedListAsync<TEntity>(this ISugarQueryable<TEntity> query, int queryIndex, int? queryCount = null, CancellationToken cancellationToken = default)
  42. where TEntity : class, new()
  43. {
  44. if (queryCount is null or 0) queryCount = 50;
  45. return query.Skip(queryIndex * queryCount.Value).Take(queryCount.Value).ToListAsync(cancellationToken);
  46. }
  47. public static Task<List<TEntity>> ToPageListWithoutTotalAsync<TEntity>(this ISugarQueryable<TEntity> query, PagedRequest request, CancellationToken cancellationToken)
  48. where TEntity : class, new()
  49. {
  50. if (request.PageSize > 500) request.PageSize = 500;
  51. return query.Skip((request.PageIndex - 1) * request.PageSize).Take(request.PageSize).ToListAsync(cancellationToken);
  52. }
  53. public static Task<List<TEntity>> ToPageListWithoutTotalAsync<TEntity>(this ISugarQueryable<TEntity> query, int pageIndex, int? pageSize = null, CancellationToken cancellationToken = default)
  54. where TEntity : class, new()
  55. {
  56. if (pageSize is null or 0) pageSize = 20;
  57. if (pageSize > 500) pageSize = 500;
  58. return query.Skip((pageIndex - 1) * pageSize.Value).Take(pageSize.Value).ToListAsync(cancellationToken);
  59. }
  60. }
  61. }