| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114 |
- <?php
- namespace App\Module\Team\Commands;
- use App\Module\Team\Logics\RelationCacheLogic;
- use App\Module\Team\Models\TeamUserReferral;
- use Illuminate\Console\Command;
- /**
- * 重建关系缓存命令
- *
- * 用于重建用户关系缓存,确保缓存数据的准确性。
- */
- class RebuildRelationCacheCommand extends Command
- {
- /**
- * 命令名称
- *
- * @var string
- */
- protected $signature = 'team:rebuild-relation-cache {--user-id= : 指定用户ID} {--debug-info : 显示详细信息}';
- /**
- * 命令描述
- *
- * @var string
- */
- protected $description = '重建用户关系缓存';
- /**
- * 关系缓存逻辑类
- *
- * @var RelationCacheLogic
- */
- protected $relationCacheLogic;
- /**
- * 创建命令实例
- *
- * @param RelationCacheLogic $relationCacheLogic 关系缓存逻辑类
- * @return void
- */
- public function __construct(RelationCacheLogic $relationCacheLogic)
- {
- parent::__construct();
- $this->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 = TeamUserReferral::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;
- }
- }
- }
|