CleanOldLogsAction.php 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. <?php
  2. namespace App\Module\Cleanup\AdminControllers\Actions;
  3. use App\Module\Cleanup\Services\CleanupService;
  4. use Dcat\Admin\Grid\Tools\AbstractTool;
  5. use Dcat\Admin\Actions\Response;
  6. use Illuminate\Http\Request;
  7. /**
  8. * 清理旧日志Action
  9. *
  10. * 用于清理过期的清理日志
  11. */
  12. class CleanOldLogsAction extends AbstractTool
  13. {
  14. /**
  15. * 按钮标题
  16. */
  17. protected $title = '清理旧日志';
  18. /**
  19. * 处理请求
  20. */
  21. public function handle(Request $request)
  22. {
  23. try {
  24. $retentionDays = $request->input('retention_days', 90);
  25. $dryRun = $request->input('dry_run', false);
  26. // 调用服务清理旧日志
  27. $result = CleanupService::cleanOldLogs($retentionDays, $dryRun);
  28. if (!$result['success']) {
  29. return $this->response()
  30. ->error('清理失败:' . $result['message']);
  31. }
  32. $data = $result['data'];
  33. if ($dryRun) {
  34. return $this->response()
  35. ->success('预览完成')
  36. ->detail("
  37. 保留天数:{$retentionDays} 天<br>
  38. 发现过期日志:" . number_format($data['expired_count']) . " 条<br>
  39. 最早日志时间:{$data['oldest_log_date']}<br>
  40. <br>
  41. <small>这是预览模式,没有实际删除任何日志。</small>
  42. ");
  43. } else {
  44. return $this->response()
  45. ->success('清理完成!')
  46. ->detail("
  47. 保留天数:{$retentionDays} 天<br>
  48. 删除日志:" . number_format($data['deleted_count']) . " 条<br>
  49. 清理时间:{$data['execution_time']}秒<br>
  50. 剩余日志:" . number_format($data['remaining_count']) . " 条
  51. ")
  52. ->refresh();
  53. }
  54. } catch (\Exception $e) {
  55. return $this->response()
  56. ->error('清理失败:' . $e->getMessage());
  57. }
  58. }
  59. /**
  60. * 确认对话框
  61. */
  62. public function confirm()
  63. {
  64. return [
  65. '清理旧日志',
  66. '将删除超过指定天数的清理日志记录。',
  67. [
  68. 'retention_days' => [
  69. 'type' => 'number',
  70. 'label' => '保留天数',
  71. 'value' => 90,
  72. 'min' => 1,
  73. 'max' => 365,
  74. 'required' => true,
  75. 'help' => '超过此天数的日志将被删除',
  76. ],
  77. 'dry_run' => [
  78. 'type' => 'checkbox',
  79. 'label' => '预览模式',
  80. 'checked' => true,
  81. 'help' => '勾选则只预览,不实际删除日志',
  82. ]
  83. ]
  84. ];
  85. }
  86. }