CreatePlanFromTemplateAction.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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 CreatePlanFromTemplateAction extends AbstractTool
  14. {
  15. /**
  16. * 按钮标题
  17. */
  18. protected $title = '从模板创建';
  19. /**
  20. * 处理请求
  21. */
  22. public function handle(Request $request)
  23. {
  24. try {
  25. $templateId = $request->input('template_id');
  26. $planName = $request->input('plan_name');
  27. if (empty($templateId)) {
  28. return $this->response()
  29. ->error('请选择模板');
  30. }
  31. if (empty($planName)) {
  32. return $this->response()
  33. ->error('请输入计划名称');
  34. }
  35. // 调用服务从模板创建计划
  36. $result = CleanupService::createPlanFromTemplate($templateId, $planName);
  37. if (!$result['success']) {
  38. return $this->response()
  39. ->error('创建失败:' . $result['message']);
  40. }
  41. $plan = $result['data'];
  42. return $this->response()
  43. ->success('计划创建成功!')
  44. ->detail("
  45. 计划ID:{$plan['id']}<br>
  46. 计划名称:{$plan['plan_name']}<br>
  47. 计划类型:{$plan['plan_type_name']}<br>
  48. 包含表数:{$plan['contents_count']}<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. $templates = CleanupPlan::where('is_template', 1)->pluck('plan_name', 'id')->toArray();
  63. if (empty($templates)) {
  64. return [
  65. '暂无可用模板',
  66. '系统中暂无可用的计划模板,请先创建模板。'
  67. ];
  68. }
  69. return [
  70. '从模板创建计划',
  71. '请选择模板并输入新计划名称。',
  72. [
  73. 'template_id' => [
  74. 'type' => 'select',
  75. 'label' => '选择模板',
  76. 'options' => $templates,
  77. 'required' => true,
  78. ],
  79. 'plan_name' => [
  80. 'type' => 'text',
  81. 'label' => '计划名称',
  82. 'placeholder' => '请输入新计划名称',
  83. 'required' => true,
  84. ]
  85. ]
  86. ];
  87. }
  88. /**
  89. * 渲染按钮
  90. */
  91. public function render()
  92. {
  93. return <<<HTML
  94. <a href="javascript:void(0);" class="btn btn-success btn-sm" data-action="{$this->getHandleRoute()}">
  95. <i class="fa fa-copy"></i> {$this->title}
  96. </a>
  97. HTML;
  98. }
  99. }