| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364 |
- <?php
- namespace App\Module\Game\Commands;
- use App\Module\Game\Services\UserLogService;
- use UCore\Command\Command;
- /**
- * 清理过期用户日志命令
- */
- class CleanExpiredUserLogsCommand extends Command
- {
- /**
- * 命令名称和参数
- *
- * @var string
- */
- protected $signature = 'game:clean-expired-user-logs
- {--days=30 : 保留天数,默认30天}
- {--dry-run : 仅显示将要删除的记录数,不实际删除}';
- /**
- * 命令描述
- *
- * @var string
- */
- protected $description = '清理过期的用户日志记录';
- /**
- * 执行命令
- */
- public function handleRun()
- {
- $days = (int) $this->option('days');
- $dryRun = $this->option('dry-run');
- $this->info("开始清理 {$days} 天前的用户日志...");
- if ($dryRun) {
- $this->info('这是一次试运行,不会实际删除数据');
-
- // 计算将要删除的记录数
- $expiredDate = now()->subDays($days);
- $count = \App\Module\Game\Models\UserLog::where('created_at', '<', $expiredDate)->count();
-
- $this->info("将要删除 {$count} 条过期日志记录");
- $this->info("过期时间:{$expiredDate->toDateTimeString()}");
-
- return;
- }
- try {
- $deletedCount = UserLogService::cleanExpiredLogs($days);
-
- $this->info("成功清理 {$deletedCount} 条过期日志");
-
- } catch (\Exception $e) {
- $this->error("清理过期日志失败:{$e->getMessage()}");
- return 1;
- }
- return 0;
- }
- }
|