talentLogic = $talentLogic; } /** * 执行命令 * * @return int */ public function handle() { $this->info('开始更新达人等级...'); $userId = $this->option('user-id'); $showDebugInfo = $this->option('debug-info'); try { if ($userId) { // 更新指定用户的达人等级 $result = $this->talentLogic->checkAndUpdateTalentLevel($userId); if ($showDebugInfo) { if ($result) { $talent = PromotionUserTalent::where('user_id', $userId)->first(); $this->info("用户 {$userId} 的达人等级更新为 {$talent->talent_level}"); } else { $this->info("用户 {$userId} 的达人等级未变更"); } } else { $this->info("更新完成"); } } else { // 更新所有用户的达人等级 $users = PromotionUserTalent::all(); $total = $users->count(); $updated = 0; if ($showDebugInfo) { $this->info("共有 {$total} 个用户需要更新达人等级"); $bar = $this->output->createProgressBar($total); $bar->start(); } foreach ($users as $user) { $result = $this->talentLogic->checkAndUpdateTalentLevel($user->user_id); if ($result) { $updated++; } if ($showDebugInfo) { $bar->advance(); } } if ($showDebugInfo) { $bar->finish(); $this->newLine(); $this->info("共更新了 {$updated} 个用户的达人等级"); } else { $this->info("更新完成"); } } return 0; } catch (\Exception $e) { $this->error("更新达人等级失败: " . $e->getMessage()); if ($showDebugInfo) { $this->error($e->getTraceAsString()); } return 1; } } }