| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173 |
- <?php
- namespace App\Console\Commands;
- use App\Module\UrsPromotion\Models\UrsUserTalent;
- use App\Module\UrsPromotion\Logics\UrsTalentLogic;
- use App\Module\UrsPromotion\Services\UrsTalentService;
- use App\Module\UrsPromotion\Services\UrsUserMappingService;
- use Illuminate\Console\Command;
- /**
- * 测试URS团队统计一致性命令
- *
- * 用于验证团队等级更新逻辑修复后的一致性
- */
- class TestUrsTeamStatsConsistencyCommand extends Command
- {
- /**
- * 命令签名
- */
- protected $signature = 'test:urs-team-stats-consistency {user_id? : 农场用户ID}';
- /**
- * 命令描述
- */
- protected $description = '测试URS团队统计数据的一致性';
- /**
- * 执行命令
- */
- public function handle()
- {
- $userId = $this->argument('user_id');
-
- if ($userId) {
- $this->testSingleUser((int) $userId);
- } else {
- $this->testRandomUsers();
- }
- }
- /**
- * 测试单个用户
- */
- private function testSingleUser(int $userId): void
- {
- $this->info("=== 测试用户 {$userId} 的团队统计一致性 ===");
- try {
- // 获取用户的URS映射
- $ursUserId = UrsUserMappingService::getMappingUrsUserId($userId);
- if (!$ursUserId) {
- $this->error("用户 {$userId} 没有URS映射关系");
- return;
- }
- $this->info("URS用户ID: {$ursUserId}");
- // 获取更新前的数据
- $beforeTalent = UrsUserTalent::where('user_id', $userId)->first();
- if ($beforeTalent) {
- $this->info("更新前数据:");
- $this->displayTalentStats($beforeTalent);
- } else {
- $this->info("用户暂无达人记录");
- }
- // 使用UrsTalentLogic更新
- $this->info("\n使用UrsTalentLogic更新...");
- $talentLogic = new UrsTalentLogic();
- $logicResult = $talentLogic->updateUserTalent($userId);
- if ($logicResult) {
- $this->info("UrsTalentLogic更新成功:");
- $this->displayTalentStats($logicResult);
- } else {
- $this->error("UrsTalentLogic更新失败");
- return;
- }
- // 使用UrsTalentService更新
- $this->info("\n使用UrsTalentService更新...");
- $serviceResult = UrsTalentService::updateTalentLevel($userId);
- $this->info("UrsTalentService更新成功:");
- $this->info("等级: {$serviceResult->talentLevel}");
- $this->info("直推: {$serviceResult->directCount}");
- $this->info("团队总数: {$serviceResult->promotionCount}");
- // 验证一致性
- $finalTalent = UrsUserTalent::where('user_id', $userId)->first();
- $this->info("\n=== 一致性验证 ===");
-
- $isConsistent = true;
-
- if ($logicResult->promotion_count !== $finalTalent->promotion_count) {
- $this->error("promotion_count不一致: Logic={$logicResult->promotion_count}, Final={$finalTalent->promotion_count}");
- $isConsistent = false;
- }
-
- if ($logicResult->direct_count !== $finalTalent->direct_count) {
- $this->error("direct_count不一致: Logic={$logicResult->direct_count}, Final={$finalTalent->direct_count}");
- $isConsistent = false;
- }
-
- if ($logicResult->talent_level !== $finalTalent->talent_level) {
- $this->error("talent_level不一致: Logic={$logicResult->talent_level}, Final={$finalTalent->talent_level}");
- $isConsistent = false;
- }
- if ($isConsistent) {
- $this->info("✅ 数据一致性验证通过");
- } else {
- $this->error("❌ 数据一致性验证失败");
- }
- // 测试模型方法
- $this->info("\n=== 模型方法测试 ===");
- $this->info("getTotalTeamCount(): {$finalTalent->getTotalTeamCount()}");
- $this->info("getThreeGenTeamCount(): {$finalTalent->getThreeGenTeamCount()}");
- $this->info("promotion_count字段: {$finalTalent->promotion_count}");
- if ($finalTalent->getTotalTeamCount() === $finalTalent->promotion_count) {
- $this->info("✅ getTotalTeamCount()方法正确");
- } else {
- $this->error("❌ getTotalTeamCount()方法不正确");
- }
- } catch (\Exception $e) {
- $this->error("测试失败: " . $e->getMessage());
- $this->error("错误详情: " . $e->getTraceAsString());
- }
- }
- /**
- * 测试随机用户
- */
- private function testRandomUsers(): void
- {
- $this->info("=== 测试随机用户的团队统计一致性 ===");
- // 获取有达人记录的用户
- $talents = UrsUserTalent::limit(3)->get();
-
- if ($talents->isEmpty()) {
- $this->warn("没有找到达人记录,无法进行测试");
- return;
- }
- foreach ($talents as $talent) {
- $this->info("\n" . str_repeat('-', 50));
- $this->testSingleUser($talent->user_id);
- }
- }
- /**
- * 显示达人统计信息
- */
- private function displayTalentStats(UrsUserTalent $talent): void
- {
- $this->table(['字段', '值'], [
- ['用户ID', $talent->user_id],
- ['达人等级', $talent->talent_level],
- ['直推人数', $talent->direct_count],
- ['间推人数', $talent->indirect_count],
- ['三推人数', $talent->third_count],
- ['团队总人数(promotion_count)', $talent->promotion_count],
- ['前三代总和', $talent->direct_count + $talent->indirect_count + $talent->third_count],
- ['getTotalTeamCount()', $talent->getTotalTeamCount()],
- ['getThreeGenTeamCount()', $talent->getThreeGenTeamCount()],
- ]);
- }
- }
|