| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237 |
- <?php
- namespace App\Module\Cleanup\AdminControllers;
- use App\Module\Cleanup\Models\CleanupPlanContent;
- use App\Module\Cleanup\Models\CleanupPlan;
- use App\Module\Cleanup\Models\CleanupConfig;
- use App\Module\Cleanup\Repositories\CleanupPlanContentRepository;
- use App\Module\Cleanup\Enums\CLEANUP_TYPE;
- use UCore\DcatAdmin\AdminController;
- use Dcat\Admin\Form;
- use Dcat\Admin\Grid;
- use Dcat\Admin\Show;
- use Dcat\Admin\Layout\Content;
- use Spatie\RouteAttributes\Attributes\Resource;
- /**
- * 计划内容管理控制器
- *
- * 路由:/admin/cleanup/plan-contents
- */
- #[Resource('cleanup/plan-contents', names: 'dcat.admin.cleanup.plan-contents')]
- class CleanupPlanContentController extends AdminController
- {
- /**
- * 页面标题
- */
- protected $title = '计划内容管理';
- /**
- * 数据仓库
- */
- protected function repository()
- {
- return CleanupPlanContentRepository::class;
- }
- /**
- * 列表页面
- */
- protected function grid(): Grid
- {
- return Grid::make(new CleanupPlanContentRepository(), function (Grid $grid) {
- // 基础设置
- $grid->column('id', 'ID')->sortable();
-
- // 计划信息
- $grid->column('plan.plan_name', '所属计划')->sortable();
- $grid->column('table_name', '表名')->sortable();
-
- // 清理类型
- $grid->column('cleanup_type', '清理类型')->using([
- 1 => '清空表',
- 2 => '删除所有',
- 3 => '按时间删除',
- 4 => '按用户删除',
- 5 => '按条件删除',
- ])->label([
- 1 => 'danger',
- 2 => 'warning',
- 3 => 'info',
- 4 => 'primary',
- 5 => 'secondary',
- ])->sortable();
- // 配置信息
- $grid->column('priority', '优先级')->sortable();
- $grid->column('batch_size', '批处理大小')->display(function ($value) {
- return number_format($value);
- })->sortable();
- // 状态
- $grid->column('is_enabled', '启用状态')->switch()->sortable();
- $grid->column('backup_enabled', '备份启用')->switch()->sortable();
- // 条件描述
- $grid->column('conditions_description', '清理条件')->display(function () {
- return $this->conditions_description;
- });
- // 时间
- $grid->column('created_at', '创建时间')->sortable();
- $grid->column('updated_at', '更新时间')->sortable();
- // 筛选器
- $grid->filter(function (Grid\Filter $filter) {
- // 按计划筛选
- $plans = CleanupPlan::pluck('plan_name', 'id')->toArray();
- $filter->equal('plan_id', '所属计划')->select($plans);
-
- // 按清理类型筛选
- $filter->equal('cleanup_type', '清理类型')->select([
- 1 => '清空表',
- 2 => '删除所有',
- 3 => '按时间删除',
- 4 => '按用户删除',
- 5 => '按条件删除',
- ]);
- // 按状态筛选
- $filter->equal('is_enabled', '启用状态')->select([
- 1 => '启用',
- 0 => '禁用',
- ]);
- $filter->equal('backup_enabled', '备份启用')->select([
- 1 => '启用',
- 0 => '禁用',
- ]);
- // 按表名搜索
- $filter->like('table_name', '表名');
-
- // 按优先级范围
- $filter->between('priority', '优先级');
- });
- // 批量操作
- $grid->batchActions([
- new \App\Module\Cleanup\AdminControllers\Actions\BatchEnableAction(),
- new \App\Module\Cleanup\AdminControllers\Actions\BatchDisableAction(),
- ]);
- // 行操作
- $grid->actions(function (Grid\Displayers\Actions $actions) {
- $actions->append(new \App\Module\Cleanup\AdminControllers\Actions\EditPlanContentAction());
- $actions->append(new \App\Module\Cleanup\AdminControllers\Actions\DeletePlanContentAction());
- $actions->append(new \App\Module\Cleanup\AdminControllers\Actions\TestCleanupAction());
- });
- // 设置每页显示数量
- $grid->paginate(20);
- });
- }
- /**
- * 详情页面
- */
- protected function detail($id): Show
- {
- return Show::make($id, new CleanupPlanContentRepository(), function (Show $show) {
- $show->field('id', 'ID');
-
- // 关联信息
- $show->field('plan.plan_name', '所属计划');
- $show->field('table_name', '表名');
-
- // 清理配置
- $show->field('cleanup_type', '清理类型')->using([
- 1 => '清空表',
- 2 => '删除所有',
- 3 => '按时间删除',
- 4 => '按用户删除',
- 5 => '按条件删除',
- ]);
- $show->field('conditions', '清理条件')->json();
- $show->field('conditions_description', '条件描述');
-
- // 执行配置
- $show->field('priority', '优先级');
- $show->field('batch_size', '批处理大小');
-
- // 状态配置
- $show->field('is_enabled', '启用状态')->using([1 => '启用', 0 => '禁用']);
- $show->field('backup_enabled', '备份启用')->using([1 => '启用', 0 => '禁用']);
-
- // 备注和时间
- $show->field('notes', '备注说明');
- $show->field('created_at', '创建时间');
- $show->field('updated_at', '更新时间');
- });
- }
- /**
- * 创建/编辑表单
- */
- protected function form(): Form
- {
- return Form::make(new CleanupPlanContentRepository(), function (Form $form) {
- $form->display('id', 'ID');
-
- // 基础信息
- $form->select('plan_id', '所属计划')
- ->options(CleanupPlan::pluck('plan_name', 'id')->toArray())
- ->required();
- // 表名选择(可以从已有配置中选择或手动输入)
- $availableTables = CleanupConfig::pluck('table_name', 'table_name')->toArray();
- $form->select('table_name', '表名')
- ->options($availableTables)
- ->required()
- ->help('选择要清理的数据表');
- // 清理类型
- $form->select('cleanup_type', '清理类型')
- ->options([
- 1 => '清空表',
- 2 => '删除所有',
- 3 => '按时间删除',
- 4 => '按用户删除',
- 5 => '按条件删除',
- ])
- ->required()
- ->help('选择清理方式');
- // 清理条件(JSON配置)
- $form->keyValue('conditions', '清理条件')
- ->help('JSON格式的清理条件配置,根据清理类型设置相应参数');
- // 执行配置
- $form->number('priority', '优先级')
- ->default(100)
- ->min(1)
- ->max(999)
- ->help('数字越小优先级越高');
- $form->number('batch_size', '批处理大小')
- ->default(1000)
- ->min(100)
- ->max(10000)
- ->help('每批处理的记录数量');
- // 状态配置
- $form->switch('is_enabled', '启用状态')->default(1);
- $form->switch('backup_enabled', '备份启用')->default(1);
- // 备注
- $form->textarea('notes', '备注说明')
- ->help('对此清理配置的说明');
- // 时间字段
- $form->display('created_at', '创建时间');
- $form->display('updated_at', '更新时间');
- });
- }
- }
|