| | |
| | | using System; |
| | | using FlexJobApi.User.Application; |
| | | using Furion.DatabaseAccessor; |
| | | using Furion.FriendlyException; |
| | | using Mapster; |
| | | using System; |
| | | using System.Collections.Generic; |
| | | using System.Linq; |
| | | using System.Linq.Expressions; |
| | | using System.Reflection; |
| | | using System.Text; |
| | | using System.Threading; |
| | | using System.Threading.Tasks; |
| | | |
| | | namespace FlexJobApi.Core |
| | | { |
| | | public class PagedListUtils |
| | | public static class PagedListUtils |
| | | { |
| | | /// <summary> |
| | | /// 查询分页列表数据 |
| | | /// </summary> |
| | | /// <typeparam name="TItem"></typeparam> |
| | | /// <param name="q"></param> |
| | | /// <param name="page"></param> |
| | | /// <param name="cancellationToken"></param> |
| | | /// <returns></returns> |
| | | public static async Task<PagedListQueryResult<TItem>> ToPagedListAsync<TItem>(this IQueryable<TItem> q, PagedListQueryPageModel page, CancellationToken cancellationToken = default) |
| | | where TItem : class, new() |
| | | { |
| | | var pagedList = await q |
| | | .OrderBy(page.OrderInput) |
| | | .ToPagedListAsync(page.Page, page.Rows, cancellationToken); |
| | | var result = new PagedListQueryResult<TItem>(); |
| | | result.PageModel = page.Adapt<PagedListQueryResultPageModel>(); |
| | | result.PageModel.TotalCount = pagedList.TotalCount; |
| | | result.PageModel.TotalPage = pagedList.TotalPages; |
| | | result.Data = pagedList.Items.ToList(); |
| | | return result; |
| | | } |
| | | |
| | | /// <summary> |
| | | /// 排序 |
| | | /// </summary> |
| | | /// <typeparam name="T"></typeparam> |
| | | /// <param name="q"></param> |
| | | /// <param name="orders"></param> |
| | | /// <returns></returns> |
| | | public static IQueryable<T> OrderBy<T>(this IQueryable<T> q, List<PagedListQueryPageModelOrderInput> orders) |
| | | { |
| | | if (orders.IsNull()) return q; |
| | | |
| | | var entityType = typeof(T); |
| | | int index = 0; |
| | | |
| | | var props = entityType.GetProperties(); |
| | | foreach (var order in orders) |
| | | { |
| | | if (string.IsNullOrEmpty(order.Property)) continue; |
| | | |
| | | // 获取排序字段的属性信息 |
| | | var propertyInfo = props.FirstOrDefault(it => it.Name.Equals(order.Property, StringComparison.OrdinalIgnoreCase)); |
| | | if (propertyInfo == null) throw Oops.Oh(EnumErrorCodeType.s404, $"该排序字段{order.Property}"); |
| | | |
| | | // 创建表达式树 |
| | | var parameter = Expression.Parameter(entityType, "x"); |
| | | var propertyAccess = Expression.Property(parameter, propertyInfo); |
| | | var lambda = Expression.Lambda(propertyAccess, parameter); |
| | | |
| | | string methodName; |
| | | if (index == 0) |
| | | { |
| | | // 首次排序 |
| | | methodName = order.Order == EnumPagedListOrder.Asc |
| | | ? "OrderBy" |
| | | : "OrderByDescending"; |
| | | } |
| | | else |
| | | { |
| | | // 二次及以后排序 |
| | | methodName = order.Order == EnumPagedListOrder.Asc |
| | | ? "ThenBy" |
| | | : "ThenByDescending"; |
| | | } |
| | | |
| | | // 调用相应的排序方法 |
| | | var resultExpression = Expression.Call( |
| | | typeof(Queryable), |
| | | methodName, |
| | | [entityType, propertyInfo.PropertyType], |
| | | q.Expression, |
| | | Expression.Quote(lambda) |
| | | ); |
| | | |
| | | q = q.Provider.CreateQuery<T>(resultExpression); |
| | | index++; |
| | | } |
| | | |
| | | return q; |
| | | } |
| | | } |
| | | } |