From d73c98b44e451b743b4a8ea89967813ae26721ed Mon Sep 17 00:00:00 2001 From: zzh <1368735060@qq.com> Date: Wed, 15 Jul 2026 13:45:08 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E5=88=97=E8=A1=A8=E5=88=86?= =?UTF-8?q?=E9=A1=B5=E4=B8=A2Where=EF=BC=9A=E6=94=B9=E7=94=A8GetListAsync?= =?UTF-8?q?=E5=88=86=E9=A1=B5=EF=BC=8C=E7=8A=B6=E6=80=81=E7=AD=9B=E9=80=89?= =?UTF-8?q?=E7=94=9F=E6=95=88=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Cursor --- .../Orders/PlatformStoreOrderService.cs | 8 ++-- .../Warehouse/WarehouseService.Inbound.cs | 8 ++-- .../Warehouse/WarehouseService.Parcel.cs | 8 ++-- .../Shared/RepositoryPageHelper.cs | 48 +++++++++++++++++++ 4 files changed, 60 insertions(+), 12 deletions(-) create mode 100644 src/Oms.Application/Shared/RepositoryPageHelper.cs diff --git a/src/Oms.Application/Services/Orders/PlatformStoreOrderService.cs b/src/Oms.Application/Services/Orders/PlatformStoreOrderService.cs index 11167d7..55c9ff0 100644 --- a/src/Oms.Application/Services/Orders/PlatformStoreOrderService.cs +++ b/src/Oms.Application/Services/Orders/PlatformStoreOrderService.cs @@ -50,12 +50,12 @@ public class PlatformStoreOrderService : ApplicationService var predicate = BuildListPredicate(customerId, input); var pageIndex = OmsDocHelper.NormalizePage(input.Page); var pageSize = OmsDocHelper.NormalizePageSize(input.PageSize); - var entities = await StoreOrderRepository.GetPagedListAsync( + // 避免 Fake GetPagedListAsync(sorting) 丢失 Where:total 正确但 items 返回全量 + var (entities, total) = await RepositoryPageHelper.GetPageByUpdateTimeDescAsync( + StoreOrderRepository, predicate, pageIndex, - pageSize, - new Dictionary { [nameof(SalesStoreOrder.UpdateTime)] = false, [nameof(SalesStoreOrder.Id)] = false }); - var total = await StoreOrderRepository.CountAsync(predicate); + pageSize); var storeIds = entities.Select(x => x.StoreId).Distinct().ToArray(); var stores = storeIds.Length == 0 diff --git a/src/Oms.Application/Services/Warehouse/WarehouseService.Inbound.cs b/src/Oms.Application/Services/Warehouse/WarehouseService.Inbound.cs index 3228673..6471831 100644 --- a/src/Oms.Application/Services/Warehouse/WarehouseService.Inbound.cs +++ b/src/Oms.Application/Services/Warehouse/WarehouseService.Inbound.cs @@ -42,12 +42,12 @@ public partial class WarehouseService var pageIndex = OmsDocHelper.NormalizePage(input.Page); var pageSize = OmsDocHelper.NormalizePageSize(input.PageSize); - var entities = await InboundNoticeRepository.GetPagedListAsync( + // 避免 Fake GetPagedListAsync(sorting) 丢失 Where:total 正确但 items 返回全量 + var (entities, total) = await RepositoryPageHelper.GetPageByUpdateTimeDescAsync( + InboundNoticeRepository, predicate, pageIndex, - pageSize, - new Dictionary { [nameof(CInboundNotice.UpdateTime)] = false, [nameof(CInboundNotice.Id)] = false }); - var total = await InboundNoticeRepository.CountAsync(predicate); + pageSize); var noticeIds = entities.Select(x => x.Id).ToArray(); var boxes = noticeIds.Length == 0 diff --git a/src/Oms.Application/Services/Warehouse/WarehouseService.Parcel.cs b/src/Oms.Application/Services/Warehouse/WarehouseService.Parcel.cs index 194a3c7..ca8c1ab 100644 --- a/src/Oms.Application/Services/Warehouse/WarehouseService.Parcel.cs +++ b/src/Oms.Application/Services/Warehouse/WarehouseService.Parcel.cs @@ -42,12 +42,12 @@ public partial class WarehouseService var pageIndex = OmsDocHelper.NormalizePage(input.Page); var pageSize = OmsDocHelper.NormalizePageSize(input.PageSize); - var entities = await ParcelRepository.GetPagedListAsync( + // 避免 Fake GetPagedListAsync(sorting) 丢失 Where:total 正确但 items 返回全量 + var (entities, total) = await RepositoryPageHelper.GetPageByUpdateTimeDescAsync( + ParcelRepository, predicate, pageIndex, - pageSize, - new Dictionary { [nameof(CParcel.UpdateTime)] = false, [nameof(CParcel.Id)] = false }); - var total = await ParcelRepository.CountAsync(predicate); + pageSize); var parcelIds = entities.Select(x => x.Id).ToArray(); var items = parcelIds.Length == 0 diff --git a/src/Oms.Application/Shared/RepositoryPageHelper.cs b/src/Oms.Application/Shared/RepositoryPageHelper.cs new file mode 100644 index 0000000..00da504 --- /dev/null +++ b/src/Oms.Application/Shared/RepositoryPageHelper.cs @@ -0,0 +1,48 @@ +using System.Linq.Expressions; +using Fake.Domain.Repositories; +using Oms.Domain.Shared; + +namespace Oms.Application.Shared; + +/// +/// 仓储分页辅助。 +/// Fake.SqlSugarCore 的 GetPagedListAsync 在传入 sorting 字典时会丢失 Where 条件 +/// (CountAsync/GetListAsync 正常,导致 total 对、items 却是全量),故改用本方法分页。 +/// +public static class RepositoryPageHelper +{ + public static async Task<(List Items, long Total)> GetPageAsync( + IRepository repository, + Expression> predicate, + int pageIndex, + int pageSize, + Func, IOrderedEnumerable> orderBy) + where TEntity : OmsAuditedAggregateRoot + { + var total = await repository.CountAsync(predicate); + if (total <= 0) + { + return ([], 0); + } + + var matched = await repository.GetListAsync(predicate); + var items = orderBy(matched) + .Skip((pageIndex - 1) * pageSize) + .Take(pageSize) + .ToList(); + return (items, total); + } + + public static Task<(List Items, long Total)> GetPageByUpdateTimeDescAsync( + IRepository repository, + Expression> predicate, + int pageIndex, + int pageSize) + where TEntity : OmsAuditedAggregateRoot => + GetPageAsync( + repository, + predicate, + pageIndex, + pageSize, + q => q.OrderByDescending(x => x.UpdateTime).ThenByDescending(x => x.Id)); +}