| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209 |
- <?php
- namespace App\Module\UrsPromotion\Commands;
- use App\Module\UrsPromotion\Services\UrsUserMappingService;
- use App\Module\UrsPromotion\Services\UrsBackfillRewardService;
- use Illuminate\Console\Command;
- /**
- * URS用户进入农场奖励补发集成测试命令
- */
- class TestUrsEntryRewardIntegrationCommand extends Command
- {
- /**
- * 命令签名
- *
- * @var string
- */
- protected $signature = 'urs:test-entry-reward-integration {urs_user_id : URS用户ID} {--clean : 清理测试数据}';
- /**
- * 命令描述
- *
- * @var string
- */
- protected $description = '测试URS用户进入农场时的推荐奖励补发完整流程';
- /**
- * 执行命令
- */
- public function handle()
- {
- $ursUserId = (int)$this->argument('urs_user_id');
- $shouldClean = $this->option('clean');
-
- $this->info("开始URS用户进入农场奖励补发集成测试");
- $this->info("URS用户ID: {$ursUserId}");
- $this->line('');
- // 1. 清理测试数据(如果需要)
- if ($shouldClean) {
- $this->cleanTestData($ursUserId);
- }
- // 2. 显示测试前状态
- $this->displayPreTestStatus($ursUserId);
- // 3. 模拟用户进入农场流程
- $this->simulateUserEntryFlow($ursUserId);
- // 4. 验证测试结果
- $this->verifyTestResults($ursUserId);
- $this->line('');
- $this->info('集成测试完成!');
- }
- /**
- * 清理测试数据
- */
- private function cleanTestData(int $ursUserId): void
- {
- $this->info("=== 清理测试数据 ===");
- try {
- // 删除用户映射关系
- $deletedMappings = DB::connection('mysql')->table('kku_urs_promotion_user_mappings')
- ->where('urs_user_id', $ursUserId)
- ->delete();
- // 删除奖励记录
- $deletedRewards = DB::connection('mysql')->table('kku_game_reward_logs')
- ->where('source_type', 'urs_promotion_backfill')
- ->where('source_id', $ursUserId)
- ->delete();
- $this->line("已删除映射关系: {$deletedMappings} 条");
- $this->line("已删除奖励记录: {$deletedRewards} 条");
- } catch (\Exception $e) {
- $this->error("清理测试数据失败: " . $e->getMessage());
- }
- $this->line('');
- }
- /**
- * 显示测试前状态
- */
- private function displayPreTestStatus(int $ursUserId): void
- {
- $this->info("=== 测试前状态 ===");
- // 检查是否已进入农场
- $hasEntered = UrsUserMappingService::hasEnteredFarm($ursUserId);
- $this->line("是否已进入农场: " . ($hasEntered ? '是' : '否'));
- // 检查下级统计
- $stats = UrsBackfillRewardService::getSubordinateStats($ursUserId);
- $this->line("直推下级数量: {$stats['direct_count']}");
- $this->line("间推下级数量: {$stats['indirect_count']}");
- $this->line("三推下级数量: {$stats['third_count']}");
- $this->line("下级总数量: {$stats['total_count']}");
- // 检查现有奖励记录
- $existingRewards = DB::connection('mysql')->table('kku_game_reward_logs')
- ->where('source_type', 'urs_promotion_backfill')
- ->where('source_id', $ursUserId)
- ->count();
- $this->line("现有奖励记录数量: {$existingRewards}");
- $this->line('');
- }
- /**
- * 模拟用户进入农场流程
- */
- private function simulateUserEntryFlow(int $ursUserId): void
- {
- $this->info("=== 模拟用户进入农场流程 ===");
- try {
- // 模拟用户通过userKey进入农场
- $userKey = "test_user_key_{$ursUserId}";
-
- $this->line("模拟用户登录并进入农场...");
- $this->line("UserKey: {$userKey}");
- // 调用获取农场用户ID的方法,这会触发自动创建和事件
- $farmUserId = UrsUserMappingService::getFarmUserIdByUserKeyWithAutoCreate($userKey, $ursUserId);
- if ($farmUserId) {
- $this->info("✅ 用户成功进入农场");
- $this->line("农场用户ID: {$farmUserId}");
-
- // 等待一下让事件处理完成
- sleep(1);
-
- } else {
- $this->error("❌ 用户进入农场失败");
- }
- } catch (\Exception $e) {
- $this->error("模拟用户进入农场流程失败: " . $e->getMessage());
- }
- $this->line('');
- }
- /**
- * 验证测试结果
- */
- private function verifyTestResults(int $ursUserId): void
- {
- $this->info("=== 验证测试结果 ===");
- try {
- // 1. 验证用户映射关系
- $hasEntered = UrsUserMappingService::hasEnteredFarm($ursUserId);
- $this->line("用户是否已进入农场: " . ($hasEntered ? '✅ 是' : '❌ 否'));
- if ($hasEntered) {
- $farmUserId = UrsUserMappingService::getFarmUserId($ursUserId);
- $this->line("农场用户ID: {$farmUserId}");
- }
- // 2. 验证奖励记录
- $rewardLogs = DB::connection('mysql')->table('kku_game_reward_logs')
- ->where('source_type', 'urs_promotion_backfill')
- ->where('source_id', $ursUserId)
- ->get();
- $this->line("奖励记录数量: " . $rewardLogs->count());
- if ($rewardLogs->count() > 0) {
- $this->line("奖励详情:");
- $groupedRewards = $rewardLogs->groupBy('group_id');
-
- foreach ($groupedRewards as $groupId => $rewards) {
- $totalQuantity = $rewards->sum(function($reward) {
- $items = json_decode($reward->reward_items, true);
- return array_sum(array_column($items, 'quantity'));
- });
-
- $this->line(" - 奖励组ID {$groupId}: {$rewards->count()} 条记录, 总数量: {$totalQuantity}");
- }
- }
- // 3. 验证下级统计
- $stats = UrsBackfillRewardService::getSubordinateStats($ursUserId);
- $expectedRewards = 0;
- if ($stats['direct_count'] > 0) $expectedRewards++;
- if ($stats['indirect_count'] > 0) $expectedRewards++;
- if ($stats['third_count'] > 0) $expectedRewards++;
- $this->line("预期奖励记录数: {$expectedRewards}");
-
- // 4. 结果判断
- if ($hasEntered && $rewardLogs->count() >= $expectedRewards) {
- $this->info("🎉 集成测试通过!");
- } else {
- $this->warn("⚠️ 集成测试结果异常,请检查日志");
- }
- } catch (\Exception $e) {
- $this->error("验证测试结果失败: " . $e->getMessage());
- }
- }
- }
|