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

namespace App\Lib\Settlement\Adapter;


class WqRule
{
    use RulePlus;

    //冠军
    public function kemp($model, $resultModel, $resultRecords)
    {
        $odds_code = $model->odds_code;
        $home_team = trim($model->home_team);
        $guest_team = trim($model->guest_team);

        if (in_array($odds_code, ['kemp_home', 'kemp_guest'])) {
            $resultModel = DB::table('st_wq_league_result')->where(['lg_id' => $model->lg_id, 'status' => 1])->first();
            $all_winner = trim($resultModel->result);
            if (!$resultModel) {
                throw new  \Exception("没有找到冠军数据-" . $model->lg_id);
            }
            if ($odds_code == 'kemp_home') {
                return $this->wq_gj_all($all_winner, $home_team);
            } else {
                return $this->wq_gj_all($all_winner, $guest_team);
            }
        }

        $stand_datas = $this->wq_scoreDatas($resultModel);
        switch ($odds_code) {
            case 'one_kemp_home':
                $return = $this->wq_gj_sine($home_team, $guest_team, $stand_datas['list']['1'], 1);
                break;
            case 'one_kemp_guest':
                $return = $this->wq_gj_sine($home_team, $guest_team, $stand_datas['list']['1'], 2);
                break;
            case 'two_kemp_home':
                $return = $this->wq_gj_sine($home_team, $guest_team, $stand_datas['list']['2'], 1);
                break;
            case 'two_kemp_guest':
                $return = $this->wq_gj_sine($home_team, $guest_team, $stand_datas['list']['2'], 2);
                break;
            default:
                $return = $this->return_error;
                break;
        }
        return $return;
    }

    private function wq_gj_all($winner, $team)
    {
        if ($winner == $team) {
            $return['result'] = 1;
        } else {
            $return['result'] = -1;
        }
        $return['matchResult'] = "($winner)";
        return $return;
    }

    //$homeguest=1 主队赢 ==2客队赢
    private function wq_gj_sine($home_name, $guest_name, $resultModel, $homeguest = 1)
    {
        $return['matchResult'] = ($homeguest == 1) ? $home_name : $guest_name;
        if ($homeguest == 1) {
            if ($resultModel->home_score > $resultModel->guest_score) {
                $return['result'] = 1;
            } else {
                $return['result'] = -1;
            }
        } else {
            if ($resultModel->guest_score > $resultModel->home_score) {
                $return['result'] = 1;
            } else {
                $return['result'] = -1;
            }
        }
        return $return;
    }


    //让盘
    public function dishes($model, $resultModel, $resultRecords)
    {
        $odds_code = $model->odds_code;
        $stand_datas = $this->wq_scoreDatas($resultModel);
        $condition = $model->condition;
        $home_score = $stand_datas['all']['home'];
        $guest_score = $stand_datas['all']['guest'];

        switch ($odds_code) {
            case 'dishes_home':
                $return = $this->zq_letball($home_score - $guest_score, $condition, $guest_score . '-' . $guest_score . ('总盘'));
                break;
            case 'dishes_guest':
                $return = $this->zq_letball($guest_score - $home_score, $condition, $guest_score . '-' . $guest_score . ('总盘'));
                break;
            default:
                $return = $this->return_error;
                break;
        }
        return $return;
    }

    //让局
    public function bureau($model, $resultModel, $resultRecords)
    {
        $odds_code = $model->odds_code;
        $condition = $model->condition;
        $stand_datas = $this->wq_scoreDatas($resultModel);
        $all_home = $stand_datas['allinning']['home'];
        $all_guest = $stand_datas['allinning']['guest'];

        switch ($odds_code) {
            case 'bureau_home':
                $return = $this->zq_letball($all_home - $all_guest, $condition, $all_home . '-' . $all_guest . ('总局数'));
                break;
            case 'bureau_guest':
                $return = $this->zq_letball($all_guest - $all_home, $condition, $all_home . '-' . $all_guest . ('总局数'));
                break;
            default:
                $return = $this->return_error;
        }
        return $return;
    }

    //总局数:大/小
    public function total_number($model, $resultModel, $resultRecords)
    {
        $odds_code = $model->odds_code;
        $condition = $model->condition;
        $stand_datas = $this->wq_scoreDatas($resultModel);
        $all_home = $stand_datas['allinning']['home'];
        $all_guest = $stand_datas['allinning']['guest'];
        $all = $all_home + $all_guest;

        $one_home = $stand_datas['oneinning']['home'];
        $one_guest = $stand_datas['oneinning']['guest'];
        $one_all = $one_home + $one_guest;

        switch ($odds_code) {
            case 'total_number_big':
                $return = $this->zq_inball_bigsmall($all, $condition, 2, $all . '(总局数)');
                break;
            case 'total_number_small':
                $return = $this->zq_inball_bigsmall($all, $condition, 1, $all . '(总局数)');
                break;
            case 'one_total_number_big':
                $return = $this->zq_inball_bigsmall($one_all, $condition, 2, $all . '(第1盘局数)');
                break;
            case 'one_total_number_small':
                $return = $this->zq_inball_bigsmall($one_all, $condition, 1, $all . '(第1盘局数)');
                break;
            default:
                $return = $this->return_error;
        }
        return $return;

    }

