FarmHouseConfigController.php 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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\SyncFarmHouseJsonTool;
  9. use App\Module\Farm\Repositories\FarmHouseConfigRepository;
  10. use App\Module\Game\AdminControllers\LazyRenderable\GameConsumeGroupLazyRenderable;
  11. use App\Module\Game\Models\GameConsumeGroup;
  12. use Dcat\Admin\Form;
  13. use Dcat\Admin\Grid;
  14. use Dcat\Admin\Show;
  15. use Spatie\RouteAttributes\Attributes\Resource;
  16. use UCore\DcatAdmin\AdminController;
  17. /**
  18. * 房屋等级配置管理控制器
  19. */
  20. #[Resource('farm-house-configs', names: 'dcat.admin.farm-house-configs')]
  21. class FarmHouseConfigController extends AdminController
  22. {
  23. /**
  24. * 页面标题
  25. *
  26. * @var string
  27. */
  28. protected $title = '房屋等级配置管理';
  29. /**
  30. * 页面描述
  31. *
  32. * @var string
  33. */
  34. protected $description = '管理房屋等级的配置信息';
  35. /**
  36. * 构建表格
  37. *
  38. * @return Grid
  39. */
  40. protected function grid()
  41. {
  42. return Grid::make(new FarmHouseConfigRepository(), function (Grid $grid) {
  43. // 检查配置表状态
  44. $status = RefreshCheckTool::checkSyncStatus();
  45. if ($status['is_synced']) {
  46. admin_success('JSON配置表状态', $status['message']);
  47. } else {
  48. admin_warning('JSON配置表状态', $status['message']);
  49. }
  50. // 添加工具按钮
  51. $grid->tools([
  52. new RefreshCheckTool($status['should_display']),
  53. new SyncFarmHouseJsonTool($status['should_display'])
  54. ]);
  55. $helper = new GridHelper($grid, $this);
  56. $helper->columnId();
  57. $grid->column('level', '等级')->sortable();
  58. $grid->column('output_bonus', '产出加成')->display(function ($value) {
  59. return ($value * 100) . '%';
  60. })->sortable();
  61. $grid->column('special_land_limit', '特殊土地上限')->sortable();
  62. $grid->column('upgrade_materials', '升级所需消耗组')->display(function ($materialsGroupId) {
  63. if (!$materialsGroupId) {
  64. return '<span class="text-muted">未设置</span>';
  65. }
  66. $consumeGroup = GameConsumeGroup::find($materialsGroupId);
  67. return $consumeGroup ? "{$consumeGroup->name} ({$consumeGroup->code})" : "未知 ({$materialsGroupId})";
  68. });
  69. $grid->column('downgrade_days', '降级天数')->sortable();
  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('level', '等级');
  76. $filterHelper->betweenDatetime('created_at', '创建时间');
  77. });
  78. $grid->actions(function (Grid\Displayers\Actions $actions) {
  79. $actions->disableDelete();
  80. });
  81. });
  82. }
  83. /**
  84. * 构建详情页
  85. *
  86. * @param mixed $id
  87. * @return Show
  88. */
  89. protected function detail($id)
  90. {
  91. return Show::make($id, new FarmHouseConfigRepository(), function (Show $show) {
  92. new ShowHelper($show, $this);
  93. $show->field('id', 'ID');
  94. $show->field('level', '等级');
  95. $show->field('output_bonus', '产出加成')->as(function ($value) {
  96. return ($value * 100) . '%';
  97. });
  98. $show->field('special_land_limit', '特殊土地上限');
  99. $show->field('upgrade_materials', '升级所需消耗组')->as(function ($materialsGroupId) {
  100. if (!$materialsGroupId) {
  101. return '<span class="text-muted">未设置消耗组</span>';
  102. }
  103. $consumeGroup = GameConsumeGroup::find($materialsGroupId);
  104. return $consumeGroup ? "{$consumeGroup->name} ({$consumeGroup->code})" : "未知 ({$materialsGroupId})";
  105. });
  106. $show->field('downgrade_days', '降级天数');
  107. $show->field('created_at', '创建时间');
  108. $show->field('updated_at', '更新时间');
  109. });
  110. }
  111. /**
  112. * 构建表单
  113. *
  114. * @return Form
  115. */
  116. protected function form()
  117. {
  118. return Form::make(new FarmHouseConfigRepository(), function (Form $form) {
  119. new FormHelper($form, $this);
  120. $form->display('id', 'ID');
  121. $form->number('level', '等级')->min(1)->max(12)->required();
  122. $form->rate('output_bonus', '产出加成')->max(1)->step(0.05)->required();
  123. $form->number('special_land_limit', '特殊土地上限')->min(0)->default(0)->required();
  124. // 添加消耗组选择(使用表格选择器)
  125. $consumeGroupTable = GameConsumeGroupLazyRenderable::make();
  126. $form->selectTable('upgrade_materials', '升级所需消耗组')
  127. ->from($consumeGroupTable)
  128. ->title('选择消耗组')
  129. ->model($consumeGroupTable->getModel(), $consumeGroupTable->getModelSelectId(), $consumeGroupTable->getModelViewName())
  130. ->help('选择消耗组后,将使用消耗组中的消耗项作为升级所需材料');
  131. $form->number('downgrade_days', '降级天数')->min(1)->help('留空表示不降级');
  132. $form->display('created_at', '创建时间');
  133. $form->display('updated_at', '更新时间');
  134. });
  135. }
  136. }