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

namespace app\api\controller\fenxiao;

use app\api\controller\Base;
use think\Db;
use app\common\model\bbfxshop\User;
use app\common\model\bbfxshop\Order;
use app\common\model\bbfxshop\FenxiaoUser;



class Team extends Base
{
    protected $noNeedLogin = [];
    protected $noNeedRight = ['*'];
    
    public $model = null;
    public function _initialize(){
        parent::_initialize();
        $this->model = new \app\common\model\bbfxshop\Fenxiao;
    }
    
    public function index(){
        $user = $this->user;
        $set = $this->model->getSet();
        $down = FenxiaoUser::getAllDown($user['id']);
        $team_num = count($down['users']);
        
        $order_count = Db::name("bbfx_order")->where(['uniacid'=>UNIACID,'status'=>['>',0],'user_id'=>$user['id']])->count();

        $where = ['uniacid'=>UNIACID,'sup_uid'=>$user['id']];
        if($set['team_tongji'] == 1){
            $where['is_distributor'] = 1;
            $where['distributor_status'] = 1;
        }
        
        $level1_user = Db::name("bbfx_user")->where($where)->count();
        $baoming_num =  Db::name("bbfx_activity_baoming")->where(['uniacid'=>UNIACID,'user_id'=>$user['id'],'is_pay'=>1])->count();
        $today_time = strtotime(date("Y-m-d 00:00:00"));
        $today_income = Db::name("bbfx_money_log")->where(['uniacid'=>UNIACID,'type'=>1,'user_id'=>$user['id'],'item_type'=>['in',[26,52,53,54]],'createtime'=>['>',$today_time]])->sum("money");
        $total_income = Db::name("bbfx_money_log")->where(['uniacid'=>UNIACID,'type'=>1,'user_id'=>$user['id'],'item_type'=>['in',[26,52,53,54]]])->sum("money");

        $today_score_income = Db::name("bbfx_money_log")->where(['uniacid'=>UNIACID,'type'=>2,'user_id'=>$user['id'],'item_type'=>['in',[26,52,53,54]],'createtime'=>['>',$today_time]])->sum("money");
        $total_score_income = Db::name("bbfx_money_log")->where(['uniacid'=>UNIACID,'type'=>2,'user_id'=>$user['id'],'item_type'=>['in',[26,52,53,54]]])->sum("money");
        
        $result = [
            "id"=>$user['id'],
            "nickname"=>$user['nickname'],
            "avatar"=>$user['avatar'],
            "levelname"=>$user['levelname'],
            "team_num"=>$team_num,
            "level1_user"=>$level1_user,
            "order_count"=>$order_count,
            "baoming_num"=>$baoming_num,
            "today_income"=>round($today_income,2),
            "total_income"=>round($total_income,2),
            "today_score_income"=>round($today_score_income,2),
            "total_score_income"=>round($total_score_income,2),
            "last_login_time"=>$user['last_login_time'],
            "createtime"=>$user['createtime'],
        ];

        if(is_numeric($result['createtime'])){
            $result['createtime'] = date("Y-m-d H:i:s",$result['createtime']);
        }
        
        __success("操作成功",$result);
    }

