From 723bfd806486bb870f0003f2cfbf61476a4e70f6 Mon Sep 17 00:00:00 2001 From: sunpengfei <i@angelzzz.com> Date: 星期一, 04 八月 2025 16:59:09 +0800 Subject: [PATCH] pref:资源接口 --- FlexJobApi.Core/Utils/PagedListUtils/PagedListUtils.cs | 91 ++++++++++++++++++++++++++++++++++++++++++++- 1 files changed, 89 insertions(+), 2 deletions(-) diff --git a/FlexJobApi.Core/Utils/PagedListUtils/PagedListUtils.cs b/FlexJobApi.Core/Utils/PagedListUtils/PagedListUtils.cs index 32c3b55..3ff720c 100644 --- a/FlexJobApi.Core/Utils/PagedListUtils/PagedListUtils.cs +++ b/FlexJobApi.Core/Utils/PagedListUtils/PagedListUtils.cs @@ -1,12 +1,99 @@ -锘縰sing System; +锘縰sing 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; + + foreach (var order in orders) + { + if (string.IsNullOrEmpty(order.Property)) continue; + + // 鑾峰彇鎺掑簭瀛楁鐨勫睘鎬т俊鎭� + var propertyInfo = entityType.GetProperty(order.Property); + 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.Ascending + ? "OrderBy" + : "OrderByDescending"; + } + else + { + // 浜屾鍙婁互鍚庢帓搴� + methodName = order.Order == EnumPagedListOrder.Ascending + ? "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; + } } } -- Gitblit v1.9.1