TestPromotionRewardCommand.php 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. <?php
  2. namespace App\Module\UrsPromotion\Commands;
  3. use Illuminate\Console\Command;
  4. use App\Module\UrsPromotion\Models\UrsUserReferral;
  5. use App\Module\UrsPromotion\Models\UrsUserTalent;
  6. use App\Module\UrsPromotion\Models\UrsProfit;
  7. use App\Module\UrsPromotion\Services\UrsProfitService;
  8. use App\Module\UrsPromotion\Enums\UrsProfitType;
  9. use Illuminate\Support\Facades\DB;
  10. /**
  11. * 测试推广奖励发放功能
  12. */
  13. class TestPromotionRewardCommand extends Command
  14. {
  15. /**
  16. * 命令签名
  17. *
  18. * @var string
  19. */
  20. protected $signature = 'urs:test-promotion-reward {--clean : 清理测试数据}';
  21. /**
  22. * 命令描述
  23. *
  24. * @var string
  25. */
  26. protected $description = '测试URS推广奖励发放功能(集成奖励组系统)';
  27. /**
  28. * 执行命令
  29. */
  30. public function handle(): int
  31. {
  32. if ($this->option('clean')) {
  33. $this->cleanTestData();
  34. $this->info('测试数据清理完成');
  35. return 0;
  36. }
  37. $this->info('开始测试URS推广奖励发放功能...');
  38. try {
  39. // 1. 准备测试数据
  40. $this->info('1. 准备测试数据...');
  41. $testData = $this->prepareTestData();
  42. // 2. 测试推广奖励发放
  43. $this->info('2. 测试推广奖励发放...');
  44. $this->testPromotionReward($testData);
  45. // 3. 验证奖励发放结果
  46. $this->info('3. 验证奖励发放结果...');
  47. $this->verifyRewardResults($testData);
  48. $this->info('✅ URS推广奖励发放功能测试通过!');
  49. return 0;
  50. } catch (\Exception $e) {
  51. $this->error('❌ 测试失败: ' . $e->getMessage());
  52. $this->error('错误详情: ' . $e->getTraceAsString());
  53. return 1;
  54. }
  55. }
  56. /**
  57. * 准备测试数据
  58. */
  59. private function prepareTestData(): array
  60. {
  61. DB::beginTransaction();
  62. try {
  63. // 创建测试用户推荐关系
  64. $newUserId = 4001; // 新注册用户
  65. $directReferrerId = 4002; // 直推(初级达人)
  66. $indirectReferrerId = 4003; // 间推(中级达人)
  67. $thirdReferrerId = 4004; // 三推(高级达人)
  68. // 清理可能存在的测试数据
  69. UrsUserReferral::whereIn('user_id', [$newUserId, $directReferrerId, $indirectReferrerId])->delete();
  70. UrsUserTalent::whereIn('user_id', [$directReferrerId, $indirectReferrerId, $thirdReferrerId])->delete();
  71. UrsProfit::where('source_type', 'user_register_test')->delete();
  72. // 创建推荐关系:4004 -> 4003 -> 4002 -> 4001
  73. UrsUserReferral::create([
  74. 'user_id' => $indirectReferrerId,
  75. 'referrer_id' => $thirdReferrerId,
  76. 'referral_code' => 'TEST_' . $thirdReferrerId,
  77. 'direct_count' => 1,
  78. 'indirect_count' => 0,
  79. 'third_count' => 0,
  80. ]);
  81. UrsUserReferral::create([
  82. 'user_id' => $directReferrerId,
  83. 'referrer_id' => $indirectReferrerId,
  84. 'referral_code' => 'TEST_' . $indirectReferrerId,
  85. 'direct_count' => 1,
  86. 'indirect_count' => 0,
  87. 'third_count' => 0,
  88. ]);
  89. UrsUserReferral::create([
  90. 'user_id' => $newUserId,
  91. 'referrer_id' => $directReferrerId,
  92. 'referral_code' => 'TEST_' . $directReferrerId,
  93. 'direct_count' => 0,
  94. 'indirect_count' => 0,
  95. 'third_count' => 0,
  96. ]);
  97. // 创建达人等级
  98. UrsUserTalent::create([
  99. 'user_id' => $directReferrerId,
  100. 'talent_level' => 1, // 初级达人
  101. 'direct_count' => 1,
  102. 'indirect_count' => 0,
  103. 'third_count' => 0,
  104. 'total_count' => 1,
  105. ]);
  106. UrsUserTalent::create([
  107. 'user_id' => $indirectReferrerId,
  108. 'talent_level' => 2, // 中级达人
  109. 'direct_count' => 1,
  110. 'indirect_count' => 1,
  111. 'third_count' => 0,
  112. 'total_count' => 2,
  113. ]);
  114. UrsUserTalent::create([
  115. 'user_id' => $thirdReferrerId,
  116. 'talent_level' => 3, // 高级达人
  117. 'direct_count' => 1,
  118. 'indirect_count' => 1,
  119. 'third_count' => 1,
  120. 'total_count' => 3,
  121. ]);
  122. DB::commit();
  123. $this->info(" - 创建推荐关系: {$thirdReferrerId}(高级) -> {$indirectReferrerId}(中级) -> {$directReferrerId}(初级) -> {$newUserId}(新用户)");
  124. return [
  125. 'new_user_id' => $newUserId,
  126. 'direct_referrer_id' => $directReferrerId,
  127. 'indirect_referrer_id' => $indirectReferrerId,
  128. 'third_referrer_id' => $thirdReferrerId,
  129. ];
  130. } catch (\Exception $e) {
  131. DB::rollBack();
  132. throw $e;
  133. }
  134. }
  135. /**
  136. * 测试推广奖励发放
  137. */
  138. private function testPromotionReward(array $testData): void
  139. {
  140. // 模拟新用户注册,触发推广奖励发放
  141. $profits = UrsProfitService::distributePromotionReward(
  142. $testData['new_user_id'],
  143. 'user_register_test',
  144. 1001
  145. );
  146. $this->info(" - 触发推广奖励发放: 新用户{$testData['new_user_id']}注册");
  147. $this->info(" - 生成收益记录数量: " . count($profits));
  148. }
  149. /**
  150. * 验证奖励发放结果
  151. */
  152. private function verifyRewardResults(array $testData): void
  153. {
  154. // 等待奖励发放完成
  155. sleep(1);
  156. // 查询生成的收益记录
  157. $profits = UrsProfit::where('source_type', 'user_register_test')
  158. ->where('source_id', 1001)
  159. ->where('profit_type', UrsProfitType::PROMOTION_REWARD->value)
  160. ->get();
  161. $this->info(" - 实际生成收益记录数量: " . $profits->count());
  162. if ($profits->isEmpty()) {
  163. throw new \Exception('未生成任何推广收益记录');
  164. }
  165. foreach ($profits as $profit) {
  166. $this->info(" - 收益记录: 用户{$profit->user_id}, 层级{$profit->relation_level}, 金额{$profit->profit_amount}, 奖励组{$profit->reward_group_id}");
  167. }
  168. // 验证收益记录的正确性
  169. $directProfit = $profits->where('user_id', $testData['direct_referrer_id'])->first();
  170. $indirectProfit = $profits->where('user_id', $testData['indirect_referrer_id'])->first();
  171. $thirdProfit = $profits->where('user_id', $testData['third_referrer_id'])->first();
  172. if (!$directProfit) {
  173. throw new \Exception('直推收益记录不存在');
  174. }
  175. if (!$indirectProfit) {
  176. throw new \Exception('间推收益记录不存在');
  177. }
  178. if (!$thirdProfit) {
  179. throw new \Exception('三推收益记录不存在');
  180. }
  181. $this->info(" ✅ 直推收益: {$directProfit->profit_amount} (奖励组: {$directProfit->reward_group_id})");
  182. $this->info(" ✅ 间推收益: {$indirectProfit->profit_amount} (奖励组: {$indirectProfit->reward_group_id})");
  183. $this->info(" ✅ 三推收益: {$thirdProfit->profit_amount} (奖励组: {$thirdProfit->reward_group_id})");
  184. }
  185. /**
  186. * 清理测试数据
  187. */
  188. private function cleanTestData(): void
  189. {
  190. UrsUserReferral::whereIn('user_id', [4001, 4002, 4003])->delete();
  191. UrsUserTalent::whereIn('user_id', [4002, 4003, 4004])->delete();
  192. UrsProfit::where('source_type', 'user_register_test')->delete();
  193. }
  194. }