FarmCropController.php 14 KB

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