时间: 2025年07月03日 21:40:00 CST
任务: 修复URS团队等级更新逻辑不一致问题
状态: ✅ 完成
在检查URS团队等级更新逻辑时发现了一个严重的数据不一致问题:
UrsTalentLogic::calculateTeamStats()方法计算出20代的promotion_countUrsUserTalent::updateTeamStats()方法只保存前3代的总和到数据库calculateTalentLevel()使用20代的promotion_count进行等级计算,但数据库中保存的是3代总和修改前:
public function updateTeamStats(int $directCount, int $indirectCount, int $thirdCount): void
{
$this->direct_count = $directCount;
$this->indirect_count = $indirectCount;
$this->third_count = $thirdCount;
$this->promotion_count = $directCount + $indirectCount + $thirdCount; // 只有3代总和
}
修改后:
public function updateTeamStats(int $directCount, int $indirectCount, int $thirdCount, int $promotionCount): void
{
$this->direct_count = $directCount;
$this->indirect_count = $indirectCount;
$this->third_count = $thirdCount;
$this->promotion_count = $promotionCount; // 正确的20代总人数
}
修改前:
$talent->updateTeamStats(
$teamStats['direct_count'],
$teamStats['indirect_count'],
$teamStats['third_count']
);
修改后:
$talent->updateTeamStats(
$teamStats['direct_count'],
$teamStats['indirect_count'],
$teamStats['third_count'],
$teamStats['promotion_count'] // 传递20代总人数
);
将直接使用updateOrCreate的方式改为使用模型方法,确保逻辑一致性:
// 获取或创建用户达人记录
$talent = UrsUserTalent::firstOrCreate(
['user_id' => $user_id],
[
'talent_level' => 0,
'direct_count' => 0,
'indirect_count' => 0,
'third_count' => 0,
'promotion_count' => 0,
]
);
// 使用模型方法更新团队统计,确保逻辑一致性
$talent->updateTeamStats($directCount, $indirectCount, $thirdCount, $totalCount);
$talent->save();
将直接使用update的方式改为使用模型方法:
// 更新团队统计数据(使用模型方法确保逻辑一致性)
$talent->updateTeamStats(
$stats['direct_count'],
$stats['indirect_count'],
$stats['third_count'],
$stats['total_team_count'] // 20代总人数
);
// 更新达人等级
if ($newLevel > $oldLevel) {
$talent->upgradeTalentLevel($newLevel);
}
$talent->save();
修复getTotalTeamCount()方法,使其返回正确的20代总人数:
/**
* 获取团队总人数(20代统计)
*/
public function getTotalTeamCount(): int
{
return $this->promotion_count;
}
/**
* 获取前三代团队总人数
*/
public function getThreeGenTeamCount(): int
{
return $this->direct_count + $this->indirect_count + $this->third_count;
}
创建了TestUrsTeamStatsConsistencyCommand命令来验证修复效果。
=== 测试用户 39148 的团队统计一致性 ===
URS用户ID: 1
更新前数据:
- 直推人数: 4
- 间推人数: 11
- 三推人数: 8
- 团队总人数(promotion_count): 59
- 前三代总和: 23
更新后数据:
- 团队总人数(promotion_count): 62
- 前三代总和: 23
- getTotalTeamCount(): 62 ✅
- getThreeGenTeamCount(): 23 ✅
=== 一致性验证 ===
✅ 数据一致性验证通过
✅ getTotalTeamCount()方法正确
promotion_count(62) > 前三代总和(23),说明20代统计在工作getTotalTeamCount()返回20代总人数,getThreeGenTeamCount()返回前三代总和app/Module/UrsPromotion/Models/UrsUserTalent.php
updateTeamStats()方法签名,增加promotionCount参数getTotalTeamCount()方法,返回20代总人数getThreeGenTeamCount()方法,返回前三代总和app/Module/UrsPromotion/Logics/UrsTalentLogic.php
updateTeamStats()调用,传递20代总人数app/Module/UrsPromotion/Services/UrsReferralService.php
updateReferrerStats()方法,使用模型方法更新统计app/Module/UrsPromotion/Services/UrsTalentService.php
app/Console/Commands/TestUrsTeamStatsConsistencyCommand.php
成功修复了URS团队等级更新逻辑中的数据不一致问题,确保了:
修复后的系统能够正确处理20代团队关系统计,为达人等级升级提供准确的数据支持。