CreateTaskFromPlanAction.php 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. <?php
  2. namespace App\Module\Cleanup\AdminControllers\Actions;
  3. use App\Module\Cleanup\Services\CleanupService;
  4. use Dcat\Admin\Grid\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. }