ViewPlanContentsAction.php 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. <?php
  2. namespace App\Module\Cleanup\AdminControllers\Actions;
  3. use App\Module\Cleanup\Models\CleanupPlan;
  4. use Dcat\Admin\Actions\RowAction;
  5. use Dcat\Admin\Actions\Response;
  6. use Illuminate\Http\Request;
  7. /**
  8. * 查看计划内容Action
  9. *
  10. * 用于查看清理计划的详细内容
  11. */
  12. class ViewPlanContentsAction extends RowAction
  13. {
  14. /**
  15. * 按钮标题
  16. */
  17. protected $title = '查看内容';
  18. /**
  19. * 处理请求
  20. */
  21. public function handle(Request $request)
  22. {
  23. try {
  24. $planId = $this->getKey();
  25. $plan = CleanupPlan::with('contents')->find($planId);
  26. if (!$plan) {
  27. return $this->response()
  28. ->error('计划不存在');
  29. }
  30. $contents = $plan->contents;
  31. if ($contents->isEmpty()) {
  32. return $this->response()
  33. ->info('该计划暂无内容');
  34. }
  35. $html = '<div class="table-responsive"><table class="table table-striped table-sm">';
  36. $html .= '<thead><tr><th>表名</th><th>清理类型</th><th>优先级</th><th>批处理大小</th><th>启用状态</th><th>备份</th></tr></thead>';
  37. $html .= '<tbody>';
  38. $cleanupTypes = [
  39. 1 => '清空表',
  40. 2 => '删除所有',
  41. 3 => '按时间删除',
  42. 4 => '按用户删除',
  43. 5 => '按条件删除',
  44. ];
  45. foreach ($contents as $content) {
  46. $cleanupType = $cleanupTypes[$content->cleanup_type] ?? '未知';
  47. $enabled = $content->is_enabled ? '<span class="badge badge-success">启用</span>' : '<span class="badge badge-secondary">禁用</span>';
  48. $backup = $content->backup_enabled ? '<span class="badge badge-info">是</span>' : '<span class="badge badge-secondary">否</span>';
  49. $html .= "<tr>";
  50. $html .= "<td>{$content->table_name}</td>";
  51. $html .= "<td>{$cleanupType}</td>";
  52. $html .= "<td>{$content->priority}</td>";
  53. $html .= "<td>" . number_format($content->batch_size) . "</td>";
  54. $html .= "<td>{$enabled}</td>";
  55. $html .= "<td>{$backup}</td>";
  56. $html .= "</tr>";
  57. }
  58. $html .= '</tbody></table></div>';
  59. return $this->response()
  60. ->success('计划内容')
  61. ->detail($html);
  62. } catch (\Exception $e) {
  63. return $this->response()
  64. ->error('查看失败:' . $e->getMessage());
  65. }
  66. }
  67. /**
  68. * 渲染按钮
  69. */
  70. public function render()
  71. {
  72. return <<<HTML
  73. <a href="javascript:void(0);" class="btn btn-primary btn-xs" data-action="{$this->getHandleRoute()}">
  74. <i class="fa fa-list"></i> {$this->title}
  75. </a>
  76. HTML;
  77. }
  78. }