<?php
/**
 * Created by PhpStorm.
 * User: Jun.peng
 * Date: 2019/5/13
 * Time: 10:19
 */
namespace App\Sports\Controller;
use BaseController\Controller;

class WriteSports extends Controller{
    public function init() {
        $this->commonFunction =  C()->get('commonFunction');

    }

    /**
     * 体育数据入库接口
     */
    public function setSports(){
        $obt = $_REQUEST['data'];

        $data = $this->getAddData($obt);

        //验证二维数组内是否有空值
//        foreach ($data['data'] as $v){
//            if(in_array('', $v)) Render([], '10001', lang('Tips','Sports')->get('PARAM_ERROR'));
//        }
        switch ($data['title']){
            case 'area'://地区
                $ret = $this->area($data['data']);
                break;
            case 'country'://国家
                $ret = $this->country($data['data']);
                break;
            case 'league'://联赛
                $ret = $this->league($data);
                break;
            case 'competition'://赛事
                $ret = $this->competition($data);
                break;
            case 'odds'://赔率
                $ret = $this->odds($data);
                break;
            case 'league_result'://联赛结果
                $ret = $this->league_result($data);
                break;
            case 'competition_result'://赛事结果
                $ret = $this->com_result($data);
                break;
            case 'competition_result_record'://赛事结果记录
                $ret = $this->com_result_record($data);
                break;
            case 'odds_record'://赔率记录
                $ret = $this->odds_record($data);
                break;
            default:
                Render([], '10007', lang('Tips','Sports')->get('abnormal'));
        }

        //写入成功
        if($ret) Render([], '1', lang('Tips','Sports')->get('success'));
        //写入失败
        Render([], '10010', lang('Tips','Sports')->get('HANDLE_ERRORS'));
    }

    //写入地区数据
    public function area($opt = []){
        $ret = lm('st_area','Sports')->insert($opt);
        return $ret;
    }

    //写入国家数据
    public function country($opt = []){
        $ret = lm('st_country','Sports')->insert($opt);
        return $ret;
    }

    //写入联赛数据
    public function league($opt = []){
        $game_code = $opt['game_code'];
        //根据球类代码获取相关model
        $model = $this->commonFunction->getModels($game_code,1);
        foreach ($opt['data'] as $k => $v){
            //验证联赛所属国家
            if(!empty($v['country_id'])){
                $country = lm('st_country','Sports')
                    ->where(['country_id'=>$v['country_id']])
                    ->count();
                if($country < 1) Render([], '10013', lang('Tips','Sports')->get('country_error'));
            }
            //验证联赛所属地区
            if(!empty($v['area_id'])){
                $area = lm('st_area','Sports')
                    ->where(['id'=>$v['area_id']])
                    ->count();
                if($area < 1) Render([], '10014', lang('Tips','Sports')->get('area_error'));
            }
            $post = lm($model['model_league'],'Sports')
                ->where(['lg_id'=>$v['lg_id']])
                ->count();
            //更新操作
            if($post > 0){
                $ret = lm($model['model_league'],'Sports')
                    -> where(['lg_id'=>$v['lg_id']])
                    -> update($v);
                if($ret < 1) Render([], '10011', lang('Tips','Sports')->get('update_error'));
            }else{
                //写入操作
                $ret = lm($model['model_league'],'Sports')->insert($v);
                if($ret != true) Render([], '10012', lang('Tips','Sports')->get('insert_error'));
            }
        }
        Render([], '1', lang('Tips','Sports')->get('success'));
    }

    //写入赛事数据
    public function competition($opt = []){
        $game_code = $opt['game_code'];
        //根据球类代码获取相关model
        $model = $this->commonFunction->getModels($game_code,1);
        foreach ($opt['data'] as $k => $v){
            //验证赛事所属联赛
            if(!empty($v['lg_id'])){
                $lg = lm($model['model_league'],'Sports')
                    ->where(['lg_id'=>$v['lg_id']])
                    ->count();
                if($lg < 1) Render([], '10015', lang('Tips','Sports')->get('league_error'));
            }
            $post = lm($model['model_match'],'Sports')
                ->where(['match_id'=>$v['match_id']])
                ->count();
            //更新操作
            if($post > 0){
                $ret = lm($model['model_match'],'Sports')
                    -> where(['match_id'=>$v['match_id']])
                    -> update($v);
                if($ret < 1) Render([], '10011', lang('Tips','Sports')->get('update_error'));
            }else{
                //写入操作
                $ret = lm($model['model_match'],'Sports')->insert($v);
                if($ret != true) Render([], '10012', lang('Tips','Sports')->get('insert_error'));
            }
        }
        Render([], '1', lang('Tips','Sports')->get('success'));
    }

