areaCode = $this->request->param('areaCode', ''); $this->streetCode = $this->request->param('streetCode', ''); if ($this->areaCode == '') { throw new ValidateException('未获取到位置信息'); } } // 溯源产品统计 public function plantProductCount() { $list = LandPlant::alias('lp')->join('land l', 'l.id=lp.land_id')->field('lp.kind,lp.qr_code') ->where(function ($query) { if ($this->streetCode != '') { $query->where('l.town_code', $this->streetCode); } else { $query->where('l.county_code', $this->areaCode); } })->limit(30)->select(); return $this->success('成功', compact('list')); } // 智能预警数据 public function landCollectionList() { // 先排序,再分组 $subQuery = Db::name('land_collection') ->order('id', 'desc') ->limit(1000) ->buildSql(); $list = Db::table($subQuery)->alias('lc') ->field('lc.*,l.title') ->join('land l', 'l.id=lc.land_id') ->where(function ($query) { if ($this->streetCode != '') { $query->where('l.town_code', $this->streetCode); } else { $query->where('l.county_code', $this->areaCode); } })->order('lc.id', 'desc')->group('l.id')->limit(30)->select(); return $this->success('成功', compact('list')); } // 预警历史记录 public function landMonitorAlarmHistory() { $landId = $this->request->get('land_id'); // 最近7日 $start = date('Y-m-d', strtotime('-6 day')); $end= date('Y-m-d H:i:s', time()); $typeRow = ['soil_temperature', 'soil_moisture', 'soil_PH', 'soil_potassium_phosphate_nitrogen', 'wind_speed', 'ambient_temperature', 'ambient_humidity', 'carbon_dioxide']; $list = []; foreach ($typeRow as $type) { // 土壤磷酸钾-氮磷钾 if ($type == 'soil_potassium_phosphate_nitrogen') { $alarmCount = MonitorAlarm::where('land_id', $landId) ->whereIn('type', ['soil_potassium_phosphate_nitrogen', 'soil_potassium_phosphate_phosphorus', 'soil_potassium_phosphate_potassium' ]) ->count(); $dataList = LandCollection::where('land_id', $landId) ->field(['soil_potassium_phosphate_nitrogen', 'soil_potassium_phosphate_phosphorus', 'soil_potassium_phosphate_potassium', "STR_TO_DATE(create_time, '%Y-%m-%d') as day"]) ->whereBetweenTime('create_time', $start, $end) ->group("STR_TO_DATE(create_time, '%Y-%m-%d')") ->select(); // 组装折线图数据 $historyList = []; $timeRow = []; $valueRow = []; foreach ($dataList as $row) { $timeRow[] = $row['day']; unset($row['day']); $valueRow[] = $row; } $historyList[]['time'] = $timeRow; $historyList[]['value'] = $valueRow; } else { $alarmCount = MonitorAlarm::where('land_id', $landId) ->where('type', $type) ->count(); $dataList = LandCollection::where('land_id', $landId) ->field("$type,STR_TO_DATE(create_time, '%Y-%m-%d') as day") ->whereBetweenTime('create_time', $start, $end) ->group("STR_TO_DATE(create_time, '%Y-%m-%d')") ->select()->toArray(); // 组装折线图数据 $historyList = []; $timeRow = []; $valueRow = []; foreach ($dataList as $row) { $timeRow[] = $row['day']; $valueRow[] = $row[$type]; } $historyList[]['time'] = $timeRow; $historyList[]['value'] = $valueRow; } $list[] = compact('alarmCount', 'historyList'); } return $this->success('成功', compact('list')); } // 土地列表 public function landList() { $list = Land::where(function ($query) { if ($this->streetCode != '') { $query->where('l.town_code', $this->streetCode); } else { $query->where('l.county_code', $this->areaCode); } })->select(); } public function centralCount() { $query = Land::alias('l')->where(function ($query) { if ($this->streetCode != '') { $query->where('l.town_code', $this->streetCode); } else { $query->where('l.county_code', $this->areaCode); } }); // 地块数量 $landCount = $query->count(); // 种植面积 $totalArea = $query->sum('total_area'); // 种植种类 $plantCount = $query->join('land_plant lp', 'l.id = lp.land_id')->group('kind')->count(); return $this->success('成功', compact('landCount', 'totalArea', 'plantCount')); } }