FarmLandUpgradeConfigController.php 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. <?php
  2. namespace App\Module\Farm\AdminControllers;
  3. use App\Module\Farm\AdminControllers\Helper\FilterHelper;
  4. use App\Module\Farm\AdminControllers\Helper\FormHelper;
  5. use App\Module\Farm\AdminControllers\Helper\GridHelper;
  6. use App\Module\Farm\AdminControllers\Helper\ShowHelper;
  7. use App\Module\Farm\AdminControllers\Tools\RefreshCheckTool;
  8. use App\Module\Farm\AdminControllers\Tools\SyncFarmLandJsonTool;
  9. use App\Module\Farm\Models\FarmLandType;
  10. use App\Module\Farm\Repositories\FarmLandTypeRepository;
  11. use App\Module\Farm\Repositories\FarmLandUpgradeConfigRepository;
  12. use App\Module\Game\AdminControllers\LazyRenderable\GameConsumeGroupLazyRenderable;
  13. use App\Module\Game\AdminControllers\LazyRenderable\GameConditionGroupLazyRenderable;
  14. use App\Module\Game\Models\GameConsumeGroup;
  15. use App\Module\Game\Models\GameConditionGroup;
  16. use Dcat\Admin\Form;
  17. use Dcat\Admin\Grid;
  18. use Dcat\Admin\Show;
  19. use UCore\DcatAdmin\AdminController;
  20. use Spatie\RouteAttributes\Attributes\Resource;
  21. /**
  22. * 土地升级配置管理控制器
  23. */
  24. #[Resource('farm-land-upgrade-configs', names: 'dcat.admin.farm-land-upgrade-configs')]
  25. class FarmLandUpgradeConfigController extends AdminController
  26. {
  27. /**
  28. * 页面标题
  29. *
  30. * @var string
  31. */
  32. protected $title = '土地升级配置管理';
  33. /**
  34. * 页面描述
  35. *
  36. * @var string
  37. */
  38. protected $description = '管理土地升级的配置信息';
  39. /**
  40. * 构建表格
  41. *
  42. * @return Grid
  43. */
  44. protected function grid()
  45. {
  46. return Grid::make(new FarmLandUpgradeConfigRepository(), function (Grid $grid) {
  47. $helper = new GridHelper($grid, $this);
  48. // 检查配置表状态
  49. $status = RefreshCheckTool::checkSyncStatus('land');
  50. if ($status['is_synced']) {
  51. admin_success('JSON配置表状态', $status['message']);
  52. } else {
  53. admin_warning('JSON配置表状态', $status['message']);
  54. }
  55. // 添加工具按钮
  56. $grid->tools([
  57. new SyncFarmLandJsonTool()
  58. ]);
  59. $helper->columnId();
  60. $grid->column('from_type_id', '起始土地类型')->display(function ($fromTypeId) {
  61. return $this->fromType ? $this->fromType->name : "未知类型 (ID: {$fromTypeId})";
  62. })->sortable();
  63. $grid->column('to_type_id', '目标土地类型')->display(function ($toTypeId) {
  64. return $this->toType ? $this->toType->name : "未知类型 (ID: {$toTypeId})";
  65. })->sortable();
  66. // 添加消耗组详情列
  67. $helper->columnConsumeGroupDetails();
  68. // 添加条件组详情列
  69. $helper->columnConditionGroupDetails();
  70. $helper->columnCreatedAt();
  71. $helper->columnUpdatedAt();
  72. $grid->filter(function (Grid\Filter $filter) {
  73. $filterHelper = new FilterHelper($filter, $this);
  74. $filterHelper->equalId();
  75. $filter->equal('from_type_id', '起始土地类型ID');
  76. $filter->equal('to_type_id', '目标土地类型ID');
  77. $filterHelper->betweenDatetime('created_at', '创建时间');
  78. });
  79. });
  80. }
  81. /**
  82. * 构建详情页
  83. *
  84. * @param mixed $id
  85. * @return Show
  86. */
  87. protected function detail($id)
  88. {
  89. return Show::make($id, new FarmLandUpgradeConfigRepository(), function (Show $show) {
  90. $helper = new ShowHelper($show, $this);
  91. $show->field('id', 'ID');
  92. $show->field('from_type_id', '起始土地类型')->as(function ($fromTypeId) {
  93. return $this->fromType ? "{$this->fromType->name} (ID: {$fromTypeId})" : "未知类型 (ID: {$fromTypeId})";
  94. });
  95. $show->field('to_type_id', '目标土地类型')->as(function ($toTypeId) {
  96. return $this->toType ? "{$this->toType->name} (ID: {$toTypeId})" : "未知类型 (ID: {$toTypeId})";
  97. });
  98. $show->field('materials', '消耗组')->as(function ($materialsGroupId) {
  99. if (!$materialsGroupId) {
  100. return '<span class="text-muted">使用 materials 字段</span>';
  101. }
  102. $consumeGroup = GameConsumeGroup::find($materialsGroupId);
  103. return $consumeGroup ? "{$consumeGroup->name} ({$consumeGroup->code})" : "未知 ({$materialsGroupId})";
  104. });
  105. $show->field('conditions', '条件组')->as(function ($conditionsGroupId) {
  106. if (!$conditionsGroupId) {
  107. return '<span class="text-muted">使用 conditions 字段</span>';
  108. }
  109. $conditionGroup = GameConditionGroup::find($conditionsGroupId);
  110. return $conditionGroup ? "{$conditionGroup->name} ({$conditionGroup->code})" : "未知 ({$conditionsGroupId})";
  111. });
  112. $show->field('created_at', '创建时间');
  113. $show->field('updated_at', '更新时间');
  114. });
  115. }
  116. /**
  117. * 构建表单
  118. *
  119. * @return Form
  120. */
  121. protected function form()
  122. {
  123. return Form::make(new FarmLandUpgradeConfigRepository(), function (Form $form) {
  124. $helper = new FormHelper($form, $this);
  125. $form->display('id', 'ID');
  126. $helper->selectModelOption('from_type_id', '起始土地类型', FarmLandType::class, 'name', 'id');
  127. $helper->selectModelOption('to_type_id', '目标土地类型', FarmLandType::class, 'name', 'id');
  128. // 添加消耗组选择(使用表格选择器)
  129. $consumeGroupTable = GameConsumeGroupLazyRenderable::make();
  130. $form->selectTable('materials', '消耗组')
  131. ->from($consumeGroupTable)
  132. ->title('选择消耗组')
  133. ->model($consumeGroupTable->getModel(), $consumeGroupTable->getModelSelectId(), $consumeGroupTable->getModelViewName())
  134. ->help('选择消耗组后,将使用消耗组中的消耗项作为升级所需材料,而不使用 materials 字段');
  135. // 添加条件组选择(使用表格选择器)
  136. $conditionGroupTable = GameConditionGroupLazyRenderable::make();
  137. $form->selectTable('conditions', '条件组')
  138. ->from($conditionGroupTable)
  139. ->title('选择条件组')
  140. ->model($conditionGroupTable->getModel(), $conditionGroupTable->getModelSelectId(), $conditionGroupTable->getModelViewName())
  141. ->help('选择条件组后,将使用条件组中的条件项作为升级条件,而不使用 conditions 字段');
  142. $form->divider('以下字段仅在未选择消耗组时使用');
  143. $form->display('created_at', '创建时间');
  144. $form->display('updated_at', '更新时间');
  145. // 验证起始和目标土地类型不能相同
  146. $form->saving(function (Form $form) {
  147. if ($form->from_type_id == $form->to_type_id) {
  148. return $form->error('起始土地类型和目标土地类型不能相同');
  149. }
  150. });
  151. });
  152. }
  153. }