CleanupPlanController.php 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  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\AddTableToPlanAction());
  91. $actions->append(new \App\Module\Cleanup\AdminControllers\Actions\CreateTaskFromPlanAction());
  92. $actions->append(new \App\Module\Cleanup\AdminControllers\Actions\PreviewPlanAction());
  93. });
  94. // 批量操作
  95. $grid->batchActions([
  96. new \App\Module\Cleanup\AdminControllers\Actions\BatchEnablePlanAction(),
  97. new \App\Module\Cleanup\AdminControllers\Actions\BatchDisablePlanAction(),
  98. ]);
  99. // 工具栏
  100. $grid->tools([
  101. new \App\Module\Cleanup\AdminControllers\Actions\CreatePlanFromTemplateAction(),
  102. ]);
  103. // 设置每页显示数量
  104. $grid->paginate(20);
  105. });
  106. }
  107. /**
  108. * 详情页面
  109. */
  110. protected function detail($id): Show
  111. {
  112. return Show::make($id, new CleanupPlanRepository(), function (Show $show) {
  113. $show->field('id', 'ID');
  114. $show->field('plan_name', '计划名称');
  115. $show->field('plan_type', '计划类型')->using([
  116. 1 => '全量清理',
  117. 2 => '模块清理',
  118. 3 => '分类清理',
  119. 4 => '自定义清理',
  120. 5 => '混合清理',
  121. ]);
  122. $show->field('target_selection', '目标选择配置')->json();
  123. $show->field('global_conditions', '全局清理条件')->json();
  124. $show->field('backup_config', '备份配置')->json();
  125. $show->field('is_template', '模板')->using([1 => '是', 0 => '否']);
  126. $show->field('is_enabled', '启用状态')->using([1 => '启用', 0 => '禁用']);
  127. $show->field('description', '计划描述');
  128. $show->field('created_by', '创建者ID');
  129. $show->field('created_at', '创建时间');
  130. $show->field('updated_at', '更新时间');
  131. // 显示计划内容
  132. $show->relation('contents', '计划内容', function ($model) {
  133. $grid = new Grid(new \App\Module\Cleanup\Models\CleanupPlanContent());
  134. $grid->model()->where('plan_id', $model->id);
  135. $grid->column('table_name', '表名');
  136. $grid->column('cleanup_type', '清理类型')->using([
  137. 1 => '清空表',
  138. 2 => '删除所有',
  139. 3 => '按时间删除',
  140. 4 => '按用户删除',
  141. 5 => '按条件删除',
  142. ]);
  143. $grid->column('priority', '优先级');
  144. $grid->column('batch_size', '批处理大小');
  145. $grid->column('is_enabled', '启用')->using([1 => '是', 0 => '否']);
  146. $grid->column('backup_enabled', '备份')->using([1 => '是', 0 => '否']);
  147. $grid->disableActions();
  148. $grid->disableCreateButton();
  149. $grid->disableFilter();
  150. $grid->disablePagination();
  151. return $grid;
  152. });
  153. // 显示关联任务
  154. $show->relation('tasks', '关联任务', function ($model) {
  155. $grid = new Grid(new \App\Module\Cleanup\Models\CleanupTask());
  156. $grid->model()->where('plan_id', $model->id);
  157. $grid->column('id', 'ID');
  158. $grid->column('task_name', '任务名称');
  159. $grid->column('status', '状态')->using([
  160. 1 => '待执行',
  161. 2 => '备份中',
  162. 3 => '执行中',
  163. 4 => '已完成',
  164. 5 => '已失败',
  165. 6 => '已取消',
  166. 7 => '已暂停',
  167. ]);
  168. $grid->column('progress', '进度')->display(function ($progress) {
  169. return $progress . '%';
  170. });
  171. $grid->column('created_at', '创建时间');
  172. $grid->disableActions();
  173. $grid->disableCreateButton();
  174. $grid->disableFilter();
  175. $grid->disablePagination();
  176. return $grid;
  177. });
  178. });
  179. }
  180. /**
  181. * 创建/编辑表单
  182. */
  183. protected function form(): Form
  184. {
  185. return Form::make(new CleanupPlanRepository(), function (Form $form) {
  186. $form->display('id', 'ID');
  187. $form->text('plan_name', '计划名称')->required();
  188. $form->select('plan_type', '计划类型')
  189. ->options([
  190. 1 => '全量清理',
  191. 2 => '模块清理',
  192. 3 => '分类清理',
  193. 4 => '自定义清理',
  194. 5 => '混合清理',
  195. ])
  196. ->required();
  197. $form->keyValue('target_selection', '目标选择配置')
  198. ->help('JSON格式的目标选择配置');
  199. $form->keyValue('global_conditions', '全局清理条件')
  200. ->help('JSON格式的全局清理条件');
  201. $form->keyValue('backup_config', '备份配置')
  202. ->help('JSON格式的备份配置');
  203. $form->switch('is_template', '设为模板')->default(0);
  204. $form->switch('is_enabled', '启用状态')->default(1);
  205. $form->textarea('description', '计划描述');
  206. $form->hidden('created_by')->value(admin_user_id());
  207. $form->display('created_at', '创建时间');
  208. $form->display('updated_at', '更新时间');
  209. });
  210. }
  211. }