| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241 |
- <?php
- namespace App\Module\UrsPromotion\Commands;
- use App\Module\UrsPromotion\Services\UrsActiveUserService;
- use Illuminate\Console\Command;
- use Illuminate\Support\Facades\Log;
- /**
- * URS用户活跃状态更新命令
- *
- * 每日定时执行,更新所有URS用户的活跃状态
- */
- class UrsUpdateActiveStatusCommand extends Command
- {
- /**
- * 命令签名
- */
- protected $signature = 'urs:update-active-status
- {--limit=1000 : 每次处理的用户数量限制}
- {--reset : 重置所有用户活跃状态}
- {--force : 强制检查所有用户,忽略last_activity_check时间限制}
- {--dry-run : 仅显示统计信息,不执行更新}';
- /**
- * 命令描述
- */
- protected $description = 'Update URS users active status based on last activity time';
- /**
- * 执行命令
- */
- public function handle()
- {
- $this->info('开始执行URS用户活跃状态更新任务...');
- $startTime = microtime(true);
- try {
- // 检查是否为重置模式
- if ($this->option('reset')) {
- return $this->handleReset();
- }
- // 检查是否为试运行模式
- if ($this->option('dry-run')) {
- return $this->handleDryRun();
- }
- // 执行正常的活跃状态更新
- return $this->handleUpdate();
- } catch (\Exception $e) {
- $this->error('执行失败:' . $e->getMessage());
- Log::error('URS用户活跃状态更新命令执行失败', [
- 'error' => $e->getMessage(),
- 'trace' => $e->getTraceAsString()
- ]);
- return 1;
- } finally {
- $duration = round(microtime(true) - $startTime, 2);
- $this->info("任务执行完成,耗时:{$duration}秒");
- }
- }
- /**
- * 处理正常的活跃状态更新
- */
- protected function handleUpdate(): int
- {
- $limit = (int) $this->option('limit');
- $force = $this->option('force');
- if ($force) {
- $this->info("开始强制更新所有用户活跃状态(限制:{$limit})...");
- } else {
- $this->info("开始批量更新用户活跃状态(限制:{$limit})...");
- }
- // 显示更新前的统计信息
- $beforeStats = UrsActiveUserService::getDetailedActiveStats();
- $this->displayStats('更新前统计', $beforeStats);
- // 执行批量更新
- if ($force) {
- $updateStats = UrsActiveUserService::forceUpdateActiveStatus($limit);
- } else {
- $updateStats = UrsActiveUserService::batchUpdateActiveStatus($limit);
- }
- $this->displayUpdateStats($updateStats);
- // 显示更新后的统计信息
- $afterStats = UrsActiveUserService::getDetailedActiveStats();
- $this->displayStats('更新后统计', $afterStats);
- // 记录执行日志
- Log::info('URS用户活跃状态更新任务完成', [
- 'force_mode' => $force,
- 'before_stats' => $beforeStats,
- 'update_stats' => $updateStats,
- 'after_stats' => $afterStats
- ]);
- $this->info('✅ 活跃状态更新完成');
- return 0;
- }
- /**
- * 处理重置模式
- */
- protected function handleReset(): int
- {
- if (!$this->confirm('确定要重置所有用户的活跃状态吗?此操作不可逆!')) {
- $this->info('操作已取消');
- return 0;
- }
- $this->info('开始重置所有用户活跃状态...');
-
- $result = UrsActiveUserService::resetAllActiveStatus();
-
- if ($result['success']) {
- $this->info("✅ 重置完成,共更新 {$result['updated_count']} 个用户");
- Log::info('URS用户活跃状态重置完成', $result);
- } else {
- $this->error("❌ 重置失败:{$result['message']}");
- return 1;
- }
- return 0;
- }
- /**
- * 处理试运行模式
- */
- protected function handleDryRun(): int
- {
- $this->info('试运行模式:仅显示统计信息,不执行更新');
-
- $stats = UrsActiveUserService::getDetailedActiveStats();
- $this->displayStats('当前统计', $stats);
- // 显示需要检查的用户示例
- if ($stats['need_check_count'] > 0) {
- $this->info("\n需要检查活跃状态的用户示例:");
- $needCheckUsers = \App\Module\UrsPromotion\Models\UrsUserMapping::getUsersNeedActivityCheck(5);
-
- $headers = ['URS用户ID', '农场用户ID', '上次检查时间', '用户最后活动时间'];
- $rows = [];
-
- foreach ($needCheckUsers as $mapping) {
- $rows[] = [
- $mapping->urs_user_id,
- $mapping->user_id,
- $mapping->last_activity_check ? $mapping->last_activity_check->format('Y-m-d H:i:s') : '从未检查',
- $mapping->user && $mapping->user->last_activity_time
- ? $mapping->user->last_activity_time->format('Y-m-d H:i:s')
- : '无活动记录'
- ];
- }
-
- $this->table($headers, $rows);
- }
- return 0;
- }
- /**
- * 显示统计信息
- */
- protected function displayStats(string $title, array $stats): void
- {
- $this->info("\n📊 {$title}:");
- $this->line("总用户数:{$stats['total_users']}");
- $this->line("活跃用户:{$stats['active_users']}");
- $this->line("不活跃用户:{$stats['inactive_users']}");
- $this->line("活跃比例:{$stats['active_percentage']}%");
-
- if (isset($stats['recent_updates'])) {
- $this->line("最近24小时更新:{$stats['recent_updates']}");
- }
-
- if (isset($stats['need_check_count'])) {
- $this->line("需要检查的用户:{$stats['need_check_count']}");
- }
-
- if (isset($stats['last_update_time'])) {
- $lastUpdate = $stats['last_update_time']
- ? \Carbon\Carbon::parse($stats['last_update_time'])->format('Y-m-d H:i:s')
- : '从未更新';
- $this->line("最后更新时间:{$lastUpdate}");
- }
- }
- /**
- * 显示更新统计信息
- */
- protected function displayUpdateStats(array $stats): void
- {
- $this->info("\n🔄 更新统计:");
- $this->line("处理总数:{$stats['total_processed']}");
- $this->line("成功更新:{$stats['successful_updates']}");
- $this->line("失败数量:{$stats['failed_updates']}");
- $this->line("新增活跃:{$stats['active_users']}");
- $this->line("新增不活跃:{$stats['inactive_users']}");
-
- if ($stats['failed_updates'] > 0) {
- $this->warn("⚠️ 有 {$stats['failed_updates']} 个用户更新失败,请检查日志");
- }
- }
- /**
- * 获取命令帮助信息
- */
- public function getHelp(): string
- {
- return <<<HELP
- URS用户活跃状态更新命令
- 用法:
- php artisan urs:update-active-status [选项]
- 选项:
- --limit=1000 每次处理的用户数量限制(默认1000)
- --reset 重置所有用户活跃状态
- --force 强制检查所有用户,忽略last_activity_check时间限制
- --dry-run 仅显示统计信息,不执行更新
- 示例:
- php artisan urs:update-active-status # 正常更新
- php artisan urs:update-active-status --limit=500 # 限制处理500个用户
- php artisan urs:update-active-status --force # 强制更新所有用户
- php artisan urs:update-active-status --dry-run # 试运行模式
- php artisan urs:update-active-status --reset # 重置所有状态
- 说明:
- - 活跃用户定义:最近15天有活动的用户
- - 建议每日凌晨执行此命令
- - 可以通过crontab设置定时任务:0 2 * * * php artisan urs:update-active-status
- HELP;
- }
- }
|