新增农户水产养殖模块接口

This commit is contained in:
weiz 2023-10-26 15:40:40 +08:00
parent aa49814d65
commit a757ccf91d
3 changed files with 186 additions and 53 deletions

View File

@ -76,7 +76,7 @@
$pageNo = empty($params['page_no']) || $params['page_no'] < 0 ? 1 : $params['page_no']; $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']; $pageSize = empty($params['page_size']) || $params['page_size'] < 0 ? 10 : $params['page_size'];
//设置搜素条件 //设置搜素条件
$query = 'user_id = '.$params['user_id']; $query = 'user_id = '.$params['user_id'].' and animal_status != 5';
if(!empty($params['keyword'])){ if(!empty($params['keyword'])){
$query .= ' and (animal_code LIKE "%'.$params['keyword'].'%" or animal_kind LIKE "%'.$params['keyword'].'%" or animal_breed LIKE "%'.$params['keyword'].'%")'; $query .= ' and (animal_code LIKE "%'.$params['keyword'].'%" or animal_kind LIKE "%'.$params['keyword'].'%" or animal_breed LIKE "%'.$params['keyword'].'%")';
} }
@ -90,7 +90,7 @@
public function animalStatus(): Json public function animalStatus(): Json
{ {
$params = $this->request->post(['animal_id','status']); $params = $this->request->post(['animal_id','status']);
if(empty($params['animal_id']) || empty($params['status']) || !in_array($params['status'],[1,2,3,4])){ if(empty($params['animal_id']) || empty($params['status']) || !in_array($params['status'],[1,2,3,4,5])){
return $this->fail('参数错误'); return $this->fail('参数错误');
} }
$result = Db::name('farmer_animal_breed')->where('id',$params['animal_id'])->update(['animal_status'=>$params['status']]); $result = Db::name('farmer_animal_breed')->where('id',$params['animal_id'])->update(['animal_status'=>$params['status']]);

View File

@ -9,6 +9,10 @@
{ {
public array $notNeedLogin = [ public array $notNeedLogin = [
'pondInfo', 'pondInfo',
'addFish','catchFish',
'addFishPic','fishPicList',
'addFishRecord','delFishRecord','ediFishRecord','fishRecordList',
'pondEnvData'
]; ];
//获取农户池塘信息 //获取农户池塘信息
@ -25,67 +29,196 @@
if($curl_result['code'] == 0){ if($curl_result['code'] == 0){
return $this->fail($curl_result['msg']); return $this->fail($curl_result['msg']);
} }
//获取种植信息 //获取养殖信息
$pond_fish = Db::name('farmer_fish_breed')->where([ $breedData = Db::name('farmer_pond_breed')->where('user_id',$params['user_id'])->where('pond_id',$params['pond_id'])->sum('number');
['user_id','=',$params['user_id']], //获取捕捞信息
['pond_id','=',$params['pond_id']], $catchData = Db::name('farmer_pond_catch')->where('user_id',$params['user_id'])->where('pond_id',$params['pond_id'])->sum('number');
])->order('id desc')->findOrEmpty(); if($breedData - $catchData != 0){
if(!empty($pond_fish)){ $curl_result['data']['is_culture'] = true;
$curl_result['data']['is_fished'] = true; $curl_result['data']['total_num'] = $breedData - $catchData;
$curl_result['data']['fish_id'] = $pond_fish['id']; //获取水产养殖种类
$curl_result['data']['source_code'] = $pond_fish['source_code']; $fishKind = Db::name('farmer_pond_breed')->distinct(true)->where('user_id',$params['user_id'])->where('pond_id',$params['pond_id'])->limit(4)->column('kind');
$curl_result['data']['kind'] = $pond_fish['kind']; //获取水产养殖具体品种
$curl_result['data']['breed'] = $pond_fish['breed']; $fiskBreed = Db::name('farmer_pond_breed')->distinct(true)->where('user_id',$params['user_id'])->where('pond_id',$params['pond_id'])->limit(4)->column('breed');
$curl_result['data']['number'] = $pond_fish['number']; $curl_result['data']['kind'] = $fishKind;
$curl_result['data']['buy_info'] = $pond_fish['buy_info']; $curl_result['data']['breed'] = $fiskBreed;
//获取图片 //获取去最新的图片
$fish_pic = Db::name('farmer_fish_pic')->field('pic,create_time')->where('fish_id',$pond_fish['id'])->order('id desc')->findOrEmpty(); $pic = Db::name('farmer_pond_pic')->field('pic,create_time')->where('pond_id',$params['pond_id'])->findOrEmpty();
$curl_result['data']['pic'] = !empty($fish_pic) ? $fish_pic['pic'] : ''; if(!empty($pic)){
$curl_result['data']['create_time'] = !empty($fish_pic) ? date('Y-m-d H:i:s',$fish_pic['create_time']) : ''; $curl_result['data']['pic_detail'] = $pic;
$curl_result['data']['pic_detail']['create_time'] = date('Y-m-d H:i:s',$pic['create_time']);
}else{
$curl_result['data']['pic_detail'] = [];
}
//获取养殖时间
$firstBreed = Db::name('farmer_pond_breed')->where('user_id',$params['user_id'])->where('pond_id',$params['pond_id'])->findOrEmpty();
$curl_result['data']['source_code'] = $firstBreed['source_code'];
$curl_result['data']['create_time'] = date('Y-m-d H:i:s',$firstBreed['create_time']);
}else{ }else{
$curl_result['data']['is_fished'] = false; $curl_result['data']['is_culture'] = false;
} }
//返回数据 //返回数据
return $this->success('请求成功',$curl_result['data']); return $this->success('请求成功',$curl_result['data']);
} }
//添加养殖信息 //添加水产养殖信息
public function addFish(): Json public function addFish(): Json
{ {
//获取参数 //获取参数
$params = $this->request->post(['user_id','pond_id','kind','breed','number','buy_info','pic']); $params = $this->request->post(['user_id','pond_id','kind','breed','number','buy_info','buy_cert']);
if(empty($params['user_id']) || empty($params['pond_id']) || empty($params['kind']) || empty($params['breed']) || empty($params['number']) || empty($params['buy_info'])){ if(empty($params['user_id']) || empty($params['pond_id']) || empty($params['kind']) || empty($params['breed']) || empty($params['number']) || empty($params['buy_info']) || empty($params['buy_cert'])){
return $this->fail('参数错误'); 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) { $result = Db::name('farmer_pond_breed')->insert([
$landCropId = Db::name('farmer_land_crop')->insertGetId([ 'user_id' => $params['user_id'],
'user_id' => $params['user_id'], 'pond_id' => $params['pond_id'],
'land_id' => $params['land_id'], 'source_code' => 'NO'.time(),
'source_code' => 'NO'.time(), 'kind' => $params['kind'],
'crop_name' => $params['crop_name'], 'breed' => $params['breed'],
'crop_variety' => $params['crop_variety'], 'number' => $params['number'],
'crop_brand' => $params['crop_brand'], 'buy_info' => $params['buy_info'],
'seed_time' => time(), 'buy_cert' => $params['buy_cert'],
'crop_buy_time' => strtotime($params['crop_buy_time']) 'create_time' => time(),
]); ]);
if(!empty($params['pic'])){
Db::name('farmer_land_crop_pic')->insert([
'crop_id' => $landCropId,
'pic' => $params['pic'],
'create_time' => time()
]);
}
});
//返回信息 //返回信息
return $this->success('添加成功'); return $result ? $this->success('添加成功') : $this->fail('添加失败');
} }
//标记成熟
public function catchFish(): Json
{
$params = $this->request->post(['user_id','pond_id','kind','number']);
if(empty($params['user_id']) || empty($params['pond_id']) || empty($params['kind']) || empty($params['number'])){
return $this->fail('参数错误');
}
//判断是否养殖
$hasRes = Db::name('farmer_pond_breed')->where('user_id',$params['user_id'])->where('pond_id',$params['pond_id'])->select();
if($hasRes->isEmpty()){
return $this->fail('该池塘未养殖');
}
//获取池塘养殖数量
$total = Db::name('farmer_pond_breed')->where('user_id',$params['user_id'])->where('pond_id',$params['pond_id'])->sum('number');
if($params['number'] > $total){
return $this->fail('捕捞数量大于池塘总数');
}
$result = Db::name('farmer_pond_catch')->insert([
'user_id' => $params['user_id'],
'pond_id' => $params['pond_id'],
'kind' => $params['kind'],
'number' => $params['number'],
'create_time' => time()
]);
return $result ? $this->success('操作成功') : $this->fail('操作失败');
}
//添加水产养殖图片
public function addFishPic(): Json
{
//获取参数
$params = $this->request->post(['pond_id','pic']);
if(empty($params['pond_id']) || empty($params['pic'])){
return $this->fail('参数错误');
}
//写入数据
$params['create_time'] = time();
$result = Db::name('farmer_pond_pic')->insert($params);
//返回
return $result ? $this->success('添加成功') : $this->fail('添加失败');
}
//水产养殖图片列表
public function fishPicList(): Json
{
//获取参数
$params = $this->request->get(['pond_id']);
if(empty($params['pond_id'])){
return $this->fail('参数错误');
}
$data = Db::name('farmer_pond_pic')->where('pond_id',$params['pond_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 addFishRecord(): Json
{
//获取参数
$params = $this->request->post(['pond_id','action_id','action_content']);
if(empty($params['pond_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_pond_record')->insert($params);
//返回
return $result ? $this->success('添加成功') : $this->fail('添加失败');
}
//删除水产养殖操作记录
public function delFishRecord(): Json
{
$params = $this->request->post(['record_id']);
if(empty($params['record_id'])){
return $this->fail('参数错误');
}
$resust = Db::name('farmer_pond_record')->where('id',$params['record_id'])->delete();
return $resust ? $this->success('删除成功') : $this->fail('删除失败');
}
//删除水产养殖操作记录
public function ediFishRecord(): 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_pond_record')->where('id',$params['record_id'])->update(['action_content'=>$params['action_content']]);
//返回
return $result ? $this->success('修改成功') : $this->fail('修改失败');
}
//水产养殖操作记录列表
public function fishRecordList(): Json
{
//获取参数
$params = $this->request->get(['pond_id','page_no','page_size']);
if(empty($params['pond_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_pond_record')->where('pond_id',$params['pond_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 pondEnvData(): Json
{
$params = $this->request->get(['user_id']);
if(empty($params['user_id'])){
return $this->fail('参数错误');
}
$data = Db::name('farmer_pond_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);
}
} }

View File

@ -76,7 +76,7 @@
$pageNo = empty($params['page_no']) || $params['page_no'] < 0 ? 1 : $params['page_no']; $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']; $pageSize = empty($params['page_size']) || $params['page_size'] < 0 ? 10 : $params['page_size'];
//设置搜素条件 //设置搜素条件
$query = 'user_id = '.$params['user_id']; $query = 'user_id = '.$params['user_id'].' and status != 5';
if(!empty($params['keyword'])){ if(!empty($params['keyword'])){
$query .= ' and (code LIKE "%'.$params['keyword'].'%" or kind LIKE "%'.$params['keyword'].'%" or breed LIKE "%'.$params['keyword'].'%")'; $query .= ' and (code LIKE "%'.$params['keyword'].'%" or kind LIKE "%'.$params['keyword'].'%" or breed LIKE "%'.$params['keyword'].'%")';
} }
@ -90,7 +90,7 @@
public function poultryStatus(): Json public function poultryStatus(): Json
{ {
$params = $this->request->post(['poultry_id','status']); $params = $this->request->post(['poultry_id','status']);
if(empty($params['poultry_id']) || empty($params['status']) || !in_array($params['status'],[1,2,3,4])){ if(empty($params['poultry_id']) || empty($params['status']) || !in_array($params['status'],[1,2,3,4,5])){
return $this->fail('参数错误'); return $this->fail('参数错误');
} }
$result = Db::name('farmer_poultry_breed')->where('id',$params['poultry_id'])->update(['status'=>$params['status']]); $result = Db::name('farmer_poultry_breed')->where('id',$params['poultry_id'])->update(['status'=>$params['status']]);