CleanupPlanController.php 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. <?php
  2. namespace App\Module\Cleanup\AdminControllers;
  3. use App\Module\Cleanup\Models\CleanupPlan;
  4. use App\Module\Cleanup\Repositories\CleanupPlanRepository;
  5. use App\Module\Cleanup\Enums\PLAN_TYPE;
  6. use UCore\DcatAdmin\AdminController;
  7. use Dcat\Admin\Form;
  8. use Dcat\Admin\Grid;
  9. use Dcat\Admin\Show;
  10. use Dcat\Admin\Layout\Content;
  11. /**
  12. * 清理计划管理控制器
  13. *
  14. * @route /admin/cleanup/plans
  15. */
  16. class CleanupPlanController extends AdminController
  17. {
  18. /**
  19. * 页面标题
  20. */
  21. protected $title = '清理计划管理';
  22. /**
  23. * 数据仓库
  24. */
  25. protected function repository()
  26. {
  27. return CleanupPlanRepository::class;
  28. }
  29. /**
  30. * 列表页面
  31. */
  32. protected function grid(): Grid
  33. {
  34. return Grid::make(new CleanupPlanRepository(), function (Grid $grid) {
  35. // 基础设置
  36. $grid->column('id', 'ID')->sortable();
  37. $grid->column('plan_name', '计划名称')->sortable();
  38. // 计划类型
  39. $grid->column('plan_type', '计划类型')->using([
  40. 1 => '全量清理',
  41. 2 => '模块清理',
  42. 3 => '分类清理',
  43. 4 => '自定义清理',
  44. 5 => '混合清理',
  45. ])->label([
  46. 1 => 'danger',
  47. 2 => 'primary',
  48. 3 => 'info',
  49. 4 => 'warning',
  50. 5 => 'secondary',
  51. ])->sortable();
  52. // 状态
  53. $grid->column('is_template', '模板')->switch()->sortable();
  54. $grid->column('is_enabled', '启用状态')->switch()->sortable();
  55. // 统计信息
  56. $grid->column('contents_count', '包含表数')->display(function () {
  57. return $this->contents()->count();
  58. });
  59. $grid->column('tasks_count', '关联任务数')->display(function () {
  60. return $this->tasks()->count();
  61. });
  62. // 时间
  63. $grid->column('created_at', '创建时间')->sortable();
  64. $grid->column('updated_at', '更新时间')->sortable();
  65. // 筛选器
  66. $grid->filter(function (Grid\Filter $filter) {
  67. $filter->equal('plan_type', '计划类型')->select([
  68. 1 => '全量清理',
  69. 2 => '模块清理',
  70. 3 => '分类清理',
  71. 4 => '自定义清理',
  72. 5 => '混合清理',
  73. ]);
  74. $filter->equal('is_template', '模板')->select([
  75. 1 => '是',
  76. 0 => '否',
  77. ]);
  78. $filter->equal('is_enabled', '启用状态')->select([
  79. 1 => '启用',
  80. 0 => '禁用',
  81. ]);
  82. $filter->like('plan_name', '计划名称');
  83. $filter->between('created_at', '创建时间')->datetime();
  84. });
  85. // 行操作
  86. $grid->actions(function (Grid\Displayers\Actions $actions) {
  87. $actions->append(new \App\Module\Cleanup\AdminControllers\Actions\ViewPlanContentsAction());
  88. $actions->append(new \App\Module\Cleanup\AdminControllers\Actions\CreateTaskFromPlanAction());
  89. $actions->append(new \App\Module\Cleanup\AdminControllers\Actions\PreviewPlanAction());
  90. });
  91. // 批量操作
  92. $grid->batchActions([
  93. new \App\Module\Cleanup\AdminControllers\Actions\BatchEnablePlanAction(),
  94. new \App\Module\Cleanup\AdminControllers\Actions\BatchDisablePlanAction(),
  95. ]);
  96. // 工具栏
  97. $grid->tools([
  98. new \App\Module\Cleanup\AdminControllers\Actions\CreatePlanFromTemplateAction(),
  99. ]);
  100. // 设置每页显示数量
  101. $grid->paginate(20);
  102. });
  103. }
  104. /**
  105. * 详情页面
  106. */
  107. protected function detail($id): Show
  108. {
  109. return Show::make($id, new CleanupPlanRepository(), function (Show $show) {
  110. $show->field('id', 'ID');
  111. $show->field('plan_name', '计划名称');
  112. $show->field('plan_type', '计划类型')->using([
  113. 1 => '全量清理',
  114. 2 => '模块清理',
  115. 3 => '分类清理',
  116. 4 => '自定义清理',
  117. 5 => '混合清理',
  118. ]);
  119. $show->field('target_selection', '目标选择配置')->json();
  120. $show->field('global_conditions', '全局清理条件')->json();
  121. $show->field('backup_config', '备份配置')->json();
  122. $show->field('is_template', '模板')->using([1 => '是', 0 => '否']);
  123. $show->field('is_enabled', '启用状态')->using([1 => '启用', 0 => '禁用']);
  124. $show->field('description', '计划描述');
  125. $show->field('created_by', '创建者ID');
  126. $show->field('created_at', '创建时间');
  127. $show->field('updated_at', '更新时间');
  128. // 显示计划内容
  129. $show->relation('contents', '计划内容', function ($model) {
  130. $grid = new Grid(new \App\Module\Cleanup\Models\CleanupPlanContent());
  131. $grid->model()->where('plan_id', $model->id);
  132. $grid->column('table_name', '表名');
  133. $grid->column('cleanup_type', '清理类型')->using([
  134. 1 => '清空表',
  135. 2 => '删除所有',
  136. 3 => '按时间删除',
  137. 4 => '按用户删除',
  138. 5 => '按条件删除',
  139. ]);
  140. $grid->column('priority', '优先级');
  141. $grid->column('batch_size', '批处理大小');
  142. $grid->column('is_enabled', '启用')->using([1 => '是', 0 => '否']);
  143. $grid->column('backup_enabled', '备份')->using([1 => '是', 0 => '否']);
  144. $grid->disableActions();
  145. $grid->disableCreateButton();
  146. $grid->disableFilter();
  147. $grid->disablePagination();
  148. return $grid;
  149. });
  150. // 显示关联任务
  151. $show->relation('tasks', '关联任务', function ($model) {
  152. $grid = new Grid(new \App\Module\Cleanup\Models\CleanupTask());
  153. $grid->model()->where('plan_id', $model->id);
  154. $grid->column('id', 'ID');
  155. $grid->column('task_name', '任务名称');
  156. $grid->column('status', '状态')->using([
  157. 1 => '待执行',
  158. 2 => '备份中',
  159. 3 => '执行中',
  160. 4 => '已完成',
  161. 5 => '已失败',
  162. 6 => '已取消',
  163. 7 => '已暂停',
  164. ]);
  165. $grid->column('progress', '进度')->display(function ($progress) {
  166. return $progress . '%';
  167. });
  168. $grid->column('created_at', '创建时间');
  169. $grid->disableActions();
  170. $grid->disableCreateButton();
  171. $grid->disableFilter();
  172. $grid->disablePagination();
  173. return $grid;
  174. });
  175. });
  176. }
  177. /**
  178. * 创建/编辑表单
  179. */
  180. protected function form(): Form
  181. {
  182. return Form::make(new CleanupPlanRepository(), function (Form $form) {
  183. $form->display('id', 'ID');
  184. $form->text('plan_name', '计划名称')->required();
  185. $form->select('plan_type', '计划类型')
  186. ->options([
  187. 1 => '全量清理',
  188. 2 => '模块清理',
  189. 3 => '分类清理',
  190. 4 => '自定义清理',
  191. 5 => '混合清理',
  192. ])
  193. ->required();
  194. $form->keyValue('target_selection', '目标选择配置')
  195. ->help('JSON格式的目标选择配置');
  196. $form->keyValue('global_conditions', '全局清理条件')
  197. ->help('JSON格式的全局清理条件');
  198. $form->keyValue('backup_config', '备份配置')
  199. ->help('JSON格式的备份配置');
  200. $form->switch('is_template', '设为模板')->default(0);
  201. $form->switch('is_enabled', '启用状态')->default(1);
  202. $form->textarea('description', '计划描述');
  203. $form->hidden('created_by')->value(admin_user_id());
  204. $form->display('created_at', '创建时间');
  205. $form->display('updated_at', '更新时间');
  206. });
  207. }
  208. }