物品管理 -> 宝箱配置 */ #[Resource('game-items-chest-configs', names: 'dcat.admin.game-items-chest-configs')] class ItemChestConfigController extends AdminController { /** * 页面标题 * * @var string */ protected $title = '宝箱配置'; /** * 数据仓库 * * @var ItemChestConfigRepository */ protected $repository; /** * 构造函数 */ public function __construct() { $this->repository = new ItemChestConfigRepository(); } /** * 列表页面 * * @return Grid */ protected function grid(): Grid { return Grid::make(new ItemChestConfig(), function (Grid $grid) { // 首先设置关联查询,确保数据正确预加载,包括子项目 $grid->model()->with(['item', 'consumeGroup.consumeItems', 'rewardGroup.rewardItems', 'conditionGroup.conditionItems']); $helper = new GridHelper($grid, $this); $helper->columnId(); $grid->column('item.name', '宝箱名称')->display(function ($name) { return $name ?: '未知宝箱'; }); $grid->column('item_id', '宝箱ID')->sortable(); // 消耗组列 - 可点击跳转 $grid->column('consumeGroup.name', '消耗组')->display(function ($name) { if (!$name || !$this->consume_group_id) { return '无消耗'; } return $name; })->link(function () { if (!$this->consume_group_id) { return ''; } return admin_url('game-consume-groups/' . $this->consume_group_id); }); // 奖励组列 - 可点击跳转 $grid->column('rewardGroup.name', '奖励组')->display(function ($name) { if (!$name || !$this->reward_group_id) { return '未配置'; } return $name; })->link(function () { if (!$this->reward_group_id) { return ''; } return admin_url('game-reward-groups/' . $this->reward_group_id); }); // 条件组列 - 可点击跳转 $grid->column('conditionGroup.name', '条件组')->display(function ($name) { if (!$name || !$this->condition_group_id) { return '无条件'; } return $name; })->link(function () { if (!$this->condition_group_id) { return ''; } return admin_url('game-condition-groups/' . $this->condition_group_id); }); // 消耗组详情列 $grid->column('consume_group_details', '消耗组详情')->display(function () { if (!$this->consumeGroup) { return '无消耗组'; } return $this->consumeGroup->formatConsumeDetails(); })->width('200px'); // 奖励组详情列 $grid->column('reward_group_details', '奖励组详情')->display(function () { if (!$this->rewardGroup) { return '无奖励组'; } return $this->rewardGroup->formatRewardDetails(); })->width('200px'); // 条件组详情列 $grid->column('condition_group_details', '条件组详情')->display(function () { if (!$this->conditionGroup) { return '无条件组'; } return $this->conditionGroup->formatConditionDetails(); })->width('200px'); $grid->column('is_active', '状态')->switch(); $grid->column('status_text', '配置状态')->display(function () { return $this->status_text; })->label([ '正常' => 'success', '未激活' => 'warning', '缺少奖励组' => 'danger', ]); $helper->columnCreatedAt(); $helper->columnUpdatedAt(); // 筛选器 $grid->filter(function (Grid\Filter $filter) { $filter->equal('item_id', '宝箱ID'); $filter->like('item.name', '宝箱名称'); $filter->equal('consume_group_id', '消耗组')->select(\App\Module\Game\Models\GameConsumeGroup::pluck('name', 'id')); $filter->equal('reward_group_id', '奖励组')->select(\App\Module\Game\Models\GameRewardGroup::pluck('name', 'id')); $filter->equal('condition_group_id', '条件组')->select(\App\Module\Game\Models\GameConditionGroup::pluck('name', 'id')); $filter->equal('is_active', '状态')->select([1 => '激活', 0 => '未激活']); }); // 工具栏 $grid->tools(function (Grid\Tools $tools) { $tools->append('返回物品管理'); }); // 行操作 $grid->actions(function (Grid\Displayers\Actions $actions) { // 添加复制按钮 $actions->append(new DuplicateItemChestConfigAction()); $actions->disableDelete(); }); }); } /** * 详情页面 * * @param mixed $id * @return Show */ protected function detail($id): Show { return Show::make($id, new ItemChestConfig(), function (Show $show) { $helper = new ShowHelper($show, $this); $helper->field('id', 'ID'); $show->field('item.name', '宝箱名称'); $show->field('item_id', '宝箱ID'); $show->divider('消耗组配置'); $show->field('consumeGroup.name', '消耗组名称'); $show->field('consumeGroup.code', '消耗组编码'); $show->field('consumeGroup.description', '消耗组描述'); $show->divider('奖励组配置'); $show->field('rewardGroup.name', '奖励组名称'); $show->field('rewardGroup.code', '奖励组编码'); $show->field('rewardGroup.description', '奖励组描述'); $show->field('rewardGroup.is_random', '是否随机')->using([0 => '否', 1 => '是']); $show->field('rewardGroup.random_count', '随机数量'); $show->divider('条件组配置'); $show->field('conditionGroup.name', '条件组名称'); $show->field('conditionGroup.code', '条件组编码'); $show->field('conditionGroup.description', '条件组描述'); $show->divider('基本信息'); $show->field('is_active', '状态')->using([0 => '未激活', 1 => '激活']); $show->field('status_text', '配置状态'); $helper->field('created_at', '创建时间'); $helper->field('updated_at', '更新时间'); }); } /** * 表单页面 * * @return Form */ protected function form(): Form { return Form::make(new ItemChestConfig(), function (Form $form) { $form->display('id', 'ID'); $form->select('item_id', '宝箱物品') ->options(Item::where('type', ITEM_TYPE::CHEST)->pluck('name', 'id')) ->required() ->help('只能选择宝箱类型的物品'); $form->divider('组配置'); $form->select('consume_group_id', '消耗组') ->options(\App\Module\Game\Models\GameConsumeGroup::pluck('name', 'id')) ->help('选择宝箱开启时需要消耗的资源组,留空表示无额外消耗'); $form->select('reward_group_id', '奖励组') ->options(\App\Module\Game\Models\GameRewardGroup::pluck('name', 'id')) ->required() ->help('选择宝箱开启时获得的奖励组'); $form->select('condition_group_id', '条件组') ->options(\App\Module\Game\Models\GameConditionGroup::pluck('name', 'id')) ->help('选择宝箱开启的前置条件组,留空表示无条件限制'); $form->switch('is_active', '激活状态') ->default(1) ->help('只有激活的配置才会生效'); $form->display('created_at', '创建时间'); $form->display('updated_at', '更新时间'); // 保存前验证 $form->saving(function (Form $form) { // 检查宝箱是否已有配置 if ($form->isCreating()) { $exists = ItemChestConfig::where('item_id', $form->item_id)->exists(); if ($exists) { return $form->response()->error('该宝箱已有配置,请编辑现有配置'); } } // 检查物品是否为宝箱类型 $item = Item::find($form->item_id); if (!$item || $item->type !== ITEM_TYPE::CHEST) { return $form->response()->error('只能为宝箱类型物品创建配置'); } }); }); } }