This commit is contained in:
yaooo 2023-11-27 16:41:57 +08:00
commit 0d4a5c4da2
3 changed files with 89 additions and 1 deletions

View File

@ -2,6 +2,7 @@
namespace app\api\controller; namespace app\api\controller;
use app\common\model\device\Device;
use app\common\model\geo\City; use app\common\model\geo\City;
use app\common\model\geo\County; use app\common\model\geo\County;
use app\common\model\geo\Group; use app\common\model\geo\Group;
@ -9,6 +10,10 @@
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\LandProduct;
use app\common\model\land\Product;
use app\common\model\product\ProductDevice;
use think\facade\Db;
use think\response\Json; use think\response\Json;
class LandController extends BaseApiController class LandController extends BaseApiController
@ -119,4 +124,65 @@
]); ]);
return $landRes->id ? $this->success('土地添加成功') : $this->fail('土地添加失败'); return $landRes->id ? $this->success('土地添加成功') : $this->fail('土地添加失败');
} }
//产品列表
public function product(): Json
{
$data = Product::field('id as product_id,code,name')->where('status',1)->select()->toArray();
foreach ($data as $k=>$v) {
$productDevice = ProductDevice::where('product_id',$v['product_id'])->select()->toArray();
if(empty($productDevice)){
unset($data[$k]);
}
}
return $this->success('请求成功',$data);
}
//绑定产品
public function bind(): Json
{
$params = $this->request->post(['land_id','product_id']);
if(empty($params['land_id']) || empty($params['product_id'])){
return $this->fail('缺少必要参数');
}
$land = Land::where('id',$params['land_id'])->findOrEmpty();
if($land->isEmpty()){
return $this->fail('土地信息错误');
}
$landProduct = LandProduct::where('land_id',$params['land_id'])->findOrEmpty();
if(!$landProduct->isEmpty()){
return $this->fail('当前土地已绑定其他产品');
}
$product = Product::where('id',$params['product_id'])->findOrEmpty();
if($product->isEmpty()){
return $this->fail('产品信息错误');
}
$productDevice = ProductDevice::where('product_id',$params['product_id'])->column('device_id');
if(empty($productDevice)){
return $this->fail('该产品没有绑定设备,请先给该产品绑定设备');
}
$productLand = LandProduct::where('product_id',$params['product_id'])->findOrEmpty();
if(!$productLand->isEmpty()){
return $this->fail('该产品已被其他土地绑定');
}
//创建数据
Db::transaction(function()use($land,$product,$productDevice) {
LandProduct::create([
'land_id' => $land['id'],
'product_id' => $product['id'],
'create_time' => time(),
'update_time' => time(),
]);
Product::where('id',$product['id'])->update([
'user_id' => $land['user_id'],
'status' => 2,
'update_time' => time(),
]);
Device::where('id','in',$productDevice)->update([
'user_id' => $land['user_id'],
'update_time' => time()
]);
});
return $this->success('绑定成功');
}
} }

View File

@ -40,10 +40,13 @@
return $this->fail('图片参数格式错误'); return $this->fail('图片参数格式错误');
} }
//获取土地信息 //获取土地信息
$land = Land::field('residual_area')->where('id',$params['land_id'])->findOrEmpty(); $land = Land::field('user_id,residual_area')->where('id',$params['land_id'])->findOrEmpty();
if($land->isEmpty()){ if($land->isEmpty()){
return $this->fail('土地信息错误'); return $this->fail('土地信息错误');
} }
if($land['user_id'] != $this->userId){
return $this->fail('土地信息与用户信息不匹配');
}
if($params['area'] > $land['residual_area']){ if($params['area'] > $land['residual_area']){
return $this->fail('种植面积超过当前土地可种植面积'); return $this->fail('种植面积超过当前土地可种植面积');
} }

19
config/monitoritem.php Normal file
View File

@ -0,0 +1,19 @@
<?php
//设备监测项数组
return [
'wind_direction'=>'风向',
'wind_speed'=>'风速',
'air_temperature'=>'空气温度',
'air_moisture'=>'空气湿度',
'co2_content'=>'空气二氧化碳含量',
'pressure'=>'大气压强',
'rainfall'=>'降雨量',
'light_intensity'=>'光照强度',
'soil_temperature'=>'土壤温度',
'soil_moisture'=>'土壤湿度',
'conductivity'=>'土壤电导率',
'ph'=>'土壤酸碱度',
'n_content'=>'土壤氮含量',
'p_content'=>'土壤磷含量',
'k_content'=>'土壤钾含量',
];