From a62c3425a25180d45383673a1f934fb480bf8b4d Mon Sep 17 00:00:00 2001 From: weiz Date: Fri, 24 Nov 2023 15:02:22 +0800 Subject: [PATCH] =?UTF-8?q?=E6=96=B0=E5=A2=9E=E8=AE=BE=E5=A4=87=E7=BB=91?= =?UTF-8?q?=E5=AE=9A=EF=BC=8C=E5=9C=9F=E5=A3=A4=E7=9B=91=E6=B5=8B=E6=95=B0?= =?UTF-8?q?=E6=8D=AE=E3=80=81=E7=A9=BA=E6=B0=94=E7=9B=91=E6=B5=8B=E6=95=B0?= =?UTF-8?q?=E6=8D=AE=E3=80=81=E6=BA=AF=E6=BA=90=E4=BA=8C=E7=BB=B4=E7=A0=81?= =?UTF-8?q?=E7=94=9F=E6=88=90=E6=8E=A5=E5=8F=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/api/controller/DeviceController.php | 51 ++++++++++++++++++++++++ app/api/controller/LandController.php | 25 ++++++++++++ app/common/model/device/Device.php | 10 +++++ app/common/model/land/LandDevice.php | 10 +++++ app/common/model/monitor/AirMonitor.php | 10 +++++ app/common/model/monitor/SoilMonitor.php | 10 +++++ 6 files changed, 116 insertions(+) create mode 100644 app/api/controller/DeviceController.php create mode 100644 app/common/model/device/Device.php create mode 100644 app/common/model/land/LandDevice.php create mode 100644 app/common/model/monitor/AirMonitor.php create mode 100644 app/common/model/monitor/SoilMonitor.php diff --git a/app/api/controller/DeviceController.php b/app/api/controller/DeviceController.php new file mode 100644 index 00000000..e56a13cf --- /dev/null +++ b/app/api/controller/DeviceController.php @@ -0,0 +1,51 @@ +request->post(['land_id','device_code']); + if(empty($params['land_id']) || empty($params['device_code'])) { + return $this->fail('缺少必要参数'); + } + $land = Land::where('id',$params['land_id'])->findOrEmpty(); + if($land->isEmpty()){ + return $this->fail('土地信息错误'); + } + $device = Device::where('code',$params['device_code'])->findOrEmpty(); + if($device->isEmpty()){ + return $this->fail('设备不存在'); + } + if($device['is_bind'] == 2){ + return $this->fail('当前设备已被绑定'); + } + $landDevice = LandDevice::where('land_id',$land['id'])->where('device_type',$device['type'])->findOrEmpty(); + if(!$landDevice->isEmpty()){ + $msg = $device['type'] == 1 ? '土壤监测设备' : ($device['type'] == 2 ? '环境监测设备' : '视频监控设备'); + return $this->fail('该土地已绑定'.$msg); + } + Db::transaction(function()use($land,$device) { + Device::where('id',$device['id'])->update([ + 'is_bind' => 2, + 'update_time' => time() + ]); + LandDevice::create([ + 'land_id' => $land['id'], + 'device_id' => $device['id'], + 'device_type' => $device['type'], + 'create_time' => time(), + 'update_time' => time() + ]); + }); + return $this->success('设备绑定成功'); + } + } \ No newline at end of file diff --git a/app/api/controller/LandController.php b/app/api/controller/LandController.php index 53a10f2c..22a8f5fe 100644 --- a/app/api/controller/LandController.php +++ b/app/api/controller/LandController.php @@ -9,6 +9,9 @@ use app\common\model\geo\Town; use app\common\model\geo\Village; use app\common\model\land\Land; + use app\common\model\land\LandDevice; + use app\common\model\monitor\AirMonitor; + use app\common\model\monitor\SoilMonitor; use think\response\Json; class LandController extends BaseApiController @@ -44,6 +47,28 @@ return $this->fail('土地信息不存在'); } $data['pic'] = json_decode($data['pic'],true); + //获取监测数据 + $landDeviceForSoil = LandDevice::where('land_id',$params['land_id'])->where('device_type',1)->findOrEmpty(); + if($landDeviceForSoil->isEmpty()){ + $data['soil_device'] = 0; + }else{ + $data['soil_device'] = 1; + $data['soil_monitor_data'] = SoilMonitor::where('device_id',$landDeviceForSoil['device_id'])->order('id desc')->findOrEmpty(); + } + $landDeviceForAir = LandDevice::where('land_id',$params['land_id'])->where('device_type',2)->findOrEmpty(); + if($landDeviceForAir->isEmpty()){ + $data['air_device'] = 0; + }else{ + $data['air_device'] = 1; + $data['air_monitor_data'] = AirMonitor::where('device_id',$landDeviceForAir['device_id'])->order('id desc')->findOrEmpty(); + } + $landDeviceForVideo = LandDevice::where('land_id',$params['land_id'])->where('device_type',3)->findOrEmpty(); + if($landDeviceForVideo->isEmpty()){ + $data['video_device'] = 0; + }else{ + $data['video_device'] = 1; + $data['video_monitor_data'] = ''; + } return $this->success('请求成功',$data->toArray()); } diff --git a/app/common/model/device/Device.php b/app/common/model/device/Device.php new file mode 100644 index 00000000..58209d15 --- /dev/null +++ b/app/common/model/device/Device.php @@ -0,0 +1,10 @@ +