<?php

namespace App\Models;
use Illuminate\Support\Facades\DB;

/**
 *
 */
class Agent_detailed extends BaseModel {

	protected $table = 'agent_detailed';
	public $timestamps = false;


	/**
	 * 代理列表数据
	 * [agent description]
	 * @param  [type] $page [description]
	 * @return [type]       [description]
	 */
	function agent($list,$page,$where='') {
	    $data=$this->select('id', 'agent_user', 'grade', 'start_time', 'phone','bank_number', 'email', 'type', 'status','agent_identity', 'state','child_bet','child_profit','sum_child','ratio','money_total')
            ->orderBy('start_time', 'desc');
		if(!empty($where)&&is_array($where)){
			$data ->where($where);
		}
			$data=$data->paginate($list);
		if (!$data < 0) {
			return -2021026003; //没有代理用户
		}
		return $data->toArray();
	}
    //查找代理id
    function findId($data){
        $datas = $this->select('id','agent_identity')->whereIn('agent_identity',$data)->get();
        if (!$datas < 0) {
            return -2021026003; //没有代理用户
        }
        return $datas->toArray();
    }

	/**
	 *代理审核
	 * [agency description]
	 * @param  [type] $page [description]
	 * @return [type]       [description]
	 */
	function agency($list,$page,$where='') {
		if(!empty($where)&&is_array($where)){
			$data = $this->select('id','agent_user', 'start_time', 'phone', 'email', 'status')
			->orderBy('start_time', 'desc')
            ->where('status', 0)
			->where($where)
			->paginate($list);
		}else{
			$data = $this->select('id','agent_user', 'start_time', 'phone', 'email', 'status')
			->orderBy('start_time', 'desc')
			->where('status', 0)
			->paginate($list);
		}

		if (!$data < 0) {
			return -2021026003; //没有代理用户
		}
		return $data->toArray();
	}
	//代理审批
	function passOrNo($id,$status){
	    if($status==1){
            $res=$this->where('id',$id)->update(['status'=>$status,'grade'=>1]);
        }else{
            $res=$this->where('id',$id)->delete();
        }
	    if(!$res){
            return -8005013322;
        }
        return 1;
    }


    //代理信息获取
    function agentInfo($id){
	    $data=$this->where('id',$id)->first();
	    if(!$data){
	        return -8005014322;
        }
        return $data->toArray();
    }
    //代理信息更新
    function updateInfo($data,$id){
        $res=$this->where('id',$id)->update($data);
        if(!$res){
            return -8005034322;
        }
        return 1;
    }
    //获取代理
    function getInfo($val,$num){
        $fild=$this->getFild($num);
        $data=$this->where($fild,$val)->first();
        if(!$data){
            return -8005014322;
        }
        return $data->toArray();
    }
    //获取字段
    function getFild($num){
        //
        $data=array(
            '1'=>'id',
            '2'=>'agent_user',
        );
        return $data[$num];
    }

    //代理统计非六合彩
    function totalSum($time,$where){
        $sql="select agent_name,total_money,grade,total_money_profit from agent_detailed f 
INNER JOIN(SELECT agent_name,sum(money) as total_money,SUM(get_money) as total_money_profit
FROM (
	SELECT SUM(money) as money,sum(get_money) as get_money,b.account_identity,min(c.agent_name) as agent_name FROM money_buy b 
RIGHT JOIN agent_child c on c.account_identity = b.account_identity  and  game_name != 'sixlottery'";
//        and  game_name != 'sixlottery'
        if(!empty($time)){
            $sql.=''.$time;
        }
    $sql.="GROUP BY b.account_identity) as d GROUP BY d.agent_name) e  ON e.agent_name=f.agent_user";

        if(!empty($where)){
            $sql.=' '.$where;
        }
        $data=DB::select($sql);
        if (!$data) {
            return -8020052003; //没有用户会员
        }
        $data = json_encode($data);
        $data = json_decode($data,1);

        return $data;
    }
    //代理统计六合彩
    function totalSumSix($time,$where){
        $sql="select agent_name,six_money,grade,six_money_profit from agent_detailed f 
INNER JOIN(SELECT agent_name,sum(money) as six_money,SUM(get_money) as six_money_profit
FROM (
	SELECT SUM(money) as money,sum(get_money) as get_money,b.account_identity,min(c.agent_name) as agent_name FROM money_buy b 
RIGHT JOIN agent_child c on c.account_identity=b.account_identity WHERE game_name = 'sixlottery'";
        if(!empty($time)){
            $sql.=' '.$time;
        }
        $sql.=" GROUP BY b.account_identity) as d GROUP BY d.agent_name) e  ON e.agent_name=f.agent_user";
        if(!empty($where)){
            $sql.=' '.$where;
        }
        $data=DB::select($sql);
        if (!$data) {
            return -8020052003; //没有
        }
        $data = json_encode($data);
        $data = json_decode($data,1);
        return $data;
    }
    protected function addMoneyCopy($agent_identity,$money){
        return $this->addMoney($agent_identity,$money);
    }
    //代理加钱
    function addMoney($agent_identity,$money){
        $res=$this->where('agent_identity',$agent_identity)->increment('money_total',$money);
        if(!$res){
            return -8005466322;
        }
        return 1;
    }
    //代理提成统计
    function sumMoney($time,$agent_name=''){

        $sql="select agent_name,money,get_money,grade from agent_detailed f 
INNER JOIN(SELECT agent_name,sum(money) as money,SUM(get_money) as get_money
FROM (
	SELECT SUM(money) as money,sum(get_money) as get_money,b.account_identity,min(c.agent_name) as agent_name FROM money_buy b 
RIGHT JOIN agent_child c on c.account_identity=b.account_identity and settle_status = 1 and game_status <> 0 and status <> 4";
        if(!empty($time)){
            $sql.=$time;
        }
//        if(!empty($agent_name)){
//            $sql.=' and agent_name ='.$agent_name;
//        }
        $sql.=" GROUP BY b.account_identity) as d GROUP BY d.agent_name) e  ON e.agent_name=f.agent_user";
//        echo $sql;die;
        $data=DB::select($sql);
        if (!$data) {
            return -8020052003; //没有
        }
        $data = json_encode($data);
        $data = json_decode($data,1);
        return $data;
    }

}

?>