suyuan/app/api/controller/LandPlantController.php

262 lines
10 KiB
PHP

<?php
namespace app\api\controller;
use think\facade\Db;
use think\response\Json;
class LandPlantController extends BaseApiController
{
public array $notNeedLogin = [
'landInfo',
'addLandCrop','addLandCropPic','landCropPicList',
'addLandCropRecord','delLandCropRecord','ediLandCropRecord','landCropRecordInfo','landCropRecordList',
'setLandCropRipe',
'landEnvDataChart','landEnvDataCurr'
];
//获取农户土地信息
public function landInfo(): Json
{
//获取参数
$params = $this->request->get(['user_id','land_id']);
if(empty($params['user_id']) || empty($params['land_id'])){
return $this->fail('参数错误');
}
//获取数据
$url = env('project.worker_domain').'/api/information/farmerLandInfo';
$curl_result = curl_post($url,[],$params);
if($curl_result['code'] == 0){
return $this->fail($curl_result['msg']);
}
//获取种植信息
$land_crop = Db::name('farmer_land_crop')->where([
['user_id','=',$params['user_id']],
['land_id','=',$params['land_id']],
])->order('id desc')->findOrEmpty();
if(!empty($land_crop)){
$curl_result['data']['is_cropped'] = true;
$curl_result['data']['crop_id'] = $land_crop['id'];
$curl_result['data']['source_code'] = $land_crop['source_code'];
$curl_result['data']['crop_name'] = $land_crop['crop_name'];
$curl_result['data']['crop_variety'] = $land_crop['crop_variety'];
$curl_result['data']['crop_brand'] = $land_crop['crop_brand'];
$curl_result['data']['crop_yield'] = $land_crop['crop_yield'];
$curl_result['data']['seed_time'] = date('Y-m-d H:i:s',$land_crop['seed_time']);
$curl_result['data']['ripe_time'] = !empty($land_crop['ripe_time']) && !empty($land_crop['crop_yield']) ? date('Y-m-d H:i:s',$land_crop['ripe_time']) : $land_crop['ripe_time'];
//获取播种图片
$land_crop_pic = Db::name('farmer_land_crop_pic')->field('pic,create_time')->where('crop_id',$land_crop['id'])->order('id desc')->findOrEmpty();
$curl_result['data']['pic'] = !empty($land_crop_pic) ? $land_crop_pic['pic'] : '';
$curl_result['data']['create_time'] = !empty($land_crop_pic) ? date('Y-m-d H:i:s',$land_crop_pic['create_time']) : '';
}else{
$curl_result['data']['is_cropped'] = false;
}
//返回数据
return $this->success('请求成功',$curl_result['data']);
}
//添加种植信息
public function addLandCrop(): Json
{
//获取参数
$params = $this->request->post(['user_id','land_id','crop_name','crop_variety','crop_brand','crop_buy_time','pic']);
if(empty($params['user_id']) || empty($params['land_id']) || empty($params['crop_name']) || empty($params['crop_variety']) || empty($params['crop_brand']) || empty($params['crop_buy_time'])){
return $this->fail('参数错误');
}
if(strtotime($params['crop_buy_time']) > time()){
return $this->fail('种子购买时间错误');
}
//判断当前土地是否种植作物
$hasRes = Db::name('farmer_land_crop')->where([
['user_id','=',$params['user_id']],
['land_id','=',$params['land_id']],
])->order('id desc')->findOrEmpty();
if(!empty($hasRes) && empty($hasRes['ripe_time']) && empty($hasRes['crop_yield'])){
return $this->fail('该土地已种植');
}
//添加数据
Db::transaction(function () use($params) {
$landCropId = Db::name('farmer_land_crop')->insertGetId([
'user_id' => $params['user_id'],
'land_id' => $params['land_id'],
'source_code' => 'NO'.time(),
'crop_name' => $params['crop_name'],
'crop_variety' => $params['crop_variety'],
'crop_brand' => $params['crop_brand'],
'seed_time' => time(),
'crop_buy_time' => strtotime($params['crop_buy_time'])
]);
if(!empty($params['pic'])){
Db::name('farmer_land_crop_pic')->insert([
'crop_id' => $landCropId,
'pic' => $params['pic'],
'create_time' => time()
]);
}
});
//返回信息
return $this->success('添加成功');
}
//添加播种图片
public function addLandCropPic(): Json
{
//获取参数
$params = $this->request->post(['crop_id','pic']);
if(empty($params['crop_id']) || empty($params['pic'])){
return $this->fail('参数错误');
}
//写入数据
$params['create_time'] = time();
$result = Db::name('farmer_land_crop_pic')->insert($params);
//返回
return $result ? $this->success('添加成功') : $this->fail('添加失败');
}
//获取播种图片列表
public function landCropPicList(): Json
{
//获取参数
$params = $this->request->get(['crop_id']);
if(empty($params['crop_id'])){
return $this->fail('参数错误');
}
$data = Db::name('farmer_land_crop_pic')->where('crop_id',$params['crop_id'])->order('id desc')->select()->each(function($item){
$item['create_time'] = date('Y-m-d H:i:s',$item['create_time']);
return $item;
})->toArray();
return $this->success('请求成功',$data);
}
//添加农户种植信息操作记录
public function addLandCropRecord(): Json
{
//获取参数
$params = $this->request->post(['crop_id','action_id','action_content']);
if(empty($params['crop_id']) || empty($params['action_id']) || empty($params['action_content'])){
return $this->fail('参数错误');
}
if(empty(json_decode($params['action_content']))) {
return $this->fail('参数错误');
}
//写入数据
$params['create_time'] = time();
$result = Db::name('farmer_land_crop_record')->insert($params);
//返回
return $result ? $this->success('添加成功') : $this->fail('添加失败');
}
//删除农户种植信息操作记录
public function delLandCropRecord(): Json
{
$params = $this->request->post(['record_id']);
if(empty($params['record_id'])){
return $this->fail('参数错误');
}
$resust = Db::name('farmer_land_crop_record')->where('id',$params['record_id'])->delete();
return $resust ? $this->success('删除成功') : $this->fail('删除失败');
}
//修改农户种植信息操作记录
public function ediLandCropRecord(): Json
{
$params = $this->request->post(['record_id','action_content']);
if(empty($params['record_id']) || empty($params['action_content'])){
return $this->fail('参数错误');
}
if(empty(json_decode($params['action_content']))) {
return $this->fail('参数错误');
}
//修改数据
$result = Db::name('farmer_land_crop_record')->where('id',$params['record_id'])->update(['action_content'=>$params['action_content']]);
//返回
return $result ? $this->success('修改成功') : $this->fail('修改失败');
}
//获取农户种植信息操作记录详情
public function landCropRecordInfo(): Json
{
$params = $this->request->get(['record_id']);
if(empty($params['record_id'])){
return $this->fail('参数错误');
}
$data = Db::name('farmer_land_crop_record')->where('id',$params['record_id'])->findOrEmpty();
if(empty($data)){
return $this->fail('数据不存在');
}
$data['action_content'] = json_decode($data['action_content'],true);
$data['action_detail'] = Db::name('farmer_action_detail')->where('action_id','in',$data['action_id'])->select()->toArray();
return $this->success('请求成功',$data);
}
//农户种植信息操作记录列表
public function landCropRecordList(): Json
{
//获取参数
$params = $this->request->get(['crop_id','page_no','page_size']);
if(empty($params['crop_id'])){
return $this->fail('参数错误');
}
//设置分页条件
$pageNo = empty($params['page_no']) || $params['page_no'] < 0 ? 1 : $params['page_no'];
$pageSize = empty($params['page_size']) || $params['page_size'] < 0 ? 10 : $params['page_size'];
$data = Db::name('farmer_land_crop_record')->where('crop_id',$params['crop_id'])->page($pageNo,$pageSize)->order('create_time desc')->select()->each(function($item){
$item['action_name'] = Db::name('farmer_action')->where('id',$item['action_id'])->findOrEmpty()['name'];
$item['action_content'] = json_decode($item['action_content'],true);
$item['create_time'] = date('Y-m-d H:i:s',$item['create_time']);
return $item;
})->toArray();
return $this->success('请求成功',$data);
}
//种植作物标记成熟
public function setLandCropRipe(): Json
{
$params = $this->request->post(['crop_id','crop_yield']);
if(empty($params['crop_id']) || empty($params['crop_yield'])){
return $this->fail('参数错误');
}
$result = Db::name('farmer_land_crop')->where('id',$params['crop_id'])->update([
'crop_yield' => $params['crop_yield'],
'ripe_time' => time()
]);
return $result ? $this->success('操作成功') : $this->fail('操作失败');
}
//农户土地监测数据统计数据
public function landEnvDataChart(): Json
{
//获取参数
$params = $this->request->get(['user_id','start_time','end_time']);
if(empty($params['user_id']) || empty($params['start_time']) || empty($params['end_time'])){
return $this->fail('参数错误');
}
//获取数据
$data = Db::name('farmer_land_env_data')->where('user_id',$params['user_id'])->whereBetweenTime('create_time', $params['start_time'], $params['end_time'])->order('id desc')->select()->each(function($item){
$item['create_time'] = date('m-d',$item['create_time']);
return $item;
})->toArray();
$result = [
['soil_temp' => array_column($data,'soil_temp'),'create_time' => array_column($data,'create_time')],
['soil_mois' => array_column($data,'soil_mois'),'create_time' => array_column($data,'create_time')],
['p_content' => array_column($data,'p_content'),'create_time' => array_column($data,'create_time')],
['n_content' => array_column($data,'n_content'),'create_time' => array_column($data,'create_time')],
['k_content' => array_column($data,'k_content'),'create_time' => array_column($data,'create_time')],
];
return $this->success('请求成功',$result);
}
//获取农户土地监测数据实时数据
public function landEnvDataCurr(): Json
{
$params = $this->request->get(['user_id']);
if(empty($params['user_id'])){
return $this->fail('参数错误');
}
$data = Db::name('farmer_land_env_data')->where('user_id',$params['user_id'])->whereDay('create_time', date('Y-m-d',time()))->findOrEmpty();
if(!empty($data)){
$data['create_time'] = date('Y-m-d H:i:s',$data['create_time']);
}
return $this->success('请求成功',$data);
}
}