diff --git a/app/api/controller/dataview/LandController.php b/app/api/controller/dataview/LandController.php index ace9150b..517e6f3e 100644 --- a/app/api/controller/dataview/LandController.php +++ b/app/api/controller/dataview/LandController.php @@ -3,14 +3,17 @@ 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']; + public array $notNeedLogin = ['plantProductCount', 'landCollectionList', 'landMonitorAlarmHistory', 'landList', 'centralCount']; public $areaCode; public $streetCode; @@ -43,7 +46,13 @@ class LandController extends BaseApiController // 智能预警数据 public function landCollectionList() { - $list = LandCollection::alias('lc') + // 先排序,再分组 + $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) { @@ -52,7 +61,70 @@ class LandController extends BaseApiController } else { $query->where('l.county_code', $this->areaCode); } - })->limit(30)->select(); + })->order('lc.id', 'desc')->group('l.id')->limit(30)->select(); return $this->success('成功', compact('list')); } + + // 预警历史记录 + public function landMonitorAlarmHistory() + { + $landId = $this->request->get('land_id'); + $start = date('Y-m-d', time()); + $end = date('Y-m-d', strtotime('-1 month')); + + $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) + ->count(); + $historyList = LandCollection::where('land_id', $landId)->field(['soil_potassium_phosphate_nitrogen', 'soil_potassium_phosphate_phosphorus', 'soil_potassium_phosphate_potassium'])->select(); + } 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(); + } + + $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')); + } } \ No newline at end of file