<?php

namespace App\Sports\Controller;

use BaseController\Controller;

/**
 * Class RollingData
 * @package App\Sports\Controller
 * User: tank
 * Date: 2019/3/22
 */
class  RollingData extends Controller {

    /**
     * @throws \Exception
     * 获取 滚球 足球相关数据
     */
    public function footbOver (){
        //===球类信息===
        $game = lm('GameType', 'Sports')->select('id','game_name','game_code','game_ico_url')->where(['status'=>1,'game_code'=>'zq'])->first();

        //赛事数据
        $ret = lm("st_zq_competition","Sports")
            ->join('st_zq_league','st_zq_league.id','=','st_zq_competition.lg_id')
            ->join('st_zq_country','st_zq_country.id','=','st_zq_league.country_id')
            ->join('st_area','st_area.id','=','st_zq_league.area_id')
            ->select('st_zq_competition.cp_id','st_zq_competition.lg_id','st_zq_league.name_chinese as leagueName','st_zq_country.name_chinese as countryName','st_area.title as areaName')
            ->where(['status'=>1])
            ->get()->toArray();


        //===按 地区-洲 分组===
        foreach ($ret as $k => $v) {
            $area[$v['areaName']]['areaName'] = $v['areaName'];
            $area[$v['areaName']][] = $v;
        }

        $areaData=array();
        foreach($area as $k=>$v){
//            var_dump($v);exit;

            $areaData[]=$v;
        }
//                var_dump($areaData);exit;

        //===end===

        //===按 国家 分组===
        foreach ($ret as $k => $v) {
            $country[$v['countryName']]['countryName'] = $v['countryName'];
            $country[$v['countryName']][] = $v;
        }

        $countryData=array();
        foreach($country as $k=>$v){
            $countryData[]=$v;
        }
        //===end===

        //===获取联赛下 赛事数量===

        $data = [
            'areaData' => $areaData,
            'countryData' => $countryData
        ];
        Render($data, '1', lang('Tips','Sports')->get('success'));

    }

    //测试接口

    public function matchData (){
        //===球类信息===
        $game = lm('GameType', 'Sports')->select('id','game_name','game_code','game_ico_url')->where(['status'=>1,'game_code'=>'zq'])->first();

        //赛事数据
        $ret = lm("st_zq_competition","Sports")
            ->join('st_zq_league','st_zq_league.id','=','st_zq_competition.lg_id')
            ->join('st_zq_country','st_zq_country.id','=','st_zq_league.country_id')
            ->join('st_area','st_area.id','=','st_zq_league.area_id')
            ->select('st_zq_competition.cp_id','st_zq_competition.lg_id','st_zq_league.name_chinese as leagueName','st_zq_country.name_chinese as countryName','st_area.title as areaName')
            ->where(['status'=>1])
            ->get()->toArray();


        //===按 地区-洲 分组===
        foreach ($ret as $k => $v) {
            $area[$v['areaName']]['areaName'] = $v['areaName'];
            $area[$v['areaName']][] = $v;
        }

        $areaData=array();
        foreach($area as $k=>$v){
//            var_dump($v);exit;

            $areaData[]=$v;
        }
//                var_dump($areaData);exit;

        //===end===

        //===按 国家 分组===
        foreach ($ret as $k => $v) {
            $country[$v['countryName']]['countryName'] = $v['countryName'];
            $country[$v['countryName']][] = $v;
        }

        $countryData=array();
        foreach($country as $k=>$v){
            $countryData[]=$v;
        }
        //===end===

        //===获取联赛下 赛事数量===

        $data = [
            'areaData' => $areaData,
            'countryData' => $countryData
        ];
        Render($data, '1', lang('Tips','Sports')->get('success'));

    }

    public function test(){
        $string = '
        {
	"gameType": "足球",
	"areaData": [{
			"areaName": "欧洲",
			"matchData": [{
					"matchName": "欧洲冠军杯",
					"matchNum": 4
				},
				{
					"matchName": "欧洲冠军杯",
					"matchNum": 4
				}, {
					"matchName": "欧洲冠军杯",
					"matchNum": 4
				}
			]
		},
		{
			"areaName": "亚洲",
			"matchData": [{
					"matchName": "欧洲冠军杯",
					"matchNum": 4
				},
				{
					"matchName": "欧洲冠军杯",
					"matchNum": 4
				}, {
					"matchName": "欧洲冠军杯",
					"matchNum": 4
				}
			]
		}
	],
	"countryData": [{
			"countryName": "英格兰",
			"matchData": [{
					"matchName": "欧洲冠军杯",
					"matchNum": 4
				},
				{
					"matchName": "欧洲冠军杯",
					"matchNum": 4
				}, {
					"matchName": "欧洲冠军杯",
					"matchNum": 4
				}
			]
		},
		{
			"countryName": "乌克兰",
			"matchData": [{
					"matchName": "欧洲冠军杯",
					"matchNum": 4
				},
				{
					"matchName": "欧洲冠军杯",
					"matchNum": 4
				}, {
					"matchName": "欧洲冠军杯",
					"matchNum": 4
				}
			]
		}
	]
}
        ';

        $data = json_decode($string,true);
        var_dump($data);
    }
}