TestUrsTeamStatsConsistencyCommand.php 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. <?php
  2. namespace App\Console\Commands;
  3. use App\Module\UrsPromotion\Models\UrsUserTalent;
  4. use App\Module\UrsPromotion\Logics\UrsTalentLogic;
  5. use App\Module\UrsPromotion\Services\UrsTalentService;
  6. use App\Module\UrsPromotion\Services\UrsUserMappingService;
  7. use Illuminate\Console\Command;
  8. /**
  9. * 测试URS团队统计一致性命令
  10. *
  11. * 用于验证团队等级更新逻辑修复后的一致性
  12. */
  13. class TestUrsTeamStatsConsistencyCommand extends Command
  14. {
  15. /**
  16. * 命令签名
  17. */
  18. protected $signature = 'test:urs-team-stats-consistency {user_id? : 农场用户ID}';
  19. /**
  20. * 命令描述
  21. */
  22. protected $description = '测试URS团队统计数据的一致性';
  23. /**
  24. * 执行命令
  25. */
  26. public function handle()
  27. {
  28. $userId = $this->argument('user_id');
  29. if ($userId) {
  30. $this->testSingleUser((int) $userId);
  31. } else {
  32. $this->testRandomUsers();
  33. }
  34. }
  35. /**
  36. * 测试单个用户
  37. */
  38. private function testSingleUser(int $userId): void
  39. {
  40. $this->info("=== 测试用户 {$userId} 的团队统计一致性 ===");
  41. try {
  42. // 获取用户的URS映射
  43. $ursUserId = UrsUserMappingService::getMappingUrsUserId($userId);
  44. if (!$ursUserId) {
  45. $this->error("用户 {$userId} 没有URS映射关系");
  46. return;
  47. }
  48. $this->info("URS用户ID: {$ursUserId}");
  49. // 获取更新前的数据
  50. $beforeTalent = UrsUserTalent::where('user_id', $userId)->first();
  51. if ($beforeTalent) {
  52. $this->info("更新前数据:");
  53. $this->displayTalentStats($beforeTalent);
  54. } else {
  55. $this->info("用户暂无达人记录");
  56. }
  57. // 使用UrsTalentLogic更新
  58. $this->info("\n使用UrsTalentLogic更新...");
  59. $talentLogic = new UrsTalentLogic();
  60. $logicResult = $talentLogic->updateUserTalent($userId);
  61. if ($logicResult) {
  62. $this->info("UrsTalentLogic更新成功:");
  63. $this->displayTalentStats($logicResult);
  64. } else {
  65. $this->error("UrsTalentLogic更新失败");
  66. return;
  67. }
  68. // 使用UrsTalentService更新
  69. $this->info("\n使用UrsTalentService更新...");
  70. $serviceResult = UrsTalentService::updateTalentLevel($userId);
  71. $this->info("UrsTalentService更新成功:");
  72. $this->info("等级: {$serviceResult->talentLevel}");
  73. $this->info("直推: {$serviceResult->directCount}");
  74. $this->info("团队总数: {$serviceResult->promotionCount}");
  75. // 验证一致性
  76. $finalTalent = UrsUserTalent::where('user_id', $userId)->first();
  77. $this->info("\n=== 一致性验证 ===");
  78. $isConsistent = true;
  79. if ($logicResult->promotion_count !== $finalTalent->promotion_count) {
  80. $this->error("promotion_count不一致: Logic={$logicResult->promotion_count}, Final={$finalTalent->promotion_count}");
  81. $isConsistent = false;
  82. }
  83. if ($logicResult->direct_count !== $finalTalent->direct_count) {
  84. $this->error("direct_count不一致: Logic={$logicResult->direct_count}, Final={$finalTalent->direct_count}");
  85. $isConsistent = false;
  86. }
  87. if ($logicResult->talent_level !== $finalTalent->talent_level) {
  88. $this->error("talent_level不一致: Logic={$logicResult->talent_level}, Final={$finalTalent->talent_level}");
  89. $isConsistent = false;
  90. }
  91. if ($isConsistent) {
  92. $this->info("✅ 数据一致性验证通过");
  93. } else {
  94. $this->error("❌ 数据一致性验证失败");
  95. }
  96. // 测试模型方法
  97. $this->info("\n=== 模型方法测试 ===");
  98. $this->info("getTotalTeamCount(): {$finalTalent->getTotalTeamCount()}");
  99. $this->info("getThreeGenTeamCount(): {$finalTalent->getThreeGenTeamCount()}");
  100. $this->info("promotion_count字段: {$finalTalent->promotion_count}");
  101. if ($finalTalent->getTotalTeamCount() === $finalTalent->promotion_count) {
  102. $this->info("✅ getTotalTeamCount()方法正确");
  103. } else {
  104. $this->error("❌ getTotalTeamCount()方法不正确");
  105. }
  106. } catch (\Exception $e) {
  107. $this->error("测试失败: " . $e->getMessage());
  108. $this->error("错误详情: " . $e->getTraceAsString());
  109. }
  110. }
  111. /**
  112. * 测试随机用户
  113. */
  114. private function testRandomUsers(): void
  115. {
  116. $this->info("=== 测试随机用户的团队统计一致性 ===");
  117. // 获取有达人记录的用户
  118. $talents = UrsUserTalent::limit(3)->get();
  119. if ($talents->isEmpty()) {
  120. $this->warn("没有找到达人记录,无法进行测试");
  121. return;
  122. }
  123. foreach ($talents as $talent) {
  124. $this->info("\n" . str_repeat('-', 50));
  125. $this->testSingleUser($talent->user_id);
  126. }
  127. }
  128. /**
  129. * 显示达人统计信息
  130. */
  131. private function displayTalentStats(UrsUserTalent $talent): void
  132. {
  133. $this->table(['字段', '值'], [
  134. ['用户ID', $talent->user_id],
  135. ['达人等级', $talent->talent_level],
  136. ['直推人数', $talent->direct_count],
  137. ['间推人数', $talent->indirect_count],
  138. ['三推人数', $talent->third_count],
  139. ['团队总人数(promotion_count)', $talent->promotion_count],
  140. ['前三代总和', $talent->direct_count + $talent->indirect_count + $talent->third_count],
  141. ['getTotalTeamCount()', $talent->getTotalTeamCount()],
  142. ['getThreeGenTeamCount()', $talent->getThreeGenTeamCount()],
  143. ]);
  144. }
  145. }