<?php
/**
 * Created by PhpStorm.
 * User: Jonlin
 * Date: 2019/3/22
 * Time: 15:54
 */
namespace App\Sports\Controller;

use BaseController\Controller;
use \System\Model;

class AllRollingBall extends Controller{

    public function allzqleague(){
        $allzq  = lm("SoccerResult","Sports")
            ->join('st_zq_league','st_zq_result.cp_id','=','st_zq_league.id')
            ->select('st_zq_result.cp_id','st_zq_league.name_chinese')
            ->distinct('st_zq_result.cp_id')
            ->where('st_zq_result.status','1')
            ->get();
        for($i=0;$i<count($allzq);$i++){
            $allzqnum = count(lm("SoccerResult","Sports")->where('st_zq_result.status','1')->where('st_zq_result.cp_id',$allzq[$i]->cp_id)->get());
            $allzq[$i]->num = $allzqnum;
            $allzqmatch  = lm("SoccerResult","Sports")
                ->select('id','host_name','guest_name','match_time','match_process','tag','match_id')
                ->where('status','1')
                ->where('cp_id',$allzq[$i]->cp_id)
                ->distinct('cp_id')
                ->get();
            $allzq[$i]->list = $allzqmatch;
            $zqleague->name = '足球';
            $zqleague->match[$i]->cp_id = $allzq[$i]->cp_id;
            $zqleague->match[$i]->league_name = $allzq[$i]->name_chinese;
            $zqleague->match[$i]->num = $allzq[$i]->num;
            for($j=0;$j<count($allzqmatch);$j++){
                $zqleague->match[$i]->list[$j]->id = $allzqmatch[$j]->id;
                $zqleague->match[$i]->list[$j]->host_name = $allzqmatch[$j]->host_name;
                $zqleague->match[$i]->list[$j]->guest_name = $allzqmatch[$j]->guest_name;
                $zqleague->match[$i]->list[$j]->match_time = $allzqmatch[$j]->match_time;
                $zqleague->match[$i]->list[$j]->match_process = $allzqmatch[$j]->match_process;
                $zqleague->match[$i]->list[$j]->tag = $allzqmatch[$j]->tag;
                $zqodds = lm('St_zq_odds','Sports')
                    ->select('id','odds_code','odds','condition','sort')
                    ->where(function ($query) {
                        $query->where('odds_code', 'concede_home')
                            ->orWhere('odds_code', 'concede_guest')
                            ->orWhere('odds_code', 'size_home')
                            ->orWhere('odds_code', 'size_guest');
                    })
                    ->where('match_id',$allzqmatch[$j]->match_id)
                    ->get()->toArray();
                //根据 排序 获取 最新让球/大小玩法赔率
                $sort = array_column($zqodds,'sort');
                array_multisort($sort,SORT_DESC,$zqodds);
                $zqodds = array_slice($zqodds,0,4);
                for($a=0;$a<count($zqodds);$a++){
                    $zqleague->match[$i]->list[$j]->zqodds[$a] = $zqodds[$a];
                }
            }
        }
        Render($zqleague, '1', lang('Tips','Sports')->get('success'));
    }

