CleanupConfigController.php 7.3 KB

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