新增农户种植相关接口

This commit is contained in:
weiz 2023-10-23 17:56:14 +08:00
parent 82ab411cef
commit 2c3be5506d
3 changed files with 383 additions and 1 deletions

View File

@ -0,0 +1,348 @@
<?php
namespace app\api\controller;
use think\facade\Db;
use think\facade\Filesystem;
use think\response\Json;
class SuYuanController extends BaseApiController
{
public array $notNeedLogin = ['farmerInfo','landInfo','addCropInfo','addCropInfoPic','cropInfoPicList','addCropRecord','delCropRecord','ediCropRecord','cropRecordInfo','cropRecordList','cropRipe','landEnvDataChart','landEnvDataCurr','actions','actionsDetail','imageUpload'];
//获取农户信息
public function farmerInfo(): Json
{
//获取参数
$params = $this->request->get(['user_id']);
if(empty($params['user_id'])){
return $this->fail('参数错误');
}
$url = env('project.worker_domain').'/api/information/farmerInfo';
$curl_result = curl_post($url,[],$params);
if($curl_result['code'] == 0){
return $this->fail($curl_result['msg']);
}
//判断是否是种植农户
if($curl_result['data']['is_zz_user']){
$lands = $curl_result['data']['land_detail'];
foreach($lands as $k=>$v) {
//查找种植信息
$land_crop = Db::name('farmer_land_crop')->where([
['user_id','=',$params['user_id']],
['land_id','=',$v['land_id']],
])->order('id desc')->findOrEmpty();
if(!empty($land_crop)){
$lands[$k]['is_cropped'] = true;
$lands[$k]['source_code'] = $land_crop['source_code'];
$lands[$k]['crop_name'] = $land_crop['crop_name'];
$lands[$k]['crop_variety'] = $land_crop['crop_variety'];
$lands[$k]['crop_brand'] = $land_crop['crop_brand'];
$lands[$k]['crop_yield'] = $land_crop['crop_yield'];
$lands[$k]['seed_time'] = date('Y-m-d H:i:s',$land_crop['seed_time']);
$lands[$k]['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'];
}else{
$lands[$k]['is_cropped'] = false;
}
}
$curl_result['data']['land_detail'] = $lands;
}
//返回数据
return json($curl_result);
}
//获取农户土地信息
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')->where('crop_id',$land_crop['id'])->order('id desc')->findOrEmpty();
$curl_result['data']['pic'] = !empty($land_crop_pic) ? $land_crop_pic['pic'] : '';
}else{
$curl_result['data']['is_cropped'] = false;
}
//返回数据
return $this->success('请求成功',$curl_result['data']);
}
//添加种植信息
public function addCropInfo(): 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 addCropInfoPic(): 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 cropInfoPicList(): 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 addCropRecord(): 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 delCropRecord(): 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 ediCropRecord(): 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 cropRecordInfo(): 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();
$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 cropRecordList(): 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_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 cropRipe(): 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();
return $this->success('请求成功',$data);
}
/*************************************************************************************************************************************************************/
//获取操作信息
public function actions(): Json
{
//获取参数
$params = $this->request->get(['type']);
if(empty($params['type']) || !in_array($params['type'],[1,2,3,4])){
return $this->fail('参数错误');
}
//获取数据
$actions = Db::name('farmer_action')->where('farmer_type',$params['type'])->select()->each(function($item){
$actionType = Db::name('farmer_action_type')->where('id',$item['type_id'])->findOrEmpty();
$item['type_name'] = $actionType['name'];
return $item;
})->toArray();
$arr1 = [];
$ar2=[];
foreach ($actions as $value) {
foreach ($actions as $val) {
if($value['type_name']==$val['type_name']){
$ar2[]=$val;
}
}
$arr1[$value['type_name']] = $ar2;
$ar2 =[];
}
return $this->success('请求成功',$arr1);
}
//获取操作详情
public function actionsDetail(): Json
{
//获取参数
$params = $this->request->get(['action_id']);
if(empty($params['action_id'])){
return $this->fail('参数错误');
}
//获取数据
$data = Db::name('farmer_action_detail')->where('action_id','in',$params['action_id'])->select()->toArray();
//返回
return $this->success('请求成功',$data);
}
//上传图片
public function imageUpload(): Json
{
if(!$this->request->isPost()) {
return $this->fail('请求方式错误');
}
$file = request()->file('file');
if(empty($file)){
return $this->fail('上传文件不存在');
}
$save_name = Filesystem::disk('public')->putFile( '', $file);
return $this->success('上传成功',['image'=>$this->request->domain().'/uploads/'.$save_name]);
}
}

View File

@ -297,3 +297,36 @@ function format_amount($float)
}
return $float;
}
function curl_get($url){
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$output = curl_exec($ch);
curl_close($ch);
return json_decode($output,true);
}
function curl_post($url,$headers,$data) {
//初始化curl
$ch = curl_init();
curl_setopt($ch,CURLOPT_URL,$url);
//设置获取的信息以文件流的形式返回,而不是直接输出。
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
//设置头文件的信息作为数据流输出
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 120);
curl_setopt($ch,CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
//设置为post方式请求
curl_setopt($ch, CURLOPT_POST, 1);
//添加参数
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
//设置header
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
//关闭请求资源
$output = curl_exec($ch);
curl_close($ch);
return json_decode($output,true);
}

View File

@ -34,7 +34,8 @@
"rmccue/requests": "^2.0",
"w7corp/easywechat": "^6.8",
"tencentcloud/sms": "^3.0",
"topthink/think-filesystem": "^2.0"
"topthink/think-filesystem": "^2.0",
"ext-curl": "*"
},
"require-dev": {
"symfony/var-dumper": "^4.2",