argument('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 ($userId) { // 更新指定用户 return $this->updateSingleUser((int) $userId, $force, $dryRun); } else { // 批量更新所有用户 return $this->updateAllUsers($batchSize, $force, $dryRun); } } catch (\Exception $e) { $this->error('命令执行失败: ' . $e->getMessage()); Log::error('URS达人等级更新命令执行失败', [ 'user_id' => $userId, 'batch_size' => $batchSize, 'force' => $force, 'dry_run' => $dryRun, 'error' => $e->getMessage(), 'trace' => $e->getTraceAsString() ]); return 1; } } /** * 更新指定用户的达人等级 * * @param int $userId 农场用户ID * @param bool $force 是否强制重新计算 * @param bool $dryRun 是否仅模拟运行 * @return int 命令退出码 */ private function updateSingleUser(int $userId, bool $force, bool $dryRun): int { $this->info("开始更新用户 {$userId} 的达人等级..."); // 检查用户是否有URS映射关系 $ursUserId = UrsUserMappingService::getMappingUrsUserId($userId); if ($ursUserId === 0) { $this->error("用户 {$userId} 没有URS映射关系,无法更新达人等级"); return 1; } $this->info("找到URS用户ID: {$ursUserId}"); if ($dryRun) { $this->info("模拟运行:将调用 UrsTalentService::updateTalentLevel({$userId})"); return 0; } try { // 调用达人等级更新服务 $talentDto = UrsTalentService::updateTalentLevel($userId); if ($talentDto) { $this->info("✓ 成功更新用户 {$userId} 的达人等级"); // 基础信息表格 $this->table(['属性', '值'], [ ['URS用户ID', $talentDto->ursUserId], ['达人等级', $talentDto->talentLevel], ['等级名称', $talentDto->talentName], ['最后更新时间', $talentDto->lastLevelUpdateTime ?? '未更新'] ]); // 团队统计表格 $this->line(''); $this->info('📊 团队统计数据:'); $this->table(['统计项', '总数', '活跃数', '活跃率'], [ [ '直推人数', $talentDto->directCount, $talentDto->activeDirectCount, $talentDto->directCount > 0 ? round($talentDto->activeDirectCount * 100 / $talentDto->directCount, 1) . '%' : '0%' ], ['间推人数', $talentDto->indirectCount, '-', '-'], ['三推人数', $talentDto->thirdCount, '-', '-'], [ '团队总人数', $talentDto->promotionCount, $talentDto->activeTotalCount, $talentDto->promotionCount > 0 ? round($talentDto->activeTotalCount * 100 / $talentDto->promotionCount, 1) . '%' : '0%' ] ]); // 升级条件检查 if ($talentDto->nextConfig) { $this->line(''); $this->info('🎯 下一等级升级条件:'); $nextLevel = $talentDto->nextConfig; $directMet = $talentDto->directCount >= $nextLevel['direct_count_required']; $teamMet = $talentDto->promotionCount >= $nextLevel['promotion_count_required']; $activeDirectMet = $talentDto->activeDirectCount >= ($nextLevel['active_direct_required'] ?? 0); $this->table(['条件', '要求', '当前', '状态'], [ [ '直推人数', $nextLevel['direct_count_required'], $talentDto->directCount, $directMet ? '✅ 已满足' : '❌ 未满足' ], [ '团队总人数', $nextLevel['promotion_count_required'], $talentDto->promotionCount, $teamMet ? '✅ 已满足' : '❌ 未满足' ], [ '活跃直推', $nextLevel['active_direct_required'] ?? 0, $talentDto->activeDirectCount, $activeDirectMet ? '✅ 已满足' : '❌ 未满足' ] ]); $allMet = $directMet && $teamMet && $activeDirectMet; if ($allMet) { $this->info("🎉 恭喜!所有升级条件已满足,可升级到 {$nextLevel['name']}"); } else { $this->warn("⚠️ 还有条件未满足,无法升级到 {$nextLevel['name']}"); } } } else { $this->error("✗ 用户 {$userId} 达人等级更新失败"); return 1; } Log::info('URS达人等级更新成功', [ 'user_id' => $userId, 'urs_user_id' => $ursUserId, 'talent_level' => $talentDto->talentLevel, 'talent_name' => $talentDto->talentName, 'force' => $force ]); return 0; } catch (\Exception $e) { $this->error("✗ 用户 {$userId} 达人等级更新失败: " . $e->getMessage()); Log::error('URS达人等级更新失败', [ 'user_id' => $userId, 'urs_user_id' => $ursUserId, 'error' => $e->getMessage(), 'trace' => $e->getTraceAsString() ]); return 1; } } /** * 批量更新所有用户的达人等级 * * @param int $batchSize 批处理大小 * @param bool $force 是否强制重新计算 * @param bool $dryRun 是否仅模拟运行 * @return int 命令退出码 */ private function updateAllUsers(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; $errorCount = 0; $processedCount = 0; // 分批处理用户 UrsUserMapping::where('status', UrsUserMapping::STATUS_VALID) ->chunk($batchSize, function ($mappings) use ( &$successCount, &$errorCount, &$processedCount, $progressBar, $force ) { foreach ($mappings as $mapping) { try { $talentDto = UrsTalentService::updateTalentLevel($mapping->user_id); if ($talentDto) { $successCount++; } else { $errorCount++; } } catch (\Exception $e) { $errorCount++; Log::error('批量更新用户达人等级失败', [ 'user_id' => $mapping->user_id, 'urs_user_id' => $mapping->urs_user_id, 'error' => $e->getMessage() ]); } $processedCount++; $progressBar->advance(); } }); $progressBar->finish(); $this->newLine(); // 显示更新结果 $this->info('=== 更新结果统计 ==='); $this->table(['项目', '数量'], [ ['总用户数', $totalUsers], ['处理用户数', $processedCount], ['成功更新数', $successCount], ['失败数量', $errorCount] ]); Log::info('URS达人等级批量更新完成', [ 'total_users' => $totalUsers, 'processed_count' => $processedCount, 'success_count' => $successCount, 'error_count' => $errorCount, 'batch_size' => $batchSize, 'force' => $force ]); if ($errorCount > 0) { $this->warn("有 {$errorCount} 个用户更新失败,请检查日志"); return 1; } $this->info('所有用户达人等级更新完成'); return 0; } }