UrsPromotionIntegrationTestCommand.php 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  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\Models\UrsUserReferral;
  7. use App\Module\UrsPromotion\Models\UrsUserTalent;
  8. use App\Module\UrsPromotion\Models\UrsProfit;
  9. use App\Module\UrsPromotion\Models\UrsTalentConfig;
  10. use Illuminate\Support\Facades\DB;
  11. /**
  12. * URS推广模块集成测试命令
  13. *
  14. * 用于全面测试URS推广系统的各项功能
  15. */
  16. class UrsPromotionIntegrationTestCommand extends Command
  17. {
  18. /**
  19. * 命令签名
  20. */
  21. protected $signature = 'urs:integration-test {--reset : 重置测试数据}';
  22. /**
  23. * 命令描述
  24. */
  25. protected $description = 'URS推广模块集成测试';
  26. /**
  27. * 执行命令
  28. */
  29. public function handle()
  30. {
  31. $this->info('开始URS推广模块集成测试...');
  32. $this->line('');
  33. // 检查是否重置数据
  34. if ($this->option('reset')) {
  35. $this->resetTestData();
  36. }
  37. // 1. 测试达人等级配置
  38. $this->testTalentConfigs();
  39. // 2. 测试推荐关系
  40. $this->testReferralRelations();
  41. // 3. 测试达人等级计算
  42. $this->testTalentLevelCalculation();
  43. // 4. 测试收益分成
  44. $this->testProfitDistribution();
  45. // 5. 测试统计功能
  46. $this->testStatistics();
  47. $this->line('');
  48. $this->info('URS推广模块集成测试完成!');
  49. }
  50. /**
  51. * 重置测试数据
  52. */
  53. private function resetTestData(): void
  54. {
  55. $this->warn('重置测试数据...');
  56. // 清空测试数据(保留配置)
  57. UrsProfit::where('source_type', 'like', 'test_%')->delete();
  58. UrsUserTalent::whereIn('user_id', [2001, 2002, 2003, 2004, 2005])->delete();
  59. UrsUserReferral::whereIn('user_id', [2001, 2002, 2003, 2004, 2005])->delete();
  60. $this->info('测试数据重置完成');
  61. $this->line('');
  62. }
  63. /**
  64. * 测试达人等级配置
  65. */
  66. private function testTalentConfigs(): void
  67. {
  68. $this->info('=== 测试达人等级配置 ===');
  69. $configs = UrsTalentService::getTalentConfigs();
  70. $this->line('达人等级配置数量: ' . count($configs));
  71. foreach ($configs as $config) {
  72. $this->line("等级{$config['level']}: {$config['name']} (直推{$config['direct_count_required']}人, 团队{$config['promotion_count_required']}人)");
  73. }
  74. $this->line('');
  75. }
  76. /**
  77. * 测试推荐关系
  78. */
  79. private function testReferralRelations(): void
  80. {
  81. $this->info('=== 测试推荐关系 ===');
  82. // 创建测试推荐关系: 2001 -> 2002 -> 2003 -> 2004
  83. $referrals = [
  84. ['user_id' => 2002, 'referrer_id' => 2001, 'referral_code' => 'TEST2001'],
  85. ['user_id' => 2003, 'referrer_id' => 2002, 'referral_code' => 'TEST2002'],
  86. ['user_id' => 2004, 'referrer_id' => 2003, 'referral_code' => 'TEST2003'],
  87. ['user_id' => 2005, 'referrer_id' => 2001, 'referral_code' => 'TEST2001'], // 2001的另一个直推
  88. ];
  89. foreach ($referrals as $referral) {
  90. UrsUserReferral::updateOrCreate(
  91. ['user_id' => $referral['user_id']],
  92. array_merge($referral, [
  93. 'referral_time' => now(),
  94. 'status' => UrsUserReferral::STATUS_VALID,
  95. ])
  96. );
  97. $this->line("创建推荐关系: 用户{$referral['user_id']} <- 用户{$referral['referrer_id']}");
  98. }
  99. $this->line('');
  100. }
  101. /**
  102. * 测试达人等级计算
  103. */
  104. private function testTalentLevelCalculation(): void
  105. {
  106. $this->info('=== 测试达人等级计算 ===');
  107. // 更新用户2001的达人等级
  108. $talent = UrsTalentService::updateUserTalent(2001);
  109. if ($talent) {
  110. $this->line("用户2001达人等级: {$talent->getTalentLevelName()}");
  111. $this->line("直推: {$talent->direct_count}, 间推: {$talent->indirect_count}, 三推: {$talent->third_count}, 总计: {$talent->promotion_count}");
  112. }
  113. // 显示推荐关系树
  114. $tree = UrsTalentService::getUserReferralTree(2001);
  115. $this->line('推荐关系树:');
  116. $this->line(" 直推: " . count($tree['direct']) . "人");
  117. $this->line(" 间推: " . count($tree['indirect']) . "人");
  118. $this->line(" 三推: " . count($tree['third']) . "人");
  119. $this->line('');
  120. }
  121. /**
  122. * 测试收益分成
  123. */
  124. private function testProfitDistribution(): void
  125. {
  126. $this->info('=== 测试收益分成 ===');
  127. // 测试推广收益分成
  128. $this->line('测试推广收益分成 (用户2005进入农场, 奖励500金币):');
  129. $profits = UrsProfitService::distributePromotionReward(2005, 'test_farm_enter', 12345, '500');
  130. $this->displayProfits($profits);
  131. // 测试种植收益分成
  132. $this->line('测试种植收益分成 (用户2004收获作物, 奖励1000金币):');
  133. $profits = UrsProfitService::distributePlantingReward(2004, 'test_farm_harvest', 67890, '1000');
  134. $this->displayProfits($profits);
  135. $this->line('');
  136. }
  137. /**
  138. * 测试统计功能
  139. */
  140. private function testStatistics(): void
  141. {
  142. $this->info('=== 测试统计功能 ===');
  143. // 获取用户2001的收益统计
  144. $stats = UrsProfitService::getUserProfitStats(2001);
  145. $this->line("用户2001总收益: {$stats['total_amount']} (共{$stats['total_count']}笔)");
  146. if (!empty($stats['by_type'])) {
  147. foreach ($stats['by_type'] as $type => $data) {
  148. $typeName = $type === 'promotion_reward' ? '推广收益' : '种植收益';
  149. $this->line(" {$typeName}: {$data['amount']} (共{$data['count']}笔)");
  150. }
  151. }
  152. if (!empty($stats['by_level'])) {
  153. foreach ($stats['by_level'] as $level => $data) {
  154. $levelName = match($level) {
  155. 1 => '直推',
  156. 2 => '间推',
  157. 3 => '三推',
  158. default => "层级{$level}",
  159. };
  160. $this->line(" {$levelName}: {$data['amount']} (共{$data['count']}笔)");
  161. }
  162. }
  163. $this->line('');
  164. }
  165. /**
  166. * 显示收益分成结果
  167. */
  168. private function displayProfits(array $profits): void
  169. {
  170. if (empty($profits)) {
  171. $this->line(' 无收益分成');
  172. return;
  173. }
  174. foreach ($profits as $profit) {
  175. $rate = round($profit->profit_rate * 100, 2);
  176. $levelName = $profit->getRelationLevelName();
  177. $typeName = $profit->getProfitTypeName();
  178. $this->line(" 用户{$profit->user_id}: {$profit->profit_amount} ({$rate}%, {$levelName}, {$typeName})");
  179. }
  180. }
  181. }