| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236 |
- <?php
- namespace App\Module\UrsPromotion\Commands;
- use App\Module\AppGame\Handler\Public\Login4uHandler;
- use App\Module\UrsPromotion\Models\UrsUserMapping;
- use App\Module\UrsPromotion\Services\UrsUserMappingService;
- use Illuminate\Console\Command;
- use Illuminate\Support\Facades\Log;
- /**
- * URS推荐关系同步命令
- *
- * 用于从URS获取用户上级关系,同步到本地数据库
- * 支持同步指定用户或所有用户的推荐关系
- * php artisan urs:sync-referral 32424 --force
- */
- class UrsReferralSyncCommand extends Command
- {
- /**
- * 命令签名
- */
- protected $signature = 'urs:sync-referral
- {urs_user_id? : URS用户ID,不指定则同步所有用户}
- {--batch-size=100 : 批处理大小}
- {--force : 强制重新同步已存在的关系}
- {--dry-run : 仅模拟运行,不实际执行同步}';
- /**
- * 命令描述
- */
- protected $description = 'URS推荐关系同步命令 - 从URS获取用户上级关系并同步到本地数据库';
- /**
- * 执行命令
- */
- public function handle()
- {
- $ursUserId = $this->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;
- }
- }
|