diff --git a/app/api/controller/dataview/LandController.php b/app/api/controller/dataview/LandController.php index 517e6f3e..3f1bbd27 100644 --- a/app/api/controller/dataview/LandController.php +++ b/app/api/controller/dataview/LandController.php @@ -69,25 +69,63 @@ class LandController extends BaseApiController public function landMonitorAlarmHistory() { $landId = $this->request->get('land_id'); - $start = date('Y-m-d', time()); - $end = date('Y-m-d', strtotime('-1 month')); - + // 最近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']) - ->whereBetweenTime('create_time', $start, $end) + ->whereIn('type', ['soil_potassium_phosphate_nitrogen', + 'soil_potassium_phosphate_phosphorus', + 'soil_potassium_phosphate_potassium' + ]) ->count(); - $historyList = LandCollection::where('land_id', $landId)->field(['soil_potassium_phosphate_nitrogen', 'soil_potassium_phosphate_phosphorus', 'soil_potassium_phosphate_potassium'])->select(); + + $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) - ->whereBetweenTime('create_time', $start, $end) ->count(); - $historyList = LandCollection::where('land_id', $landId)->field($type)->select(); + + $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');