AddTableToPlanAction.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. <?php
  2. namespace App\Module\Cleanup\AdminControllers\Actions;
  3. use App\Module\Cleanup\Models\CleanupPlan;
  4. use App\Module\Cleanup\Models\CleanupPlanContent;
  5. use App\Module\Cleanup\Models\CleanupConfig;
  6. use Dcat\Admin\Actions\Response;
  7. use Dcat\Admin\Grid\RowAction;
  8. use Dcat\Admin\Widgets\Form;
  9. /**
  10. * 添加表到计划Action
  11. */
  12. class AddTableToPlanAction extends RowAction
  13. {
  14. /**
  15. * 标题
  16. */
  17. protected $title = '添加表';
  18. /**
  19. * 处理请求
  20. */
  21. public function handle()
  22. {
  23. $planId = $this->getKey();
  24. $data = $this->form;
  25. try {
  26. $plan = CleanupPlan::findOrFail($planId);
  27. // 检查表是否已存在
  28. $exists = CleanupPlanContent::where('plan_id', $planId)
  29. ->where('table_name', $data['table_name'])
  30. ->exists();
  31. if ($exists) {
  32. return $this->response()
  33. ->error('该表已存在于计划中');
  34. }
  35. // 获取表的默认配置
  36. $config = CleanupConfig::where('table_name', $data['table_name'])->first();
  37. // 创建计划内容
  38. CleanupPlanContent::create([
  39. 'plan_id' => $planId,
  40. 'table_name' => $data['table_name'],
  41. 'cleanup_type' => $data['cleanup_type'] ?? ($config->default_cleanup_type ?? 2),
  42. 'conditions' => $data['conditions'] ?? ($config->default_conditions ?? []),
  43. 'priority' => $data['priority'] ?? ($config->priority ?? 100),
  44. 'batch_size' => $data['batch_size'] ?? ($config->batch_size ?? 1000),
  45. 'is_enabled' => $data['is_enabled'] ?? 1,
  46. 'backup_enabled' => $data['backup_enabled'] ?? 1,
  47. 'notes' => $data['notes'] ?? '',
  48. 'conditions_description' => $this->generateConditionsDescription($data['cleanup_type'] ?? 2, $data['conditions'] ?? []),
  49. ]);
  50. return $this->response()
  51. ->success('添加成功')
  52. ->refresh();
  53. } catch (\Exception $e) {
  54. return $this->response()
  55. ->error('添加失败:' . $e->getMessage());
  56. }
  57. }
  58. /**
  59. * 表单配置
  60. */
  61. public function form()
  62. {
  63. $plan = CleanupPlan::findOrFail($this->getKey());
  64. // 获取可用的表(排除已添加的)
  65. $existingTables = CleanupPlanContent::where('plan_id', $this->getKey())
  66. ->pluck('table_name')
  67. ->toArray();
  68. $availableTables = CleanupConfig::whereNotIn('table_name', $existingTables)
  69. ->pluck('table_name', 'table_name')
  70. ->toArray();
  71. return Form::make()->action($this->getHandleRoute())->body([
  72. // 基础信息
  73. Form::display('plan_name', '所属计划')->value($plan->plan_name),
  74. Form::select('table_name', '选择表')
  75. ->options($availableTables)
  76. ->required()
  77. ->help('选择要添加到计划中的数据表'),
  78. // 清理配置
  79. Form::select('cleanup_type', '清理类型')
  80. ->options([
  81. 1 => '清空表',
  82. 2 => '删除所有',
  83. 3 => '按时间删除',
  84. 4 => '按用户删除',
  85. 5 => '按条件删除',
  86. ])
  87. ->default(2)
  88. ->required(),
  89. Form::keyValue('conditions', '清理条件')
  90. ->help('JSON格式的清理条件配置'),
  91. // 执行配置
  92. Form::number('priority', '优先级')
  93. ->default(100)
  94. ->min(1)
  95. ->max(999)
  96. ->help('数字越小优先级越高'),
  97. Form::number('batch_size', '批处理大小')
  98. ->default(1000)
  99. ->min(100)
  100. ->max(10000)
  101. ->help('每批处理的记录数量'),
  102. // 状态配置
  103. Form::switch('is_enabled', '启用状态')->default(1),
  104. Form::switch('backup_enabled', '备份启用')->default(1),
  105. // 备注
  106. Form::textarea('notes', '备注说明')
  107. ->help('对此清理配置的说明'),
  108. ]);
  109. }
  110. /**
  111. * 生成条件描述
  112. */
  113. private function generateConditionsDescription(int $cleanupType, array $conditions): string
  114. {
  115. switch ($cleanupType) {
  116. case 1:
  117. return '清空表(TRUNCATE)';
  118. case 2:
  119. return '删除所有记录(DELETE)';
  120. case 3:
  121. $days = $conditions['days'] ?? 30;
  122. return "删除{$days}天前的记录";
  123. case 4:
  124. $userIds = $conditions['user_ids'] ?? [];
  125. return '删除指定用户的记录:' . implode(',', $userIds);
  126. case 5:
  127. return '按自定义条件删除';
  128. default:
  129. return '未知清理类型';
  130. }
  131. }
  132. /**
  133. * 权限检查
  134. */
  135. public function allowed()
  136. {
  137. return true; // 根据实际需求设置权限
  138. }
  139. }