| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201 |
- <?php
- namespace App\Module\UrsPromotion\Commands;
- use App\Module\UrsPromotion\Events\UrsUserEnteredFarmEvent;
- use App\Module\UrsPromotion\Services\UrsBackfillRewardService;
- use App\Module\UrsPromotion\Services\UrsReferralService;
- use App\Module\UrsPromotion\Services\UrsTalentService;
- use App\Module\UrsPromotion\Services\UrsUserMappingService;
- use Illuminate\Console\Command;
- /**
- * URS推荐奖励补发测试命令
- */
- class TestBackfillRewardCommand extends Command
- {
- /**
- * 命令签名
- *
- * @var string
- */
- protected $signature = 'urs:test-backfill-reward {urs_user_id : URS用户ID}';
- /**
- * 命令描述
- *
- * @var string
- */
- protected $description = '测试URS推荐奖励补发功能';
- /**
- * 执行命令
- */
- public function handle()
- {
- $ursUserId = (int)$this->argument('urs_user_id');
-
- $this->info("开始测试URS推荐奖励补发功能");
- $this->info("URS用户ID: {$ursUserId}");
- $this->line('');
- // 1. 显示用户基本信息
- $this->displayUserInfo($ursUserId);
- // 2. 显示用户下级统计
- $this->displaySubordinateStats($ursUserId);
- // 3. 检查是否需要补发奖励
- $this->checkBackfillNeed($ursUserId);
- // 4. 测试事件触发
- $this->testEventTrigger($ursUserId);
- // 5. 模拟补发奖励(如果用户已进入农场)
- $this->simulateBackfillReward($ursUserId);
- $this->line('');
- $this->info('测试完成!');
- }
- /**
- * 显示用户基本信息
- */
- private function displayUserInfo(int $ursUserId): void
- {
- $this->info("=== 用户基本信息 ===");
- // 检查是否已进入农场
- $hasEntered = UrsUserMappingService::hasEnteredFarm($ursUserId);
- $this->line("是否已进入农场: " . ($hasEntered ? '是' : '否'));
- if ($hasEntered) {
- $farmUserId = UrsUserMappingService::getFarmUserId($ursUserId);
- $this->line("农场用户ID: {$farmUserId}");
- }
- // 获取达人等级
- $talentDto = UrsTalentService::getTalentInfo($ursUserId);
- if ($talentDto) {
- $this->line("达人等级: {$talentDto->talentLevel}");
- $this->line("直推人数: {$talentDto->directCount}");
- $this->line("团队总人数: {$talentDto->promotionCount}");
- } else {
- $this->line("达人等级: 0 (青铜级,默认等级)");
- }
- $this->line('');
- }
- /**
- * 显示用户下级统计
- */
- private function displaySubordinateStats(int $ursUserId): void
- {
- $this->info("=== 下级统计信息 ===");
- $stats = UrsBackfillRewardService::getSubordinateStats($ursUserId);
- $this->line("直推下级数量: {$stats['direct_count']}");
- $this->line("间推下级数量: {$stats['indirect_count']}");
- $this->line("三推下级数量: {$stats['third_count']}");
- $this->line("下级总数量: {$stats['total_count']}");
- if ($stats['total_count'] > 0) {
- // 显示直推下级详情
- $directReferrals = UrsReferralService::getDirectReferrals($ursUserId);
- if (!empty($directReferrals)) {
- $this->line("直推下级列表:");
- foreach ($directReferrals as $directUserId) {
- $hasEnteredFarm = UrsUserMappingService::hasEnteredFarm($directUserId);
- $status = $hasEnteredFarm ? '已进入农场' : '未进入农场';
- $this->line(" - URS用户ID: {$directUserId} ({$status})");
- }
- }
- }
- $this->line('');
- }
- /**
- * 检查是否需要补发奖励
- */
- private function checkBackfillNeed(int $ursUserId): void
- {
- $this->info("=== 补发奖励检查 ===");
- $needsBackfill = UrsBackfillRewardService::needsBackfillReward($ursUserId);
- $this->line("是否需要补发奖励: " . ($needsBackfill ? '是' : '否'));
- if (!$needsBackfill) {
- $this->warn("用户无下级,无需补发奖励");
- }
- $this->line('');
- }
- /**
- * 测试事件触发
- */
- private function testEventTrigger(int $ursUserId): void
- {
- $this->info("=== 事件触发测试 ===");
- // 检查用户是否已进入农场
- $farmUserId = UrsUserMappingService::getFarmUserId($ursUserId);
- if (!$farmUserId) {
- $this->warn("用户尚未进入农场,无法测试事件触发");
- $this->line('');
- return;
- }
- // 手动触发用户进入农场事件
- $this->line("手动触发URS用户进入农场事件...");
- try {
- event(new UrsUserEnteredFarmEvent($ursUserId, $farmUserId, 'test_user_key', true));
- $this->info("✅ 事件触发成功!");
- } catch (\Exception $e) {
- $this->error("❌ 事件触发失败: " . $e->getMessage());
- }
- $this->line('');
- }
- /**
- * 模拟补发奖励
- */
- private function simulateBackfillReward(int $ursUserId): void
- {
- $this->info("=== 补发奖励测试 ===");
- // 检查用户是否已进入农场
- $farmUserId = UrsUserMappingService::getFarmUserId($ursUserId);
- if (!$farmUserId) {
- $this->warn("用户尚未进入农场,无法测试补发奖励");
- return;
- }
- // 执行补发奖励
- $this->line("正在执行补发奖励...");
- $result = UrsBackfillRewardService::backfillPromotionReward($ursUserId, $farmUserId);
- if ($result['success']) {
- $this->info("✅ 补发奖励成功!");
- $this->line("补发奖励总数量: {$result['backfilled_count']}");
- $this->line("下级总人数: {$result['total_subordinates']}");
- $this->line("达人等级: {$result['talent_level']}");
- // 显示分层级奖励详情
- if (!empty($result['reward_details'])) {
- $this->line("奖励详情:");
- foreach ($result['reward_details'] as $type => $detail) {
- $this->line(" - {$detail['type']}: {$detail['count']}人 -> {$detail['items_count']}个奖励 (奖励组ID: {$detail['reward_group_id']})");
- }
- }
- } else {
- $this->error("❌ 补发奖励失败");
- $this->line("失败原因: {$result['message']}");
- $this->line("下级总人数: {$result['total_subordinates']}");
- $this->line("达人等级: {$result['talent_level']}");
- }
- }
- }
|