CreateTaskFromPlanAction.php 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. <?php
  2. namespace App\Module\Cleanup\AdminControllers\Actions;
  3. use App\Module\Cleanup\Services\CleanupService;
  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 CreateTaskFromPlanAction 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. $taskName = $request->input('task_name');
  26. if (empty($taskName)) {
  27. return $this->response()
  28. ->error('请输入任务名称');
  29. }
  30. // 调用服务创建任务
  31. $result = CleanupService::createTaskFromPlan($planId, $taskName);
  32. if (!$result['success']) {
  33. return $this->response()
  34. ->error('创建失败:' . $result['message']);
  35. }
  36. $task = $result['data'];
  37. return $this->response()
  38. ->success('任务创建成功!')
  39. ->detail("
  40. 任务ID:{$task['id']}<br>
  41. 任务名称:{$task['task_name']}<br>
  42. 关联计划:{$task['plan_name']}<br>
  43. 包含表数:{$task['total_tables']}<br>
  44. 状态:待执行
  45. ")
  46. ->redirect('/admin/cleanup/tasks/' . $task['id']);
  47. } catch (\Exception $e) {
  48. return $this->response()
  49. ->error('创建失败:' . $e->getMessage());
  50. }
  51. }
  52. /**
  53. * 确认对话框
  54. */
  55. public function confirm()
  56. {
  57. return [
  58. '确认创建任务?',
  59. '将基于此计划创建一个新的清理任务。',
  60. [
  61. 'task_name' => [
  62. 'type' => 'text',
  63. 'label' => '任务名称',
  64. 'placeholder' => '请输入任务名称',
  65. 'required' => true,
  66. ]
  67. ]
  68. ];
  69. }
  70. /**
  71. * 渲染按钮
  72. */
  73. public function render()
  74. {
  75. return <<<HTML
  76. <a href="javascript:void(0);" class="btn btn-success btn-xs" data-action="{$this->getHandleRoute()}">
  77. <i class="fa fa-plus"></i> {$this->title}
  78. </a>
  79. HTML;
  80. }
  81. }