<?php

namespace App\Models;
use Illuminate\Support\Facades\DB;
/**
 *
 */
class NagentChild extends BaseModel {

	protected $table = 'nagent_child';
	public $timestamps = false;
	//获取下级信息
    function getChild($identity,$where,$list=50){
//        echo $status;
//        exit;
//        DB::connection()->enableQueryLog();
        $data=$this->rightJoin('account_detailed','account_detailed.account_identity',$this->table.'.account_identity')
            ->where('agent_identity',$identity);
        if(!empty($where)&&count($where)>0){
            $data->where($where);
        }
        $data=$data ->paginate($list);
//        $queries = DB::getQueryLog();
//        print_r($queries);
//            exit;
        if(!$data){
            return -8005444322;
        }
        return $data->toArray();
    }
    //获取代理信息
    //代理详情
    function getAgentChild($identity,$where,$list=50){

        $data=$this->rightJoin('nagent_detailed','nagent_detailed.nagent_detailed',$this->table.'.nagent_detailed')
            ->where('agent_identity',$identity);
        if(!empty($where)&&count($where)>0){
            $data->where($where);
        }
        $data=$data ->paginate($list);
        if(!$data){
            return -8005444322;
        }
        return $data->toArray();
    }
    //获取会员存取报表
    function getMoneyList($where){
        DB::connection()->enableQueryLog();
        $sql="SELECT SUM(money) as money,trade_type,money_type, agent_name,min(num) as num FROM money_details b 
RIGHT JOIN (
    SELECT a.agent_identity,num,a.account_identity,a.agent_name FROM nagent_child as a
    LEFT JOIN (
        SELECT agent_identity,COUNT(account_name) as num FROM nagent_child GROUP BY agent_identity 
    ) as d on d.agent_identity=a.agent_identity
) c on c.account_identity=b.account_identity";

        if(!empty($where)){
            $sql.=$where;
        }
        $sql.=" GROUP BY b.trade_type,b.money_type,agent_name";
        $data=DB::select($sql);
        // $queries = DB::getQueryLog();
        // print_r($queries);
        if (!$data) {
            return -8020052003; //没有
        }
        $data = json_encode($data);
        $data = json_decode($data,1);
        return $data;

    }
    //获取会员下级会员
    function getChildStr($agent_name){
        $data=$this->select('account_name')->where('agent_name',$agent_name)->get();
        $arr='';
        if(!$data){
            return $arr;
        }
        $data=$data->toArray();
        foreach ($data as $v){
            $arr.=$v['account_name'].',';
        }
        return $arr;
    }
    //获取下级用户名数组
    function getChildArray($value,$num){
        $key=$this->getfild($num);
        $data=$this->select('account_name')->where($key,$value)->get();
        $arr=array();
        if(!$data){
            return $arr;
        }
        $data=$data->toArray();
        foreach ($data as $v){
            $arr[]=$v['account_name'];
        }
        return $arr;
    }
    //获取
    function getfild($num){
        $arr=array(
            '1'=>'agent_identity',
            '2'=>'agent_name',
        );
        return $arr[$num];
    }
    //获取代理id键对下级人数的数组
    function getChildSumArray($agent_identity_arr){
        $return_arr=array();
        $data=$this->select(DB::raw('count(id) as sum_child,agent_identity'))
            ->whereIn('agent_identity',$agent_identity_arr)
            ->groupBy('agent_identity')
            ->get();
        if(!$data){
            return $return_arr;
        }
        $data=$data->toArray();
        foreach ($data as $k=>$v){
            $return_arr[$v['agent_identity']]=$v['sum_child'];
        }
        return $return_arr;
    }
}