    //写入赔率数据
    public function odds($opt){
        $game_code = $opt['game_code'];
        //根据球类代码获取相关model
        $model = $this->commonFunction->getModels($game_code,1);
        foreach ($opt['data'] as $k => $v){
            //验证赔率所属赛事
            if(!empty($v['match_id'])){
                $match = lm($model['model_match'],'Sports')
                    ->where(['match_id'=>$v['match_id']])
                    ->count();
                if($match < 1) Render([], '10016', lang('Tips','Sports')->get('match_error'));
            }
            //验证赔率所属联赛 冠军盘口
            if(!empty($v['lg_id'])){
                $lg = lm($model['model_league'],'Sports')
                    ->where(['lg_id'=>$v['lg_id']])
                    ->count();
                if($lg < 1) Render([], '10015', lang('Tips','Sports')->get('league_error'));
            }
            $post = lm($model['model_odds'],'Sports')
                ->where(['match_id'=>$v['match_id'],'odds_code'=>$v['odds_code'],'sort'=>$v['sort']])
                ->count();
            //更新操作
            if($post > 0){
                $ret = lm($model['model_odds'],'Sports')
                    ->where(['match_id'=>$v['match_id'],'odds_code'=>$v['odds_code'],'sort'=>$v['sort']])
                    -> update($v);
                if($ret < 1) Render([], '10011', lang('Tips','Sports')->get('update_error'));
            }else{
                //写入操作
                $ret = lm($model['model_odds'],'Sports')->insert($v);
                if($ret != true) Render([], '10012', lang('Tips','Sports')->get('insert_error'));
            }
        }
        Render([], '1', lang('Tips','Sports')->get('success'));
    }

    //写入联赛结果
    public function league_result($opt){
        $game_code = $opt['game_code'];
        //根据球类代码获取相关model
        $model = $this->commonFunction->getModels($game_code,1);
        foreach ($opt['data'] as $k => $v){
            //验证结果所属联赛
            if(!empty($v['lg_id'])){
                $lg = lm($model['model_league'],'Sports')
                    ->where(['lg_id'=>$v['lg_id']])
                    ->count();
                if($lg < 1) Render([], '10015', lang('Tips','Sports')->get('league_error'));
            }
            $post = lm($model['model_league_result'],'Sports')
                ->where(['lg_id'=>$v['lg_id'],'game_name'=>$v['game_name']])
                ->count();
            //更新操作
            if($post > 0){
                $ret = lm($model['model_league_result'],'Sports')
                    ->where(['lg_id'=>$v['lg_id'],'game_name'=>$v['game_name']])
                    -> update($v);
                if($ret < 1) Render([], '10011', lang('Tips','Sports')->get('update_error'));
            }else{
                //写入操作
                $ret = lm($model['model_league_result'],'Sports')->insert($v);
                if($ret != true) Render([], '10012', lang('Tips','Sports')->get('insert_error'));
            }
        }
        Render([], '1', lang('Tips','Sports')->get('success'));
    }