    public function alllqleague(){
        $alllq  = lm("BasketballResult","Sports")
            ->join('st_lq_league','st_lq_result.cp_id','=','st_lq_league.id')
            ->select('st_lq_result.cp_id','st_lq_league.name_chinese')
            ->distinct('st_lq_result.cp_id')
            ->where('st_lq_result.status','1')
            ->get();
        for($i=0;$i<count($alllq);$i++){
            $alllqnum = count(lm("BasketballResult","Sports")->where('st_lq_result.status','1')->where('st_lq_result.cp_id',$alllq[$i]->cp_id)->get());
            $alllq[$i]->num = $alllqnum;
            $alllqmatch  = lm("BasketballResult","Sports")
                ->select('id','host_name','guest_name','match_score','match_time','match_process','tag','match_id')
                ->where('status','1')
                ->where('cp_id',$alllq[$i]->cp_id)
                ->distinct('cp_id')
                ->get();
            $alllq[$i]->list = $alllqmatch;
            $lqleague->name = '篮球';
            $lqleague->match[$i]->cp_id = $alllq[$i]->cp_id;
            $lqleague->match[$i]->league_name = $alllq[$i]->name_chinese;
            $lqleague->match[$i]->num = $alllq[$i]->num;
            for($j=0;$j<count($alllqmatch);$j++){
                $lqleague->match[$i]->list[$j]->id = $alllqmatch[$j]->id;
                $lqleague->match[$i]->list[$j]->host_name = $alllqmatch[$j]->host_name;
                $lqleague->match[$i]->list[$j]->guest_name = $alllqmatch[$j]->guest_name;
                $lqleague->match[$i]->list[$j]->match_score = $alllqmatch[$j]->match_score;
                $lqleague->match[$i]->list[$j]->match_time = $alllqmatch[$j]->match_time;
                $lqleague->match[$i]->list[$j]->match_process = $alllqmatch[$j]->match_process;
                $lqleague->match[$i]->list[$j]->tag = $alllqmatch[$j]->tag;
                $lqodds = lm('St_lq_odds','Sports')
                    ->select('id','odds_code','odds','condition','sort')
                    ->where(function ($query) {
                        $query->where('odds_code', 'concede_home')
                            ->orWhere('odds_code', 'concede_guest')
                            ->orWhere('odds_code', 'size_home')
                            ->orWhere('odds_code', 'size_guest');
                    })
                    ->where('match_id',$alllqmatch[$j]->match_id)
                    ->get()->toArray();
                //根据 排序 获取 最新让球/大小玩法赔率
                $sort = array_column($lqodds,'sort');
                array_multisort($sort,SORT_DESC,$lqodds);
                $lqodds = array_slice($lqodds,0,4);
                for($a=0;$a<count($lqodds);$a++){
                    $lqleague->match[$i]->list[$j]->lqodds[$a] = $lqodds[$a];
                }
            }
        }
        Render($lqleague, '1', lang('Tips','Sports')->get('success'));
    }

    /**
     * @throws \Exception
     */
    public function allwqleague(){
        $allwq  = lm("TennisResult","Sports")
            ->join('st_wq_league','st_wq_result.cp_id','=','st_wq_league.id')
            ->select('st_wq_result.cp_id','st_wq_league.name_chinese')
            ->distinct('st_wq_result.cp_id')
            ->where('st_wq_result.status','1')
            ->get();
        for($i=0;$i<count($allwq);$i++){
            $allwqnum = count(lm("TennisResult","Sports")->where('st_wq_result.status','1')->where('st_wq_result.cp_id',$allwq[$i]->cp_id)->get());
            $allwq[$i]->num = $allwqnum;
            $allwqmatch  = lm("TennisResult","Sports")
                ->select('id','host_player_name','guest_player_name','first_inning_score','second_inning_score','third_inning_score','match_time','match_process','tag','match_id')
                ->where('status','1')
                ->where('cp_id',$allwq[$i]->cp_id)
                ->distinct('cp_id')
                ->get();
            //$allwq[$i]->list = $allwqmatch;
            $wqleague->name = '网球';
            $wqleague->match[$i]->cp_id = $allwq[$i]->cp_id;
            $wqleague->match[$i]->league_name = $allwq[$i]->name_chinese;
            $wqleague->match[$i]->num = $allwq[$i]->num;
            for($j=0;$j<count($allwqmatch);$j++){
                $wqleague->match[$i]->list[$j]->id = $allwqmatch[$j]->id;
                $wqleague->match[$i]->list[$j]->host_player_name = $allwqmatch[$j]->host_player_name;
                $wqleague->match[$i]->list[$j]->guest_player_name = $allwqmatch[$j]->guest_player_name;
                $wqleague->match[$i]->list[$j]->first_inning_score = $allwqmatch[$j]->first_inning_score;
                $wqleague->match[$i]->list[$j]->second_inning_score = $allwqmatch[$j]->second_inning_score;
                $wqleague->match[$i]->list[$j]->third_inning_score = $allwqmatch[$j]->third_inning_score;
                $wqleague->match[$i]->list[$j]->match_time = $allwqmatch[$j]->match_time;
                $wqleague->match[$i]->list[$j]->match_process = $allwqmatch[$j]->match_process;
                $wqleague->match[$i]->list[$j]->tag = $allwqmatch[$j]->tag;
                $wqodds = lm('St_wq_odds','Sports')
                    ->select('id','odds_code','odds','condition','sort')
                    ->where(function ($query) {
                        $query->where('odds_code', 'concede_home')
                            ->orWhere('odds_code', 'concede_guest')
                            ->orWhere('odds_code', 'size_home')
                            ->orWhere('odds_code', 'size_guest');
                    })
                    ->where('match_id',$allwqmatch[$j]->match_id)
                    ->get()->toArray();
                //根据 排序 获取 最新让球/大小玩法赔率
                $sort = array_column($wqodds,'sort');
                array_multisort($sort,SORT_DESC,$wqodds);
                $wqodds = array_slice($wqodds,0,4);
                for($a=0;$a<count($wqodds);$a++){
                    $wqleague->match[$i]->list[$j]->wqodds[$a] = $wqodds[$a];
                }
            }
        }
        Render($wqleague, '1', lang('Tips','Sports')->get('success'));
    }