    //总局数:单/双
    public function two_game_sides($model, $resultModel, $resultRecords)
    {
        $odds_code = $model->odds_code;
        $condition = $model->condition;
        $stand_datas = $this->wq_scoreDatas($resultModel);
        $all_home = $stand_datas['allinning']['home'];
        $all_guest = $stand_datas['allinning']['guest'];
        $all = $all_home + $all_guest;

        switch ($odds_code) {
            case 'two_sides_single':
            case 'two_sides_double':
                $return = $this->zq_doublesing($all, $condition, $all . '(总局数)');
                break;
            default:
                $return = $this->return_error;
        }
        return $return;

    }

    //波胆
    public function bodan($model, $resultModel, $resultRecords)
    {
        $odds_code = $model->odds_code;
        $stand_datas = $this->wq_scoreDatas($resultModel);
        $all_home = $stand_datas['all']['home'];
        $all_guest = $stand_datas['all']['guest'];
        $word = $all_home . '-' . $all_guest . '(比分)';

        switch ($odds_code) {
            case 'three_bodan_home_two_zero':
                $return = $this->bodan_fun($all_home, $all_guest, '2-0', $word);
                break;
            case 'three_bodan_home_two_one':
                $return = $this->bodan_fun($all_home, $all_guest, '2-1', $word);
                break;
            case 'three_bodan_guest_zero_two':
                $return = $this->bodan_fun($all_home, $all_guest, '0-2', $word);
                break;
            case 'three_bodan_gueste_one_two':
                $return = $this->bodan_fun($all_home, $all_guest, '1-2', $word);
                break;
            default:
                $return = $this->return_error;
        }
        return $return;
    }

    public function bodan_fun($home, $guest, $type, $word)
    {
        $return['matchResult'] = $word;
        $return['result'] = 0;

        $tret = $home . '-' . $guest;
        if ($type === $tret) {
            $return['result'] = 1;
        } else {
            $return['result'] = -1;
        }
        return $return;
    }

    public function wq_getResultBySn($recoreModesArray, $type = 4, $sn = 1)
    {
        $return = [];
        foreach ($recoreModesArray as $key => $val) {
            $nowsn = intval($val->match_process);
            $return[$nowsn] = $val;
        }

        //网球只能为三盘或5盘
        if (count($return) != 3 || count($return) != 5) {
            throw new  \Exception("网球球结果不为3或5的数组，或match_process有错");
        } else {
            if (count($return) == 3 && !empty(array_diff([1, 2, 3], array_keys($return)))) {
                throw new  \Exception("网球球结果不为3的数组，或match_process有错");
            }
            if (count($return) == 5 && !empty(array_diff([1, 2, 3, 4, 5], array_keys($return)))) {
                throw new  \Exception("网球球结果不为5的数组，或match_process有错");
            }
        }

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

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

    }


    //网球比分数据汇总，以方便后面使用
    public function wq_scoreDatas($recoreModesArray)
    {
        $arr = $this->wq_getResultBySn($recoreModesArray);
        $return = [
            'list' => $arr,
            'all' => ['home' => 0, 'guest' => 0],         //总赢盘数统计
            'allinning' => ['home' => 0, 'guest' => 0],   //总赢局数统计
            'oneinning' => ['home' => 0, 'guest' => 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;

            $juinfo = $this->wq_JuInfo($val);
            $return['allinning']['home'] += $juinfo['all']['home'];
            $return['allinning']['guest'] += $juinfo['all']['guest'];

            if ($key == 1) {
                $return['oneinning']['home'] = $juinfo['all']['home'];
                $return['oneinning']['guest'] = $juinfo['all']['guest'];
            }
        }
        return $return;
    }


    //某盘  局的输赢统计
    public function wq_JuInfo($Resultobj)
    {
        $home = $Resultobj->inning->home;

        $hw = $gw = 0;
        foreach ($home as $val) {
            if ($val) {
                $hw++;
            } else {
                $gw++;
            }
        }
        if (($gw + $gw) < 6) {
            throw  new  \Exception('数据有误,每盘的局数不应小于6');
        }

        $return = [
            'list' => $Resultobj->inning,
            'all' => ['inning' => $hw + $gw, 'home' => $hw, 'guest' => $gw],
            'home' => $Resultobj->inning->home,
            'guest' => $Resultobj->inning->guest,
        ];
        return $return;
    }


}