relationCacheLogic = $relationCacheLogic; } /** * 执行命令 * * @return int */ public function handle() { $this->info('开始重建关系缓存...'); $userId = $this->option('user-id'); $showDebugInfo = $this->option('debug-info'); try { if ($userId) { // 重建指定用户的关系缓存 $this->relationCacheLogic->rebuildUserRelationCache($userId); if ($showDebugInfo) { $this->info("用户 {$userId} 的关系缓存重建完成"); } else { $this->info("重建完成"); } } else { // 重建所有用户的关系缓存 $this->relationCacheLogic->clearAllRelationCache(); $users = PromotionUserReferral::all(); $total = $users->count(); $current = 0; if ($showDebugInfo) { $this->info("共有 {$total} 个用户需要重建关系缓存"); $bar = $this->output->createProgressBar($total); $bar->start(); } foreach ($users as $user) { $this->relationCacheLogic->buildUserRelationCache($user->user_id); $current++; if ($showDebugInfo) { $bar->advance(); } } if ($showDebugInfo) { $bar->finish(); $this->newLine(); $this->info("所有用户的关系缓存重建完成"); } else { $this->info("重建完成"); } } return 0; } catch (\Exception $e) { $this->error("重建关系缓存失败: " . $e->getMessage()); if ($showDebugInfo) { $this->error($e->getTraceAsString()); } return 1; } } }