新增家禽养殖模块API

This commit is contained in:
weiz 2023-10-25 11:01:17 +08:00
parent 119d1dde61
commit d4de18c0a4
4 changed files with 264 additions and 2 deletions

View File

@ -18,7 +18,7 @@
public function addAnimal(): Json
{
$params = $this->request->post(['user_id','kind','breed','gender','age','status','weight','pic']);
if(empty($params['user_id']) || empty($params['name']) || empty($params['breed']) || empty($params['type']) || !in_array($params['type'],[1,2]) || empty($params['age']) || empty($params['status']) || !in_array($params['status'],[1,2,3,4]) || empty($params['weight'])){
if(empty($params['user_id']) || empty($params['kind']) || empty($params['breed']) || empty($params['gender']) || !in_array($params['gender'],[1,2]) || empty($params['age']) || empty($params['status']) || !in_array($params['status'],[1,2,3,4]) || empty($params['weight'])){
return $this->fail('参数错误');
}
//写入数据
@ -54,9 +54,14 @@
return $this->fail('参数错误');
}
$data = Db::name('farmer_animal_breed')->where('id',$params['animal_id'])->findOrEmpty();
if(empty($data)){
return $this->fail('数据不存在');
}
$data['pic_detail'] = Db::name('farmer_animal_pic')->field('pic,create_time')->where('animal_id',$params['animal_id'])->order('id desc')->findOrEmpty();
$data['create_time'] = date('Y-m-d H:i:s',$data['create_time']);
$data['pic_detail']['create_time'] = date('Y-m-d H:i:s',$data['pic_detail']['create_time']);
if(!empty($data['pic_detail'])){
$data['pic_detail']['create_time'] = date('Y-m-d H:i:s',$data['pic_detail']['create_time']);
}
return $this->success('请求成功',$data);
}
@ -212,6 +217,9 @@
return $this->fail('参数错误');
}
$data = Db::name('farmer_animal_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

@ -252,6 +252,9 @@
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);
}

View File

