From a8e06e2eda117802dd8280d158b6815069cc78ed Mon Sep 17 00:00:00 2001 From: hailin Date: Tue, 3 Mar 2026 02:44:35 -0800 Subject: [PATCH] =?UTF-8?q?fix(pre-planting):=20=E5=90=88=E5=B9=B6?= =?UTF-8?q?=E9=80=BB=E8=BE=91=E6=94=B9=E4=B8=BA=E6=8C=89=E4=BB=BD=E6=95=B0?= =?UTF-8?q?=E7=B4=AF=E8=AE=A1=EF=BC=8C=E6=94=AF=E6=8C=81=E5=A4=9A=E4=BB=BD?= =?UTF-8?q?=E8=AE=A2=E5=8D=95=E5=90=88=E5=B9=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 问题:用户一笔订单可购买多份(portionCount>1),但 performMerge 按订单条数校验是否够 10 条,导致 6 笔订单共 10 份却报错 "不足 10 笔已支付订单进行合并"。 修复: - performMerge: 遍历 PAID 订单累加 portionCount 直到凑满 10 份 - findPaidOrdersByUserId: 去掉 limit 参数,获取所有 PAID 订单 - PrePlantingMerge.create: 去掉 sourceOrderNos.length === 10 校验 改为 length > 0(份数校验已在 performMerge 完成) Co-Authored-By: Claude Opus 4.6 --- .../pre-planting-application.service.ts | 24 ++++++++++++------- .../pre-planting-merge.aggregate.ts | 4 ++-- .../pre-planting-order.repository.ts | 2 -- 3 files changed, 17 insertions(+), 13 deletions(-) diff --git a/backend/services/planting-service/src/pre-planting/application/services/pre-planting-application.service.ts b/backend/services/planting-service/src/pre-planting/application/services/pre-planting-application.service.ts index 995415d2..0bcf7c9f 100644 --- a/backend/services/planting-service/src/pre-planting/application/services/pre-planting-application.service.ts +++ b/backend/services/planting-service/src/pre-planting/application/services/pre-planting-application.service.ts @@ -574,18 +574,24 @@ export class PrePlantingApplicationService { accountSequence: string, position: import('../../domain/aggregates/pre-planting-position.aggregate').PrePlantingPosition, ) { - // 获取待合并的已支付订单(数量由 PRE_PLANTING_PORTIONS_PER_TREE 决定) - const paidOrders = await this.orderRepo.findPaidOrdersByUserId( - tx, - userId, - PRE_PLANTING_PORTIONS_PER_TREE, - ); + // 获取所有已支付订单,按份数累加取够 PRE_PLANTING_PORTIONS_PER_TREE 份 + // 注意:一笔订单可能包含多份(portionCount > 1),所以不能按订单条数判断 + const allPaidOrders = await this.orderRepo.findPaidOrdersByUserId(tx, userId); - if (paidOrders.length < PRE_PLANTING_PORTIONS_PER_TREE) { - throw new Error(`不足 ${PRE_PLANTING_PORTIONS_PER_TREE} 笔已支付订单进行合并`); + let accumulatedPortions = 0; + const sourceOrders: typeof allPaidOrders = []; + for (const order of allPaidOrders) { + sourceOrders.push(order); + accumulatedPortions += order.portionCount; + if (accumulatedPortions >= PRE_PLANTING_PORTIONS_PER_TREE) break; + } + + if (accumulatedPortions < PRE_PLANTING_PORTIONS_PER_TREE) { + throw new Error( + `不足 ${PRE_PLANTING_PORTIONS_PER_TREE} 份进行合并(当前 ${accumulatedPortions} 份,来自 ${sourceOrders.length} 笔订单)`, + ); } - const sourceOrders = paidOrders.slice(0, PRE_PLANTING_PORTIONS_PER_TREE); const sourceOrderNos = sourceOrders.map((o) => o.orderNo); const mergeNo = this.generateMergeNo(); diff --git a/backend/services/planting-service/src/pre-planting/domain/aggregates/pre-planting-merge.aggregate.ts b/backend/services/planting-service/src/pre-planting/domain/aggregates/pre-planting-merge.aggregate.ts index fd3d2301..68c44d87 100644 --- a/backend/services/planting-service/src/pre-planting/domain/aggregates/pre-planting-merge.aggregate.ts +++ b/backend/services/planting-service/src/pre-planting/domain/aggregates/pre-planting-merge.aggregate.ts @@ -70,8 +70,8 @@ export class PrePlantingMerge { cityCode: string, totalTreesMergedAfter: number, ): PrePlantingMerge { - if (sourceOrderNos.length !== PRE_PLANTING_PORTIONS_PER_TREE) { - throw new Error(`合并需要 ${PRE_PLANTING_PORTIONS_PER_TREE} 个订单,收到 ${sourceOrderNos.length} 个`); + if (sourceOrderNos.length === 0) { + throw new Error('合并至少需要 1 笔源订单'); } const merge = new PrePlantingMerge( diff --git a/backend/services/planting-service/src/pre-planting/infrastructure/repositories/pre-planting-order.repository.ts b/backend/services/planting-service/src/pre-planting/infrastructure/repositories/pre-planting-order.repository.ts index c8baf0dd..457670d8 100644 --- a/backend/services/planting-service/src/pre-planting/infrastructure/repositories/pre-planting-order.repository.ts +++ b/backend/services/planting-service/src/pre-planting/infrastructure/repositories/pre-planting-order.repository.ts @@ -61,12 +61,10 @@ export class PrePlantingOrderRepository { async findPaidOrdersByUserId( tx: Prisma.TransactionClient, userId: bigint, - limit: number, ): Promise { const records = await tx.prePlantingOrder.findMany({ where: { userId, status: PrePlantingOrderStatus.PAID }, orderBy: { createdAt: 'asc' }, - take: limit, }); return records.map((r) => this.toDomain(r)); }