52 lines
1.6 KiB
PHP
52 lines
1.6 KiB
PHP
<?php
|
|
|
|
namespace app\api\controller;
|
|
|
|
use app\common\model\device\Device;
|
|
use app\common\model\land\Land;
|
|
use app\common\model\land\LandDevice;
|
|
use think\facade\Db;
|
|
use think\response\Json;
|
|
|
|
class DeviceController extends BaseApiController
|
|
{
|
|
//绑定设备
|
|
public function bind(): Json
|
|
{
|
|
$params = $this->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('设备绑定成功');
|
|
}
|
|
} |