FarmCropLogController.php 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. <?php
  2. namespace App\Module\Farm\AdminControllers;
  3. use App\Module\Farm\Models\FarmCropLog;
  4. use App\Module\Farm\Repositories\FarmCropLogRepository;
  5. use UCore\DcatAdmin\AdminController;
  6. use App\Module\Farm\AdminControllers\Helper\GridHelper;
  7. use App\Module\Farm\AdminControllers\Helper\ShowHelper;
  8. use App\Module\Farm\AdminControllers\Helper\FilterHelper;
  9. use Dcat\Admin\Grid;
  10. use Dcat\Admin\Show;
  11. use Dcat\Admin\Http\Controllers\AdminController as BaseAdminController;
  12. use Dcat\Admin\Layout\Content;
  13. use Dcat\Admin\Widgets\Alert;
  14. use Spatie\RouteAttributes\Attributes\Resource;
  15. /**
  16. * 作物事件日志管理控制器
  17. */
  18. #[Resource('farm-crop-logs', names: 'dcat.admin.farm-crop-logs')]
  19. class FarmCropLogController extends AdminController
  20. {
  21. /**
  22. * 页面标题
  23. *
  24. * @var string
  25. */
  26. protected $title = '作物事件日志管理';
  27. /**
  28. * 页面描述
  29. *
  30. * @var string
  31. */
  32. protected $description = '查看作物生长过程中的各种事件记录';
  33. /**
  34. * 构建表格
  35. *
  36. * @return Grid
  37. */
  38. protected function grid()
  39. {
  40. return Grid::make(new FarmCropLogRepository(['crop', 'seed', 'land']), function (Grid $grid) {
  41. $helper = new GridHelper($grid, $this);
  42. $helper->columnId();
  43. $grid->column('user_id', '用户ID')->sortable();
  44. $grid->column('crop.id', '作物ID')->display(function ($value) {
  45. return "<span class='badge badge-primary'>作物#{$value}</span>";
  46. });
  47. $grid->column('seed.name', '种子名称')->display(function ($value) {
  48. return "<span class='badge badge-info'>{$value}</span>";
  49. });
  50. $grid->column('land.id', '土地ID')->display(function ($value) {
  51. return "<span class='badge badge-secondary'>土地#{$value}</span>";
  52. });
  53. $grid->column('event_type', '事件类型')->display(function ($value) {
  54. $colors = [
  55. FarmCropLog::EVENT_FRUIT_CONFIRMED => 'success',
  56. FarmCropLog::EVENT_OUTPUT_CALCULATED => 'info',
  57. FarmCropLog::EVENT_DISASTER_OCCURRED => 'danger',
  58. FarmCropLog::EVENT_DISASTER_CLEARED => 'warning',
  59. FarmCropLog::EVENT_HARVESTED => 'primary',
  60. ];
  61. $color = $colors[$value] ?? 'secondary';
  62. $names = [
  63. FarmCropLog::EVENT_FRUIT_CONFIRMED => '确认果实种类',
  64. FarmCropLog::EVENT_OUTPUT_CALCULATED => '确认产出数量',
  65. FarmCropLog::EVENT_DISASTER_OCCURRED => '灾害产生',
  66. FarmCropLog::EVENT_DISASTER_CLEARED => '灾害清除',
  67. FarmCropLog::EVENT_HARVESTED => '收获',
  68. ];
  69. $name = $names[$value] ?? '未知事件';
  70. return "<span class='badge badge-{$color}'>{$name}</span>";
  71. });
  72. $grid->column('growth_stage_name', '生长阶段')->display(function ($value) {
  73. return "<span class='badge badge-info'>{$value}</span>";
  74. });
  75. $grid->column('event_data', '事件数据')->display(function ($value) {
  76. if (empty($value)) {
  77. return '<span class="text-muted">无数据</span>';
  78. }
  79. // 简单显示JSON数据的前100个字符
  80. $jsonStr = json_encode($value, JSON_UNESCAPED_UNICODE);
  81. if (strlen($jsonStr) > 100) {
  82. $jsonStr = substr($jsonStr, 0, 100) . '...';
  83. }
  84. return "<small class='text-info'>{$jsonStr}</small>";
  85. });
  86. $helper->columnCreatedAt();
  87. // 筛选器
  88. $grid->filter(function (Grid\Filter $filter) {
  89. $helper = new FilterHelper($filter, $this);
  90. $filter->equal('user_id', '用户ID');
  91. $filter->equal('crop_id', '作物ID');
  92. $filter->equal('event_type', '事件类型')->select([
  93. FarmCropLog::EVENT_FRUIT_CONFIRMED => '确认果实种类',
  94. FarmCropLog::EVENT_OUTPUT_CALCULATED => '确认产出数量',
  95. FarmCropLog::EVENT_DISASTER_OCCURRED => '灾害产生',
  96. FarmCropLog::EVENT_DISASTER_CLEARED => '灾害清除',
  97. FarmCropLog::EVENT_HARVESTED => '收获',
  98. ]);
  99. $filter->equal('growth_stage', '生长阶段')->select([
  100. 1 => '种子期',
  101. 2 => '发芽期',
  102. 3 => '生长期',
  103. 4 => '成熟期',
  104. ]);
  105. $helper->betweenDatetime('created_at', '事件时间');
  106. });
  107. // 禁用新增、编辑、删除操作(只读日志)
  108. $grid->disableCreateButton();
  109. $grid->disableEditButton();
  110. $grid->disableDeleteButton();
  111. $grid->disableBatchDelete();
  112. // 设置默认排序
  113. $grid->model()->orderBy('created_at', 'desc');
  114. // 设置每页显示数量
  115. $grid->paginate(20);
  116. });
  117. }
  118. /**
  119. * 构建详情页
  120. *
  121. * @param mixed $id
  122. * @return Show
  123. */
  124. protected function detail($id)
  125. {
  126. return Show::make($id, new FarmCropLogRepository(['crop', 'seed', 'land']), function (Show $show) {
  127. $helper = new ShowHelper($show, $this);
  128. $show->field('id', 'ID');
  129. $show->field('user_id', '用户ID');
  130. $show->field('crop.id', '作物ID');
  131. $show->field('seed.name', '种子名称');
  132. $show->field('land.id', '土地ID');
  133. $show->field('event_type_name', '事件类型');
  134. $show->field('growth_stage_name', '生长阶段');
  135. $show->field('land_type', '土地类型');
  136. $show->field('event_data', '事件详细数据')->as(function ($value) {
  137. return '<pre>' . json_encode($value, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE) . '</pre>';
  138. });
  139. $show->field('created_at', '创建时间');
  140. $show->field('updated_at', '更新时间');
  141. // 禁用编辑和删除按钮
  142. $show->disableEditButton();
  143. $show->disableDeleteButton();
  144. });
  145. }
  146. /**
  147. * 重写index方法,添加统计信息
  148. */
  149. public function index(Content $content)
  150. {
  151. return $content
  152. ->title($this->title())
  153. ->description($this->description())
  154. ->body($this->getStatsCards())
  155. ->body($this->grid());
  156. }
  157. /**
  158. * 获取统计卡片
  159. */
  160. protected function getStatsCards()
  161. {
  162. $stats = [
  163. '总事件数' => FarmCropLog::count(),
  164. '今日事件数' => FarmCropLog::whereDate('created_at', today())->count(),
  165. '确认果实事件' => FarmCropLog::byEventType(FarmCropLog::EVENT_FRUIT_CONFIRMED)->count(),
  166. '产出计算事件' => FarmCropLog::byEventType(FarmCropLog::EVENT_OUTPUT_CALCULATED)->count(),
  167. '灾害产生事件' => FarmCropLog::byEventType(FarmCropLog::EVENT_DISASTER_OCCURRED)->count(),
  168. '灾害清除事件' => FarmCropLog::byEventType(FarmCropLog::EVENT_DISASTER_CLEARED)->count(),
  169. '收获事件' => FarmCropLog::byEventType(FarmCropLog::EVENT_HARVESTED)->count(),
  170. ];
  171. $cards = '';
  172. foreach ($stats as $title => $count) {
  173. $cards .= "
  174. <div class='col-md-3'>
  175. <div class='small-box bg-info'>
  176. <div class='inner'>
  177. <h3>{$count}</h3>
  178. <p>{$title}</p>
  179. </div>
  180. <div class='icon'>
  181. <i class='fa fa-list'></i>
  182. </div>
  183. </div>
  184. </div>
  185. ";
  186. }
  187. return "<div class='row'>{$cards}</div>";
  188. }
  189. }