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)); }