<?php
/*
 本代码由 焕岱科技 创建
 技术支持 WX：15285007200
 严禁反编译、逆向等任何形式的侵权行为，违者将追究法律责任
*/

namespace app\common\model\bbfxshop;

use app\common\model\bbfxshop\User;

use think\Db;
use think\Cache;


class FenxiaoUser
{
    public static function getAllDown($openid,$nlevel=[],$fields=""){
        global $_W;

        $set = $_W['base']['fenxiao'];
        $ids = [];
        $openids = [];
        $users = [];
        
        $user_id = (int)$openid;
        if ($user_id == 0) {
            $info = Db::name("bbfx_user")->where(['uniacid' => UNIACID, 'openid' => $openid])->field(['id'])->find();
            if (!empty($info)) {
                $user_id = $info['id'];
            }
        }

        if(empty($fields)){
            $fields = 'id,openid,avatar,nickname,mobile,is_distributor,distributor_status,level,status,sup_uid,createtime,become_distributor_time,createtime,last_login_time';
        }
        
        if($user_id > 0){
            if($set['achievement_jisuan'] == 1 && ($set['transfer_mode'] == 1 || $set['yies_mode'] == 1 || $set['base']['double_track']['status'] == 1 || $set['base']['sconnection']['status'] == 1 || $set['base']['pyramid']['status'] == 1 || $set['base']['repurchase_jiandan']['status'] == 1)){
                
                /*
                $where = ['uniacid'=>UNIACID];
                if(!empty($nlevel)){
                    // $where['level'] = ['not in',$nlevel];
                }
                
                $user_list = Db::name("bbfx_user")->where($where)->where(function($query) use($user_id){
                    $query->whereOr(function($query) use($user_id){
                        $query->where(['first_sup_uid'=>0,'sup_uid'=>$user_id]);
                    });
                    $query->whereOr(function($query) use($user_id){
                        $query->where(['first_sup_uid'=>$user_id]);
                    }); 
                })->field('id,openid,avatar,nickname,mobile,is_distributor,distributor_status,level,status,sup_uid,createtime,become_distributor_time,createtime,last_login_time')->select();
                // print_r($user_list);die; 
                foreach ($user_list as $val) {
                    if(empty($nlevel) || (!empty($nlevel) && !in_array($val['level'],$nlevel))){
                        if($set['team_tongji'] == 0 || ($set['team_tongji'] == 1 && $val['is_distributor'] == 1 && $val['distributor_status'] == 1)){
                            $ids[] = $val['id'];
                            $openids[] = $val['openid'];
                            $users[] = $val; 
                        }
                    }
                    
                    $result = self::getAllDown($val['id'],$nlevel);
                    if(!empty($result)){
                        $ids = array_merge($ids,$result['ids']);
                        $openids = array_merge($openids,$result['openids']);
                        $users = array_merge($users,$result['users']);
                    }

                }
                */

                $where = ['uniacid' => UNIACID, 'o_sup_path' => ['like',"%-".$user_id."-%"]];
            }
            else{
                $where = ['uniacid' => UNIACID, 'sup_path' => ['like',"%-".$user_id."-%"]];
            }

            if(!empty($nlevel)){
                $where['level'] = ['not in',$nlevel];
            }
            if($set['team_tongji'] == 1){
                $where['is_distributor'] = 1;
                $where['distributor_status'] = 1;
            }
            
            $user_list = Db::name("bbfx_user")->where($where)->field($fields)->select();
            
            foreach ($user_list as $val) {
                $ids[] = $val['id'];
                $openids[] = $val['openid'];
                $users[$val['id']] = $val;
            }
        }
        
        return [
            'ids' => $ids,
            'openids' => $openids,
            'users' => $users
        ];
    }
}