| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177 |
- <?php
- namespace App\Module\Farm\AdminControllers;
- use App\Module\Farm\AdminControllers\Helper\FilterHelper;
- use App\Module\Farm\AdminControllers\Helper\FormHelper;
- use App\Module\Farm\AdminControllers\Helper\GridHelper;
- use App\Module\Farm\AdminControllers\Helper\ShowHelper;
- use App\Module\Farm\AdminControllers\Tools\RefreshCheckTool;
- use App\Module\Farm\AdminControllers\Tools\SyncFarmLandJsonTool;
- use App\Module\Farm\Models\FarmLandType;
- use App\Module\Farm\Repositories\FarmLandTypeRepository;
- use App\Module\Farm\Repositories\FarmLandUpgradeConfigRepository;
- use App\Module\Game\AdminControllers\LazyRenderable\GameConsumeGroupLazyRenderable;
- use App\Module\Game\AdminControllers\LazyRenderable\GameConditionGroupLazyRenderable;
- use App\Module\Game\Models\GameConsumeGroup;
- use App\Module\Game\Models\GameConditionGroup;
- use Dcat\Admin\Form;
- use Dcat\Admin\Grid;
- use Dcat\Admin\Show;
- use UCore\DcatAdmin\AdminController;
- use Spatie\RouteAttributes\Attributes\Resource;
- /**
- * 土地升级配置管理控制器
- */
- #[Resource('farm-land-upgrade-configs', names: 'dcat.admin.farm-land-upgrade-configs')]
- class FarmLandUpgradeConfigController extends AdminController
- {
- /**
- * 页面标题
- *
- * @var string
- */
- protected $title = '土地升级配置管理';
- /**
- * 页面描述
- *
- * @var string
- */
- protected $description = '管理土地升级的配置信息';
- /**
- * 构建表格
- *
- * @return Grid
- */
- protected function grid()
- {
- return Grid::make(new FarmLandUpgradeConfigRepository(), function (Grid $grid) {
- $helper = new GridHelper($grid, $this);
- // 检查配置表状态
- $status = RefreshCheckTool::checkSyncStatus('land');
- if ($status['is_synced']) {
- admin_success('JSON配置表状态', $status['message']);
- } else {
- admin_warning('JSON配置表状态', $status['message']);
- }
- // 添加工具按钮
- $grid->tools([
- new SyncFarmLandJsonTool()
- ]);
- $helper->columnId();
- $grid->column('from_type_id', '起始土地类型')->display(function ($fromTypeId) {
- return $this->fromType ? $this->fromType->name : "未知类型 (ID: {$fromTypeId})";
- })->sortable();
- $grid->column('to_type_id', '目标土地类型')->display(function ($toTypeId) {
- return $this->toType ? $this->toType->name : "未知类型 (ID: {$toTypeId})";
- })->sortable();
- // 添加消耗组详情列
- $helper->columnConsumeGroupDetails();
- // 添加条件组详情列
- $helper->columnConditionGroupDetails();
- $helper->columnCreatedAt();
- $helper->columnUpdatedAt();
- $grid->filter(function (Grid\Filter $filter) {
- $filterHelper = new FilterHelper($filter, $this);
- $filterHelper->equalId();
- $filter->equal('from_type_id', '起始土地类型ID');
- $filter->equal('to_type_id', '目标土地类型ID');
- $filterHelper->betweenDatetime('created_at', '创建时间');
- });
- });
- }
- /**
- * 构建详情页
- *
- * @param mixed $id
- * @return Show
- */
- protected function detail($id)
- {
- return Show::make($id, new FarmLandUpgradeConfigRepository(), function (Show $show) {
- $helper = new ShowHelper($show, $this);
- $show->field('id', 'ID');
- $show->field('from_type_id', '起始土地类型')->as(function ($fromTypeId) {
- return $this->fromType ? "{$this->fromType->name} (ID: {$fromTypeId})" : "未知类型 (ID: {$fromTypeId})";
- });
- $show->field('to_type_id', '目标土地类型')->as(function ($toTypeId) {
- return $this->toType ? "{$this->toType->name} (ID: {$toTypeId})" : "未知类型 (ID: {$toTypeId})";
- });
- $show->field('materials', '消耗组')->as(function ($materialsGroupId) {
- if (!$materialsGroupId) {
- return '<span class="text-muted">使用 materials 字段</span>';
- }
- $consumeGroup = GameConsumeGroup::find($materialsGroupId);
- return $consumeGroup ? "{$consumeGroup->name} ({$consumeGroup->code})" : "未知 ({$materialsGroupId})";
- });
- $show->field('conditions', '条件组')->as(function ($conditionsGroupId) {
- if (!$conditionsGroupId) {
- return '<span class="text-muted">使用 conditions 字段</span>';
- }
- $conditionGroup = GameConditionGroup::find($conditionsGroupId);
- return $conditionGroup ? "{$conditionGroup->name} ({$conditionGroup->code})" : "未知 ({$conditionsGroupId})";
- });
- $show->field('created_at', '创建时间');
- $show->field('updated_at', '更新时间');
- });
- }
- /**
- * 构建表单
- *
- * @return Form
- */
- protected function form()
- {
- return Form::make(new FarmLandUpgradeConfigRepository(), function (Form $form) {
- $helper = new FormHelper($form, $this);
- $form->display('id', 'ID');
- $helper->selectModelOption('from_type_id', '起始土地类型', FarmLandType::class, 'name', 'id');
- $helper->selectModelOption('to_type_id', '目标土地类型', FarmLandType::class, 'name', 'id');
- // 添加消耗组选择(使用表格选择器)
- $consumeGroupTable = GameConsumeGroupLazyRenderable::make();
- $form->selectTable('materials', '消耗组')
- ->from($consumeGroupTable)
- ->title('选择消耗组')
- ->model($consumeGroupTable->getModel(), $consumeGroupTable->getModelSelectId(), $consumeGroupTable->getModelViewName())
- ->help('选择消耗组后,将使用消耗组中的消耗项作为升级所需材料,而不使用 materials 字段');
- // 添加条件组选择(使用表格选择器)
- $conditionGroupTable = GameConditionGroupLazyRenderable::make();
- $form->selectTable('conditions', '条件组')
- ->from($conditionGroupTable)
- ->title('选择条件组')
- ->model($conditionGroupTable->getModel(), $conditionGroupTable->getModelSelectId(), $conditionGroupTable->getModelViewName())
- ->help('选择条件组后,将使用条件组中的条件项作为升级条件,而不使用 conditions 字段');
- $form->divider('以下字段仅在未选择消耗组时使用');
- $form->display('created_at', '创建时间');
- $form->display('updated_at', '更新时间');
- // 验证起始和目标土地类型不能相同
- $form->saving(function (Form $form) {
- if ($form->from_type_id == $form->to_type_id) {
- return $form->error('起始土地类型和目标土地类型不能相同');
- }
- });
- });
- }
- }
|