| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121 |
- <?php
- namespace App\Module\UrsPromotion\Commands;
- use App\Module\UrsPromotion\Logics\UrsRelationCacheLogic;
- use Illuminate\Console\Command;
- /**
- * URS用户关系缓存重建命令
- *
- * 用于批量重建URS用户关系缓存,提升查询性能
- */
- class UrsRebuildRelationCacheCommand extends Command
- {
- /**
- * 命令签名
- */
- protected $signature = 'urs:rebuild-relation-cache
- {--batch-size=100 : 批处理大小}
- {--check : 仅检查完整性,不重建}
- {--fix : 修复发现的问题}';
- /**
- * 命令描述
- */
- protected $description = 'URS用户关系缓存重建命令';
- /**
- * 执行命令
- */
- public function handle()
- {
- $logic = new UrsRelationCacheLogic();
-
- // 仅检查完整性
- if ($this->option('check')) {
- $this->info('开始检查URS关系缓存完整性...');
- $result = $logic->checkRelationCacheIntegrity();
-
- if (isset($result['error'])) {
- $this->error('检查失败: ' . $result['error']);
- return 1;
- }
-
- $this->info('检查结果:');
- $this->table(['项目', '数量'], [
- ['总用户数', $result['total_users']],
- ['有缓存的用户数', $result['users_with_cache']],
- ['缺失缓存的用户数', $result['missing_users']],
- ['循环推荐关系数', $result['circular_relations']],
- ['孤立缓存数', $result['orphaned_caches']]
- ]);
-
- if ($result['missing_users'] > 0 || $result['orphaned_caches'] > 0) {
- $this->warn('发现问题,建议使用 --fix 参数修复');
- return 1;
- }
-
- $this->info('缓存完整性检查通过');
- return 0;
- }
-
- // 修复问题
- if ($this->option('fix')) {
- $this->info('开始修复URS关系缓存问题...');
- $result = $logic->fixRelationCacheIssues();
-
- if (isset($result['error'])) {
- $this->error('修复失败: ' . $result['error']);
- return 1;
- }
-
- $this->info('修复结果:');
- $this->table(['项目', '修复数量'], [
- ['缺失用户缓存', $result['fixed']['missing_users']],
- ['孤立缓存清理', $result['fixed']['orphaned_caches']]
- ]);
-
- $this->info('问题修复完成');
- return 0;
- }
-
- // 重建所有缓存
- $batchSize = (int) $this->option('batch-size');
-
- if (!$this->confirm('确定要重建所有URS用户关系缓存吗?这将清空现有缓存并重新生成。')) {
- $this->info('操作已取消');
- return 0;
- }
-
- $this->info("开始重建URS关系缓存,批处理大小: {$batchSize}");
-
- $progressBar = $this->output->createProgressBar();
- $progressBar->start();
-
- $result = $logic->rebuildAllRelationCache($batchSize);
-
- $progressBar->finish();
- $this->newLine();
-
- if (isset($result['error'])) {
- $this->error('重建失败: ' . $result['error']);
- return 1;
- }
-
- $this->info('重建完成:');
- $this->table(['项目', '数量'], [
- ['总用户数', $result['total']],
- ['成功数', $result['success']],
- ['失败数', $result['fail']]
- ]);
-
- if ($result['fail'] > 0) {
- $this->warn('部分用户缓存生成失败,请检查日志');
- return 1;
- }
-
- $this->info('所有URS用户关系缓存重建完成');
- return 0;
- }
- }
|