| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219 |
- <?php
- namespace App\Module\UrsPromotion\Commands;
- use Illuminate\Console\Command;
- use App\Module\UrsPromotion\Services\UrsProfitService;
- use App\Module\UrsPromotion\Services\UrsTalentService;
- use App\Module\UrsPromotion\Models\UrsUserReferral;
- use App\Module\UrsPromotion\Models\UrsUserTalent;
- use App\Module\UrsPromotion\Models\UrsProfit;
- use App\Module\UrsPromotion\Models\UrsTalentConfig;
- use Illuminate\Support\Facades\DB;
- /**
- * URS推广模块集成测试命令
- *
- * 用于全面测试URS推广系统的各项功能
- */
- class UrsPromotionIntegrationTestCommand extends Command
- {
- /**
- * 命令签名
- */
- protected $signature = 'urs:integration-test {--reset : 重置测试数据}';
- /**
- * 命令描述
- */
- protected $description = 'URS推广模块集成测试';
- /**
- * 执行命令
- */
- public function handle()
- {
- $this->info('开始URS推广模块集成测试...');
- $this->line('');
-
- // 检查是否重置数据
- if ($this->option('reset')) {
- $this->resetTestData();
- }
-
- // 1. 测试达人等级配置
- $this->testTalentConfigs();
-
- // 2. 测试推荐关系
- $this->testReferralRelations();
-
- // 3. 测试达人等级计算
- $this->testTalentLevelCalculation();
-
- // 4. 测试收益分成
- $this->testProfitDistribution();
-
- // 5. 测试统计功能
- $this->testStatistics();
-
- $this->line('');
- $this->info('URS推广模块集成测试完成!');
- }
-
- /**
- * 重置测试数据
- */
- private function resetTestData(): void
- {
- $this->warn('重置测试数据...');
-
- // 清空测试数据(保留配置)
- UrsProfit::where('source_type', 'like', 'test_%')->delete();
- UrsUserTalent::whereIn('user_id', [2001, 2002, 2003, 2004, 2005])->delete();
- UrsUserReferral::whereIn('user_id', [2001, 2002, 2003, 2004, 2005])->delete();
-
- $this->info('测试数据重置完成');
- $this->line('');
- }
-
- /**
- * 测试达人等级配置
- */
- private function testTalentConfigs(): void
- {
- $this->info('=== 测试达人等级配置 ===');
-
- $configs = UrsTalentService::getTalentConfigs();
- $this->line('达人等级配置数量: ' . count($configs));
-
- foreach ($configs as $config) {
- $this->line("等级{$config['level']}: {$config['name']} (直推{$config['direct_count_required']}人, 团队{$config['promotion_count_required']}人)");
- }
-
- $this->line('');
- }
-
- /**
- * 测试推荐关系
- */
- private function testReferralRelations(): void
- {
- $this->info('=== 测试推荐关系 ===');
-
- // 创建测试推荐关系: 2001 -> 2002 -> 2003 -> 2004
- $referrals = [
- ['user_id' => 2002, 'referrer_id' => 2001, 'referral_code' => 'TEST2001'],
- ['user_id' => 2003, 'referrer_id' => 2002, 'referral_code' => 'TEST2002'],
- ['user_id' => 2004, 'referrer_id' => 2003, 'referral_code' => 'TEST2003'],
- ['user_id' => 2005, 'referrer_id' => 2001, 'referral_code' => 'TEST2001'], // 2001的另一个直推
- ];
-
- foreach ($referrals as $referral) {
- UrsUserReferral::updateOrCreate(
- ['user_id' => $referral['user_id']],
- array_merge($referral, [
- 'referral_time' => now(),
- 'status' => UrsUserReferral::STATUS_VALID,
- ])
- );
- $this->line("创建推荐关系: 用户{$referral['user_id']} <- 用户{$referral['referrer_id']}");
- }
-
- $this->line('');
- }
-
- /**
- * 测试达人等级计算
- */
- private function testTalentLevelCalculation(): void
- {
- $this->info('=== 测试达人等级计算 ===');
-
- // 更新用户2001的达人等级
- $talent = UrsTalentService::updateUserTalent(2001);
- if ($talent) {
- $this->line("用户2001达人等级: {$talent->getTalentLevelName()}");
- $this->line("直推: {$talent->direct_count}, 间推: {$talent->indirect_count}, 三推: {$talent->third_count}, 总计: {$talent->promotion_count}");
- }
-
- // 显示推荐关系树
- $tree = UrsTalentService::getUserReferralTree(2001);
- $this->line('推荐关系树:');
- $this->line(" 直推: " . count($tree['direct']) . "人");
- $this->line(" 间推: " . count($tree['indirect']) . "人");
- $this->line(" 三推: " . count($tree['third']) . "人");
-
- $this->line('');
- }
-
- /**
- * 测试收益分成
- */
- private function testProfitDistribution(): void
- {
- $this->info('=== 测试收益分成 ===');
-
- // 测试推广收益分成
- $this->line('测试推广收益分成 (用户2005进入农场, 奖励500金币):');
- $profits = UrsProfitService::distributePromotionReward(2005, 'test_farm_enter', 12345, '500');
- $this->displayProfits($profits);
-
- // 测试种植收益分成
- $this->line('测试种植收益分成 (用户2004收获作物, 奖励1000金币):');
- $profits = UrsProfitService::distributePlantingReward(2004, 'test_farm_harvest', 67890, '1000');
- $this->displayProfits($profits);
-
- $this->line('');
- }
-
- /**
- * 测试统计功能
- */
- private function testStatistics(): void
- {
- $this->info('=== 测试统计功能 ===');
-
- // 获取用户2001的收益统计
- $stats = UrsProfitService::getUserProfitStats(2001);
- $this->line("用户2001总收益: {$stats['total_amount']} (共{$stats['total_count']}笔)");
-
- if (!empty($stats['by_type'])) {
- foreach ($stats['by_type'] as $type => $data) {
- $typeName = $type === 'promotion_reward' ? '推广收益' : '种植收益';
- $this->line(" {$typeName}: {$data['amount']} (共{$data['count']}笔)");
- }
- }
-
- if (!empty($stats['by_level'])) {
- foreach ($stats['by_level'] as $level => $data) {
- $levelName = match($level) {
- 1 => '直推',
- 2 => '间推',
- 3 => '三推',
- default => "层级{$level}",
- };
- $this->line(" {$levelName}: {$data['amount']} (共{$data['count']}笔)");
- }
- }
-
- $this->line('');
- }
-
- /**
- * 显示收益分成结果
- */
- private function displayProfits(array $profits): void
- {
- if (empty($profits)) {
- $this->line(' 无收益分成');
- return;
- }
-
- foreach ($profits as $profit) {
- $rate = round($profit->profit_rate * 100, 2);
- $levelName = $profit->getRelationLevelName();
- $typeName = $profit->getProfitTypeName();
- $this->line(" 用户{$profit->user_id}: {$profit->profit_amount} ({$rate}%, {$levelName}, {$typeName})");
- }
- }
- }
|