suyuan/app/api/controller/UserController.php

161 lines
6.0 KiB
PHP

<?php
namespace app\api\controller;
use think\facade\Db;
use think\facade\Filesystem;
use think\response\Json;
class UserController extends BaseApiController
{
public array $notNeedLogin = ['userInfo','actions','actionsDetail','imageUpload'];
//获取农户信息
public function userInfo(): 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;
}
//判断是否是家畜养殖户
if($curl_result['data']['is_dw_user']){
//获取家畜养殖数量
$animalCount = Db::name('farmer_animal_breed')->where('user_id',$params['user_id'])->count();
//获取家畜是公的数量
$maleCount = Db::name('farmer_animal_breed')->where('user_id',$params['user_id'])->where('animal_gender',1)->count();
//获取家畜是母的数量
$maternalCount = Db::name('farmer_animal_breed')->where('user_id',$params['user_id'])->where('animal_gender',2)->count();
//获取家畜养殖种类
$animalKind = Db::name('farmer_animal_breed')->distinct(true)->where('user_id',$params['user_id'])->limit(4)->column('animal_kind');
//获取家畜养殖具体品种
$animalBreed = Db::name('farmer_animal_breed')->distinct(true)->where('user_id',$params['user_id'])->limit(4)->column('animal_breed');
$curl_result['data']['animal_detail'] = [
'total_count' => $animalCount,
'male_count' => $maleCount,
'maternal_count' => $maternalCount,
'kind' => $animalKind,
'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);
}
//获取操作信息
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']]['type_id'] = $value['type_id'];
$arr1[$value['type_name']]['actions'] = $ar2;
$ar2 =[];
}
if($params['type'] == 3){
foreach($arr1 as $k=>$v) {
$actionIds = array_column($v['actions'],'id');
$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);
}
//获取操作详情
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]);
}
}