TestBackfillRewardCommand.php 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. <?php
  2. namespace App\Module\UrsPromotion\Commands;
  3. use App\Module\UrsPromotion\Events\UrsUserEnteredFarmEvent;
  4. use App\Module\UrsPromotion\Services\UrsBackfillRewardService;
  5. use App\Module\UrsPromotion\Services\UrsReferralService;
  6. use App\Module\UrsPromotion\Services\UrsTalentService;
  7. use App\Module\UrsPromotion\Services\UrsUserMappingService;
  8. use Illuminate\Console\Command;
  9. /**
  10. * URS推荐奖励补发测试命令
  11. */
  12. class TestBackfillRewardCommand extends Command
  13. {
  14. /**
  15. * 命令签名
  16. *
  17. * @var string
  18. */
  19. protected $signature = 'urs:test-backfill-reward {urs_user_id : URS用户ID}';
  20. /**
  21. * 命令描述
  22. *
  23. * @var string
  24. */
  25. protected $description = '测试URS推荐奖励补发功能';
  26. /**
  27. * 执行命令
  28. */
  29. public function handle()
  30. {
  31. $ursUserId = (int)$this->argument('urs_user_id');
  32. $this->info("开始测试URS推荐奖励补发功能");
  33. $this->info("URS用户ID: {$ursUserId}");
  34. $this->line('');
  35. // 1. 显示用户基本信息
  36. $this->displayUserInfo($ursUserId);
  37. // 2. 显示用户下级统计
  38. $this->displaySubordinateStats($ursUserId);
  39. // 3. 检查是否需要补发奖励
  40. $this->checkBackfillNeed($ursUserId);
  41. // 4. 测试事件触发
  42. $this->testEventTrigger($ursUserId);
  43. // 5. 模拟补发奖励(如果用户已进入农场)
  44. $this->simulateBackfillReward($ursUserId);
  45. $this->line('');
  46. $this->info('测试完成!');
  47. }
  48. /**
  49. * 显示用户基本信息
  50. */
  51. private function displayUserInfo(int $ursUserId): void
  52. {
  53. $this->info("=== 用户基本信息 ===");
  54. // 检查是否已进入农场
  55. $hasEntered = UrsUserMappingService::hasEnteredFarm($ursUserId);
  56. $this->line("是否已进入农场: " . ($hasEntered ? '是' : '否'));
  57. if ($hasEntered) {
  58. $farmUserId = UrsUserMappingService::getFarmUserId($ursUserId);
  59. $this->line("农场用户ID: {$farmUserId}");
  60. }
  61. // 获取达人等级
  62. $talentDto = UrsTalentService::getTalentInfo($ursUserId);
  63. if ($talentDto) {
  64. $this->line("达人等级: {$talentDto->talentLevel}");
  65. $this->line("直推人数: {$talentDto->directCount}");
  66. $this->line("团队总人数: {$talentDto->promotionCount}");
  67. } else {
  68. $this->line("达人等级: 0 (青铜级,默认等级)");
  69. }
  70. $this->line('');
  71. }
  72. /**
  73. * 显示用户下级统计
  74. */
  75. private function displaySubordinateStats(int $ursUserId): void
  76. {
  77. $this->info("=== 下级统计信息 ===");
  78. $stats = UrsBackfillRewardService::getSubordinateStats($ursUserId);
  79. $this->line("直推下级数量: {$stats['direct_count']}");
  80. $this->line("间推下级数量: {$stats['indirect_count']}");
  81. $this->line("三推下级数量: {$stats['third_count']}");
  82. $this->line("下级总数量: {$stats['total_count']}");
  83. if ($stats['total_count'] > 0) {
  84. // 显示直推下级详情
  85. $directReferrals = UrsReferralService::getDirectReferrals($ursUserId);
  86. if (!empty($directReferrals)) {
  87. $this->line("直推下级列表:");
  88. foreach ($directReferrals as $directUserId) {
  89. $hasEnteredFarm = UrsUserMappingService::hasEnteredFarm($directUserId);
  90. $status = $hasEnteredFarm ? '已进入农场' : '未进入农场';
  91. $this->line(" - URS用户ID: {$directUserId} ({$status})");
  92. }
  93. }
  94. }
  95. $this->line('');
  96. }
  97. /**
  98. * 检查是否需要补发奖励
  99. */
  100. private function checkBackfillNeed(int $ursUserId): void
  101. {
  102. $this->info("=== 补发奖励检查 ===");
  103. $needsBackfill = UrsBackfillRewardService::needsBackfillReward($ursUserId);
  104. $this->line("是否需要补发奖励: " . ($needsBackfill ? '是' : '否'));
  105. if (!$needsBackfill) {
  106. $this->warn("用户无下级,无需补发奖励");
  107. }
  108. $this->line('');
  109. }
  110. /**
  111. * 测试事件触发
  112. */
  113. private function testEventTrigger(int $ursUserId): void
  114. {
  115. $this->info("=== 事件触发测试 ===");
  116. // 检查用户是否已进入农场
  117. $farmUserId = UrsUserMappingService::getFarmUserId($ursUserId);
  118. if (!$farmUserId) {
  119. $this->warn("用户尚未进入农场,无法测试事件触发");
  120. $this->line('');
  121. return;
  122. }
  123. // 手动触发用户进入农场事件
  124. $this->line("手动触发URS用户进入农场事件...");
  125. try {
  126. event(new UrsUserEnteredFarmEvent($ursUserId, $farmUserId, 'test_user_key', true));
  127. $this->info("✅ 事件触发成功!");
  128. } catch (\Exception $e) {
  129. $this->error("❌ 事件触发失败: " . $e->getMessage());
  130. }
  131. $this->line('');
  132. }
  133. /**
  134. * 模拟补发奖励
  135. */
  136. private function simulateBackfillReward(int $ursUserId): void
  137. {
  138. $this->info("=== 补发奖励测试 ===");
  139. // 检查用户是否已进入农场
  140. $farmUserId = UrsUserMappingService::getFarmUserId($ursUserId);
  141. if (!$farmUserId) {
  142. $this->warn("用户尚未进入农场,无法测试补发奖励");
  143. return;
  144. }
  145. // 执行补发奖励
  146. $this->line("正在执行补发奖励...");
  147. $result = UrsBackfillRewardService::backfillPromotionReward($ursUserId, $farmUserId);
  148. if ($result['success']) {
  149. $this->info("✅ 补发奖励成功!");
  150. $this->line("补发奖励总数量: {$result['backfilled_count']}");
  151. $this->line("下级总人数: {$result['total_subordinates']}");
  152. $this->line("达人等级: {$result['talent_level']}");
  153. // 显示分层级奖励详情
  154. if (!empty($result['reward_details'])) {
  155. $this->line("奖励详情:");
  156. foreach ($result['reward_details'] as $type => $detail) {
  157. $this->line(" - {$detail['type']}: {$detail['count']}人 -> {$detail['items_count']}个奖励 (奖励组ID: {$detail['reward_group_id']})");
  158. }
  159. }
  160. } else {
  161. $this->error("❌ 补发奖励失败");
  162. $this->line("失败原因: {$result['message']}");
  163. $this->line("下级总人数: {$result['total_subordinates']}");
  164. $this->line("达人等级: {$result['talent_level']}");
  165. }
  166. }
  167. }