sunpengfei
2025-08-05 5a5a0ed1fa93a83537eb36e69fac5a14994dc103
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
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 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;
        }
    }
}