TestUrsProfitCommand.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. <?php
  2. namespace App\Module\UrsPromotion\Commands;
  3. use Illuminate\Console\Command;
  4. use App\Module\UrsPromotion\Services\UrsProfitService;
  5. use App\Module\UrsPromotion\Services\UrsTalentService;
  6. use App\Module\UrsPromotion\Enums\UrsProfitType;
  7. /**
  8. * URS收益分成测试命令
  9. *
  10. * 用于测试URS推广系统的收益分成功能
  11. */
  12. class TestUrsProfitCommand extends Command
  13. {
  14. /**
  15. * 命令签名
  16. */
  17. protected $signature = 'urs:test-profit {user_id} {amount} {type=planting_reward}';
  18. /**
  19. * 命令描述
  20. */
  21. protected $description = '测试URS收益分成功能';
  22. /**
  23. * 执行命令
  24. */
  25. public function handle()
  26. {
  27. $userId = (int)$this->argument('user_id');
  28. $amount = $this->argument('amount');
  29. $type = $this->argument('type');
  30. $this->info("开始测试URS收益分成功能");
  31. $this->info("用户ID: {$userId}");
  32. $this->info("收益金额: {$amount}");
  33. $this->info("收益类型: {$type}");
  34. $this->line('');
  35. // 显示用户的推荐关系树
  36. $this->info("=== 用户推荐关系树 ===");
  37. $tree = UrsTalentService::getUserReferralTree($userId);
  38. $this->displayReferralTree($tree);
  39. $this->line('');
  40. // 执行收益分成
  41. $this->info("=== 执行收益分成 ===");
  42. if ($type === 'promotion_reward') {
  43. $profits = UrsProfitService::distributePromotionReward(
  44. $userId,
  45. 'test_command',
  46. time(),
  47. $amount
  48. );
  49. } else {
  50. $profits = UrsProfitService::distributePlantingReward(
  51. $userId,
  52. 'test_command',
  53. time(),
  54. $amount
  55. );
  56. }
  57. if (empty($profits)) {
  58. $this->warn("没有产生任何收益分成");
  59. } else {
  60. $this->info("成功分成给 " . count($profits) . " 个用户:");
  61. foreach ($profits as $profit) {
  62. $rate = round($profit->profit_rate * 100, 2);
  63. $levelName = $profit->getRelationLevelName();
  64. $this->line("- 用户{$profit->user_id}: {$profit->profit_amount} ({$rate}%, {$levelName})");
  65. }
  66. }
  67. $this->line('');
  68. $this->info("测试完成!");
  69. }
  70. /**
  71. * 显示推荐关系树
  72. */
  73. private function displayReferralTree(array $tree): void
  74. {
  75. $this->line("用户 {$tree['user_id']} 的团队结构:");
  76. if (!empty($tree['direct'])) {
  77. $this->line(" 直推用户 (" . count($tree['direct']) . "人):");
  78. foreach ($tree['direct'] as $user) {
  79. $this->line(" - 用户{$user['user_id']}");
  80. }
  81. }
  82. if (!empty($tree['indirect'])) {
  83. $this->line(" 间推用户 (" . count($tree['indirect']) . "人):");
  84. foreach ($tree['indirect'] as $user) {
  85. $this->line(" - 用户{$user['user_id']} (通过用户{$user['referrer_id']})");
  86. }
  87. }
  88. if (!empty($tree['third'])) {
  89. $this->line(" 三推用户 (" . count($tree['third']) . "人):");
  90. foreach ($tree['third'] as $user) {
  91. $this->line(" - 用户{$user['user_id']} (通过用户{$user['referrer_id']})");
  92. }
  93. }
  94. if (empty($tree['direct']) && empty($tree['indirect']) && empty($tree['third'])) {
  95. $this->line(" 无团队成员");
  96. }
  97. }
  98. }