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, 'user_id' => $land['user_id'], '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('设备绑定成功'); } }