From 1fbb88f773292702a9157ceadd057d158af47400 Mon Sep 17 00:00:00 2001 From: hailin Date: Tue, 13 Jan 2026 20:29:53 -0800 Subject: [PATCH] fix(contribution): use setDirectReferralAdoptedCount for accurate count update Changed updateReferrerUnlockStatus to: 1. Create account if not exists (for full-reset scenarios) 2. Use setDirectReferralAdoptedCount instead of increment loop 3. This ensures the count is always accurate regardless of processing order Co-Authored-By: Claude Opus 4.5 --- .../contribution-calculation.service.ts | 21 ++++++++++--------- 1 file changed, 11 insertions(+), 10 deletions(-) diff --git a/backend/services/contribution-service/src/application/services/contribution-calculation.service.ts b/backend/services/contribution-service/src/application/services/contribution-calculation.service.ts index 74185c3a..dc82abf5 100644 --- a/backend/services/contribution-service/src/application/services/contribution-calculation.service.ts +++ b/backend/services/contribution-service/src/application/services/contribution-calculation.service.ts @@ -400,28 +400,29 @@ export class ContributionCalculationService { * 更新上线的解锁状态(直推用户认种后) */ private async updateReferrerUnlockStatus(referrerAccountSequence: string): Promise { - const account = await this.contributionAccountRepository.findByAccountSequence(referrerAccountSequence); - if (!account) return; + let account = await this.contributionAccountRepository.findByAccountSequence(referrerAccountSequence); - // 重新计算直推认种用户数 + // 如果账户不存在,先创建一个 + if (!account) { + account = ContributionAccountAggregate.create(referrerAccountSequence); + } + + // 重新计算直推认种用户数(从数据库实时查询) const directReferralAdoptedCount = await this.syncedDataRepository.getDirectReferralAdoptedCount( referrerAccountSequence, ); - // 更新解锁状态 + // 直接设置计数值(不是增量更新),确保数据一致性 const currentCount = account.directReferralAdoptedCount; - if (directReferralAdoptedCount > currentCount) { - // 需要增量更新 - for (let i = currentCount; i < directReferralAdoptedCount; i++) { - account.incrementDirectReferralAdoptedCount(); - } + if (directReferralAdoptedCount !== currentCount) { + account.setDirectReferralAdoptedCount(directReferralAdoptedCount); await this.contributionAccountRepository.save(account); // 发布账户更新事件到 outbox(用于 CDC 同步到 mining-admin-service) await this.publishContributionAccountUpdatedEvent(account); this.logger.debug( - `Updated referrer ${referrerAccountSequence} unlock status: level=${account.unlockedLevelDepth}, bonus=${account.unlockedBonusTiers}`, + `Updated referrer ${referrerAccountSequence} unlock status: count=${directReferralAdoptedCount}, level=${account.unlockedLevelDepth}, bonus=${account.unlockedBonusTiers}`, ); } }