columnId(); $grid->column('name', '规则名称'); $grid->column('code', '规则编码'); $grid->column('description', '描述')->limit(50); $grid->column('rule_type', '规则类型')->display(function () { if (!empty($this->item_id)) { return '物品规则'; } elseif (!empty($this->category_id)) { return '分类规则'; } else { return '通用规则'; } }); $grid->column('item.name', '物品名称'); $grid->column('category.name', '分类名称'); $grid->column('consume_group_id', '消耗组')->display(function ($value) { if (!$value) return '无'; $group = \App\Module\Game\Models\GameConsumeGroup::find($value); return $group ? $group->name : "消耗组 {$value}"; }); $grid->column('reward_group_id', '奖励组')->display(function ($value) { if (!$value) return '无'; $group = \App\Module\Game\Models\GameRewardGroup::find($value); return $group ? $group->name : "奖励组 {$value}"; }); $grid->column('condition_group_id', '条件组')->display(function ($value) { if (!$value) return '无'; $group = \App\Module\Game\Models\GameConditionGroup::find($value); return $group ? $group->name : "条件组 {$value}"; }); $grid->column('priority', '优先级')->sortable(); $grid->column('is_active', '是否启用')->switch(); $grid->column('created_at', '创建时间'); $grid->column('updated_at', '更新时间'); // 筛选 $grid->filter(function ($filter) { $helper = new FilterHelper($filter, $this); $helper->equal('id', 'ID'); $filter->like('name', '规则名称'); $filter->like('code', '规则编码'); $filter->equal('item_id', '物品')->select( \App\Module\GameItems\Models\Item::pluck('name', 'id') ); $filter->equal('category_id', '分类')->select( \App\Module\GameItems\Models\ItemCategory::pluck('name', 'id') ); $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', '是否启用')->radio([ 1 => '是', 0 => '否', ]); }); }); } /** * 详情页 * * @param mixed $id * @param Content $content * @return Content */ public function show($id, Content $content) { return $content ->header($this->title) ->description('详情') ->body($this->detail($id)); } /** * 详情页 * * @param mixed $id * @return Show */ protected function detail($id) { return Show::make($id, new ItemDismantleRuleRepository(), function (Show $show) { $helper = new ShowHelper($show, $this); $helper->field('id', 'ID'); $helper->field('name', '规则名称'); $helper->field('code', '规则编码'); $helper->field('description', '规则描述'); // 规则类型 $show->field('rule_type', '规则类型')->as(function () { if (!empty($this->item_id)) { return '物品规则'; } elseif (!empty($this->category_id)) { return '分类规则'; } else { return '通用规则'; } }); // 根据规则类型显示不同字段 $show->field('item.name', '物品名称'); $show->field('category.name', '分类名称'); $helper->field('priority', '优先级'); $helper->field('sort_order', '排序权重'); $show->field('is_active', '是否启用')->as(function ($value) { return $value ? '是' : '否'; }); // 显示消耗组 $show->divider('消耗组'); $show->field('consumeGroup.name', '消耗组名称'); $show->field('consumeGroup.description', '消耗组描述'); // 显示奖励组 $show->divider('奖励组'); $show->field('rewardGroup.name', '奖励组名称'); $show->field('rewardGroup.description', '奖励组描述'); // 显示条件组 $show->divider('条件组'); $show->field('conditionGroup.name', '条件组名称'); $show->field('conditionGroup.description', '条件组描述'); $helper->field('created_at', '创建时间'); $helper->field('updated_at', '更新时间'); }); } /** * 创建页 * * @param Content $content * @return Content */ public function create(Content $content) { return $content ->header($this->title) ->description('创建') ->body($this->form()); } /** * 编辑页 * * @param mixed $id * @param Content $content * @return Content */ public function edit($id, Content $content) { return $content ->header($this->title) ->description('编辑') ->body($this->form()->edit($id)); } /** * 表单 * * @return Form */ protected function form() { return Form::make(new ItemDismantleRuleRepository(), function (Form $form) { $helper = new \App\Module\GameItems\AdminControllers\Helper\FormHelper($form, $this); $helper->text('name', '规则名称')->required(); $helper->text('code', '规则编码') ->help('唯一的规则编码,用于程序识别'); $form->textarea('description', '规则描述') ->help('规则的详细描述'); // 规则类型 $form->radio('rule_type', '规则类型') ->options(['item' => '物品规则', 'category' => '分类规则', 'general' => '通用规则']) ->default('general') ->when('item', function (Form $form) { $form->select('item_id', '物品') ->options(\App\Module\GameItems\Models\Item::pluck('name', 'id')) ->required(); }) ->when('category', function (Form $form) { $form->select('category_id', '分类') ->options(\App\Module\GameItems\Models\ItemCategory::pluck('name', 'id')) ->required(); }); // 消耗组选择 $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')) ->help('选择分解后获得的奖励组') ->required(); // 条件组选择 $form->select('condition_group_id', '条件组') ->options(\App\Module\Game\Models\GameConditionGroup::pluck('name', 'id')) ->help('选择使用该分解规则需要满足的条件组(可选)'); $helper->number('priority') ->default(0) ->help('数字越大优先级越高,当物品同时匹配多个规则时,使用优先级最高的规则'); $form->number('sort_order', '排序权重') ->default(0) ->help('数值越大排序越靠前'); $form->switch('is_active', '是否启用') ->default(true) ->help('是否启用该分解规则'); // 保存前回调 $form->saving(function (Form $form) { // 根据规则类型设置对应的字段 if ($form->rule_type == 'item') { $form->category_id = null; } elseif ($form->rule_type == 'category') { $form->item_id = null; } else { // 通用规则 $form->item_id = null; $form->category_id = null; } }); }); } }