CleanupPlanController.php 8.8 KB

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