From 6e84e370ee14744325de98487399a987c778e77a Mon Sep 17 00:00:00 2001 From: hailin Date: Thu, 25 Dec 2025 22:51:32 -0800 Subject: [PATCH] =?UTF-8?q?fix(authorization-service):=20=E4=BD=BF?= =?UTF-8?q?=E7=94=A8=20accountSequence=20=E6=9B=BF=E4=BB=A3=20userId=20?= =?UTF-8?q?=E6=9F=A5=E8=AF=A2=E5=9B=A2=E9=98=9F=E7=BB=9F=E8=AE=A1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 问题:planting-service 发送的 PlantingOrderPaid 事件中的 userId 是 订单表的自增主键(如 15),而不是 referral-service 中的真实 user_id (如 25122600006)。这导致 handleTreePlanted 方法查询团队统计时 返回 null,社区权益无法被自动激活。 修复:改用事件中的 accountSequence 字段查询团队统计,因为 accountSequence 是跨服务一致的用户标识。 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Opus 4.5 --- .../kafka/event-consumer.controller.ts | 32 ++++++++++++------- 1 file changed, 21 insertions(+), 11 deletions(-) diff --git a/backend/services/authorization-service/src/infrastructure/kafka/event-consumer.controller.ts b/backend/services/authorization-service/src/infrastructure/kafka/event-consumer.controller.ts index 49659986..2fe1bd01 100644 --- a/backend/services/authorization-service/src/infrastructure/kafka/event-consumer.controller.ts +++ b/backend/services/authorization-service/src/infrastructure/kafka/event-consumer.controller.ts @@ -19,6 +19,7 @@ import { EventAckPublisher } from './event-ack.publisher' interface TreePlantedPayload { userId: string + accountSequence?: string treeCount: number provinceCode?: string cityCode?: string @@ -195,31 +196,40 @@ export class EventConsumerController { * 处理认种事件 * 1. 检查用户是否有待激活的授权(初始考核) * 2. 更新正在进行的月度考核进度 + * + * 注意:planting-service 发送的 userId 是订单表的自增主键,不是 referral-service 的 user_id + * 因此必须使用 accountSequence 来查询团队统计 */ private async handleTreePlanted(payload: TreePlantedPayload): Promise { - const { userId, treeCount } = payload - this.logger.log(`[PLANTING] Processing tree planted event: userId=${userId}, count=${treeCount}`) + const { userId, accountSequence, treeCount } = payload + this.logger.log(`[PLANTING] Processing tree planted event: userId=${userId}, accountSequence=${accountSequence}, count=${treeCount}`) + + // 优先使用 accountSequence 查询,因为 userId 可能是 planting-service 的订单主键 + if (!accountSequence) { + this.logger.warn(`[PLANTING] No accountSequence in payload, skipping. userId=${userId}`) + return + } try { - // 1. 获取用户团队统计 - const teamStats = await this.statsRepository.findByUserId(userId) + // 1. 使用 accountSequence 获取用户团队统计 + const teamStats = await this.statsRepository.findByAccountSequence(accountSequence) if (!teamStats) { - this.logger.warn(`[PLANTING] No team stats found for user ${userId}, skipping`) + this.logger.warn(`[PLANTING] No team stats found for accountSequence ${accountSequence}, skipping`) return } // 使用下级团队认种数(不含自己)进行考核判断 const subordinateTreeCount = teamStats.subordinateTeamPlantingCount - this.logger.debug(`[PLANTING] User ${userId} subordinate team planting count: ${subordinateTreeCount}`) + this.logger.debug(`[PLANTING] User ${accountSequence} subordinate team planting count: ${subordinateTreeCount}`) // 2. 获取用户所有授权 const authorizations = await this.authorizationRepository.findByUserId( - UserId.create(userId, teamStats.accountSequence), + UserId.create(teamStats.userId, accountSequence), ) // 3. 处理每个授权(如果有的话) if (authorizations.length === 0) { - this.logger.debug(`[PLANTING] User ${userId} has no authorizations, skipping individual auth processing`) + this.logger.debug(`[PLANTING] User ${accountSequence} has no authorizations, skipping individual auth processing`) } for (const auth of authorizations) { @@ -232,7 +242,7 @@ export class EventConsumerController { this.logger.debug(`[PLANTING] Checking initial target: ${subordinateTreeCount}/${initialTarget}`) if (subordinateTreeCount >= initialTarget) { - this.logger.log(`[PLANTING] User ${userId} reached initial target for ${auth.roleType}, activating benefit`) + this.logger.log(`[PLANTING] User ${accountSequence} reached initial target for ${auth.roleType}, activating benefit`) auth.activateBenefit() await this.authorizationRepository.save(auth) await this.eventPublisher.publishAll(auth.domainEvents) @@ -258,9 +268,9 @@ export class EventConsumerController { // 通过监听 referral.team-statistics.events 事件来触发升级检查 // 这样确保在 referral-service 完成统计更新后再进行检查,避免竞态条件 - this.logger.log(`[PLANTING] Completed processing tree planted event for user ${userId}`) + this.logger.log(`[PLANTING] Completed processing tree planted event for accountSequence ${accountSequence}`) } catch (error) { - this.logger.error(`[PLANTING] Error processing tree planted for user ${userId}:`, error) + this.logger.error(`[PLANTING] Error processing tree planted for accountSequence ${accountSequence}:`, error) throw error } }