FarmCropController.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  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\Enums\GROWTH_STAGE;
  8. use App\Module\Farm\Repositories\FarmCropRepository;
  9. use App\Module\Farm\Repositories\FarmSeedRepository;
  10. use Dcat\Admin\Form;
  11. use Dcat\Admin\Grid;
  12. use Dcat\Admin\Show;
  13. use UCore\DcatAdmin\AdminController;
  14. use Spatie\RouteAttributes\Attributes\Resource;
  15. use Spatie\RouteAttributes\Attributes\Post;
  16. /**
  17. * 作物管理控制器
  18. */
  19. #[Resource('farm-crops', names: 'dcat.admin.farm-crops')]
  20. class FarmCropController extends AdminController
  21. {
  22. /**
  23. * 页面标题
  24. *
  25. * @var string
  26. */
  27. protected $title = '作物管理';
  28. /**
  29. * 页面描述
  30. *
  31. * @var string
  32. */
  33. protected $description = '管理用户种植的作物';
  34. /**
  35. * 构建表格
  36. *
  37. * @return Grid
  38. */
  39. protected function grid()
  40. {
  41. return Grid::make(new FarmCropRepository(), function (Grid $grid) {
  42. $helper = new GridHelper($grid, $this);
  43. $helper->columnId();
  44. $grid->column('land_id', '土地ID')->sortable();
  45. $helper->columnUserId();
  46. $grid->column('seed_id', '种子ID')->sortable();
  47. $grid->column('plant_time', '种植时间')->sortable();
  48. $helper->columnUseingEnmu('growth_stage', \App\Module\Farm\Enums\GROWTH_STAGE::class,'生长阶段');
  49. $grid->column('stage_end_time', '阶段结束时间')->sortable();
  50. // 添加产出相关字段
  51. $grid->column('final_output_item_id', '产出物品ID')->sortable()->display(function ($value) {
  52. if ($value) {
  53. return "<span class='label label-success'>{$value}</span>";
  54. }
  55. return "<span class='label label-warning'>未确定</span>";
  56. });
  57. $grid->column('final_output_amount', '预定产量')->sortable()->display(function ($value) {
  58. if ($value) {
  59. return "<span class='label label-info'>{$value}</span>";
  60. }
  61. return "<span class='label label-warning'>未确定</span>";
  62. });
  63. // 添加数据完整性状态列
  64. $grid->column('data_status', '数据状态')->display(function () {
  65. $hasItemId = !empty($this->final_output_item_id);
  66. $hasAmount = !empty($this->final_output_amount);
  67. $isMature = $this->growth_stage == GROWTH_STAGE::MATURE->value;
  68. if ($isMature) {
  69. if ($hasItemId && $hasAmount) {
  70. return "<span class='label label-success'>完整</span>";
  71. } elseif ($hasItemId) {
  72. return "<span class='label label-warning'>缺少产量</span>";
  73. } else {
  74. return "<span class='label label-danger'>缺少产出物品</span>";
  75. }
  76. } else {
  77. if ($hasItemId) {
  78. return "<span class='label label-info'>已确定产出物品</span>";
  79. } else {
  80. return "<span class='label label-default'>未确定</span>";
  81. }
  82. }
  83. });
  84. $helper->columnFertilized();
  85. $helper->columnCreatedAt();
  86. $helper->columnUpdatedAt();
  87. // 添加批量操作
  88. $grid->tools(function (Grid\Tools $tools) {
  89. $tools->append('<a href="javascript:void(0)" class="btn btn-sm btn-warning" onclick="fixMatureCrops()">
  90. <i class="fa fa-wrench"></i> 修复成熟期产量
  91. </a>');
  92. $tools->append('<script>
  93. function fixMatureCrops() {
  94. if (confirm("确定要修复所有成熟期作物的产量吗?这将为缺少产量的成熟期作物计算产量。")) {
  95. $.post("/admin/farm-crops/fix-mature-output", {
  96. _token: LA.token
  97. }).done(function(result) {
  98. if (result.status) {
  99. Dcat.success(result.message || "修复完成");
  100. Dcat.reload();
  101. } else {
  102. Dcat.error(result.message || "修复失败");
  103. }
  104. }).fail(function() {
  105. Dcat.error("请求失败");
  106. });
  107. }
  108. }
  109. </script>');
  110. });
  111. $grid->filter(function (Grid\Filter $filter) {
  112. $filterHelper = new FilterHelper($filter, $this);
  113. $filterHelper->equalId();
  114. $filter->equal('land_id', '土地ID');
  115. $filterHelper->equalUserId();
  116. $filter->equal('seed_id', '种子ID');
  117. $filterHelper->equalGrowthStage();
  118. $filterHelper->betweenDatetime('plant_time', '种植时间');
  119. $filterHelper->betweenDatetime('stage_end_time', '阶段结束时间');
  120. // 添加产出相关过滤器
  121. $filter->equal('final_output_item_id', '产出物品ID');
  122. $filter->where('final_output_amount', '产量状态', function ($query) {
  123. $value = $this->input;
  124. if ($value == 'has_amount') {
  125. $query->whereNotNull('final_output_amount');
  126. } elseif ($value == 'no_amount') {
  127. $query->whereNull('final_output_amount');
  128. }
  129. })->select([
  130. 'has_amount' => '已确定产量',
  131. 'no_amount' => '未确定产量'
  132. ]);
  133. // 添加数据完整性过滤器
  134. $filter->where('data_completeness', '数据完整性', function ($query) {
  135. $value = $this->input;
  136. if ($value == 'complete') {
  137. $query->whereNotNull('final_output_item_id')
  138. ->whereNotNull('final_output_amount')
  139. ->where('growth_stage', GROWTH_STAGE::MATURE->value);
  140. } elseif ($value == 'incomplete_mature') {
  141. $query->where('growth_stage', GROWTH_STAGE::MATURE->value)
  142. ->where(function ($q) {
  143. $q->whereNull('final_output_item_id')
  144. ->orWhereNull('final_output_amount');
  145. });
  146. } elseif ($value == 'missing_amount') {
  147. $query->whereNotNull('final_output_item_id')
  148. ->whereNull('final_output_amount');
  149. }
  150. })->select([
  151. 'complete' => '数据完整(成熟期)',
  152. 'incomplete_mature' => '数据不完整(成熟期)',
  153. 'missing_amount' => '缺少产量'
  154. ]);
  155. $filterHelper->equalFertilized();
  156. $filterHelper->betweenDatetime('created_at', '创建时间');
  157. });
  158. });
  159. }
  160. /**
  161. * 构建详情页
  162. *
  163. * @param mixed $id
  164. * @return Show
  165. */
  166. protected function detail($id)
  167. {
  168. return Show::make($id, new FarmCropRepository(), function (Show $show) {
  169. $helper = new ShowHelper($show, $this);
  170. $show->field('id', 'ID');
  171. $show->field('land_id', '土地ID');
  172. $helper->fieldUserId('user_id', '用户ID');
  173. $show->field('seed_id', '种子ID');
  174. $show->field('plant_time', '种植时间');
  175. $helper->fieldGrowthStage('growth_stage', '生长阶段');
  176. $show->field('stage_end_time', '阶段结束时间');
  177. // 添加产出相关字段(增强版本)
  178. $show->field('final_output_item_id', '产出物品ID')->as(function ($value) {
  179. return $value ? "<span class='label label-success'>{$value}</span>" : "<span class='label label-warning'>未确定</span>";
  180. });
  181. $show->field('final_output_amount', '预定产量')->as(function ($value) {
  182. return $value ? "<span class='label label-info'>{$value}</span>" : "<span class='label label-warning'>未确定</span>";
  183. });
  184. // 添加数据完整性状态
  185. $show->field('data_status', '数据状态')->as(function () {
  186. $hasItemId = !empty($this->final_output_item_id);
  187. $hasAmount = !empty($this->final_output_amount);
  188. $isMature = $this->growth_stage == GROWTH_STAGE::MATURE->value;
  189. if ($isMature) {
  190. if ($hasItemId && $hasAmount) {
  191. return "<span class='label label-success'>数据完整,可正常收获</span>";
  192. } elseif ($hasItemId) {
  193. return "<span class='label label-warning'>缺少产量,需要修复</span>";
  194. } else {
  195. return "<span class='label label-danger'>缺少产出物品ID,严重错误</span>";
  196. }
  197. } else {
  198. if ($hasItemId) {
  199. return "<span class='label label-info'>已确定产出物品,等待成熟期确定产量</span>";
  200. } else {
  201. return "<span class='label label-default'>等待发芽期确定产出物品</span>";
  202. }
  203. }
  204. });
  205. // 使用新的灾害显示方法
  206. $show->fieldModelCatsJson2('disasters', '灾害情况');
  207. $helper->fieldFertilized('fertilized', '已施肥');
  208. $show->field('created_at', '创建时间');
  209. $show->field('updated_at', '更新时间');
  210. });
  211. }
  212. /**
  213. * 构建表单
  214. *
  215. * @return Form
  216. */
  217. protected function form()
  218. {
  219. return Form::make(new FarmCropRepository(), function (Form $form) {
  220. $helper = new FormHelper($form, $this);
  221. $form->display('id', 'ID');
  222. $form->text('land_id', '土地ID')->required()->rules('required|integer');
  223. $helper->display('user_id', '用户ID');
  224. // 获取所有种子选项
  225. $form->display('seed_id', '种子ID');
  226. $form->datetime('plant_time', '种植时间')->required();
  227. $helper->selectOptionCast('growth_stage', '生长阶段');
  228. $form->datetime('stage_end_time', '阶段结束时间');
  229. // 添加产出相关字段
  230. $form->number('final_output_item_id', '产出物品ID')
  231. ->help('发芽期自动确定,也可手动设置');
  232. $form->number('final_output_amount', '预定产量')
  233. ->min(1)
  234. ->help('成熟期自动计算,也可手动设置。注意:手动设置后收获时会直接使用此产量');
  235. // 添加数据状态提示
  236. $form->html('<div class="alert alert-info">
  237. <h4>产出数据说明:</h4>
  238. <ul>
  239. <li><strong>产出物品ID</strong>:在发芽期自动确定,决定收获什么物品</li>
  240. <li><strong>预定产量</strong>:在成熟期自动计算,决定收获数量</li>
  241. <li><strong>成熟期作物</strong>:必须有完整的产出数据才能正常收获</li>
  242. <li><strong>修复建议</strong>:如果数据不完整,请运行修复命令:<code>php artisan farm:fix-crop-mature-output</code></li>
  243. </ul>
  244. </div>');
  245. $helper->display('disasters', '灾害情况');
  246. $helper->switchFertilized();
  247. $form->display('created_at', '创建时间');
  248. $form->display('updated_at', '更新时间');
  249. });
  250. }
  251. /**
  252. * 修复成熟期作物产量
  253. *
  254. * @return \Illuminate\Http\JsonResponse
  255. */
  256. #[Post('farm-crops/fix-mature-output')]
  257. public function fixMatureOutput()
  258. {
  259. try {
  260. // 调用修复命令
  261. \Illuminate\Support\Facades\Artisan::call('farm:fix-crop-mature-output', [
  262. '--limit' => 100
  263. ]);
  264. $output = \Illuminate\Support\Facades\Artisan::output();
  265. return response()->json([
  266. 'status' => true,
  267. 'message' => '修复完成!' . $output
  268. ]);
  269. } catch (\Exception $e) {
  270. return response()->json([
  271. 'status' => false,
  272. 'message' => '修复失败:' . $e->getMessage()
  273. ]);
  274. }
  275. }
  276. }