CleanExpiredRewardLogsCommand.php 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. <?php
  2. namespace App\Module\Game\Commands;
  3. use App\Module\Game\Models\GameRewardLog;
  4. use Carbon\Carbon;
  5. use Illuminate\Console\Command;
  6. /**
  7. * 清理过期奖励日志命令
  8. */
  9. class CleanExpiredRewardLogsCommand extends Command
  10. {
  11. /**
  12. * 命令名称
  13. *
  14. * @var string
  15. */
  16. protected $signature = 'game:clean-reward-logs {days=30 : 保留多少天内的日志}';
  17. /**
  18. * 命令描述
  19. *
  20. * @var string
  21. */
  22. protected $description = '清理过期的奖励发放日志';
  23. /**
  24. * 执行命令
  25. *
  26. * @return int
  27. */
  28. public function handle()
  29. {
  30. $days = (int)$this->argument('days');
  31. if ($days <= 0) {
  32. $this->error("保留天数必须大于0");
  33. return 1;
  34. }
  35. $cutoffDate = Carbon::now()->subDays($days);
  36. $this->info("将清理 {$cutoffDate} 之前的奖励日志");
  37. // 询问确认
  38. if (!$this->confirm('确定要继续吗?此操作不可撤销')) {
  39. $this->info('操作已取消');
  40. return 0;
  41. }
  42. try {
  43. // 删除过期日志
  44. $count = GameRewardLog::where('created_at', '<', $cutoffDate)->delete();
  45. $this->info("成功清理 {$count} 条过期奖励日志");
  46. return 0;
  47. } catch (\Exception $e) {
  48. $this->error("清理过期奖励日志失败: {$e->getMessage()}");
  49. return 1;
  50. }
  51. }
  52. }