fix(referral): 团队统计使用accountSequence替代userId

- UpdateTeamStatisticsCommand 改用 accountSequence
- PlantingCreatedHandler 传递 accountSequence
- TeamStatisticsService 使用 findByAccountSequence 查询
- 修复上家团队认种数不正确的问题

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
hailin 2025-12-14 05:46:11 -08:00
parent a7c36b5ee1
commit 7d95a35204
3 changed files with 12 additions and 9 deletions

View File

@ -1,6 +1,6 @@
export class UpdateTeamStatisticsCommand { export class UpdateTeamStatisticsCommand {
constructor( constructor(
public readonly userId: bigint, public readonly accountSequence: string, // 格式: D + YYMMDD + 5位序号跨服务关联标识
public readonly plantingCount: number, public readonly plantingCount: number,
public readonly provinceCode: string, public readonly provinceCode: string,
public readonly cityCode: string, public readonly cityCode: string,

View File

@ -82,8 +82,9 @@ export class PlantingCreatedHandler implements OnModuleInit {
try { try {
// 步骤1更新团队统计 // 步骤1更新团队统计
// 使用 accountSequence 作为跨服务关联标识
const command = new UpdateTeamStatisticsCommand( const command = new UpdateTeamStatisticsCommand(
BigInt(event.data.userId), event.data.accountSequence,
event.data.treeCount, event.data.treeCount,
event.data.provinceCode, event.data.provinceCode,
event.data.cityCode, event.data.cityCode,
@ -92,7 +93,7 @@ export class PlantingCreatedHandler implements OnModuleInit {
await this.teamStatisticsService.handlePlantingEvent(command); await this.teamStatisticsService.handlePlantingEvent(command);
this.logger.log( this.logger.log(
`Updated team statistics for user ${event.data.userId}, count: ${event.data.treeCount}`, `Updated team statistics for accountSequence ${event.data.accountSequence}, count: ${event.data.treeCount}`,
); );
// 步骤2发送 planting.order.paid 事件给 reward-service // 步骤2发送 planting.order.paid 事件给 reward-service

View File

@ -30,15 +30,17 @@ export class TeamStatisticsService {
* - * -
*/ */
async handlePlantingEvent(command: UpdateTeamStatisticsCommand): Promise<void> { async handlePlantingEvent(command: UpdateTeamStatisticsCommand): Promise<void> {
// 获取用户的推荐关系 // 使用 accountSequence 获取用户的推荐关系
const relationship = await this.referralRepo.findByUserId(command.userId); const relationship = await this.referralRepo.findByAccountSequence(command.accountSequence);
if (!relationship) { if (!relationship) {
this.logger.warn(`User ${command.userId} has no referral relationship`); this.logger.warn(`User with accountSequence ${command.accountSequence} has no referral relationship`);
return; return;
} }
const userId = relationship.userId;
// 更新用户自己的个人认种统计 // 更新用户自己的个人认种统计
const userStats = await this.teamStatsRepo.findByUserId(command.userId); const userStats = await this.teamStatsRepo.findByUserId(userId);
if (userStats) { if (userStats) {
userStats.addPersonalPlanting(command.plantingCount, command.provinceCode, command.cityCode); userStats.addPersonalPlanting(command.plantingCount, command.provinceCode, command.cityCode);
await this.teamStatsRepo.save(userStats); await this.teamStatsRepo.save(userStats);
@ -69,7 +71,7 @@ export class TeamStatisticsService {
countDelta: command.plantingCount, countDelta: command.plantingCount,
provinceCode: command.provinceCode, provinceCode: command.provinceCode,
cityCode: command.cityCode, cityCode: command.cityCode,
fromDirectReferralId: i === 0 ? command.userId : ancestors[0], fromDirectReferralId: i === 0 ? userId : ancestors[0],
}); });
} }
@ -77,7 +79,7 @@ export class TeamStatisticsService {
await this.teamStatsRepo.batchUpdateTeamCounts(updates); await this.teamStatsRepo.batchUpdateTeamCounts(updates);
this.logger.log( this.logger.log(
`Updated team statistics for ${ancestors.length} ancestors of user ${command.userId}`, `Updated team statistics for ${ancestors.length} ancestors of accountSequence ${command.accountSequence}`,
); );
} }