<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019/4/25
 * Time: 14:10
 */

namespace App\Lib\Settlement\Adapter;

class BqRule
{
    use RulePlus;


    //让球
    public function CO($model, $resultModels, $resultRecords)
    {
        $stand_Datas = $this->bq_scoreDatas($resultModels);
        $odds_code = $model->odds_code;

        $home = $stand_Datas['all']['home'];
        $guest = $stand_Datas['all']['guest'];
        $home_half = $stand_Datas['half']['home'];
        $guest_half = $stand_Datas['half']['home'];
        $condition = $model->condition;

        switch ($odds_code) {
            case 'coh':
                $return = $this->zq_letball($home - $guest, $condition, $home . '-' . $guest . '(全场)');
                break;
            case 'cog':
                $return = $this->zq_letball($guest - $home, $condition, $home . '-' . $guest . '(全场)');
                break;
            case 'cog_5':
                $return = $this->zq_letball($home_half - $guest_half, $condition, $home_half . '-' . $guest_half . '(半场)');
                break;
            case 'coh_5':
                $return = $this->zq_letball($home_half - $guest_half, $condition, $home_half . '-' . $guest_half . '(半场)');
                break;
            default:
                $return = $this->return_he;
                break;
        }
        return $return;
    }

    //总得分大小
    public function TN($model, $resultModels, $resultRecords)
    {
        $stand_Datas = $this->bq_scoreDatas($resultModels);
        $odds_code = $model->odds_code;

        $home = $stand_Datas['all']['home'];
        $guest = $stand_Datas['all']['guest'];
        $home_half = $stand_Datas['half']['home'];
        $guest_half = $stand_Datas['half']['home'];
        $condition = $model->condition;

        switch ($odds_code) {
            case 'tnb':
                $return = $this->zq_inball_bigsmall($home + $guest, $condition, 2, $home . '-' . $guest . '(全场)');
                break;
            case 'tns':
                $return = $this->zq_inball_bigsmall($home + $guest, $condition, 1, $home . '-' . $guest . '(全场)');
                break;
            case 'tnb_5':
                $return = $this->zq_inball_bigsmall($home_half + $guest_half, $condition, 2, $home_half . '-' . $guest_half . '(半场)');
                break;
            case 'tns_5':
                $return = $this->zq_inball_bigsmall($home_half + $guest_half, $condition, 1, $home_half . '-' . $guest_half . '(半场)');
                break;
            default:
                $return = $this->return_he;
                break;
        }
        return $return;
    }


    //单双
    public function TS($model, $resultModels, $resultRecords)
    {
        $stand_Datas = $this->bq_scoreDatas($resultModels);
        $odds_code = $model->odds_code;

        $home = $stand_Datas['all']['home'];
        $guest = $stand_Datas['all']['guest'];
        $condition = $model->condition;

        switch ($odds_code) {
            case 'tss':
                $return = $this->zq_doublesing($home + $guest, $condition, $home . '-' . $guest . '(全场)');
                break;
            case 'tsd':
                $return = $this->zq_doublesing($home + $guest, $condition, $home . '-' . $guest . '(全场)');
                break;
            default:
                $return = $this->return_he;
                break;
        }
        return $return;

    }


    //独赢
    public function C($model, $resultModels, $resultRecords)
    {
        $stand_Datas = $this->bq_scoreDatas($resultModels);
        $odds_code = $model->odds_code;

        $home = $stand_Datas['all']['home'];
        $guest = $stand_Datas['all']['guest'];


        switch ($odds_code) {
            case 'ch':
                if ($home > $guest) {
                    $return['result'] = 1;
                } else {
                    $return['result'] = -1;
                }
                $return['matchResult'] = $home . '-' . $guest . '(全场)';

                break;
            case 'cg':
                if ($guest > $home) {
                    $return['result'] = 1;
                } else {
                    $return['result'] = -1;
                }
                $return['matchResult'] = $home . '-' . $guest . '(全场)';
                break;
            default:
                $return = $this->return_error;
                break;
        }
        return $return;
    }


    //棒球 type ==1 返回第$sn节   ==2返回上下节,sn=1上半场,sn=2下半场    type==4全部按节数的数组
    public function bq_getResultBySn($recoreModesArray, $type = 4, $sn = 1)
    {
        if (!isset($recoreModesArray['0']) || empty($recoreModesArray['0'])) {
            throw new  \Exception("网球结果不能为空");
            return;
        }
        $RecoedModel = $recoreModesArray['0'];

        $imatch_id = $RecoedModel->match_id;
        $all_datas = json_decode($RecoedModel->match_score_t, true);

        $return = [];
        foreach ($all_datas as $key => $val) {
            $tt = clone $RecoedModel;
            $tmpobj = json_decode(json_encode($tt, 256), true);
            $tmpobj['home_score'] = $all_datas[$key]['home'];
            $tmpobj['guest_score'] = $all_datas[$key]['guest'];
            $return[$key] = json_decode(json_encode($tmpobj, 256));
        }

        if (count($return) != 10 || !empty(array_diff([1, 2, 3, 4, 5, 6, 7, 8, 9, 'other'], array_keys($return)))) {
            throw new  \Exception("棒球球结果不为10的数组，或match_process有错");
        }

        if ($type == 4) {
            return $return;
        }

        if ($type == 1) {
            return $return[$sn];
        }

        if ($type == 2) {
            if ($sn == 1) {
                $ret = [
                    $return[1],
                    $return[2],
                ];
            } else {
                $ret = [
                    $return[3],
                    $return[4],
                ];
            }
        }
        return $ret;
    }

    //棒球比分数据汇总，以方便后面使用
    public function bq_scoreDatas($recoreModesArray)
    {
        $arr = $this->bq_getResultBySn($recoreModesArray);
        $return = [
            'list' => $arr,
            'all' => ['home' => 0, 'guest' => 0, 'home_rate' => 0, 'guest_rate' => 0],
            'half' => ['home' => 0, 'guest' => 0, 'home_rate' => 0, 'guest_rate' => 0],
            'half2' => ['home' => 0, 'guest' => 0, 'home_rate' => 0, 'guest_rate' => 0],
        ];

        foreach ($arr as $key => $val) {
            $tmp_home = intval($val->home_score);
            $tmp_guest = intval($val->guest_score);

            $return['all']['home'] += $tmp_home;
            $return['all']['guest'] += $tmp_guest;

            if (in_array($key, [1, 2, 3, 4, 5])) {
                $return['half']['home'] += $tmp_home;
                $return['half']['guest'] += $tmp_guest;
            } else {
                $return['half2']['home'] += $tmp_home;
                $return['half2']['guest'] += $tmp_guest;
            }
        }
        return $return;
    }

}