    public function allbqleague(){
        $allbq  = lm("BaseballResult","Sports")
            ->join('st_bq_league','st_bq_result.cp_id','=','st_bq_league.id')
            ->select('st_bq_result.cp_id','st_bq_league.name_chinese')
            ->distinct('st_bq_result.cp_id')
            ->where('st_bq_result.status','1')
            ->get();
        for($i=0;$i<count($allbq);$i++){
            $allbqnum = count(lm("BaseballResult","Sports")->where('st_bq_result.status','1')->where('st_bq_result.cp_id',$allbq[$i]->cp_id)->get());
            $allbq[$i]->num = $allbqnum;
            $allbqmatch  = lm("BaseballResult","Sports")
                ->select('id','host_name','guest_name','match_score','match_time','match_process','tag','match_id')
                ->where('status','1')
                ->where('cp_id',$allbq[$i]->cp_id)
                ->distinct('cp_id')
                ->get();
            $allbq[$i]->list = $allbqmatch;
            $bqleague->name = '棒球';
            $bqleague->match[$i]->cp_id = $allbq[$i]->cp_id;
            $bqleague->match[$i]->league_name = $allbq[$i]->name_chinese;
            $bqleague->match[$i]->num = $allbq[$i]->num;
            for($j=0;$j<count($allbqmatch);$j++){
                $bqleague->match[$i]->list[$j]->id = $allbqmatch[$j]->id;
                $bqleague->match[$i]->list[$j]->host_name = $allbqmatch[$j]->host_name;
                $bqleague->match[$i]->list[$j]->guest_name = $allbqmatch[$j]->guest_name;
                $bqleague->match[$i]->list[$j]->match_score = $allbqmatch[$j]->match_score;
                $bqleague->match[$i]->list[$j]->match_time = $allbqmatch[$j]->match_time;
                $bqleague->match[$i]->list[$j]->match_process = $allbqmatch[$j]->match_process;
                $bqleague->match[$i]->list[$j]->tag = $allbqmatch[$j]->tag;
                $bqodds = lm('St_bq_odds','Sports')
                    ->select('id','odds_code','odds','condition','sort')
                    ->where(function ($query) {
                        $query->where('odds_code', 'concede_home')
                            ->orWhere('odds_code', 'concede_guest')
                            ->orWhere('odds_code', 'size_home')
                            ->orWhere('odds_code', 'size_guest');
                    })
                    ->where('match_id',$allbqmatch[$j]->match_id)
                    ->get()->toArray();
                //根据 排序 获取 最新让球/大小玩法赔率
                $sort = array_column($bqodds,'sort');
                array_multisort($sort,SORT_DESC,$bqodds);
                $bqodds = array_slice($bqodds,0,4);
                for($a=0;$a<count($bqodds);$a++){
                    $bqleague->match[$i]->list[$j]->bqodds[$a] = $bqodds[$a];
                }
            }
        }
        Render($bqleague, '1', lang('Tips','Sports')->get('success'));
    }

}