新增设备绑定,土壤监测数据、空气监测数据、溯源二维码生成接口

This commit is contained in:
weiz 2023-11-24 15:02:22 +08:00
parent 2ea80b15b1
commit a62c3425a2
6 changed files with 116 additions and 0 deletions

View File

@ -0,0 +1,51 @@
<?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,
'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('设备绑定成功');
}
}

View File

@ -9,6 +9,9 @@
use app\common\model\geo\Town; use app\common\model\geo\Town;
use app\common\model\geo\Village; use app\common\model\geo\Village;
use app\common\model\land\Land; 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; use think\response\Json;
class LandController extends BaseApiController class LandController extends BaseApiController
@ -44,6 +47,28 @@
return $this->fail('土地信息不存在'); return $this->fail('土地信息不存在');
} }
$data['pic'] = json_decode($data['pic'],true); $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()); return $this->success('请求成功',$data->toArray());
} }

View File

@ -0,0 +1,10 @@
<?php
namespace app\common\model\device;
use app\common\model\BaseModel;
class Device extends BaseModel
{
protected $name = 'device';
}

View File

@ -0,0 +1,10 @@
<?php
namespace app\common\model\land;
use app\common\model\BaseModel;
class LandDevice extends BaseModel
{
protected $name = 'land_device';
}

View File

@ -0,0 +1,10 @@
<?php
namespace app\common\model\monitor;
use app\common\model\BaseModel;
class AirMonitor extends BaseModel
{
protected $name = 'air_monitor';
}

View File

@ -0,0 +1,10 @@
<?php
namespace app\common\model\monitor;
use app\common\model\BaseModel;
class SoilMonitor extends BaseModel
{
protected $name = 'soil_monitor';
}