suyuan/app/api/controller/dataview/LandController.php

168 lines
6.1 KiB
PHP
Raw Normal View History

<?php
namespace app\api\controller\dataview;
use app\api\controller\BaseApiController;
use app\common\model\device\MonitorAlarm;
use app\common\model\land\Land;
use app\common\model\land\LandPlant;
use app\common\model\LandCollection;
use think\facade\Db;
use think\exception\ValidateException;
class LandController extends BaseApiController
{
public array $notNeedLogin = ['plantProductCount', 'landCollectionList', 'landMonitorAlarmHistory', 'landList', 'centralCount'];
public $areaCode;
public $streetCode;
public function initialize()
{
parent::initialize();
$this->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');
2023-12-15 11:33:44 +08:00
// 最近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)
2023-12-15 11:33:44 +08:00
->whereIn('type', ['soil_potassium_phosphate_nitrogen',
'soil_potassium_phosphate_phosphorus',
'soil_potassium_phosphate_potassium'
])
->count();
2023-12-15 11:33:44 +08:00
$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();
2023-12-15 11:33:44 +08:00
$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'));
}
}