DeletePlanContentAction.php 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. <?php
  2. namespace App\Module\Cleanup\AdminControllers\Actions;
  3. use App\Module\Cleanup\Models\CleanupPlanContent;
  4. use Dcat\Admin\Actions\Response;
  5. use Dcat\Admin\Grid\RowAction;
  6. /**
  7. * 删除计划内容Action
  8. */
  9. class DeletePlanContentAction extends RowAction
  10. {
  11. /**
  12. * 标题
  13. */
  14. protected $title = '删除内容';
  15. /**
  16. * 处理请求
  17. */
  18. public function handle()
  19. {
  20. $id = $this->getKey();
  21. try {
  22. $content = CleanupPlanContent::findOrFail($id);
  23. // 检查是否可以删除
  24. $planContentsCount = CleanupPlanContent::where('plan_id', $content->plan_id)->count();
  25. if ($planContentsCount <= 1) {
  26. return $this->response()
  27. ->error('计划至少需要保留一个清理内容');
  28. }
  29. // 删除计划内容
  30. $content->delete();
  31. return $this->response()
  32. ->success('删除成功')
  33. ->refresh();
  34. } catch (\Exception $e) {
  35. return $this->response()
  36. ->error('删除失败:' . $e->getMessage());
  37. }
  38. }
  39. /**
  40. * 确认对话框
  41. */
  42. public function confirm()
  43. {
  44. return [
  45. '确认删除此计划内容?',
  46. '⚠️ 删除后无法恢复,请谨慎操作!'
  47. ];
  48. }
  49. /**
  50. * 权限检查
  51. */
  52. public function allowed()
  53. {
  54. return true; // 根据实际需求设置权限
  55. }
  56. }