FarmLandUpgradeConfigController.php 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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', '起始土地类型ID')->sortable();
  61. $grid->column('to_type_id', '目标土地类型ID')->sortable();
  62. $grid->column('materials', '消耗组')->display(function ($materialsGroupId) {
  63. if (!$materialsGroupId) {
  64. return '<span class="text-muted">使用 materials 字段</span>';
  65. }
  66. $consumeGroup = GameConsumeGroup::find($materialsGroupId);
  67. return $consumeGroup ? "{$consumeGroup->name} ({$consumeGroup->code})" : "未知 ({$materialsGroupId})";
  68. });
  69. $grid->column('condition', '条件组')->display(function ($conditionsGroupId) {
  70. if (!$conditionsGroupId) {
  71. return '<span class="text-muted">使用 conditions 字段</span>';
  72. }
  73. $conditionGroup = GameConditionGroup::find($conditionsGroupId);
  74. return $conditionGroup ? "{$conditionGroup->name} ({$conditionGroup->code})" : "未知 ({$conditionsGroupId})";
  75. });
  76. $helper->columnCreatedAt();
  77. $helper->columnUpdatedAt();
  78. $grid->filter(function (Grid\Filter $filter) {
  79. $filterHelper = new FilterHelper($filter, $this);
  80. $filterHelper->equalId();
  81. $filter->equal('from_type_id', '起始土地类型ID');
  82. $filter->equal('to_type_id', '目标土地类型ID');
  83. $filterHelper->betweenDatetime('created_at', '创建时间');
  84. });
  85. });
  86. }
  87. /**
  88. * 构建详情页
  89. *
  90. * @param mixed $id
  91. * @return Show
  92. */
  93. protected function detail($id)
  94. {
  95. return Show::make($id, new FarmLandUpgradeConfigRepository(), function (Show $show) {
  96. $helper = new ShowHelper($show, $this);
  97. $show->field('id', 'ID');
  98. $show->field('from_type_id', '起始土地类型ID');
  99. $show->field('to_type_id', '目标土地类型ID');
  100. $show->field('material', '消耗组')->as(function ($materialsGroupId) {
  101. $consumeGroup = GameConsumeGroup::find($materialsGroupId);
  102. return $consumeGroup ? "{$consumeGroup->name} ({$consumeGroup->code})" : "未知 ({$materialsGroupId})";
  103. });
  104. $show->field('conditions', '条件组')->as(function ($conditionsGroupId) {
  105. $conditionGroup = GameConditionGroup::find($conditionsGroupId);
  106. return $conditionGroup ? "{$conditionGroup->name} ({$conditionGroup->code})" : "未知 ({$conditionsGroupId})";
  107. });
  108. $helper->fieldModelCatsJson('materials', '升级所需材料(已废弃)');
  109. $helper->fieldModelCatsJson('conditions', '其他升级条件');
  110. $show->field('created_at', '创建时间');
  111. $show->field('updated_at', '更新时间');
  112. });
  113. }
  114. /**
  115. * 构建表单
  116. *
  117. * @return Form
  118. */
  119. protected function form()
  120. {
  121. return Form::make(new FarmLandUpgradeConfigRepository(), function (Form $form) {
  122. $helper = new FormHelper($form, $this);
  123. $form->display('id', 'ID');
  124. $helper->selectModelOption('from_type_id', '起始土地类型ID', FarmLandType::class, 'name', 'id');
  125. $helper->selectModelOption('to_type_id', '目标土地类型ID', FarmLandType::class, 'name', 'id');
  126. // 添加消耗组选择(使用表格选择器)
  127. $consumeGroupTable = GameConsumeGroupLazyRenderable::make();
  128. $form->selectTable('materials', '消耗组')
  129. ->from($consumeGroupTable)
  130. ->title('选择消耗组')
  131. ->model($consumeGroupTable->getModel(), $consumeGroupTable->getModelSelectId(), $consumeGroupTable->getModelViewName())
  132. ->help('选择消耗组后,将使用消耗组中的消耗项作为升级所需材料,而不使用 materials 字段');
  133. // 添加条件组选择(使用表格选择器)
  134. $conditionGroupTable = GameConditionGroupLazyRenderable::make();
  135. $form->selectTable('conditions', '条件组')
  136. ->from($conditionGroupTable)
  137. ->title('选择条件组')
  138. ->model($conditionGroupTable->getModel(), $conditionGroupTable->getModelSelectId(), $conditionGroupTable->getModelViewName())
  139. ->help('选择条件组后,将使用条件组中的条件项作为升级条件,而不使用 conditions 字段');
  140. $form->divider('以下字段仅在未选择消耗组时使用');
  141. $form->display('created_at', '创建时间');
  142. $form->display('updated_at', '更新时间');
  143. // 验证起始和目标土地类型不能相同
  144. $form->saving(function (Form $form) {
  145. if ($form->from_type_id == $form->to_type_id) {
  146. return $form->error('起始土地类型和目标土地类型不能相同');
  147. }
  148. });
  149. });
  150. }
  151. }