Merge pull request '新增获取所有农户接口' (#150) from zhangwei into dev

Reviewed-on: #150
This commit is contained in:
weiz 2023-11-01 16:30:15 +08:00
commit fafc964faf

View File

@ -315,7 +315,13 @@ class InformationController extends BaseApiController
return $this->fail('参数错误_worker');
}
//获取农户信息
$farmerInfo = UserInformationg::where('id', $params['user_id'])->field('id,create_user_id,company_id,name,phone,area_id,street_id,village_id,brigade_id,area_id area_name,street_id street_name,village_id village_name,brigade_id brigade_name')->findOrEmpty()->toArray();
$farmerInfo = UserInformationg::alias('f')
->leftJoin('company c','f.company_id = c.id')
->leftJoin('user u','u.id = f.create_user_id')
->leftJoin('user g','g.id = c.user_id')
->where('f.id', $params['user_id'])
->field('f.id,f.create_user_id,f.company_id,f.name,f.phone,f.area_id,f.street_id,f.village_id,f.brigade_id,f.area_id area_name,f.street_id street_name,f.village_id village_name,f.brigade_id brigade_name,c.company_name,f.address,u.nickname as captain_name,g.nickname as group_name')
->findOrEmpty()->toArray();
if(empty($farmerInfo)){
return $this->fail('用户不存在_worker');
}
@ -412,89 +418,106 @@ class InformationController extends BaseApiController
}
//获取农户列表
public function farmerList() {
public function farmerList(): Json
{
$params = $this->request->post(['farmer_name','farmer_phone','group_name','captain_name','page_no','page_size']);
if(empty($params['page_no']) || empty($params['page_size'])){
return $this->fail('参数错误');
}
$search = [];
if(!empty($params['farmer_name'])){
$search[] = ['f.name','like','%'.$params['farmer_name'].'%'];
}
if(!empty($params['farmer_phone'])){
$search[] = ['f.phone','like','%'.$params['farmer_phone'].'%'];
}
if(!empty($params['group_name'])){
$search[] = ['g.nickname','like','%'.$params['group_name'].'%'];
}
if(!empty($params['captain_name'])){
$search[] = ['u.nickname','like','%'.$params['captain_name'].'%'];
}
$data = UserInformationg::alias('f')
->field('f.id as farmer_id,f.create_user_id,f.company_id,f.name as farmer_name,f.phone as farmer_phone,c.company_name,u.nickname as captain_name,g.nickname as group_name')
->field('f.id,f.create_user_id,f.company_id,f.name,f.phone,f.area_id,f.street_id,f.village_id,f.brigade_id,c.company_name,u.nickname as captain_name,g.nickname as group_name,f.area_id area_name,f.street_id street_name,f.village_id village_name,f.brigade_id brigade_name,f.address')
->leftJoin('company c','f.company_id = c.id')
->leftJoin('user u','u.id = f.create_user_id')
->leftJoin('user g','g.id = c.user_id')
->page($params['page_no'],$params['page_size'])->order('id desc')->select()->toArray();
/*
$data = UserInformationg::field('id,create_user_id,company_id,name,phone,area_id,street_id,village_id,brigade_id,area_id area_name,street_id street_name,village_id village_name,brigade_id brigade_name,address')->page($params['page_no'],$params['page_size'])->order('id desc')->select()->each(function($item){
$item['address'] = $item['area_name'].$item['street_name'].$item['village_name'].$item['brigade_name'].$item['address'];
unset($item['area_name'],$item['street_name'],$item['village_name'],$item['brigade_name'],$item['area_id'],$item['street_id'],$item['village_id'],$item['brigade_id']);
//获取公司信息
$company = Company::field('company_name,user_id')->where('id',$item['company_id'])->findOrEmpty();
$item['company_name'] = $company['company_name'];
//获取生产队长信息
$captain = User::field('nickname')->where('id',$item['create_user_id'])->findOrEmpty();
$item['captain_name'] = $captain['nickname'];
//获取小组服务公司用户信息
$group = User::field('nickname')->where('id',$company['user_id'])->findOrEmpty();
$item['group_name'] = $group['nickname'];
$userInformationgDemand = UserInformationgDemand::field('id,category_child,data')->where('information_id', $item['id'])->where('category_id',6)->where('category_child','in','7,32')->order('id', 'desc')->select()->toArray();
//是否是种植户
$is_zz_user = false;
//是否是水产养殖户
$is_sc_user = false;
//是否是家禽养殖户
$is_jq_user = false;
//是否是大型动物养殖户
$is_dw_user = false;
//土地总面积
$total_land_area = 0;
//池塘总面积
$total_pond_area = 0;
//家禽养殖场总面积
$total_henhouse_area = 0;
//动物养殖场总面积
$total_pasture_area = 0;
foreach($userInformationgDemand as $v){
if($v['category_child'] == 7){
$is_zz_user = true;
$total_land_area += floatval($v['data']['area']);
}
if($v['category_child'] == 32){
if($v['data']['breeding_type'] == 1){//水产
$is_sc_user = true;
$total_pond_area += floatval($v['data']['area']);
}elseif ($v['data']['breeding_type'] == 2) {//脯乳动物
$is_jq_user = true;
$total_henhouse_area += floatval($v['data']['area']);
}elseif ($v['data']['breeding_type'] == 3){//家禽
$is_dw_user = true;
$total_pasture_area += floatval($v['data']['area']);
}
}
}
$str = [];
$area = [];
if($is_zz_user){
$str[] = '种植户';
$area[] = $total_land_area;
}
if($is_sc_user){
$str[] = '水产养殖户';
$area[] = $total_pond_area;
}
if($is_jq_user){
$str[] = '家禽养殖户';
$area[] = $total_henhouse_area;
}
if($is_dw_user){
$str[] = '家畜养殖户';
$area[] = $total_pasture_area;
}
$item['farmer_type'] = implode('/',$str);
$item['farmer_area'] = implode('/',$area);
return $item;
})->toArray();
*/
dump($data);
->where($search)
->page($params['page_no'],$params['page_size'])->order('id desc')->select()
->each(function($item){
$item['address'] = $item['area_name'].$item['street_name'].$item['village_name'].$item['brigade_name'].$item['address'];
unset($item['area_name'],$item['street_name'],$item['village_name'],$item['brigade_name'],$item['area_id'],$item['street_id'],$item['village_id'],$item['brigade_id']);
$userInformationgDemand = UserInformationgDemand::field('id,category_child,data')->where('information_id', $item['id'])->where('category_id',6)->where('category_child','in','7,32')->order('id', 'desc')->select()->toArray();
//是否是种植户
$is_zz_user = false;
//是否是水产养殖户
$is_sc_user = false;
//是否是家禽养殖户
$is_jq_user = false;
//是否是大型动物养殖户
$is_dw_user = false;
//土地总面积
$total_land_area = 0;
//池塘总面积
$total_pond_area = 0;
//家禽养殖场总面积
$total_henhouse_area = 0;
//动物养殖场总面积
$total_pasture_area = 0;
foreach($userInformationgDemand as $v){
if($v['category_child'] == 7){
$is_zz_user = true;
$total_land_area += floatval($v['data']['area']);
}
if($v['category_child'] == 32){
if($v['data']['breeding_type'] == 1){//水产
$is_sc_user = true;
$total_pond_area += floatval($v['data']['area']);
}elseif ($v['data']['breeding_type'] == 2) {//脯乳动物
$is_jq_user = true;
$total_henhouse_area += floatval($v['data']['area']);
}elseif ($v['data']['breeding_type'] == 3){//家禽
$is_dw_user = true;
$total_pasture_area += floatval($v['data']['area']);
}
}
}
$str = [];
$area = [];
if($is_zz_user){
$str[] = '种植户';
$area[] = $total_land_area;
}
if($is_sc_user){
$str[] = '水产养殖户';
$area[] = $total_pond_area;
}
if($is_jq_user){
$str[] = '家禽养殖户';
$area[] = $total_henhouse_area;
}
if($is_dw_user){
$str[] = '家畜养殖户';
$area[] = $total_pasture_area;
}
$item['farmer_type'] = implode('/',$str);
$item['farmer_area'] = implode('/',$area);
return $item;
})->toArray();
$count = UserInformationg::alias('f')
->field('f.id,f.create_user_id,f.company_id,f.name,f.phone,f.area_id,f.street_id,f.village_id,f.brigade_id,c.company_name,u.nickname as captain_name,g.nickname as group_name,f.area_id area_name,f.street_id street_name,f.village_id village_name,f.brigade_id brigade_name,f.address')
->leftJoin('company c','f.company_id = c.id')
->leftJoin('user u','u.id = f.create_user_id')
->leftJoin('user g','g.id = c.user_id')
->where($search)
->count();
$result = [
'count' => $count,
'page_no' => $params['page_no'],
'page_size' => $params['page_size'],
'lists' => $data
];
return $this->success('请求成功',$result);
}
}