CleanExpiredLogsCommand.php 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. <?php
  2. namespace App\Module\Farm\Commands;
  3. use App\Module\Farm\Repositories\FarmHarvestLogRepository;
  4. use App\Module\Farm\Repositories\FarmTeamProfitRepository;
  5. use App\Module\Farm\Repositories\FarmUpgradeLogRepository;
  6. use Illuminate\Console\Command;
  7. use Illuminate\Support\Facades\Log;
  8. /**
  9. * 清理过期日志命令
  10. */
  11. class CleanExpiredLogsCommand extends Command
  12. {
  13. /**
  14. * 命令名称
  15. *
  16. * @var string
  17. */
  18. protected $signature = 'farm:clean-expired-logs {--days=90 : 保留天数}';
  19. /**
  20. * 命令描述
  21. *
  22. * @var string
  23. */
  24. protected $description = '清理过期的日志记录';
  25. /**
  26. * 执行命令
  27. *
  28. * @return int
  29. */
  30. public function handle()
  31. {
  32. $days = $this->option('days');
  33. $this->info("开始清理 {$days} 天前的日志记录...");
  34. try {
  35. // 清理收获记录
  36. $harvestLogRepository = new FarmHarvestLogRepository();
  37. $harvestCount = $harvestLogRepository->cleanupOldLogs($days);
  38. $this->info("清理了 {$harvestCount} 条收获记录");
  39. // 清理升级记录
  40. $upgradeLogRepository = new FarmUpgradeLogRepository();
  41. $upgradeCount = $upgradeLogRepository->cleanupOldLogs($days);
  42. $this->info("清理了 {$upgradeCount} 条升级记录");
  43. // 清理团队收益记录
  44. $teamProfitRepository = new FarmTeamProfitRepository();
  45. $profitCount = $teamProfitRepository->cleanupOldLogs($days);
  46. $this->info("清理了 {$profitCount} 条团队收益记录");
  47. $totalCount = $harvestCount + $upgradeCount + $profitCount;
  48. $this->info("共清理了 {$totalCount} 条日志记录");
  49. Log::info('日志清理成功', [
  50. 'days' => $days,
  51. 'harvest_count' => $harvestCount,
  52. 'upgrade_count' => $upgradeCount,
  53. 'profit_count' => $profitCount,
  54. 'total_count' => $totalCount
  55. ]);
  56. return 0;
  57. } catch (\Exception $e) {
  58. $this->error('日志清理失败: ' . $e->getMessage());
  59. Log::error('日志清理失败', [
  60. 'days' => $days,
  61. 'error' => $e->getMessage(),
  62. 'trace' => $e->getTraceAsString()
  63. ]);
  64. return 1;
  65. }
  66. }
  67. }