CleanupConfigController.php 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. <?php
  2. namespace App\Module\Cleanup\AdminControllers;
  3. use App\Module\Cleanup\Models\CleanupConfig;
  4. use App\Module\Cleanup\Repositories\CleanupConfigRepository;
  5. use App\Module\Cleanup\Enums\DATA_CATEGORY;
  6. use App\Module\Cleanup\Enums\CLEANUP_TYPE;
  7. use UCore\DcatAdmin\AdminController;
  8. use Dcat\Admin\Form;
  9. use Dcat\Admin\Grid;
  10. use Dcat\Admin\Show;
  11. use Dcat\Admin\Layout\Content;
  12. /**
  13. * 清理配置管理控制器
  14. *
  15. * @route /admin/cleanup/configs
  16. */
  17. class CleanupConfigController extends AdminController
  18. {
  19. /**
  20. * 页面标题
  21. */
  22. protected $title = '清理配置管理';
  23. /**
  24. * 数据仓库
  25. */
  26. protected function repository()
  27. {
  28. return CleanupConfigRepository::class;
  29. }
  30. /**
  31. * 列表页面
  32. */
  33. protected function grid(): Grid
  34. {
  35. return Grid::make(new CleanupConfigRepository(), function (Grid $grid) {
  36. // 基础设置
  37. $grid->column('id', 'ID')->sortable();
  38. $grid->column('table_name', '表名')->sortable();
  39. $grid->column('module_name', '模块')->sortable();
  40. // 数据分类
  41. $grid->column('data_category', '数据分类')->using([
  42. 1 => '用户数据',
  43. 2 => '日志数据',
  44. 3 => '交易数据',
  45. 4 => '缓存数据',
  46. 5 => '配置数据',
  47. ])->label([
  48. 1 => 'primary',
  49. 2 => 'info',
  50. 3 => 'warning',
  51. 4 => 'secondary',
  52. 5 => 'danger',
  53. ])->sortable();
  54. // 清理类型
  55. $grid->column('default_cleanup_type', '默认清理类型')->using([
  56. 1 => '清空表',
  57. 2 => '删除所有',
  58. 3 => '按时间删除',
  59. 4 => '按用户删除',
  60. 5 => '按条件删除',
  61. ])->sortable();
  62. // 状态和优先级
  63. $grid->column('is_enabled', '启用状态')->switch()->sortable();
  64. $grid->column('priority', '优先级')->sortable();
  65. $grid->column('batch_size', '批处理大小')->sortable();
  66. // 最后清理时间
  67. $grid->column('last_cleanup_at', '最后清理时间')->sortable();
  68. $grid->column('created_at', '创建时间')->sortable();
  69. // 筛选器
  70. $grid->filter(function (Grid\Filter $filter) {
  71. $filter->equal('module_name', '模块')->select(
  72. CleanupConfig::distinct()->pluck('module_name', 'module_name')->toArray()
  73. );
  74. $filter->equal('data_category', '数据分类')->select([
  75. 1 => '用户数据',
  76. 2 => '日志数据',
  77. 3 => '交易数据',
  78. 4 => '缓存数据',
  79. 5 => '配置数据',
  80. ]);
  81. $filter->equal('default_cleanup_type', '清理类型')->select([
  82. 1 => '清空表',
  83. 2 => '删除所有',
  84. 3 => '按时间删除',
  85. 4 => '按用户删除',
  86. 5 => '按条件删除',
  87. ]);
  88. $filter->equal('is_enabled', '启用状态')->select([
  89. 1 => '启用',
  90. 0 => '禁用',
  91. ]);
  92. $filter->like('table_name', '表名');
  93. $filter->between('priority', '优先级');
  94. });
  95. // 批量操作
  96. $grid->batchActions([
  97. new \App\Module\Cleanup\AdminControllers\Actions\BatchEnableAction(),
  98. new \App\Module\Cleanup\AdminControllers\Actions\BatchDisableAction(),
  99. ]);
  100. // 工具栏
  101. $grid->tools([
  102. new \App\Module\Cleanup\AdminControllers\Actions\ScanTablesAction(),
  103. ]);
  104. // 行操作
  105. $grid->actions(function (Grid\Displayers\Actions $actions) {
  106. $actions->append(new \App\Module\Cleanup\AdminControllers\Actions\TestCleanupAction());
  107. });
  108. // 禁用创建按钮(配置通过扫描生成)
  109. $grid->disableCreateButton();
  110. // 设置每页显示数量
  111. $grid->paginate(50);
  112. });
  113. }
  114. /**
  115. * 详情页面
  116. */
  117. protected function detail($id): Show
  118. {
  119. return Show::make($id, new CleanupConfigRepository(), function (Show $show) {
  120. $show->field('id', 'ID');
  121. $show->field('table_name', '表名');
  122. $show->field('module_name', '模块名称');
  123. $show->field('data_category', '数据分类')->using([
  124. 1 => '用户数据',
  125. 2 => '日志数据',
  126. 3 => '交易数据',
  127. 4 => '缓存数据',
  128. 5 => '配置数据',
  129. ]);
  130. $show->field('default_cleanup_type', '默认清理类型')->using([
  131. 1 => '清空表',
  132. 2 => '删除所有',
  133. 3 => '按时间删除',
  134. 4 => '按用户删除',
  135. 5 => '按条件删除',
  136. ]);
  137. $show->field('default_conditions', '默认条件')->json();
  138. $show->field('is_enabled', '启用状态')->using([1 => '启用', 0 => '禁用']);
  139. $show->field('priority', '优先级');
  140. $show->field('batch_size', '批处理大小');
  141. $show->field('description', '描述');
  142. $show->field('last_cleanup_at', '最后清理时间');
  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 CleanupConfigRepository(), function (Form $form) {
  153. $form->display('id', 'ID');
  154. $form->display('table_name', '表名');
  155. $form->display('module_name', '模块名称');
  156. $form->display('data_category', '数据分类')->using([
  157. 1 => '用户数据',
  158. 2 => '日志数据',
  159. 3 => '交易数据',
  160. 4 => '缓存数据',
  161. 5 => '配置数据',
  162. ]);
  163. $form->select('default_cleanup_type', '默认清理类型')
  164. ->options([
  165. 1 => '清空表',
  166. 2 => '删除所有',
  167. 3 => '按时间删除',
  168. 4 => '按用户删除',
  169. 5 => '按条件删除',
  170. ])
  171. ->required();
  172. $form->keyValue('default_conditions', '默认条件')
  173. ->help('JSON格式的清理条件配置');
  174. $form->switch('is_enabled', '启用状态')->default(1);
  175. $form->number('priority', '优先级')->default(100)->min(1)->max(999);
  176. $form->number('batch_size', '批处理大小')->default(1000)->min(100)->max(10000);
  177. $form->textarea('description', '描述');
  178. $form->display('last_cleanup_at', '最后清理时间');
  179. $form->display('created_at', '创建时间');
  180. $form->display('updated_at', '更新时间');
  181. });
  182. }
  183. }