| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159 |
- <?php
- namespace App\Module\Game\AdminControllers;
- use App\Module\Fund\Models\Currency;
- use App\Module\Game\Enums\CONSUME_TYPE;
- use App\Module\Game\Models\GameConsumeGroup;
- use App\Module\Game\Repositorys\GameConsumeItemRepository;
- use App\Module\GameItems\Models\Item;
- use Dcat\Admin\Form;
- use Dcat\Admin\Grid;
- use Dcat\Admin\Show;
- use Dcat\Admin\Http\Controllers\AdminController;
- use Spatie\RouteAttributes\Attributes\Resource;
- /**
- * 消耗项管理控制器
- */
- #[Resource('game-consume-items', names: 'dcat.admin.game-consume-items')]
- class GameConsumeItemController extends AdminController
- {
- /**
- * 标题
- *
- * @return string
- */
- protected function title()
- {
- return '消耗项管理';
- }
- /**
- * 创建表格
- *
- * @return Grid
- */
- protected function grid()
- {
- return Grid::make(new GameConsumeItemRepository(), function (Grid $grid) {
- $grid->column('id', 'ID')->sortable();
- $grid->column('group_id', '消耗组')->display(function ($groupId) {
- $group = GameConsumeGroup::find($groupId);
- return $group ? "{$group->name} ({$group->code})" : "未知 ({$groupId})";
- });
- $grid->column('consume_type', '消耗类型')->display(function ($type) {
- return CONSUME_TYPE::getName($type);
- });
- $grid->column('target_id', '目标ID')->display(function ($targetId) {
- // 根据消耗类型显示目标名称
- if ($this->consume_type == CONSUME_TYPE::ITEM->value) {
- $item = Item::find($targetId);
- return $item ? "{$item->name} ({$targetId})" : $targetId;
- } elseif ($this->consume_type == CONSUME_TYPE::CURRENCY->value) {
- $currency = Currency::find($targetId);
- return $currency ? "{$currency->name} ({$targetId})" : $targetId;
- }
- return $targetId;
- });
- $grid->column('quantity', '数量');
- $grid->column('created_at', '创建时间');
- $grid->column('updated_at', '更新时间');
- $grid->filter(function (Grid\Filter $filter) {
- $filter->equal('id', 'ID');
- $filter->equal('group_id', '消耗组')->select(
- GameConsumeGroup::pluck('name', 'id')
- );
- $filter->equal('consume_type', '消耗类型')->select(CONSUME_TYPE::getAll());
- $filter->equal('target_id', '目标ID');
- });
- });
- }
- /**
- * 创建详情页
- *
- * @param mixed $id
- * @return Show
- */
- protected function detail($id)
- {
- return Show::make($id, new GameConsumeItemRepository(), function (Show $show) {
- $show->field('id', 'ID');
- $show->field('group_id', '消耗组')->as(function ($groupId) {
- $group = GameConsumeGroup::find($groupId);
- return $group ? "{$group->name} ({$group->code})" : "未知 ({$groupId})";
- });
- $show->field('consume_type', '消耗类型')->as(function ($type) {
- return CONSUME_TYPE::getName($type);
- });
- $show->field('target_id', '目标ID')->as(function ($targetId) {
- // 根据消耗类型显示目标名称
- if ($this->consume_type == CONSUME_TYPE::ITEM->value) {
- $item = Item::find($targetId);
- return $item ? "{$item->name} ({$targetId})" : $targetId;
- } elseif ($this->consume_type == CONSUME_TYPE::CURRENCY->value) {
- $currency = Currency::find($targetId);
- return $currency ? "{$currency->name} ({$targetId})" : $targetId;
- }
- return $targetId;
- });
- $show->field('param1', '参数1');
- $show->field('param2', '参数2');
- $show->field('quantity', '数量');
- $show->field('extra_data', '额外数据')->json();
- $show->field('created_at', '创建时间');
- $show->field('updated_at', '更新时间');
- });
- }
- /**
- * 创建表单
- *
- * @return Form
- */
- protected function form()
- {
- return Form::make(new GameConsumeItemRepository(), function (Form $form) {
- $form->display('id', 'ID');
- $form->select('group_id', '消耗组')
- ->options(GameConsumeGroup::pluck('name', 'id'))
- ->required();
-
- $form->select('consume_type', '消耗类型')
- ->options(CONSUME_TYPE::getAll())
- ->required()
- ->when(CONSUME_TYPE::ITEM->value, function (Form $form) {
- // 物品消耗
- $form->select('target_id', '物品')
- ->options(Item::pluck('name', 'id'))
- ->required()
- ->help('选择要消耗的物品');
- })
- ->when(CONSUME_TYPE::CURRENCY->value, function (Form $form) {
- // 货币消耗
- $form->select('target_id', '货币')
- ->options(Currency::pluck('name', 'id'))
- ->required()
- ->help('选择要消耗的货币');
- });
-
- $form->number('param1', '参数1')
- ->default(0)
- ->help('根据消耗类型不同含义,如物品的品质、货币的来源等');
- $form->number('param2', '参数2')
- ->default(0)
- ->help('根据消耗类型不同含义,如物品的绑定状态、货币的类型等');
- $form->number('quantity', '数量')
- ->default(1)
- ->min(1)
- ->required();
- $form->textarea('extra_data', '额外数据')
- ->help('JSON格式,可存储特定消耗类型的额外参数');
- $form->display('created_at', '创建时间');
- $form->display('updated_at', '更新时间');
- });
- }
- }
|