342 lines
12 KiB
PHP
342 lines
12 KiB
PHP
<?php
|
|
|
|
namespace app\api\controller;
|
|
|
|
use think\facade\Db;
|
|
use think\response\Json;
|
|
|
|
class AnimalBreedController extends BaseApiController
|
|
{
|
|
public array $notNeedLogin = [
|
|
'addAnimal','animalInfo','animalList','animalSell','animalStatus','animalChangeList','animalSearchByCode',
|
|
'addAnimalPic','animalPicList',
|
|
'addAnimalRecord','delAnimalRecord','ediAnimalRecord','animalRecordInfo','animalRecordList',
|
|
'animalEnvData'
|
|
];
|
|
|
|
//获取状态文本
|
|
private function getStatusText($status): string
|
|
{
|
|
//养殖动物状态 1-健康 2-怀孕中 3-生病隔离中 4-可出栏 5-已出栏
|
|
$str = '';
|
|
switch ($status) {
|
|
case 1:
|
|
$str = '健康';
|
|
break;
|
|
case 2:
|
|
$str = '怀孕中';
|
|
break;
|
|
case 3:
|
|
$str = '生病隔离中';
|
|
break;
|
|
case 4:
|
|
$str = '可出栏';
|
|
break;
|
|
case 5:
|
|
$str = '已出栏';
|
|
break;
|
|
}
|
|
return $str;
|
|
}
|
|
|
|
//添加饲养动物
|
|
public function addAnimal(): Json
|
|
{
|
|
$params = $this->request->post(['user_id','code','kind','breed','gender','age','status','weight','pic']);
|
|
if(empty($params['user_id']) || empty($params['code']) || 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_animal_breed')->insertGetId([
|
|
'user_id' => $params['user_id'],
|
|
'animal_code' => $params['code'],
|
|
'animal_kind' => $params['kind'],
|
|
'animal_breed' => $params['breed'],
|
|
'animal_gender' => $params['gender'],
|
|
'animal_age' => $params['age'],
|
|
'animal_status' => $params['status'],
|
|
'animal_weight' => $params['weight'],
|
|
'create_time' => time()
|
|
]);
|
|
if(!empty($params['pic'])){
|
|
Db::name('farmer_animal_pic')->insert([
|
|
'animal_id' => $animalId,
|
|
'pic' => $params['pic'],
|
|
'create_time' => time()
|
|
]);
|
|
}
|
|
});
|
|
//返回信息
|
|
return $this->success('添加成功',[],1,1);
|
|
}
|
|
|
|
//获取饲养动物详情
|
|
public function animalInfo(): Json
|
|
{
|
|
$params = $this->request->get(['animal_id']);
|
|
if(empty($params['animal_id'])){
|
|
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']);
|
|
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 animalList(): 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'].' and animal_status != 5';
|
|
if(!empty($params['keyword'])){
|
|
$query .= ' and (animal_code LIKE "%'.$params['keyword'].'%" or animal_kind LIKE "%'.$params['keyword'].'%" or animal_breed LIKE "%'.$params['keyword'].'%")';
|
|
}
|
|
//获取数据
|
|
$data = Db::name('farmer_animal_breed')->whereRaw($query)->page($pageNo,$pageSize)->order('id desc')->select()->toArray();
|
|
//返回数据
|
|
return $this->success('请求成功',$data);
|
|
}
|
|
|
|
//通过编码查询动物
|
|
public function animalSearchByCode(): Json
|
|
{
|
|
$params = $this->request->get(['code']);
|
|
if(empty($params['code'])){
|
|
return $this->fail('参数错误');
|
|
}
|
|
$data = Db::name('farmer_animal_breed')->where('animal_code',$params['code'])->findOrEmpty();
|
|
return $this->success('请求成功',$data);
|
|
}
|
|
|
|
//标记动物为出栏状态
|
|
public function animalSell(): Json
|
|
{
|
|
$params = $this->request->post(['animal_id']);
|
|
if(empty($params['animal_id'])){
|
|
return $this->fail('参数错误');
|
|
}
|
|
$result = Db::name('farmer_animal_breed')->where('id',$params['animal_id'])->update(['animal_status'=>5]);
|
|
return $result ? $this->success('更新成功',[],1,1) : $this->fail('更新失败',[],1,1);
|
|
}
|
|
|
|
//更新动物状态
|
|
public function animalStatus(): Json
|
|
{
|
|
$params = $this->request->post(['animal_id','status','animal_weight']);
|
|
if(empty($params['animal_id'])){
|
|
return $this->fail('参数错误');
|
|
}
|
|
if(empty($params['status']) && empty($params['animal_weight'])){
|
|
return $this->fail('没有可更改的项');
|
|
}
|
|
//获取元数据
|
|
$data = Db::name('farmer_animal_breed')->where('id',$params['animal_id'])->findOrEmpty();
|
|
if(empty($data)){
|
|
return $this->fail('数据错误');
|
|
}
|
|
if(isset($params['status']) && !empty($params['status'])){
|
|
if(!in_array($params['status'],[1,2,3,4])){
|
|
return $this->fail('状态之错误');
|
|
}
|
|
}
|
|
$updateData = [];
|
|
$textData = [];
|
|
if(!empty($params['status'])){
|
|
$updateData['animal_status'] = $params['status'];
|
|
$textData['状态变更']['原状态'] = $this->getStatusText($data['animal_status']);
|
|
$textData['状态变更']['新状态'] = $this->getStatusText($params['status']);
|
|
}
|
|
if(!empty($params['animal_weight'])){
|
|
$updateData['animal_weight'] = $params['animal_weight'];
|
|
$textData['体重变更']['原体重'] = $data['animal_weight'];
|
|
$textData['体重变更']['新体重'] = $params['animal_weight'];
|
|
}
|
|
//设置数据
|
|
$saveData = [
|
|
'animal_id' => $params['animal_id'],
|
|
'action_name' => '生长状态变更',
|
|
'action_content' => json_encode($textData),
|
|
'create_time' => time()
|
|
];
|
|
Db::transaction(function () use($params,$saveData,$updateData) {
|
|
Db::name('farmer_animal_status')->insert($saveData);
|
|
if(!empty($updateData)) {
|
|
Db::name('farmer_animal_breed')->where('id', $params['animal_id'])->update($updateData);
|
|
}
|
|
});
|
|
return $this->success('更新成功',[],1,1);
|
|
}
|
|
|
|
//获取状态列表
|
|
public function animalChangeList(): Json
|
|
{
|
|
$params = $this->request->get(['animal_id','page_no','page_size']);
|
|
if(empty($params['animal_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_animal_status')->where('animal_id',$params['animal_id'])->page($pageNo,$pageSize)->order('create_time 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 addAnimalPic(): Json
|
|
{
|
|
$params = $this->request->post(['animal_id','pic']);
|
|
if(empty($params['animal_id']) || empty($params['pic'])){
|
|
return $this->fail('参数错误');
|
|
}
|
|
//写入数据
|
|
$params['create_time'] = time();
|
|
$result = Db::name('farmer_animal_pic')->insert($params);
|
|
//返回
|
|
return $result ? $this->success('添加成功',[],1,1) : $this->fail('添加失败',[],1,1);
|
|
}
|
|
|
|
//动物养殖情况图片列表
|
|
public function animalPicList(): Json
|
|
{
|
|
//获取参数
|
|
$params = $this->request->get(['animal_id']);
|
|
if(empty($params['animal_id'])){
|
|
return $this->fail('参数错误');
|
|
}
|
|
$data = Db::name('farmer_animal_pic')->where('animal_id',$params['animal_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 addAnimalRecord(): Json
|
|
{
|
|
$params = $this->request->post(['user_id','action_id','action_content','action_extends']);
|
|
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('参数错误');
|
|
}
|
|
Db::transaction(function () use($params) {
|
|
//写入数据
|
|
$params['create_time'] = time();
|
|
Db::name('farmer_animal_record')->insert($params);
|
|
if(!empty($params['action_extends'])){
|
|
$extends = json_decode($params['action_extends'],true);
|
|
if($extends && !empty($extends['animal_id'])){
|
|
foreach($extends['animal_id'] as $v){
|
|
Db::name('farmer_animal_status')->insert([
|
|
'animal_id' => $v,
|
|
'action_name' => '疫苗注射记录',
|
|
'action_content' => $params['action_content'],
|
|
'create_time' => time()
|
|
]);
|
|
}
|
|
}
|
|
}
|
|
});
|
|
//返回
|
|
return $this->success('添加成功',[],1,1);
|
|
}
|
|
|
|
//删除饲养动物操作记录
|
|
public function delAnimalRecord(): Json
|
|
{
|
|
$params = $this->request->post(['record_id']);
|
|
if(empty($params['record_id'])){
|
|
return $this->fail('参数错误');
|
|
}
|
|
$result = Db::name('farmer_animal_record')->where('id',$params['record_id'])->delete();
|
|
return $result ? $this->success('删除成功',[],1,1) : $this->fail('删除失败',[],1,1);
|
|
}
|
|
|
|
//编辑饲养动物操作记录
|
|
public function ediAnimalRecord(): 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_animal_record')->where('id',$params['record_id'])->update(['action_content'=>$params['action_content']]);
|
|
//返回
|
|
return $result ? $this->success('修改成功',[],1,1) : $this->fail('修改失败',[],1,1);
|
|
}
|
|
|
|
//获取饲养动物操作记录详情
|
|
public function animalRecordInfo(): Json
|
|
{
|
|
$params = $this->request->get(['record_id']);
|
|
if(empty($params['record_id'])){
|
|
return $this->fail('参数错误');
|
|
}
|
|
$data = Db::name('farmer_animal_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 animalRecordList(): 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_animal_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 animalEnvData(): Json
|
|
{
|
|
$params = $this->request->get(['user_id']);
|
|
if(empty($params['user_id'])){
|
|
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);
|
|
}
|
|
} |