suyuan/app/api/controller/LandController.php

147 lines
5.5 KiB
PHP
Raw Normal View History

<?php
namespace app\api\controller;
use app\common\model\geo\City;
use app\common\model\geo\County;
use app\common\model\geo\Group;
use app\common\model\geo\Province;
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
{
2023-11-22 18:03:28 +08:00
//土地列表
public function list(): Json
{
2023-11-23 13:38:46 +08:00
$params = $this->request->get(['page_no','page_size','keyword']);
2023-11-22 18:03:28 +08:00
$pageNo = empty($params['page_no']) ? 1 : $params['page_no'];
$pageSize = empty($params['page_size']) ? 6 : $params['page_size'];
2023-11-23 13:38:46 +08:00
$where = [
['user_id','=',$this->userId]
];
if(!empty($params['keyword'])){
$where[] = ['title','like','%'.$params['keyword'].'%'];
}
$data = Land::where($where)->page($pageNo,$pageSize)->order('create_time desc')->select()->each(function($item){
2023-11-22 18:03:28 +08:00
$item['pic'] = json_decode($item['pic']);
return $item;
})->toArray();
return $this->success('请求成功',$data);
}
//土地详情
public function detail(): Json
{
$params = $this->request->get(['land_id']);
if(empty($params['land_id'])){
return $this->fail('缺少必要参数');
}
$data = Land::where('id',$params['land_id'])->findOrEmpty();
if($data->isEmpty()){
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());
}
//添加土地
2023-11-22 18:03:28 +08:00
public function add(): Json
{
2023-11-22 18:03:28 +08:00
//获取参数并验证
$fields = ['title','area','province_code','city_code','county_code','town_code','village_code','group_code','master_name','master_phone','pic'];
$params = $this->request->post($fields);
foreach($fields as $v){
if(!isset($params[$v]) || $params[$v] == ''){
return $this->fail('缺少必要参数');
}
}
2023-11-22 18:03:28 +08:00
//验证土地是否存在
$land = Land::where('user_id',$this->userId)->where('title',$params['title'])->findOrEmpty();
if(!$land->isEmpty()){
return $this->fail('土地名称已存在');
}
2023-11-22 18:03:28 +08:00
//获取省市县镇村组名称
$province = Province::field('province_name')->where('province_code',$params['province_code'])->findOrEmpty();
if($province->isEmpty()){
return $this->fail('省份编码错误');
}
$city = City::field('city_name')->where('province_code',$params['province_code'])->where('city_code',$params['city_code'])->findOrEmpty();
if($city->isEmpty()){
return $this->fail('城市编码错误');
}
$county = County::field('county_name')->where('city_code',$params['city_code'])->where('county_code',$params['county_code'])->findOrEmpty();
if($county->isEmpty()){
return $this->fail('区县编码错误');
}
$town = Town::field('town_name')->where('county_code',$params['county_code'])->where('town_code',$params['town_code'])->findOrEmpty();
if($town->isEmpty()){
return $this->fail('镇街编码错误');
}
$village = Village::field('village_name')->where('town_code',$params['town_code'])->where('village_code',$params['village_code'])->findOrEmpty();
if($village->isEmpty()){
return $this->fail('乡村编码错误');
}
$group = Group::field('group_name')->where('id',$params['group_code'])->findOrEmpty();
if($group->isEmpty()){
return $this->fail('小组编码错误');
}
2023-11-22 18:03:28 +08:00
//验证土地图片字段是否是json数组
$pic = json_decode($params['pic'],true);
if(empty($pic)){
return $this->fail('图片数据格式错误');
}
//创建数据
$landRes = Land::create([
'user_id' => $this->userId,
'title' => $params['title'],
'total_area' => $params['area'],
'residual_area' => $params['area'],
'province_code' => $params['province_code'],
'province_name' => $province['province_name'],
'city_code' => $params['city_code'],
'city_name' => $city['city_name'],
'county_code' => $params['county_code'],
'county_name' => $county['county_name'],
'town_code' => $params['town_code'],
'town_name' => $town['town_name'],
'village_code' => $params['village_code'],
'village_name' => $village['village_name'],
'group_code' => $params['group_code'],
'group_name' => $group['group_name'],
'master_name' => $params['master_name'],
'master_phone' => $params['master_phone'],
2023-11-22 18:03:28 +08:00
'pic' => $params['pic'],
'create_time' => time(),
'update_time' => time(),
]);
2023-11-22 18:03:28 +08:00
return $landRes->id ? $this->success('土地添加成功') : $this->fail('土地添加失败');
}
}