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

namespace App\Lib\Settlement\Adapter;

class BqRule
{
    use RulePlus;

    public function concede($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 'concede_home':
                $return = $this->zq_letball($home - $guest, $condition, $home . '-' . $guest . '(全场)');
                break;
            case 'concede_guest':
                $return = $this->zq_letball($guest - $home, $condition, $home . '-' . $guest . '(全场)');
                break;
            case 'five_concede_guest':
                $return = $this->zq_letball($home_half - $guest_half, $condition, $home_half . '-' . $guest_half . '(半场)');
                break;
            case 'five_concede_home':
                $return = $this->zq_letball($home_half - $guest_half, $condition, $home_half . '-' . $guest_half . '(半场)');
                break;
            default:
                $return = $this->return_error;
                break;
        }
        return $return;
    }

    public function total_size($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 'total_size_big':
                $return = $this->zq_inball_bigsmall($home + $guest, $condition, 2, $home . '-' . $guest . '(全场)');
                break;
            case 'total_size_small':
                $return = $this->zq_inball_bigsmall($home + $guest, $condition, 1, $home . '-' . $guest . '(全场)');
                break;
            case 'five_total_size_big':
                $return = $this->zq_inball_bigsmall($home_half + $guest_half, $condition, 2, $home_half . '-' . $guest_half . '(半场)');
                break;
            case 'five_total_size_small':
                $return = $this->zq_inball_bigsmall($home_half + $guest_half, $condition, 1, $home_half . '-' . $guest_half . '(半场)');
                break;
            default:
                $return = $this->return_error;
                break;
        }
        return $return;
    }

    public function two_sides($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 'two_sides_single':
                $return = $this->zq_doublesing($home + $guest, $condition, $home . '-' . $guest . '(全场)');
                break;
            case 'two_sides_double':
                $return = $this->zq_doublesing($home + $guest, $condition, $home . '-' . $guest . '(全场)');
                break;
            default:
                $return = $this->return_error;
                break;
        }
        return $return;

    }

    public function capot($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 'capot_home':
                if ($home > $guest) {
                    $return['result'] = 1;
                } else {
                    $return['result'] = -1;
                }
                $return['matchResult'] = $home . '-' . $guest . '(全场)';

                break;
            case 'capot_guest':
                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)
    {
        $return = [];
        foreach ($recoreModesArray as $key => $val) {
            $nowsn = intval($val->match_process);
            $return[$nowsn] = $val;
        }

        if (count($return)!=9 || !empty(array_diff([1,2,3,4,5,6,7,8,9],array_keys($return)))){
            throw new  \Exception("棒球球结果不为9的数组，或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;
    }

}