查询上三级,下三级

 1     public function getSearchDealer($type='',$name='',$mobile='')
 2     {
 3         //分销商
 4         $dealer = new UserModel; 
 5         $name = input('get.real_name');
 6         if(!empty($name))
 7         {
 8             $where[] = ['real_name','=',$name];
 9         }
10         
11         $mobile = input('get.mobile');
12         if(!empty($mobile))
13         {
14             $where[] = ['mobile','=',$mobile];
15         }
16         $where[] = ['is_delete','=',0];
17        
18         $list = $dealer->where($where)->field('real_name as partner_name,user_id')->find();//查出自己
19         $user_id = $dealer->where($where)->value('user_id'); 
20         $list["children"] = $this->getTree($user_id);//查出下级
21         
22         if($type==1){
23             $list["superior"] = $this->getSuperior($user_id); //上级
24         }
25        
26         return $this->renderSuccess(compact('list'));
27     }
28     public function getSuperior($user_id,$i=1){
29         //推荐人
30         $list = RefereeModel::where(["user_id"=>$user_id,"level"=>1])->find();
31         $tree = [];
32         if(!empty($list)){
33             $i++;
34             if($i>3){
35                 return ;
36             }
37             $tree[]= UserInfoModel::where("user_id",$list['dealer_id'])->field('nick_name')->find();
38             $tree[0]['superior'] =$this->getSuperior($list['dealer_id'],$i);
39         }
40         return $tree; 
41     }
42     public  function getTree($user_id,$i=0)
43     {   
44         //推荐人
45         $list = RefereeModel::where(["dealer_id"=>["=",$user_id],"level"=>1])->where('dealer_id','>','0')->select()->toArray();
46        
47         $tree = [];
48         if(!empty($list)){
49             $i++;
50              if($i>3){
51                 return ;
52             }
53             foreach ($list as $key => $val) { 
54                 $tree[$key]= UserInfoModel::where("user_id",$val['user_id'])->field('nick_name')->find();
55                 $tree[$key]['children'] = $this->getTree($val['user_id'],$i);
56             }
57         }
58         return $tree;
59     }

 

posted @ 2023-09-16 18:44  糖粿  阅读(4)  评论(0编辑  收藏  举报