CleanupPlanContentController.php 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. <?php
  2. namespace App\Module\Cleanup\AdminControllers;
  3. use App\Module\Cleanup\Models\CleanupPlanContent;
  4. use App\Module\Cleanup\Models\CleanupPlan;
  5. use App\Module\Cleanup\Models\CleanupConfig;
  6. use App\Module\Cleanup\Repositories\CleanupPlanContentRepository;
  7. use App\Module\Cleanup\Enums\CLEANUP_TYPE;
  8. use UCore\DcatAdmin\AdminController;
  9. use Dcat\Admin\Form;
  10. use Dcat\Admin\Grid;
  11. use Dcat\Admin\Show;
  12. use Dcat\Admin\Layout\Content;
  13. use Spatie\RouteAttributes\Attributes\Resource;
  14. /**
  15. * 计划内容管理控制器
  16. *
  17. * 路由:/admin/cleanup/plan-contents
  18. */
  19. #[Resource('cleanup/plan-contents', names: 'dcat.admin.cleanup.plan-contents')]
  20. class CleanupPlanContentController extends AdminController
  21. {
  22. /**
  23. * 页面标题
  24. */
  25. protected $title = '计划内容管理';
  26. /**
  27. * 数据仓库
  28. */
  29. protected function repository()
  30. {
  31. return CleanupPlanContentRepository::class;
  32. }
  33. /**
  34. * 列表页面
  35. */
  36. protected function grid(): Grid
  37. {
  38. return Grid::make(new CleanupPlanContentRepository(), function (Grid $grid) {
  39. // 基础设置
  40. $grid->column('id', 'ID')->sortable();
  41. // 计划信息
  42. $grid->column('plan.plan_name', '所属计划')->sortable();
  43. $grid->column('table_name', '表名')->sortable();
  44. // 清理类型
  45. $grid->column('cleanup_type', '清理类型')->using([
  46. 1 => '清空表',
  47. 2 => '删除所有',
  48. 3 => '按时间删除',
  49. 4 => '按用户删除',
  50. 5 => '按条件删除',
  51. ])->label([
  52. 1 => 'danger',
  53. 2 => 'warning',
  54. 3 => 'info',
  55. 4 => 'primary',
  56. 5 => 'secondary',
  57. ])->sortable();
  58. // 配置信息
  59. $grid->column('priority', '优先级')->sortable();
  60. $grid->column('batch_size', '批处理大小')->display(function ($value) {
  61. return number_format($value);
  62. })->sortable();
  63. // 状态
  64. $grid->column('is_enabled', '启用状态')->switch()->sortable();
  65. $grid->column('backup_enabled', '备份启用')->switch()->sortable();
  66. // 条件描述
  67. $grid->column('conditions_description', '清理条件')->display(function () {
  68. return $this->conditions_description;
  69. });
  70. // 时间
  71. $grid->column('created_at', '创建时间')->sortable();
  72. $grid->column('updated_at', '更新时间')->sortable();
  73. // 筛选器
  74. $grid->filter(function (Grid\Filter $filter) {
  75. // 按计划筛选
  76. $plans = CleanupPlan::pluck('plan_name', 'id')->toArray();
  77. $filter->equal('plan_id', '所属计划')->select($plans);
  78. // 按清理类型筛选
  79. $filter->equal('cleanup_type', '清理类型')->select([
  80. 1 => '清空表',
  81. 2 => '删除所有',
  82. 3 => '按时间删除',
  83. 4 => '按用户删除',
  84. 5 => '按条件删除',
  85. ]);
  86. // 按状态筛选
  87. $filter->equal('is_enabled', '启用状态')->select([
  88. 1 => '启用',
  89. 0 => '禁用',
  90. ]);
  91. $filter->equal('backup_enabled', '备份启用')->select([
  92. 1 => '启用',
  93. 0 => '禁用',
  94. ]);
  95. // 按表名搜索
  96. $filter->like('table_name', '表名');
  97. // 按优先级范围
  98. $filter->between('priority', '优先级');
  99. });
  100. // 批量操作
  101. $grid->batchActions([
  102. new \App\Module\Cleanup\AdminControllers\Actions\BatchEnableAction(),
  103. new \App\Module\Cleanup\AdminControllers\Actions\BatchDisableAction(),
  104. ]);
  105. // 行操作
  106. $grid->actions(function (Grid\Displayers\Actions $actions) {
  107. $actions->append(new \App\Module\Cleanup\AdminControllers\Actions\EditPlanContentAction());
  108. $actions->append(new \App\Module\Cleanup\AdminControllers\Actions\DeletePlanContentAction());
  109. $actions->append(new \App\Module\Cleanup\AdminControllers\Actions\TestCleanupAction());
  110. });
  111. // 设置每页显示数量
  112. $grid->paginate(20);
  113. });
  114. }
  115. /**
  116. * 详情页面
  117. */
  118. protected function detail($id): Show
  119. {
  120. return Show::make($id, new CleanupPlanContentRepository(), function (Show $show) {
  121. $show->field('id', 'ID');
  122. // 关联信息
  123. $show->field('plan.plan_name', '所属计划');
  124. $show->field('table_name', '表名');
  125. // 清理配置
  126. $show->field('cleanup_type', '清理类型')->using([
  127. 1 => '清空表',
  128. 2 => '删除所有',
  129. 3 => '按时间删除',
  130. 4 => '按用户删除',
  131. 5 => '按条件删除',
  132. ]);
  133. $show->field('conditions', '清理条件')->json();
  134. $show->field('conditions_description', '条件描述');
  135. // 执行配置
  136. $show->field('priority', '优先级');
  137. $show->field('batch_size', '批处理大小');
  138. // 状态配置
  139. $show->field('is_enabled', '启用状态')->using([1 => '启用', 0 => '禁用']);
  140. $show->field('backup_enabled', '备份启用')->using([1 => '启用', 0 => '禁用']);
  141. // 备注和时间
  142. $show->field('notes', '备注说明');
  143. $show->field('created_at', '创建时间');
  144. $show->field('updated_at', '更新时间');
  145. });
  146. }
  147. /**
  148. * 创建/编辑表单
  149. */
  150. protected function form(): Form
  151. {
  152. return Form::make(new CleanupPlanContentRepository(), function (Form $form) {
  153. $form->display('id', 'ID');
  154. // 基础信息
  155. $form->select('plan_id', '所属计划')
  156. ->options(CleanupPlan::pluck('plan_name', 'id')->toArray())
  157. ->required();
  158. // 表名选择(可以从已有配置中选择或手动输入)
  159. $availableTables = CleanupConfig::pluck('table_name', 'table_name')->toArray();
  160. $form->select('table_name', '表名')
  161. ->options($availableTables)
  162. ->required()
  163. ->help('选择要清理的数据表');
  164. // 清理类型
  165. $form->select('cleanup_type', '清理类型')
  166. ->options([
  167. 1 => '清空表',
  168. 2 => '删除所有',
  169. 3 => '按时间删除',
  170. 4 => '按用户删除',
  171. 5 => '按条件删除',
  172. ])
  173. ->required()
  174. ->help('选择清理方式');
  175. // 清理条件(JSON配置)
  176. $form->keyValue('conditions', '清理条件')
  177. ->help('JSON格式的清理条件配置,根据清理类型设置相应参数');
  178. // 执行配置
  179. $form->number('priority', '优先级')
  180. ->default(100)
  181. ->min(1)
  182. ->max(999)
  183. ->help('数字越小优先级越高');
  184. $form->number('batch_size', '批处理大小')
  185. ->default(1000)
  186. ->min(100)
  187. ->max(10000)
  188. ->help('每批处理的记录数量');
  189. // 状态配置
  190. $form->switch('is_enabled', '启用状态')->default(1);
  191. $form->switch('backup_enabled', '备份启用')->default(1);
  192. // 备注
  193. $form->textarea('notes', '备注说明')
  194. ->help('对此清理配置的说明');
  195. // 时间字段
  196. $form->display('created_at', '创建时间');
  197. $form->display('updated_at', '更新时间');
  198. });
  199. }
  200. }