    //写入赛事结果
    public function com_result($opt){
        $game_code = $opt['game_code'];
        //根据球类代码获取相关model
        $model = $this->commonFunction->getModels($game_code,1);
        foreach ($opt['data'] as $k => $v){
            //验证结果所属联赛
            if(!empty($v['lg_id'])){
                $lg = lm($model['model_league'],'Sports')
                    ->where(['lg_id'=>$v['lg_id']])
                    ->count();
                if($lg < 1) Render([], '10015', lang('Tips','Sports')->get('league_error'));
            }
            //验证结果所属赛事
            if(!empty($v['match_id'])){
                $lg = lm($model['model_match'],'Sports')
                    ->where(['match_id'=>$v['match_id']])
                    ->count();
                if($lg < 1) Render([], '10016', lang('Tips','Sports')->get('match_error'));
            }
            $post = lm($model['model_result'],'Sports')
                ->where(['match_id'=>$v['match_id']])
                ->count();
            //更新操作
            if($post > 0){
                $ret = lm($model['model_result'],'Sports')
                    -> where(['match_id'=>$v['match_id']])
                    -> update($v);
                if($ret < 1) Render([], '10011', lang('Tips','Sports')->get('update_error'));
            }else{
                //写入操作
                $ret = lm($model['model_result'],'Sports')->insert($v);
                if($ret != true) Render([], '10012', lang('Tips','Sports')->get('insert_error'));
            }
        }
        Render([], '1', lang('Tips','Sports')->get('success'));
    }

    //写入赛事结果记录
    public function com_result_record($opt){
        $game_code = $opt['game_code'];
        //根据球类代码获取相关model
        $model = $this->commonFunction->getModels($game_code,1);
        foreach ($opt['data'] as $k => $v){
            //验证结果所属联赛
            if(!empty($v['lg_id'])){
                $lg = lm($model['model_league'],'Sports')
                    ->where(['lg_id'=>$v['lg_id']])
                    ->count();
                if($lg < 1) Render([], '10015', lang('Tips','Sports')->get('league_error'));
            }
            //验证结果所属赛事
            if(!empty($v['match_id'])){
                $lg = lm($model['model_match'],'Sports')
                    ->where(['match_id'=>$v['match_id']])
                    ->count();
                if($lg < 1) Render([], '10016', lang('Tips','Sports')->get('match_error'));
            }
            $post = lm($model['model_result_record'],'Sports')
                ->where(['match_id'=>$v['match_id'],'match_time'=>$v['match_time']])
                ->count();
            //更新操作
            if($post > 0){
                $ret = lm($model['model_result_record'],'Sports')
                    ->where(['match_id'=>$v['match_id'],'match_time'=>$v['match_time']])
                    -> update($v);
                if($ret < 1) Render([], '10011', lang('Tips','Sports')->get('update_error'));
            }else{
                //写入操作
                $ret = lm($model['model_result_record'],'Sports')->insert($v);
                if($ret != true) Render([], '10012', lang('Tips','Sports')->get('insert_error'));
            }
        }
        Render([], '1', lang('Tips','Sports')->get('success'));
    }

    //写入赔率记录
    public function odds_record($opt){
        $game_code = $opt['game_code'];
        //根据球类代码获取相关model
        $model = $this->commonFunction->getModels($game_code,1);
        foreach ($opt['data'] as $k => $v){
            //验证赔率所属赛事
            if(!empty($v['match_id'])){
                $match = lm($model['model_match'],'Sports')
                    ->where(['match_id'=>$v['match_id']])
                    ->count();
                if($match < 1) Render([], '10016', lang('Tips','Sports')->get('match_error'));
            }
            //验证赔率所属联赛 冠军盘口
            if(!empty($v['lg_id'])){
                $lg = lm($model['model_league'],'Sports')
                    ->where(['lg_id'=>$v['lg_id']])
                    ->count();
                if($lg < 1) Render([], '10015', lang('Tips','Sports')->get('league_error'));
            }
            $post = lm($model['model_odds_record'],'Sports')
                ->where(['match_id'=>$v['match_id'],'odds_code'=>$v['odds_code'],'sort'=>$v['sort']])
                ->count();
            //更新操作
            if($post > 0){
                $ret = lm($model['model_odds_record'],'Sports')
                    ->where(['match_id'=>$v['match_id'],'odds_code'=>$v['odds_code'],'sort'=>$v['sort']])
                    -> update($v);
                if($ret < 1) Render([], '10011', lang('Tips','Sports')->get('update_error'));
            }else{
                //写入操作
                $ret = lm($model['model_odds_record'],'Sports')->insert($v);
                if($ret != true) Render([], '10012', lang('Tips','Sports')->get('insert_error'));
            }
        }
        Render([], '1', lang('Tips','Sports')->get('success'));
    }

    /**
     * @param $data
     * @return mixed
     * json转数组
     */
    public function getAddData($data){
        $data = json_decode($data,true);
        return $data;
    }
}