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 @@ +