@ -0,0 +1,225 @@
<?php
namespace app\api\controller;
use think\facade\Db;
use think\response\Json;
class PoultryBreedController extends BaseApiController
{
public array $notNeedLogin = [
'addPoultry','poultryInfo','poultryList','poultryStatus',
'addPoultryPic','poultryPicList',
'addPoultryRecord','delPoultryRecord','ediPoultryRecord','poultryRecordInfo','poultryRecordList',
'poultryEnvData'
];
//添加饲养家禽
public function addPoultry(): Json
{
$params = $this->request->post(['user_id','kind','breed','gender','age','status','weight','pic']);
if(empty($params['user_id']) || empty($params['kind']) || empty($params['breed']) || empty($params['gender']) || !in_array($params['gender'],[1,2]) || empty($params['age']) || empty($params['status']) || !in_array($params['status'],[1,2,3,4]) || empty($params['weight'])){
return $this->fail('参数错误');
}
//写入数据
Db::transaction(function () use($params) {
$animalId = Db::name('farmer_poultry_breed')->insertGetId([
'user_id' => $params['user_id'],
'code' => '',
'kind' => $params['kind'],
'breed' => $params['breed'],
'gender' => $params['gender'],
'age' => $params['age'],
'status' => $params['status'],
'weight' => $params['weight'],
'create_time' => time()
]);
if(!empty($params['pic'])){
Db::name('farmer_poultry_pic')->insert([
'poultry_id' => $animalId,
'pic' => $params['pic'],
'create_time' => time()
]);
}
});
//返回信息
return $this->success('添加成功');
}
//获取饲养家禽详情
public function poultryInfo(): Json
{
$params = $this->request->get(['poultry_id']);
if(empty($params['poultry_id'])){
return $this->fail('参数错误');
}
$data = Db::name('farmer_poultry_breed')->where('id',$params['poultry_id'])->findOrEmpty();
if(empty($data)){
return $this->fail('数据不存在');
}
$data['pic_detail'] = Db::name('farmer_poultry_pic')->field('pic,create_time')->where('poultry_id',$params['poultry_id'])->order('id desc')->findOrEmpty();
$data['create_time'] = date('Y-m-d H:i:s',$data['create_time']);
if(!empty($data['pic_detail'])){
$data['pic_detail']['create_time'] = date('Y-m-d H:i:s',$data['pic_detail']['create_time']);
}
return $this->success('请求成功',$data);
}
//饲养家禽列表
public function poultryList(): Json
{
$params = $this->request->get(['user_id','page_no','page_size','keyword']);
if(empty($params['user_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'];
//设置搜素条件
$query = 'user_id = '.$params['user_id'];
if(!empty($params['keyword'])){
$query .= ' and (code LIKE "%'.$params['keyword'].'%" or kind LIKE "%'.$params['keyword'].'%" or breed LIKE "%'.$params['keyword'].'%")';
}
//获取数据
$data = Db::name('farmer_poultry_breed')->whereRaw($query)->page($pageNo,$pageSize)->order('id desc')->select()->toArray();
//返回数据
return $this->success('请求成功',$data);
}
//更新饲养家禽状态
public function poultryStatus(): Json
{
$params = $this->request->post(['poultry_id','status']);
if(empty($params['poultry_id']) || empty($params['status']) || !in_array($params['status'],[1,2,3,4])){
return $this->fail('参数错误');
}
$result = Db::name('farmer_poultry_breed')->where('id',$params['poultry_id'])->update(['status'=>$params['status']]);
return $result ? $this->success('更新成功') : $this->fail('更新失败');
}
//上传家禽养殖情况图片
public function addPoultryPic(): Json
{
$params = $this->request->post(['poultry_id','pic']);
if(empty($params['poultry_id']) || empty($params['pic'])){
return $this->fail('参数错误');
}
//写入数据
$params['create_time'] = time();
$result = Db::name('farmer_poultry_pic')->insert($params);
//返回
return $result ? $this->success('添加成功') : $this->fail('添加失败');
}
//家禽养殖情况图片列表
public function poultryPicList(): Json
{
//获取参数
$params = $this->request->get(['poultry_id']);
if(empty($params['poultry_id'])){
return $this->fail('参数错误');
}
$data = Db::name('farmer_poultry_pic')->where('poultry_id',$params['poultry_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 addPoultryRecord(): Json
{
$params = $this->request->post(['user_id','action_id','action_content']);
if(empty($params['user_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_poultry_record')->insert($params);
//返回
return $result ? $this->success('添加成功') : $this->fail('添加失败');
}
//删除饲养家禽操作记录
public function delPoultryRecord(): Json
{
$params = $this->request->post(['record_id']);
if(empty($params['record_id'])){
return $this->fail('参数错误');
}
$result = Db::name('farmer_poultry_record')->where('id',$params['record_id'])->delete();
return $result ? $this->success('删除成功') : $this->fail('删除失败');
}
//编辑饲养家禽操作记录
public function ediPoultryRecord(): 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_poultry_record')->where('id',$params['record_id'])->update(['action_content'=>$params['action_content']]);
//返回
return $result ? $this->success('修改成功') : $this->fail('修改失败');
}
//获取饲养家禽操作记录详情
public function poultryRecordInfo(): Json
{
$params = $this->request->get(['record_id']);
if(empty($params['record_id'])){
return $this->fail('参数错误');
}
$data = Db::name('farmer_poultry_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 poultryRecordList(): Json
{
$params = $this->request->get(['user_id','action_type_id','page_no','page_size']);
if(empty($params['user_id']) || empty($params['action_type_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'];
//获取操作分类下的操作id
$actions = Db::name('farmer_action')->where('type_id',$params['action_type_id'])->select()->toArray();
$actionIds = array_column($actions,'id');
//获取数据
$data = Db::name('farmer_poultry_record')->where('user_id',$params['user_id'])->where('action_id','in',$actionIds)->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 poultryEnvData(): Json
{
$params = $this->request->get(['user_id']);
if(empty($params['user_id'])){
return $this->fail('参数错误');
}
$data = Db::name('farmer_poultry_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

@ -66,6 +66,26 @@ class UserController extends BaseApiController
'breed' => $animalBreed
];
}
//判断是否是家禽养殖户
if($curl_result['data']['is_jq_user']){
//获取家畜养殖数量
$poultryCount = Db::name('farmer_poultry_breed')->where('user_id',$params['user_id'])->count();
//获取家畜是公的数量
$maleCount = Db::name('farmer_poultry_breed')->where('user_id',$params['user_id'])->where('gender',1)->count();
//获取家畜是母的数量
$maternalCount = Db::name('farmer_poultry_breed')->where('user_id',$params['user_id'])->where('gender',2)->count();
//获取家畜养殖种类
$poultryKind = Db::name('farmer_poultry_breed')->distinct(true)->where('user_id',$params['user_id'])->limit(4)->column('kind');
//获取家畜养殖具体品种
$poultryBreed = Db::name('farmer_poultry_breed')->distinct(true)->where('user_id',$params['user_id'])->limit(4)->column('breed');
$curl_result['data']['poultry_detail'] = [
'total_count' => $poultryCount,
'male_count' => $maleCount,
'maternal_count' => $maternalCount,
'kind' => $poultryKind,
'breed' => $poultryBreed
];
}
//返回数据
return json($curl_result);
}
@ -102,6 +122,12 @@ class UserController extends BaseApiController
$arr1[$k]['action_record'] = Db::name('farmer_animal_record')->where('action_id','in',$actionIds)->order('id desc')->limit(5)->select();
}
}
if($params['type'] == 2) {
foreach($arr1 as $k=>$v) {
$actionIds = array_column($v['actions'],'id');
$arr1[$k]['action_record'] = Db::name('farmer_poultry_record')->where('action_id','in',$actionIds)->order('id desc')->limit(5)->select();
}
}
return $this->success('请求成功',$arr1);
}