argument('urs_user_id'); $batchSize = (int) $this->option('batch-size'); $force = $this->option('force'); $dryRun = $this->option('dry-run'); $this->info('=== URS推荐关系同步命令 ==='); if ($dryRun) { $this->warn('模拟运行模式 - 不会实际执行同步操作'); } try { if ($ursUserId) { // 同步指定用户 return $this->syncSingleUser((int) $ursUserId, $force, $dryRun); } else { // 同步所有用户 return $this->syncAllUsers($batchSize, $force, $dryRun); } } catch (\Exception $e) { $this->error('命令执行失败: ' . $e->getMessage()); Log::error('URS推荐关系同步命令执行失败', [ 'urs_user_id' => $ursUserId, 'batch_size' => $batchSize, 'force' => $force, 'dry_run' => $dryRun, 'error' => $e->getMessage(), 'trace' => $e->getTraceAsString() ]); return 1; } } /** * 同步指定用户的推荐关系 * * @param int $ursUserId URS用户ID * @param bool $force 是否强制重新同步 * @param bool $dryRun 是否仅模拟运行 * @return int 命令退出码 */ private function syncSingleUser(int $ursUserId, bool $force, bool $dryRun): int { $this->info("开始同步URS用户 {$ursUserId} 的推荐关系..."); // 检查用户是否已进入农场 $farmUserId = UrsUserMappingService::getFarmUserId($ursUserId); if (!$farmUserId) { $this->error("URS用户 {$ursUserId} 尚未进入农场,无法同步推荐关系"); return 1; } $this->info("找到农场用户ID: {$farmUserId}"); if ($dryRun) { $this->info("模拟运行:将调用 Login4uHandler::syncReferralRelations({$ursUserId}, {$farmUserId})"); return 0; } try { // 调用现有的同步方法 $isFirstEntry = Login4uHandler::syncReferralRelations($ursUserId, $farmUserId); if ($isFirstEntry) { $this->info("✓ 成功创建URS用户 {$ursUserId} 的推荐关系"); } else { if ($force) { $this->info("✓ URS用户 {$ursUserId} 的推荐关系已存在(强制模式)"); } else { $this->warn("URS用户 {$ursUserId} 的推荐关系已存在,跳过同步"); } } Log::info('URS推荐关系同步成功', [ 'urs_user_id' => $ursUserId, 'farm_user_id' => $farmUserId, 'is_first_entry' => $isFirstEntry, 'force' => $force ]); return 0; } catch (\Exception $e) { $this->error("✗ URS用户 {$ursUserId} 推荐关系同步失败: " . $e->getMessage()); Log::error('URS推荐关系同步失败', [ 'urs_user_id' => $ursUserId, 'farm_user_id' => $farmUserId, 'error' => $e->getMessage(), 'trace' => $e->getTraceAsString() ]); return 1; } } /** * 同步所有用户的推荐关系 * * @param int $batchSize 批处理大小 * @param bool $force 是否强制重新同步 * @param bool $dryRun 是否仅模拟运行 * @return int 命令退出码 */ private function syncAllUsers(int $batchSize, bool $force, bool $dryRun): int { $this->info("开始批量同步所有用户的推荐关系..."); $this->info("批处理大小: {$batchSize}"); // 获取所有有效的用户映射 $totalUsers = UrsUserMapping::where('status', UrsUserMapping::STATUS_VALID)->count(); if ($totalUsers === 0) { $this->warn('没有找到需要同步的用户'); return 0; } $this->info("找到 {$totalUsers} 个用户需要同步"); if ($dryRun) { $this->info("模拟运行:将分批处理 {$totalUsers} 个用户"); return 0; } $progressBar = $this->output->createProgressBar($totalUsers); $progressBar->start(); $successCount = 0; $skipCount = 0; $errorCount = 0; $processedCount = 0; // 分批处理用户 UrsUserMapping::where('status', UrsUserMapping::STATUS_VALID) ->chunk($batchSize, function ($mappings) use ( &$successCount, &$skipCount, &$errorCount, &$processedCount, $progressBar, $force ) { foreach ($mappings as $mapping) { try { $isFirstEntry = Login4uHandler::syncReferralRelations( $mapping->urs_user_id, $mapping->user_id ); if ($isFirstEntry) { $successCount++; } else { $skipCount++; } } catch (\Exception $e) { $errorCount++; Log::error('批量同步用户推荐关系失败', [ 'urs_user_id' => $mapping->urs_user_id, 'farm_user_id' => $mapping->user_id, 'error' => $e->getMessage() ]); } $processedCount++; $progressBar->advance(); } }); $progressBar->finish(); $this->newLine(); // 显示同步结果 $this->info('=== 同步结果统计 ==='); $this->table(['项目', '数量'], [ ['总用户数', $totalUsers], ['处理用户数', $processedCount], ['成功同步数', $successCount], ['跳过数量', $skipCount], ['失败数量', $errorCount] ]); Log::info('URS推荐关系批量同步完成', [ 'total_users' => $totalUsers, 'processed_count' => $processedCount, 'success_count' => $successCount, 'skip_count' => $skipCount, 'error_count' => $errorCount, 'batch_size' => $batchSize, 'force' => $force ]); if ($errorCount > 0) { $this->warn("有 {$errorCount} 个用户同步失败,请检查日志"); return 1; } $this->info('所有用户推荐关系同步完成'); return 0; } }