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:
parent
a7c36b5ee1
commit
7d95a35204
|
|
@ -1,6 +1,6 @@
|
|||
export class UpdateTeamStatisticsCommand {
|
||||
constructor(
|
||||
public readonly userId: bigint,
|
||||
public readonly accountSequence: string, // 格式: D + YYMMDD + 5位序号,跨服务关联标识
|
||||
public readonly plantingCount: number,
|
||||
public readonly provinceCode: string,
|
||||
public readonly cityCode: string,
|
||||
|
|
|
|||
|
|
@ -82,8 +82,9 @@ export class PlantingCreatedHandler implements OnModuleInit {
|
|||
|
||||
try {
|
||||
// 步骤1:更新团队统计
|
||||
// 使用 accountSequence 作为跨服务关联标识
|
||||
const command = new UpdateTeamStatisticsCommand(
|
||||
BigInt(event.data.userId),
|
||||
event.data.accountSequence,
|
||||
event.data.treeCount,
|
||||
event.data.provinceCode,
|
||||
event.data.cityCode,
|
||||
|
|
@ -92,7 +93,7 @@ export class PlantingCreatedHandler implements OnModuleInit {
|
|||
await this.teamStatisticsService.handlePlantingEvent(command);
|
||||
|
||||
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
|
||||
|
|
|
|||
|
|
@ -30,15 +30,17 @@ export class TeamStatisticsService {
|
|||
* 处理认种事件 - 更新用户及其所有上级的团队统计
|
||||
*/
|
||||
async handlePlantingEvent(command: UpdateTeamStatisticsCommand): Promise<void> {
|
||||
// 获取用户的推荐关系
|
||||
const relationship = await this.referralRepo.findByUserId(command.userId);
|
||||
// 使用 accountSequence 获取用户的推荐关系
|
||||
const relationship = await this.referralRepo.findByAccountSequence(command.accountSequence);
|
||||
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;
|
||||
}
|
||||
|
||||
const userId = relationship.userId;
|
||||
|
||||
// 更新用户自己的个人认种统计
|
||||
const userStats = await this.teamStatsRepo.findByUserId(command.userId);
|
||||
const userStats = await this.teamStatsRepo.findByUserId(userId);
|
||||
if (userStats) {
|
||||
userStats.addPersonalPlanting(command.plantingCount, command.provinceCode, command.cityCode);
|
||||
await this.teamStatsRepo.save(userStats);
|
||||
|
|
@ -69,7 +71,7 @@ export class TeamStatisticsService {
|
|||
countDelta: command.plantingCount,
|
||||
provinceCode: command.provinceCode,
|
||||
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);
|
||||
|
||||
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}`,
|
||||
);
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Reference in New Issue