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