CreateTaskAction.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. <?php
  2. namespace App\Module\Cleanup\AdminControllers\Actions;
  3. use App\Module\Cleanup\Models\CleanupPlan;
  4. use App\Module\Cleanup\Services\CleanupService;
  5. use Dcat\Admin\Grid\Tools\AbstractTool;
  6. use Dcat\Admin\Actions\Response;
  7. use Illuminate\Http\Request;
  8. /**
  9. * 创建任务Action
  10. *
  11. * 用于在任务管理页面创建新任务
  12. */
  13. class CreateTaskAction extends AbstractTool
  14. {
  15. /**
  16. * 按钮标题
  17. */
  18. protected $title = '创建任务';
  19. /**
  20. * 处理请求
  21. */
  22. public function handle(Request $request)
  23. {
  24. try {
  25. $planId = $request->input('plan_id');
  26. $taskName = $request->input('task_name');
  27. if (empty($planId)) {
  28. return $this->response()
  29. ->error('请选择清理计划');
  30. }
  31. if (empty($taskName)) {
  32. return $this->response()
  33. ->error('请输入任务名称');
  34. }
  35. // 调用服务创建任务
  36. $result = CleanupService::createTaskFromPlan($planId, $taskName);
  37. if (!$result['success']) {
  38. return $this->response()
  39. ->error('创建失败:' . $result['message']);
  40. }
  41. $task = $result['data'];
  42. return $this->response()
  43. ->success('任务创建成功!')
  44. ->detail("
  45. 任务ID:{$task['id']}<br>
  46. 任务名称:{$task['task_name']}<br>
  47. 关联计划:{$task['plan_name']}<br>
  48. 包含表数:{$task['total_tables']}<br>
  49. 状态:待执行
  50. ")
  51. ->refresh();
  52. } catch (\Exception $e) {
  53. return $this->response()
  54. ->error('创建失败:' . $e->getMessage());
  55. }
  56. }
  57. /**
  58. * 确认对话框
  59. */
  60. public function confirm()
  61. {
  62. $plans = CleanupPlan::where('is_enabled', 1)->pluck('plan_name', 'id')->toArray();
  63. return [
  64. '创建清理任务',
  65. '请选择清理计划并输入任务名称。',
  66. [
  67. 'plan_id' => [
  68. 'type' => 'select',
  69. 'label' => '清理计划',
  70. 'options' => $plans,
  71. 'required' => true,
  72. ],
  73. 'task_name' => [
  74. 'type' => 'text',
  75. 'label' => '任务名称',
  76. 'placeholder' => '请输入任务名称',
  77. 'required' => true,
  78. ]
  79. ]
  80. ];
  81. }
  82. /**
  83. * 渲染按钮
  84. */
  85. public function render()
  86. {
  87. return <<<HTML
  88. <a href="javascript:void(0);" class="btn btn-primary btn-sm" data-action="{$this->getHandleRoute()}">
  89. <i class="fa fa-plus"></i> {$this->title}
  90. </a>
  91. HTML;
  92. }
  93. }