| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214 |
- <?php
- namespace App\Module\GameItems\AdminControllers;
- use App\Module\GameItems\Enums\ITEM_TYPE;
- use App\Module\GameItems\Models\Item;
- use App\Module\GameItems\Models\ItemChestConfig;
- use App\Module\GameItems\Repositories\ItemChestConfigRepository;
- use Dcat\Admin\Form;
- use Dcat\Admin\Grid;
- use Dcat\Admin\Show;
- use Spatie\RouteAttributes\Attributes\Resource;
- use UCore\DcatAdmin\AdminController;
- use App\Module\GameItems\AdminControllers\Helper\FilterHelper;
- use App\Module\GameItems\AdminControllers\Helper\FormHelper;
- use App\Module\GameItems\AdminControllers\Helper\GridHelper;
- use App\Module\GameItems\AdminControllers\Helper\ShowHelper;
- /**
- * 宝箱配置控制器
- *
- * 路由: /admin/game-items/chest-configs
- * 菜单: 游戏管理 -> 物品管理 -> 宝箱配置
- */
- #[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) {
- $helper = new GridHelper($grid, $this);
- $helper->columnId();
- $grid->column('item.name', '宝箱名称')->display(function ($name) {
- return $name ?: '未知宝箱';
- });
- $grid->column('item_id', '宝箱ID')->sortable();
- $grid->column('consume_group.name', '消耗组')->display(function ($name) {
- return $name ?: '<span class="text-muted">无消耗</span>';
- });
- $grid->column('reward_group.name', '奖励组')->display(function ($name) {
- return $name ?: '<span class="text-danger">未配置</span>';
- });
- $grid->column('condition_group.name', '条件组')->display(function ($name) {
- return $name ?: '<span class="text-muted">无条件</span>';
- });
- $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) {
- $filterHelper = new FilterHelper($filter, $this);
- $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->model()->with(['item', 'consumeGroup', 'rewardGroup', 'conditionGroup']);
- // 工具栏
- $grid->tools(function (Grid\Tools $tools) {
- $tools->append('<a href="' . admin_url('game-items') . '" class="btn btn-sm btn-primary">返回物品管理</a>');
- });
- // 行操作
- $grid->actions(function (Grid\Displayers\Actions $actions) {
- $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('consume_group.name', '消耗组名称');
- $show->field('consume_group.code', '消耗组编码');
- $show->field('consume_group.description', '消耗组描述');
- $show->divider('奖励组配置');
- $show->field('reward_group.name', '奖励组名称');
- $show->field('reward_group.code', '奖励组编码');
- $show->field('reward_group.description', '奖励组描述');
- $show->field('reward_group.is_random', '是否随机')->using([0 => '否', 1 => '是']);
- $show->field('reward_group.random_count', '随机数量');
- $show->divider('条件组配置');
- $show->field('condition_group.name', '条件组名称');
- $show->field('condition_group.code', '条件组编码');
- $show->field('condition_group.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) {
- $helper = new FormHelper($form, $this);
- $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('只能为宝箱类型物品创建配置');
- }
- });
- });
- }
- }
|