| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107 |
- <?php
- namespace App\Module\Cleanup\AdminControllers\Actions;
- use App\Module\Cleanup\Models\CleanupPlan;
- use App\Module\Cleanup\Services\CleanupService;
- use Dcat\Admin\Grid\Tools\AbstractTool;
- use Dcat\Admin\Actions\Response;
- use Illuminate\Http\Request;
- /**
- * 创建任务Action
- *
- * 用于在任务管理页面创建新任务
- */
- class CreateTaskAction extends AbstractTool
- {
- /**
- * 按钮标题
- */
- protected $title = '创建任务';
- /**
- * 处理请求
- */
- public function handle(Request $request)
- {
- try {
- $planId = $request->input('plan_id');
- $taskName = $request->input('task_name');
-
- if (empty($planId)) {
- return $this->response()
- ->error('请选择清理计划');
- }
-
- if (empty($taskName)) {
- return $this->response()
- ->error('请输入任务名称');
- }
-
- // 调用服务创建任务
- $result = CleanupService::createTaskFromPlan($planId, $taskName);
-
- if (!$result['success']) {
- return $this->response()
- ->error('创建失败:' . $result['message']);
- }
-
- $task = $result['data'];
-
- return $this->response()
- ->success('任务创建成功!')
- ->detail("
- 任务ID:{$task['id']}<br>
- 任务名称:{$task['task_name']}<br>
- 关联计划:{$task['plan_name']}<br>
- 包含表数:{$task['total_tables']}<br>
- 状态:待执行
- ")
- ->refresh();
-
- } catch (\Exception $e) {
- return $this->response()
- ->error('创建失败:' . $e->getMessage());
- }
- }
- /**
- * 确认对话框
- */
- public function confirm()
- {
- $plans = CleanupPlan::where('is_enabled', 1)->pluck('plan_name', 'id')->toArray();
-
- return [
- '创建清理任务',
- '请选择清理计划并输入任务名称。',
- [
- 'plan_id' => [
- 'type' => 'select',
- 'label' => '清理计划',
- 'options' => $plans,
- 'required' => true,
- ],
- 'task_name' => [
- 'type' => 'text',
- 'label' => '任务名称',
- 'placeholder' => '请输入任务名称',
- 'required' => true,
- ]
- ]
- ];
- }
- /**
- * 渲染按钮
- */
- public function render()
- {
- return <<<HTML
- <a href="javascript:void(0);" class="btn btn-primary btn-sm" data-action="{$this->getHandleRoute()}">
- <i class="fa fa-plus"></i> {$this->title}
- </a>
- HTML;
- }
- }
|