| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980 |
- <?php
- namespace App\Module\Farm\Commands;
- use App\Module\Farm\Repositories\FarmHarvestLogRepository;
- use App\Module\Farm\Repositories\FarmTeamProfitRepository;
- use App\Module\Farm\Repositories\FarmUpgradeLogRepository;
- use Illuminate\Console\Command;
- use Illuminate\Support\Facades\Log;
- /**
- * 清理过期日志命令
- */
- class CleanExpiredLogsCommand extends Command
- {
- /**
- * 命令名称
- *
- * @var string
- */
- protected $signature = 'farm:clean-expired-logs {--days=90 : 保留天数}';
- /**
- * 命令描述
- *
- * @var string
- */
- protected $description = '清理过期的日志记录';
- /**
- * 执行命令
- *
- * @return int
- */
- public function handle()
- {
- $days = $this->option('days');
- $this->info("开始清理 {$days} 天前的日志记录...");
-
- try {
- // 清理收获记录
- $harvestLogRepository = new FarmHarvestLogRepository();
- $harvestCount = $harvestLogRepository->cleanupOldLogs($days);
- $this->info("清理了 {$harvestCount} 条收获记录");
-
- // 清理升级记录
- $upgradeLogRepository = new FarmUpgradeLogRepository();
- $upgradeCount = $upgradeLogRepository->cleanupOldLogs($days);
- $this->info("清理了 {$upgradeCount} 条升级记录");
-
- // 清理团队收益记录
- $teamProfitRepository = new FarmTeamProfitRepository();
- $profitCount = $teamProfitRepository->cleanupOldLogs($days);
- $this->info("清理了 {$profitCount} 条团队收益记录");
-
- $totalCount = $harvestCount + $upgradeCount + $profitCount;
- $this->info("共清理了 {$totalCount} 条日志记录");
-
- Log::info('日志清理成功', [
- 'days' => $days,
- 'harvest_count' => $harvestCount,
- 'upgrade_count' => $upgradeCount,
- 'profit_count' => $profitCount,
- 'total_count' => $totalCount
- ]);
-
- return 0;
- } catch (\Exception $e) {
- $this->error('日志清理失败: ' . $e->getMessage());
-
- Log::error('日志清理失败', [
- 'days' => $days,
- 'error' => $e->getMessage(),
- 'trace' => $e->getTraceAsString()
- ]);
-
- return 1;
- }
- }
- }
|