| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114 |
- <?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 CreatePlanFromTemplateAction extends AbstractTool
- {
- /**
- * 按钮标题
- */
- protected $title = '从模板创建';
- /**
- * 处理请求
- */
- public function handle(Request $request)
- {
- try {
- $templateId = $request->input('template_id');
- $planName = $request->input('plan_name');
-
- if (empty($templateId)) {
- return $this->response()
- ->error('请选择模板');
- }
-
- if (empty($planName)) {
- return $this->response()
- ->error('请输入计划名称');
- }
-
- // 调用服务从模板创建计划
- $result = CleanupService::createPlanFromTemplate($templateId, $planName);
-
- if (!$result['success']) {
- return $this->response()
- ->error('创建失败:' . $result['message']);
- }
-
- $plan = $result['data'];
-
- return $this->response()
- ->success('计划创建成功!')
- ->detail("
- 计划ID:{$plan['id']}<br>
- 计划名称:{$plan['plan_name']}<br>
- 计划类型:{$plan['plan_type_name']}<br>
- 包含表数:{$plan['contents_count']}<br>
- 状态:已启用
- ")
- ->refresh();
-
- } catch (\Exception $e) {
- return $this->response()
- ->error('创建失败:' . $e->getMessage());
- }
- }
- /**
- * 确认对话框
- */
- public function confirm()
- {
- $templates = CleanupPlan::where('is_template', 1)->pluck('plan_name', 'id')->toArray();
-
- if (empty($templates)) {
- return [
- '暂无可用模板',
- '系统中暂无可用的计划模板,请先创建模板。'
- ];
- }
-
- return [
- '从模板创建计划',
- '请选择模板并输入新计划名称。',
- [
- 'template_id' => [
- 'type' => 'select',
- 'label' => '选择模板',
- 'options' => $templates,
- 'required' => true,
- ],
- 'plan_name' => [
- 'type' => 'text',
- 'label' => '计划名称',
- 'placeholder' => '请输入新计划名称',
- 'required' => true,
- ]
- ]
- ];
- }
- /**
- * 渲染按钮
- */
- public function render()
- {
- return <<<HTML
- <a href="javascript:void(0);" class="btn btn-success btn-sm" data-action="{$this->getHandleRoute()}">
- <i class="fa fa-copy"></i> {$this->title}
- </a>
- HTML;
- }
- }
|