<?php
namespace App\Sports\Controller;

use BaseController\Controller;
use Biz\Match\GetmatchData;
use Biz\Match\GetOddsData;

/**
 * 前台赛果数据接口
 * User: tank
 * Date: 2019/9/24
 */
class ResultMatch extends Controller{
    public function init() {
        $this->commonFunction =  C()->get('commonFunction');
        $this->getTypeData = new GetmatchData();
        $this->getOddsData = new GetOddsData();

    }

    /**
     * 获取各球类赛果数据
     */
    public function getMatchResult(){
        
        //获取球类代码
        $game_code = $_REQUEST['game_code'];
        //获取开赛日期
        $match_date = $_REQUEST['match_date'];
        try {
            if(empty($game_code) || empty($match_date)){
                throw new \Exception(Render([], '10001', lang('Tips','Sports')->get('PARAM_ERROR')));
            }
            //获取球类 model
            $getModels = $this->commonFunction->getModels($game_code);
            $model_result_express = $getModels['model_result_express'];

            //拼装查询条件
            $where = [
                ['match_date','=',$match_date]
            ];

            //获取赛果数据

            //获取联赛数据
            $match_result_league= lm($model_result_express,"Sports")
                ->select('league_id','league_name')
                ->where($where)
                ->groupBy('league_id','league_name')
                ->orderBy('league_id','asc')
                ->get()
                ->toArray();

            //获取各球类查询字段
            if($game_code == 'zq'){
                $select = ['league_id','league_name','match_date','match_time','home_team','guest_team','score_half','score_full','play_data','c_time','source','match_id'];
            }
            if($game_code == 'lq'){
                $select = ['league_id','league_name','match_date','match_time','home_team','guest_team','score_half','score_below','score_result','play_data','c_time','source','match_id'];
            }
            if($game_code == 'wq'){
                $select = ['league_id','league_name','match_date','match_time','home_team','guest_team','score_half','score_full','score_result','play_data','c_time','source','match_id'];
            }
            if($game_code == 'bq'){
                $select = ['league_id','league_name','match_date','match_time','home_team','guest_team','score_half','score_full','score_result','play_data','c_time','source','match_id'];
            }

            //获取赛事数据
            $match_result_match = lm($model_result_express,"Sports")
                ->select($select)
                ->where($where)
                ->get()
                ->toArray();

            //拼装数据
            $match_result_data = [];
            if(!empty($match_result_league) and !empty($match_result_match)){
                foreach($match_result_league as $k=>$v){
                    foreach($match_result_match as $kk=>$vv){
                        if($v['league_id'] == $vv['league_id']){
                            $vv['play_data'] = \json_decode($vv['play_data'],true);
                            $v['match_data'][] = $vv;
                        }
                    }

                    $match_result_data[$k] = $v;
                }
            }

            $data = [$match_result_league,$match_result_data];

            Render($data, '1', lang('Tips','Sports')->get('success'));
        } catch (\Exception $e) {
            echo $e->getMessage();
        }
    }


}