032140-URS团队等级更新逻辑一致性修复.md 6.4 KB

URS团队等级更新逻辑一致性修复

时间: 2025年07月03日 21:40:00 CST
任务: 修复URS团队等级更新逻辑不一致问题
状态: ✅ 完成

问题描述

在检查URS团队等级更新逻辑时发现了一个严重的数据不一致问题:

问题根源

  1. 计算逻辑不一致UrsTalentLogic::calculateTeamStats()方法计算出20代的promotion_count
  2. 保存逻辑错误UrsUserTalent::updateTeamStats()方法只保存前3代的总和到数据库
  3. 等级计算使用错误数据calculateTalentLevel()使用20代的promotion_count进行等级计算,但数据库中保存的是3代总和

影响范围

  • 达人等级计算不准确
  • 团队统计数据不一致
  • 可能导致用户等级升级异常

修复方案

1. 修复UrsUserTalent模型的updateTeamStats方法

修改前

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代总人数
}

2. 更新UrsTalentLogic中的调用

修改前

$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代总人数
);

3. 修复UrsReferralService中的逻辑

将直接使用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();

4. 修复UrsTalentService中的逻辑

将直接使用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();

5. 更新模型方法

修复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()方法正确

验证要点

  1. 20代统计正确promotion_count(62) > 前三代总和(23),说明20代统计在工作
  2. 数据一致性:所有更新方法产生的数据完全一致
  3. 模型方法正确getTotalTeamCount()返回20代总人数,getThreeGenTeamCount()返回前三代总和

文件变更

修改的文件

  1. app/Module/UrsPromotion/Models/UrsUserTalent.php

    • 修改updateTeamStats()方法签名,增加promotionCount参数
    • 修改getTotalTeamCount()方法,返回20代总人数
    • 新增getThreeGenTeamCount()方法,返回前三代总和
  2. app/Module/UrsPromotion/Logics/UrsTalentLogic.php

    • 修改updateTeamStats()调用,传递20代总人数
  3. app/Module/UrsPromotion/Services/UrsReferralService.php

    • 修改updateReferrerStats()方法,使用模型方法更新统计
  4. app/Module/UrsPromotion/Services/UrsTalentService.php

    • 修改团队统计更新逻辑,使用模型方法确保一致性

新增的文件

  1. app/Console/Commands/TestUrsTeamStatsConsistencyCommand.php
    • 团队统计一致性测试命令

技术改进

1. 数据一致性

  • 所有团队统计更新都通过统一的模型方法
  • 确保20代总人数正确保存到数据库
  • 消除了计算逻辑与保存逻辑的不一致

2. 代码可维护性

  • 统一使用模型方法,避免直接数据库操作
  • 清晰的方法命名,区分20代和3代统计
  • 完善的测试验证机制

3. 业务逻辑正确性

  • 达人等级计算基于正确的20代团队统计
  • 保持向后兼容,不影响现有功能
  • 提供了前三代统计的独立访问方法

影响评估

正面影响

  1. 数据准确性提升:达人等级计算基于正确的20代统计
  2. 系统稳定性增强:消除了数据不一致导致的潜在问题
  3. 业务逻辑正确:团队统计真正反映20代关系深度

风险控制

  1. 向后兼容:保留了前三代统计的访问方法
  2. 渐进式修复:不影响现有数据,只修复新的计算逻辑
  3. 充分测试:通过测试命令验证修复效果

总结

成功修复了URS团队等级更新逻辑中的数据不一致问题,确保了:

  • 20代团队统计数据的正确计算和保存
  • 达人等级计算基于准确的团队数据
  • 所有相关服务和逻辑的一致性
  • 系统的稳定性和可维护性

修复后的系统能够正确处理20代团队关系统计,为达人等级升级提供准确的数据支持。