| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162 |
- <?php
- namespace App\Module\Game\Commands;
- use App\Module\Game\Models\GameRewardLog;
- use Carbon\Carbon;
- use Illuminate\Console\Command;
- /**
- * 清理过期奖励日志命令
- */
- class CleanExpiredRewardLogsCommand extends Command
- {
- /**
- * 命令名称
- *
- * @var string
- */
- protected $signature = 'game:clean-reward-logs {days=30 : 保留多少天内的日志}';
- /**
- * 命令描述
- *
- * @var string
- */
- protected $description = '清理过期的奖励发放日志';
- /**
- * 执行命令
- *
- * @return int
- */
- public function handle()
- {
- $days = (int)$this->argument('days');
-
- if ($days <= 0) {
- $this->error("保留天数必须大于0");
- return 1;
- }
-
- $cutoffDate = Carbon::now()->subDays($days);
- $this->info("将清理 {$cutoffDate} 之前的奖励日志");
-
- // 询问确认
- if (!$this->confirm('确定要继续吗?此操作不可撤销')) {
- $this->info('操作已取消');
- return 0;
- }
-
- try {
- // 删除过期日志
- $count = GameRewardLog::where('created_at', '<', $cutoffDate)->delete();
-
- $this->info("成功清理 {$count} 条过期奖励日志");
- return 0;
- } catch (\Exception $e) {
- $this->error("清理过期奖励日志失败: {$e->getMessage()}");
- return 1;
- }
- }
- }
|