| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151 |
- <?php
- namespace App\Module\GameItems\AdminControllers;
- use App\Module\GameItems\Enums\CHEST_COST_TYPE;
- use App\Module\GameItems\Models\Item;
- use App\Module\GameItems\Models\ItemChestOpenCost;
- use Dcat\Admin\Form;
- use Dcat\Admin\Grid;
- use Dcat\Admin\Show;
- use Dcat\Admin\Http\Controllers\AdminController;
- use Dcat\Admin\Layout\Content;
- use Spatie\RouteAttributes\Attributes\Resource;
- /**
- * 宝箱开启消耗配置控制器
- */
- #[Resource('game-items-chest-costs', names: 'dcat.admin.game-items-chest-costs')]
- class ItemChestOpenCostController extends AdminController
- {
- protected $title ='宝箱开启消耗配置';
- /**
- * 列表页
- *
- * @return Grid
- */
- protected function grid()
- {
- $grid = new Grid(new ItemChestOpenCost());
- $grid->column('id', 'ID')->sortable();
- $grid->column('chest.name', '宝箱名称')->link(function () {
- return admin_url('game-items/items/' . $this->chest_id);
- });
- $grid->column('cost_type', '消耗类型')->display(function ($value) {
- return CHEST_COST_TYPE::getAll()[$value] ?? '未知';
- });
- $grid->column('cost_id', '消耗ID')->display(function ($value) {
- if ($this->cost_type == CHEST_COST_TYPE::ITEM->value) {
- $item = Item::find($value);
- return $item ? "{$item->name} (ID: {$value})" : $value;
- }
- return $value;
- });
- $grid->column('cost_quantity', '消耗数量');
- $grid->column('is_active', '是否激活')->switch();
- $grid->column('created_at', '创建时间');
- $grid->column('updated_at', '更新时间');
- // 筛选器
- $grid->filter(function (Grid\Filter $filter) {
- $filter->equal('chest_id', '宝箱ID');
- $filter->where('chest_name', function ($query) {
- $query->whereHas('chest', function ($query) {
- $query->where('name', 'like', "%{$this->input}%");
- });
- }, '宝箱名称');
- $filter->equal('cost_type', '消耗类型')->select(CHEST_COST_TYPE::getAll());
- $filter->equal('cost_id', '消耗ID');
- $filter->equal('is_active', '是否激活')->select([0 => '否', 1 => '是']);
- });
- // 批量操作
- $grid->batchActions(function (Grid\Tools\BatchActions $batch) {
- $batch->add(new \App\Module\GameItems\AdminControllers\Actions\BatchActivate());
- $batch->add(new \App\Module\GameItems\AdminControllers\Actions\BatchDeactivate());
- });
- // 工具栏
- $grid->tools(function (Grid\Tools $tools) {
- $tools->append(new \App\Module\GameItems\AdminControllers\Actions\CopyToAnotherChest());
- });
- return $grid;
- }
- /**
- * 详情页
- *
- * @param mixed $id
- * @return Show
- */
- protected function detail($id)
- {
- $show = new Show(ItemChestOpenCost::findOrFail($id));
- $show->field('id', 'ID');
- $show->field('chest.name', '宝箱名称');
- $show->field('chest_id', '宝箱ID');
- $show->field('cost_type', '消耗类型')->as(function ($value) {
- return CHEST_COST_TYPE::getAll()[$value] ?? '未知';
- });
- $show->field('cost_id', '消耗ID');
- $show->field('cost_quantity', '消耗数量');
- $show->field('is_active', '是否激活')->as(function ($value) {
- return $value ? '是' : '否';
- });
- $show->field('created_at', '创建时间');
- $show->field('updated_at', '更新时间');
- return $show;
- }
- /**
- * 表单
- *
- * @return Form
- */
- protected function form()
- {
- $form = new Form(new ItemChestOpenCost());
- // 只显示宝箱类型的物品
- $chests = Item::where('type', 3)->pluck('name', 'id');
- $form->select('chest_id', '宝箱')->options($chests)->required();
- $form->select('cost_type', '消耗类型')->options(CHEST_COST_TYPE::getAll())->required();
- // 根据消耗类型显示不同的选择器
- $form->select('cost_id', '消耗ID')->options(function ($id) {
- $costType = request()->get('cost_type');
- if ($costType == CHEST_COST_TYPE::ITEM->value) {
- return Item::pluck('name', 'id');
- }
- return [];
- })->required();
- $form->number('cost_quantity', '消耗数量')->min(1)->default(1)->required();
- $form->switch('is_active', '是否激活')->default(1);
- // 保存前回调
- $form->saving(function (Form $form) {
- // 验证消耗ID
- if ($form->cost_type == CHEST_COST_TYPE::ITEM->value) {
- $item = Item::find($form->cost_id);
- if (!$item) {
- return $form->response()->error('无效的物品ID');
- }
- }
- });
- return $form;
- }
- }
|