    public function achievement(){
        $user = $this->user;
        $set = $this->model->getSet();
        $down = FenxiaoUser::getAllDown($user['id']);
        $team_ids = $down['ids'];
        if($set['selfbuy_achievement'] == 1){
            $team_ids[] = $user['id'];
        }

        if(empty($team_ids)){
            $team_ids = [-1];
        }

        $orderWhere = ['o.uniacid'=>UNIACID,'o.is_pay'=>1,'o.user_id'=>['in',$team_ids],'o.refund_status'=>['in',[-1,2]]];
        $order_achievement = Db::name("bbfx_order_goods")->alias("og")->join("bbfx_order o","og.order_id=o.id","LEFT")
        ->where($orderWhere)
        // ->whereIn('o.user_id',function($query) use($user_id,$config){
        //     $like_data = "%-".$user_id."-%";
        //     if($config['selfbuy_achievement'] == 1){
        //         $like_data = "%-".$user_id."%";
        //     }
        //     $query->name('bbfx_user')->where(['sup_path' => ['like',$like_data]])->field('id');
        // })
        ->field("
            SUM(og.price) AS achievement_total,
            SUM(CASE WHEN DATE(FROM_UNIXTIME(o.createtime)) = CURDATE() THEN og.price ELSE 0 END) AS achievement_today,
            SUM(CASE WHEN DATE(FROM_UNIXTIME(o.createtime)) = CURDATE() THEN og.price ELSE 0 END) AS achievement_today,
            SUM(CASE WHEN DATE_FORMAT(FROM_UNIXTIME(o.createtime),'%Y%m') = DATE_FORMAT(NOW(),'%Y%m') THEN og.price ELSE 0 END) AS achievement_month,
            SUM(CASE WHEN DATE_FORMAT(FROM_UNIXTIME(o.createtime),'%Y%m') = DATE_FORMAT(DATE_SUB(NOW(),INTERVAL 1 MONTH),'%Y%m') THEN og.price ELSE 0 END) AS achievement_smonth
        ")
        ->find();
        // echo "<pre>";
        // print_r($order_achievement);die;
        $achievement_total = $order_achievement['achievement_total'];
        $achievement_today = $order_achievement['achievement_today'];
        $achievement_month = $order_achievement['achievement_month'];
        $achievement_smonth = $order_achievement['achievement_smonth'];

        $achievement_daqu = 0;
        $achievement_xiaoqu = 0;
        $level1_user = Db::name("bbfx_user")->where(['uniacid'=>UNIACID,'sup_uid'=>$this->user['id']])->field("id")->select();
        if(!empty($level1_user)){
            $daqu_user_id = 0;
            $daqu_uids = [];
            foreach ($level1_user as $key => &$val){
                $alldown = FenxiaoUser::getAllDown($val['id']);
                $achievement = 0;
                $ids = [-1];
                if(!empty($alldown)){
                    $ids = $alldown['ids'];
                }

                if($set['selfbuy_achievement'] == 1){
                    $ids[] = $val['id'];
                }
                $achievement = Order::getAchievement(['o.user_id'=>['in',$ids]],[]);

                if(empty($daqu_user_id)){
                    $daqu_user_id = $val['id'];
                    $achievement_daqu = $achievement;
                }
                else{
                    if($achievement > $achievement_daqu){
                        $daqu_user_id = $val['id'];
                        $achievement_daqu = $achievement;
                        $daqu_uids = $ids;
                    }
                }
                $val['ids'] = $ids;
                $val['achievement'] = $achievement;
            }
            unset($val);

            $xiaoqu_ids = [];
            foreach ($level1_user as $key => $val){
                if($val['id'] != $daqu_user_id){
                    $xiaoqu_ids = array_merge($xiaoqu_ids,$val['ids']);
                    $achievement_xiaoqu += $val['achievement'];
                }
            }
        }

        $result = [
            "id"=>$user['id'],
            "nickname"=>$user['nickname'],
            "avatar"=>$user['avatar'],
            "levelname"=>$user['levelname'],
            "achievement_total"=>round($achievement_total,2),
            "achievement_today"=>round($achievement_today,2),
            "achievement_month"=>round($achievement_month,2),
            "achievement_smonth"=>round($achievement_smonth,2),
            "achievement_daqu"=>round($achievement_daqu,2),
            "achievement_xiaoqu"=>round($achievement_xiaoqu,2),
            "last_login_time"=>$user['last_login_time'],
            "createtime"=>$user['createtime'],
        ];

        __success('操作成功',$result);
    }
    
    public function list(){
        global $_W;
        $params = $this->param();
        $keyword = $params['keyword'];
        $level = intval($params['level']);
        $base = $_W['base'];
        $texts = $base['fenxiao']['texts'];
        $page = max(1,$params['page']);
        $limit = 10;
        $levels = \app\common\model\bbfxshop\FenxiaoLevel::getLevels(1);
        
        $user = $this->user;
        $double_track_zhu_users = [];
        $field = 'id,openid,avatar,nickname,mobile,is_distributor,distributor_status,level,status,sup_uid,createtime,become_distributor_time,createtime,last_login_time';
        if($level == 1){
            $where = ["uniacid"=>UNIACID,"sup_uid"=>$user['id']];
            if($base['fenxiao']['team_tongji'] == 1){
                $where['is_distributor'] = 1;
                $where['distributor_status'] = 1;
            }
            if($base['pyramid']['status'] == 1){
                $where = function($query) use($user){
                    $query->whereOr(function($query) use($user){
                		$query->where(['first_sup_uid'=>0,'sup_uid'=>$user['id']]);
                	});
                	$query->whereOr(function($query) use($user){
                		$query->where(['first_sup_uid'=>$user['id']]);
                	});
                };
            }

            $double_track_zhu_users = [];
            if($base['double_track']['status'] == 1){
                $double_track_zhu_users = Db::name("bbfx_user")->where(['uniacid'=>UNIACID,'sup_uid'=>$user['id'],'is_distributor'=>1,'distributor_status'=>1])->field("id")->order("become_distributor_time asc,id asc")->limit(0,$base['double_track']['num'])->select();
                if(empty($double_track_zhu_users)){
                    $double_track_zhu_users = [];
                }
                $double_track_zhu_users = array_column($double_track_zhu_users,"id");
            }

            $title = $texts['level1_team'];
        }
        else{
            $type = intval($params['type']);
            $time = 0;
            if($type == 1 || $type == 2){
                if($type == 1){
                    $title = $texts['t4'];
                    $time = strtotime(date("Y-m-d 00:00:00"));
                }
                else{
                    $title = $texts['t5'];
                    $time = strtotime(date("Y-m-01 00:00:00"));
                }
            }   
            else{
                $title = $texts['t6'];
            }

            $where = ['uniacid' => UNIACID, 'sup_path' => ['like',"%-".$this->user['id']."-%"]];
            if($base['fenxiao']['achievement_jisuan'] == 1){
                $where['o_sup_path'] = ['like',"%-".$this->user['id']."-%"];
            }
            else{
                $where['sup_path'] = ['like',"%-".$this->user['id']."-%"];
            }
            
            if($base['fenxiao']['team_tongji'] == 1){
                $where['is_distributor'] = 1;
                $where['distributor_status'] = 1;
            }
            
            if($time > 0){
                $where['createtime'] = ['>',$time];
            }
        }
        $where1 = function($query) use($keyword){
            $whereOr = [];
            if(!empty($keyword)){
                $whereOr['nickname'] = ['like','%'.$keyword.'%'];
                $whereOr['id'] = intval($keyword);
            }
            
            $query->whereOr($whereOr);
            
        };
        $list = Db::name("bbfx_user")->where($where)->where($where1)->field($field)->order('createtime desc')->limit(($page-1)*$limit,$limit)->select();
        $total = Db::name("bbfx_user")->where($where)->where($where1)->count();
        $start_time = strtotime(date("Y-m-01 00:00:00"));
        $end_time = strtotime(date("Y-m-01 00:00:00",$start_time)." +1 month");
        $today_time = strtotime(date("Y-m-d 00:00:00"));

        $order_counts = [];
        $level1_users = [];
        $baoming_nums = [];
        $today_incomes = [];
        $total_incomes = [];
        $today_score_incomes = [];
        $total_score_incomes = [];
        if(!empty($list)){
            if(empty($list)) $list = [];
            $uids = array_column($list,"id");
            $order_count_all = Db::name("bbfx_order")->where(['uniacid'=>UNIACID,'status'=>['>',0],'user_id'=>['in',$uids]])->field("user_id,count(*) as count")->group("user_id")->select();
            if(!empty($order_count_all)){
                foreach ($order_count_all as $val){
                    $order_counts[$val['user_id']] = $val['count'];
                }
            }
            $where = ['uniacid'=>UNIACID,'sup_uid'=>['in',$uids]];
            if($base['fenxiao']['team_tongji'] == 1){
                $where['distributor_status'] = 1;
                $where['is_distributor'] = 1;
            }
            $level1_user_all = Db::name("bbfx_user")->where($where)->field("sup_uid,count(*) as count")->group("sup_uid")->select();
            if(!empty($level1_user_all)){
                foreach ($level1_user_all as $val){
                    $level1_users[$val['sup_uid']] = $val['count'];
                }
            }
            $baoming_num_all = Db::name("bbfx_activity_baoming")->where(['uniacid'=>UNIACID,'user_id'=>['in',$uids],'is_pay'=>1])->field("user_id,count(*) as count")->group("user_id")->select();
            if(!empty($baoming_num_all)){
                foreach ($baoming_num_all as $val){
                    $baoming_nums[$val['user_id']] = $val['count'];
                }
            }
            $baoming_num_all = Db::name("bbfx_activity_baoming")->where(['uniacid'=>UNIACID,'user_id'=>['in',$uids],'is_pay'=>1])->field("user_id,count(*) as count")->group("user_id")->select();
            if(!empty($baoming_num_all)){
                foreach ($baoming_num_all as $val){
                    $baoming_nums[$val['user_id']] = $val['count'];
                }
            }
            $today_income_all = Db::name("bbfx_money_log")->where(['uniacid'=>UNIACID,'type'=>1,'user_id'=>['in',$uids],'item_type'=>['in',[26,52,53,54]],'createtime'=>['>',$today_time]])->field("user_id,sum(money) as money")->group("user_id")->select();
            if(!empty($today_income_all)){
                foreach ($today_income_all as $val){
                    $today_incomes[$val['user_id']] = $val['money'];
                }
            }
            $total_income_all = Db::name("bbfx_money_log")->where(['uniacid'=>UNIACID,'type'=>1,'user_id'=>['in',$uids],'item_type'=>['in',[26,52,53,54]]])->field("user_id,sum(money) as money")->group("user_id")->select();
            if(!empty($total_income_all)){
                foreach ($total_income_all as $val){
                    $total_incomes[$val['user_id']] = $val['money'];
                }
            }
            $today_score_income_all = Db::name("bbfx_money_log")->where(['uniacid'=>UNIACID,'type'=>2,'user_id'=>['in',$uids],'item_type'=>['in',[26,52,53,54]],'createtime'=>['>',$today_time]])->field("user_id,sum(money) as money")->group("user_id")->select();
            if(!empty($today_score_income_all)){
                foreach ($today_score_income_all as $val){
                    $today_score_incomes[$val['user_id']] = $val['money'];
                }
            }
            $total_score_income_all = Db::name("bbfx_money_log")->where(['uniacid'=>UNIACID,'type'=>2,'user_id'=>['in',$uids],'item_type'=>['in',[26,52,53,54]]])->field("user_id,sum(money) as money")->group("user_id")->select();
            if(!empty($total_score_income_all)){
                foreach ($total_score_income_all as $val){
                    $total_score_incomes[$val['user_id']] = $val['money'];
                }
            }
        }

        foreach ($list as &$val){
            if($base['fenxiao']['is_hide_team_mobile'] == 1){
                $val['mobile'] = secret($val['mobile'], true, 3, 4);
            }
            $val['is_transfer'] = 0;
            if($level == 1 && $base['double_track']['status'] == 1){
                if(!in_array($val['id'],$double_track_zhu_users) && $val['is_distributor'] == 1 && $val['distributor_status']){
                    $val['is_transfer'] = 1;
                }
            }

            $val['avatar'] = cdnurl($val['avatar']);
            $val['createtime'] = date("Y-m-d H:i:s",$val['createtime']);
            if(!empty($val['last_login_time'])){
                $val['last_login_time'] = date("Y-m-d H:i:s",$val['last_login_time']);
            }
            else{
                $val['last_login_time'] = '';
            }
            $val['become_distributor_time'] = date("Y-m-d H:i:s",$val['become_distributor_time']);
            $val['levelname'] = "普通用户";
            if($val['is_distributor'] == 1 && $val['distributor_status'] == 1){
                $val['levelname'] = $levels[$val['level']]['title'];
            }
            $alldown = FenxiaoUser::getAllDown($val['id']);
            $val['team_num'] = 0;
            if(!empty($alldown['users'])){
                $val['team_num'] = count($alldown['users']);
            }
            
            $uids = [];
            if(!empty($alldown)){
                $uids = $alldown['ids'];
            }
            if($base['fenxiao']['selfbuy_achievement'] == 1){
                $uids[] = $val['id'];
            }
            // $month_achievement = Db::name("bbfx_order")->where(['uniacid'=>UNIACID,'user_id'=>['in',$uids],'is_pay'=>1,'createtime'=>['>',$start_time],'refund_status'=>['in',[-1,2]]])->where(['createtime'=>['<',$end_time]])->sum("price");
            $month_achievement = Order::getAchievement(['o.user_id'=>['in',$uids],'o.createtime'=>['between',[$start_time,$end_time]]],[],$val['id']);
            $val['month_achievement'] = round($month_achievement,2);
            
            // $order_count = Db::name("bbfx_order")->where(['uniacid'=>UNIACID,'status'=>['>',0],'user_id'=>$val['id']])->count();
            // $level1_user = Db::name("bbfx_user")->where(['uniacid'=>UNIACID,'sup_uid'=>$val['id']])->count();
            // $baoming_num =  Db::name("bbfx_activity_baoming")->where(['uniacid'=>UNIACID,'user_id'=>$val['id'],'is_pay'=>1])->count();
            // $today_income = Db::name("bbfx_money_log")->where(['uniacid'=>UNIACID,'type'=>1,'user_id'=>$val['id'],'item_type'=>['in',[26,52,53,54]],'createtime'=>['>',$today_time]])->sum("money");
            // $total_income = Db::name("bbfx_money_log")->where(['uniacid'=>UNIACID,'type'=>1,'user_id'=>$val['id'],'item_type'=>['in',[26,52,53,54]]])->sum("money");
            // $today_score_income = Db::name("bbfx_money_log")->where(['uniacid'=>UNIACID,'type'=>2,'user_id'=>$val['id'],'item_type'=>['in',[26,52,53,54]],'createtime'=>['>',$today_time]])->sum("money");
            // $total_score_income = Db::name("bbfx_money_log")->where(['uniacid'=>UNIACID,'type'=>2,'user_id'=>$val['id'],'item_type'=>['in',[26,52,53,54]]])->sum("money");

            $val['baoming_num'] = intval($baoming_nums[$val['id']]);
            $val['order_count'] = intval($order_counts[$val['id']]);
            $val['level1_user'] = intval($level1_users[$val['id']]);
            $val['today_income'] = round($today_incomes[$val['id']],2);
            $val['total_income'] = round($total_incomes[$val['id']],2);
            $val['today_score_income'] = round($today_score_incomes[$val['id']],2);
            $val['total_score_income'] = round($total_score_incomes[$val['id']],2);
        }
        unset($val);
        
        __success("操作成功",["list"=>$list,"title"=>$title,'total'=>$total]);
    }

    // 大区业绩
    public function getDaquAchievementList(){
        $config = $this->model->getSet();
        $page = max(1,$this->param('page/d'));
        $type = $this->param('type/d');
        $limit = 5;
        $level1_user = Db::name("bbfx_user")->where(['uniacid'=>UNIACID,'sup_uid'=>$this->user['id']])->field("id")->select();
        $list = [];
        if(!empty($level1_user)){
            

            $daqu_user_id = 0;
            $daqu_achievement = 0;
            $daqu_uids = [];
            foreach ($level1_user as $key => &$val){
                $alldown = FenxiaoUser::getAllDown($val['id']);
                $achievement = 0;
                $ids = [-1];
                if(!empty($alldown)){
                    $ids = $alldown['ids'];
                }

                if($config['selfbuy_achievement'] == 1){
                    $ids[] = $val['id'];
                }
                $achievement = Order::getAchievement(['o.user_id'=>['in',$ids]],[]);

                if(empty($daqu_user_id)){
                    $daqu_user_id = $val['id'];
                    $daqu_achievement = $achievement;
                }
                else{
                    if($achievement > $daqu_achievement){
                        $daqu_user_id = $val['id'];
                        $daqu_achievement = $achievement;
                        $daqu_uids = $ids;
                    }
                }
                $val['ids'] = $ids;
            }
            unset($val);

            // $xiaoqu_ids = [];
            // foreach ($level1_user as $key => $val){
            //     if($val['id'] != $daqu_user_id){
            //         $xiaoqu_ids = array_merge($xiaoqu_ids,$val['ids']);
            //     }
            // }
            
            if($type == 1){
                $list = Db::name("bbfx_scoreshop_order")->where(['uniacid'=>UNIACID,'user_id'=>['in',$daqu_uids],'status'=>['in',[1,2,3]],'refund_status'=>-1])->field("id,order_no,user_id,price,score,createtime")->order("createtime desc")->page($page,$limit)->select();
            }
            else{
                $list = Db::name("bbfx_order")->where(['uniacid'=>UNIACID,'user_id'=>['in',$daqu_uids],'status'=>['in',[1,2,3]],'refund_status'=>-1])->field("id,order_no,user_id,price,createtime")->order("createtime desc")->page($page,$limit)->select();
            }

            $uids = array_column($list,'user_id');
            $users = Db::name("bbfx_user")->where(['uniacid'=>UNIACID,'id'=>['in',$uids]])->field("avatar,nickname,id")->select();
            $users = array_skey($users,"id");
            
            foreach ($list as $key => &$val){
                $val['avatar'] = cdnurl($users[$val['user_id']]['avatar']);
                $val['nickname'] = $users[$val['user_id']]['nickname'];
                if($type == 1){
                    $order_goods = Db::name("bbfx_scoreshop_order_goods")->where(['uniacid'=>UNIACID,'order_id'=>$val['id']])->field("id,goods_id,total,price,score,goods_info")->select();
                }
                else{
                    $order_goods = Db::name("bbfx_order_goods")->where(['uniacid'=>UNIACID,'order_id'=>$val['id']])->field("id,goods_id,total,price,goods_info")->select();
                }
                foreach ($order_goods as $k => &$v){
                    $goods = iunserializer($v['goods_info']);
                    $v['goods_info'] = ['title'=>$goods['title'],'thumb'=>cdnurl($goods['thumb'])];
                }
                unset($v);
                $val['order_goods'] = $order_goods;
                $val['createtime'] = date("Y-m-d H:i:s",$val['createtime']);
            }
            unset($val);
        }
        
        __success("success",["list"=>$list]);
    }

    // 小区业绩
    public function getXiaoquAchievementList(){
        $config = $this->model->getSet();
        $page = max(1,$this->param('page/d'));
        $type = $this->param('type/d');
        $limit = 5;
        $level1_user = Db::name("bbfx_user")->where(['uniacid'=>UNIACID,'sup_uid'=>$this->user['id']])->field("id")->select();
        $list = [];
        if(!empty($level1_user)){
            

            $daqu_user_id = 0;
            $daqu_achievement = 0;
            $daqu_uids = [];
            foreach ($level1_user as $key => &$val){
                $alldown = FenxiaoUser::getAllDown($val['id']);
                $achievement = 0;
                $ids = [-1];
                if(!empty($alldown)){
                    $ids = $alldown['ids'];
                }

                if($config['selfbuy_achievement'] == 1){
                    $ids[] = $val['id'];
                }
                $achievement = Order::getAchievement(['o.user_id'=>['in',$ids]],[]);

                if(empty($daqu_user_id)){
                    $daqu_user_id = $val['id'];
                    $daqu_achievement = $achievement;
                }
                else{
                    if($achievement > $daqu_achievement){
                        $daqu_user_id = $val['id'];
                        $daqu_achievement = $achievement;
                        $daqu_uids = $ids;
                    }
                }
                $val['ids'] = $ids;
            }
            unset($val);

            $xiaoqu_ids = [];
            foreach ($level1_user as $key => $val){
                if($val['id'] != $daqu_user_id){
                    $xiaoqu_ids = array_merge($xiaoqu_ids,$val['ids']);
                }
            }
            
            if($type == 1){
                $list = Db::name("bbfx_scoreshop_order")->where(['uniacid'=>UNIACID,'user_id'=>['in',$xiaoqu_ids],'status'=>['in',[1,2,3]],'refund_status'=>-1])->field("id,order_no,user_id,price,score,createtime")->order("createtime desc")->page($page,$limit)->select();
            }
            else{
                $list = Db::name("bbfx_order")->where(['uniacid'=>UNIACID,'user_id'=>['in',$xiaoqu_ids],'status'=>['in',[1,2,3]],'refund_status'=>-1])->field("id,order_no,user_id,price,createtime")->order("createtime desc")->page($page,$limit)->select();
            }

            $uids = array_column($list,'user_id');
            $users = Db::name("bbfx_user")->where(['uniacid'=>UNIACID,'id'=>['in',$uids]])->field("avatar,nickname,id")->select();
            $users = array_skey($users,"id");
            
            foreach ($list as $key => &$val){
                $val['avatar'] = cdnurl($users[$val['user_id']]['avatar']);
                $val['nickname'] = $users[$val['user_id']]['nickname'];
                if($type == 1){
                    $order_goods = Db::name("bbfx_scoreshop_order_goods")->where(['uniacid'=>UNIACID,'order_id'=>$val['id']])->field("id,goods_id,total,price,score,goods_info")->select();
                }
                else{
                    $order_goods = Db::name("bbfx_order_goods")->where(['uniacid'=>UNIACID,'order_id'=>$val['id']])->field("id,goods_id,total,price,goods_info")->select();
                }
                foreach ($order_goods as $k => &$v){
                    $goods = iunserializer($v['goods_info']);
                    $v['goods_info'] = ['title'=>$goods['title'],'thumb'=>cdnurl($goods['thumb'])];
                }
                unset($v);
                $val['order_goods'] = $order_goods;
                $val['createtime'] = date("Y-m-d H:i:s",$val['createtime']);
            }
            unset($val);
        }
        
        __success("success",["list"=>$list]);
    }
    
    // 总业绩
    public function getAchievementList(){
        $config = $this->model->getSet();
        $page = max(1,$this->param('page/d'));
        $type = $this->param('type/d');
        $limit = 5;
        $alldown = FenxiaoUser::getAllDown($this->user['id']);
        $list = [];
        if(!empty($alldown)){
            $users = $alldown['users'];
            $ids = $alldown['ids'];
            if(empty($users)) $users = [];
            if($config['selfbuy_achievement'] == 1){
                $users[] = $this->user['id'];
                $ids[] = $this->user['id'];
            }
            
            if($type == 1){
                $list = Db::name("bbfx_scoreshop_order")->where(['uniacid'=>UNIACID,'user_id'=>['in',$ids],'status'=>['in',[1,2,3]],'refund_status'=>-1])->field("id,order_no,user_id,price,score,createtime")->order("createtime desc")->page($page,$limit)->select();
            }
            else{
                $list = Db::name("bbfx_order")->where(['uniacid'=>UNIACID,'user_id'=>['in',$ids],'status'=>['in',[1,2,3]],'refund_status'=>-1])->field("id,order_no,user_id,price,createtime")->order("createtime desc")->page($page,$limit)->select();
            }

            foreach ($list as $key => &$val){
                $val['avatar'] = cdnurl($users[$val['user_id']]['avatar']);
                $val['nickname'] = $users[$val['user_id']]['nickname'];
                if($type == 1){
                    $order_goods = Db::name("bbfx_scoreshop_order_goods")->where(['uniacid'=>UNIACID,'order_id'=>$val['id']])->field("id,goods_id,total,price,score,goods_info")->select();
                }
                else{
                    $order_goods = Db::name("bbfx_order_goods")->where(['uniacid'=>UNIACID,'order_id'=>$val['id']])->field("id,goods_id,total,price,goods_info")->select();
                }
                foreach ($order_goods as $k => &$v){
                    $goods = iunserializer($v['goods_info']);
                    $v['goods_info'] = ['title'=>$goods['title'],'thumb'=>cdnurl($goods['thumb'])];
                }
                unset($v);
                $val['order_goods'] = $order_goods;
                $val['createtime'] = date("Y-m-d H:i:s",$val['createtime']);
            }
            unset($val);
        }
        
        
        __success("success",["list"=>$list]);
    }
    
}