| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231 |
- <?php
- namespace App\Module\UrsPromotion\Commands;
- use Illuminate\Console\Command;
- use App\Module\UrsPromotion\Models\UrsUserReferral;
- use App\Module\UrsPromotion\Models\UrsUserTalent;
- use App\Module\UrsPromotion\Models\UrsProfit;
- use App\Module\UrsPromotion\Services\UrsProfitService;
- use App\Module\UrsPromotion\Enums\UrsProfitType;
- use Illuminate\Support\Facades\DB;
- /**
- * 测试推广奖励发放功能
- */
- class TestPromotionRewardCommand extends Command
- {
- /**
- * 命令签名
- *
- * @var string
- */
- protected $signature = 'urs:test-promotion-reward {--clean : 清理测试数据}';
- /**
- * 命令描述
- *
- * @var string
- */
- protected $description = '测试URS推广奖励发放功能(集成奖励组系统)';
- /**
- * 执行命令
- */
- public function handle(): int
- {
- if ($this->option('clean')) {
- $this->cleanTestData();
- $this->info('测试数据清理完成');
- return 0;
- }
- $this->info('开始测试URS推广奖励发放功能...');
- try {
- // 1. 准备测试数据
- $this->info('1. 准备测试数据...');
- $testData = $this->prepareTestData();
- // 2. 测试推广奖励发放
- $this->info('2. 测试推广奖励发放...');
- $this->testPromotionReward($testData);
- // 3. 验证奖励发放结果
- $this->info('3. 验证奖励发放结果...');
- $this->verifyRewardResults($testData);
- $this->info('✅ URS推广奖励发放功能测试通过!');
- return 0;
- } catch (\Exception $e) {
- $this->error('❌ 测试失败: ' . $e->getMessage());
- $this->error('错误详情: ' . $e->getTraceAsString());
- return 1;
- }
- }
- /**
- * 准备测试数据
- */
- private function prepareTestData(): array
- {
- DB::beginTransaction();
- try {
- // 创建测试用户推荐关系
- $newUserId = 4001; // 新注册用户
- $directReferrerId = 4002; // 直推(初级达人)
- $indirectReferrerId = 4003; // 间推(中级达人)
- $thirdReferrerId = 4004; // 三推(高级达人)
- // 清理可能存在的测试数据
- UrsUserReferral::whereIn('user_id', [$newUserId, $directReferrerId, $indirectReferrerId])->delete();
- UrsUserTalent::whereIn('user_id', [$directReferrerId, $indirectReferrerId, $thirdReferrerId])->delete();
- UrsProfit::where('source_type', 'user_register_test')->delete();
- // 创建推荐关系:4004 -> 4003 -> 4002 -> 4001
- UrsUserReferral::create([
- 'user_id' => $indirectReferrerId,
- 'referrer_id' => $thirdReferrerId,
- 'referral_code' => 'TEST_' . $thirdReferrerId,
- 'direct_count' => 1,
- 'indirect_count' => 0,
- 'third_count' => 0,
- ]);
- UrsUserReferral::create([
- 'user_id' => $directReferrerId,
- 'referrer_id' => $indirectReferrerId,
- 'referral_code' => 'TEST_' . $indirectReferrerId,
- 'direct_count' => 1,
- 'indirect_count' => 0,
- 'third_count' => 0,
- ]);
- UrsUserReferral::create([
- 'user_id' => $newUserId,
- 'referrer_id' => $directReferrerId,
- 'referral_code' => 'TEST_' . $directReferrerId,
- 'direct_count' => 0,
- 'indirect_count' => 0,
- 'third_count' => 0,
- ]);
- // 创建达人等级
- UrsUserTalent::create([
- 'user_id' => $directReferrerId,
- 'talent_level' => 1, // 初级达人
- 'direct_count' => 1,
- 'indirect_count' => 0,
- 'third_count' => 0,
- 'total_count' => 1,
- ]);
- UrsUserTalent::create([
- 'user_id' => $indirectReferrerId,
- 'talent_level' => 2, // 中级达人
- 'direct_count' => 1,
- 'indirect_count' => 1,
- 'third_count' => 0,
- 'total_count' => 2,
- ]);
- UrsUserTalent::create([
- 'user_id' => $thirdReferrerId,
- 'talent_level' => 3, // 高级达人
- 'direct_count' => 1,
- 'indirect_count' => 1,
- 'third_count' => 1,
- 'total_count' => 3,
- ]);
- DB::commit();
- $this->info(" - 创建推荐关系: {$thirdReferrerId}(高级) -> {$indirectReferrerId}(中级) -> {$directReferrerId}(初级) -> {$newUserId}(新用户)");
- return [
- 'new_user_id' => $newUserId,
- 'direct_referrer_id' => $directReferrerId,
- 'indirect_referrer_id' => $indirectReferrerId,
- 'third_referrer_id' => $thirdReferrerId,
- ];
- } catch (\Exception $e) {
- DB::rollBack();
- throw $e;
- }
- }
- /**
- * 测试推广奖励发放
- */
- private function testPromotionReward(array $testData): void
- {
- // 模拟新用户注册,触发推广奖励发放
- $profits = UrsProfitService::distributePromotionReward(
- $testData['new_user_id'],
- 'user_register_test',
- 1001
- );
- $this->info(" - 触发推广奖励发放: 新用户{$testData['new_user_id']}注册");
- $this->info(" - 生成收益记录数量: " . count($profits));
- }
- /**
- * 验证奖励发放结果
- */
- private function verifyRewardResults(array $testData): void
- {
- // 等待奖励发放完成
- sleep(1);
- // 查询生成的收益记录
- $profits = UrsProfit::where('source_type', 'user_register_test')
- ->where('source_id', 1001)
- ->where('profit_type', UrsProfitType::PROMOTION_REWARD->value)
- ->get();
- $this->info(" - 实际生成收益记录数量: " . $profits->count());
- if ($profits->isEmpty()) {
- throw new \Exception('未生成任何推广收益记录');
- }
- foreach ($profits as $profit) {
- $this->info(" - 收益记录: 用户{$profit->user_id}, 层级{$profit->relation_level}, 金额{$profit->profit_amount}, 奖励组{$profit->reward_group_id}");
- }
- // 验证收益记录的正确性
- $directProfit = $profits->where('user_id', $testData['direct_referrer_id'])->first();
- $indirectProfit = $profits->where('user_id', $testData['indirect_referrer_id'])->first();
- $thirdProfit = $profits->where('user_id', $testData['third_referrer_id'])->first();
- if (!$directProfit) {
- throw new \Exception('直推收益记录不存在');
- }
- if (!$indirectProfit) {
- throw new \Exception('间推收益记录不存在');
- }
- if (!$thirdProfit) {
- throw new \Exception('三推收益记录不存在');
- }
- $this->info(" ✅ 直推收益: {$directProfit->profit_amount} (奖励组: {$directProfit->reward_group_id})");
- $this->info(" ✅ 间推收益: {$indirectProfit->profit_amount} (奖励组: {$indirectProfit->reward_group_id})");
- $this->info(" ✅ 三推收益: {$thirdProfit->profit_amount} (奖励组: {$thirdProfit->reward_group_id})");
- }
- /**
- * 清理测试数据
- */
- private function cleanTestData(): void
- {
- UrsUserReferral::whereIn('user_id', [4001, 4002, 4003])->delete();
- UrsUserTalent::whereIn('user_id', [4002, 4003, 4004])->delete();
- UrsProfit::where('source_type', 'user_register_test')->delete();
- }
- }
|