<?php


namespace app\services\opening;


use app\models\bet\Bet;
use app\models\process\NumberTab;
use app\models\user\User;
use app\services\connect\InitTableService;
use freedom\utils\CardPositionNn;
use freedom\utils\RedisUtil;
use freedom\utils\SocketSession;
use think\swoole\Websocket;

class OpeningNnService
{
    /**
     * TODO Dt开结果处理
     * @param array $event
     * @param array $tableInfo
     * @param Websocket $ws
     * @return void
     */
    public static function openingNn(array $event, array $tableInfo, Websocket $ws){
        //判断结果并推送结果
        $res = self::doOpening($event,$tableInfo);
        if ($res['status'] == false){
            $ws->emit('openingNn',['status' => false, 'msg' => $res['msg']]);
            return;
        }
        list($data,$lastNumberTabInfo,$newNumberTabInfo) = $res['data'];
        $round = [
            'boot_id' => $newNumberTabInfo['boot_id'],
            'boot_num' => $newNumberTabInfo['boot_num'],
            'number_tab_id' => $newNumberTabInfo['id'],
            'previous_number_tab_id' => $lastNumberTabInfo['id'],
            'number_tab_number' => $newNumberTabInfo['number'] + 1,
            'number_tab_status' => InitTableService::numberTabStatus($newNumberTabInfo)
        ];
        $round = array_merge($round,$data);
        $ws->emit('openingNn',['status' => true, 'table_id' => $tableInfo['id'], 'round' => $round]);
        $ws->to(SocketSession::HOUSE_NAME)->emit('openingNnResult',['status' => true, 'table_id' => $tableInfo['id'], 'round' => $round]);
        //处理注单
        self::doBet($ws,$tableInfo,$lastNumberTabInfo,$data);
    }
    /**
     * TODO 判断结果
     * @param array $event
     * @param array $tableInfo
     * @return array
     */
    public static function doOpening(array $event, array $tableInfo): array
    {
        if (!isset($event['number_tab_id'])) return ['status' => false, 'msg' => 'opening_fail_2'];
        $numberTabId = intval($event['number_tab_id']);
        $numberTabInfo = NumberTab::getByTableIdOrderByIdDesc($tableInfo);
        if (!$numberTabInfo || $numberTabId != $numberTabInfo['id']) return ['status' => false, 'msg' => 'opening_fail_2'];
        if ($numberTabInfo['bet_status'] != 2) return ['status' => false, 'msg' => 'opening_fail_4'];
        //NN只有扫描台,不是扫描台返回false
        if ($tableInfo['is_scavenging'] != 1) return ['status' => false, 'msg' => 'opening_fail'];
        $cardInfo = RedisUtil::getCard($numberTabId);
        if (!$cardInfo) return ['status' => false, 'msg' => 'opening_fail_6'];
        // 判断是否所有的牌都已经扫描
        foreach ($cardInfo AS $v){
            if ($v == 0){
                return ['status' => false, 'msg' => 'opening_fail_6'];
            }
        }
        // 比较结果
        $player1Card[0] = $cardInfo['player_1_card_1'];
        $player1Card[1] = $cardInfo['player_1_card_2'];
        $player1Card[2] = $cardInfo['player_1_card_3'];
        $player1Card[3] = $cardInfo['player_1_card_4'];
        $player1Card[4] = $cardInfo['player_1_card_5'];
        $player2Card[0] = $cardInfo['player_2_card_1'];
        $player2Card[1] = $cardInfo['player_2_card_2'];
        $player2Card[2] = $cardInfo['player_2_card_3'];
        $player2Card[3] = $cardInfo['player_2_card_4'];
        $player2Card[4] = $cardInfo['player_2_card_5'];
        $player3Card[0] = $cardInfo['player_3_card_1'];
        $player3Card[1] = $cardInfo['player_3_card_2'];
        $player3Card[2] = $cardInfo['player_3_card_3'];
        $player3Card[3] = $cardInfo['player_3_card_4'];
        $player3Card[4] = $cardInfo['player_3_card_5'];
        $bankerCard[0] = $cardInfo['banker_card_1'];
        $bankerCard[1] = $cardInfo['banker_card_2'];
        $bankerCard[2] = $cardInfo['banker_card_3'];
        $bankerCard[3] = $cardInfo['banker_card_4'];
        $bankerCard[4] = $cardInfo['banker_card_5'];
        $player1Result = CardPositionNn::JudgeCowCow($player1Card);
        $player2Result = CardPositionNn::JudgeCowCow($player2Card);
        $player3Result = CardPositionNn::JudgeCowCow($player3Card);
        $bankerResult = CardPositionNn::JudgeCowCow($bankerCard);

        $data = [];
        $data['result_player_1'] = $player1Result['cow'];
        $data['result_player_2'] = $player2Result['cow'];
        $data['result_player_3'] = $player3Result['cow'];
        $data['result_banker'] = $bankerResult['cow'];
        if($data['result_player_1'] > $data['result_banker']){
            $data['win_player_1'] = 1;
        }else if($data['result_player_1'] < $data['result_banker']){
            $data['win_player_1'] = 0;
        }else if($data['result_player_1'] == $data['result_banker']){
            $data['win_player_1'] = CardPositionNn::compareCard($player1Result['max'], $bankerResult['max']);
        }
        if($data['result_player_2'] > $data['result_banker']){
            $data['win_player_2'] = 1;
        }else if($data['result_player_2'] < $data['result_banker']){
            $data['win_player_2'] = 0;
        }else if($data['result_player_2'] == $data['result_banker']){
            $data['win_player_2'] = CardPositionNn::compareCard($player2Result['max'], $bankerResult['max']);
        }

        if($data['result_player_3'] > $data['result_banker']){
            $data['win_player_3'] = 1;
        }else if($data['result_player_3'] < $data['result_banker']){
            $data['win_player_3'] = 0;
        }else if($data['result_player_3'] == $data['result_banker']){
            $data['win_player_3'] = CardPositionNn::compareCard($player3Result['max'], $bankerResult['max']);
        }
        //开盘
        $numberTabUpdate = [
            'result_player_1' => $data['result_player_1'],
            'result_player_2' => $data['result_player_2'],
            'result_player_3' => $data['result_player_3'],
            'result_banker' => $data['result_banker'],
            'win_player_1' => $data['win_player_1'],
            'win_player_2' => $data['win_player_2'],
            'win_player_3' => $data['win_player_3'],
            'end_time' => time(),
            'bet_status' => 3,
        ];
        $newNumberTabInfo = NumberTab::next($numberTabInfo,$numberTabUpdate,$tableInfo);
        if ($newNumberTabInfo){
            $lastNumberTabInfo = array_merge($numberTabInfo,$numberTabUpdate);
            return ['status' => true, 'data' => [$data,$lastNumberTabInfo,$newNumberTabInfo]];
        }else{
            return ['status' => false, 'msg' => 'opening_fail'];
        }
    }

    /**
     * TODO Bet处理
     * @param Websocket $ws  桌子信息
     * @param array $tableInfo  桌子信息
     * @param array $numberTabInfo  开结果的当前局信息
     * @param array $data   输赢数据
     * @return void
     */
    public static function doBet(Websocket $ws, array $tableInfo, array $numberTabInfo, array $data){
        $bankerWinTotal = 0;
        if($numberTabInfo['rob_banker_id'] > 0 ){
            $betInfo = Bet::getByNumberTabIdNotRob($numberTabInfo['id'],$numberTabInfo['rob_banker_id']);
            $bankerBetInfo = Bet::getByNumberTabIdRob($numberTabInfo['id'],$numberTabInfo['rob_banker_id']);
        }else{
            $betInfo = Bet::getByNumberTabIdValid($numberTabInfo['id']);
        }
        foreach($betInfo AS $v){
            $userInfo = User::get(intval($v['user_id']));
            if (!$userInfo){
                continue;
            }
            $amount = $v['amount_player_1'] +
                $v['amount_player_1_times'] +
                $v['amount_player_1_banker'] +
                $v['amount_player_1_banker_times'] +
                $v['amount_player_2'] +
                $v['amount_player_2_times'] +
                $v['amount_player_2_banker'] +
                $v['amount_player_2_banker_times'] +
                $v['amount_player_3'] +
                $v['amount_player_3_times'] +
                $v['amount_player_3_banker'] +
                $v['amount_player_3_banker_times'];
            $withholdAmount = $v['withhold_player_1_times'] +
                $v['withhold_player_1_banker_times'] +
                $v['withhold_player_2_times'] +
                $v['withhold_player_2_banker_times'] +
                $v['withhold_player_3_times'] +
                $v['withhold_player_3_banker_times'];
            $winTotal = 0;
            $winTotalActual = 0;
            $timesPlayer1 = 1;
            $timesPlayer2 = 1;
            $timesPlayer3 = 1;
            $rebate = 0;
            // 双边洗码 （庄正闲负绝对值）
            $rebatePlayer1 = 0;
            $rebatePlayer2 = 0;
            $rebatePlayer3 = 0;
            //闲1
            if($data['win_player_1'] == 1){
                if($v['amount_player_1'] > 0){
                    $winTotal += round($v['amount_player_1'] * $userInfo['price_n0_n6'],2);
                    $winTotalActual += round($v['amount_player_1'] * 1,2);
                    $rebatePlayer1 -= $v['amount_player_1'];
                }
                if($v['amount_player_1_times'] > 0){
                    if(0 <= $data['result_player_1'] && $data['result_player_1'] < 7){
                        $winTotal += round($v['amount_player_1_times'] * $userInfo['price_n0_n6'],2);
                        $winTotalActual += round($v['amount_player_1_times'] * 1,2);
                    }elseif(7 <= $data['result_player_1'] && $data['result_player_1'] <= 9){
                        $winTotal += round($v['amount_player_1_times'] * $userInfo['price_n7_n9'],2) ;
                        $winTotalActual += round($v['amount_player_1_times'] * 2,2);
                        $timesPlayer1 = $userInfo['price_n7_n9'];
                    }elseif($data['result_player_1'] == 10){
                        $winTotal += round($v['amount_player_1_times'] * $userInfo['price_nn'],2);
                        $winTotalActual += round($v['amount_player_1_times'] *  3,2);
                        $timesPlayer1 = $userInfo['price_nn'];
                    }elseif($data['result_player_1'] == 11){
                        $winTotal += round($v['amount_player_1_times'] * $userInfo['price_5n'],2);
                        $winTotalActual += round($v['amount_player_1_times'] * 4,2);
                        $timesPlayer1 = $userInfo['price_5n'];
                    }elseif($data['result_player_1'] == 12){
                        $winTotal += round($v['amount_player_1_times'] * $userInfo['price_bomb'],2);
                        $winTotalActual += round($v['amount_player_1_times'] * 5,2);
                        $timesPlayer1 = $userInfo['price_bomb'];
                    }
                    $rebatePlayer1 -= $v['amount_player_1_times'];
                }
                if($v['amount_player_1_banker'] > 0){
                    $winTotal -= $v['amount_player_1_banker'];
                    $winTotalActual -= $v['amount_player_1_banker'];
                    $rebate += $v['amount_player_1_banker'];
                    $rebatePlayer1 += $v['amount_player_1_banker'];
                }
                if($v['amount_player_1_banker_times'] > 0){
                    if(0 <= $data['result_player_1'] && $data['result_player_1'] < 7){
                        $winTotal -= $v['amount_player_1_banker_times'];
                        $rebate += $v['amount_player_1_banker_times'];
                        $rebatePlayer1 += $v['amount_player_1_banker_times'];
                    }elseif(7 <= $data['result_player_1'] && $data['result_player_1'] <= 9){
                        $winTotal -= round($v['amount_player_1_banker_times'] * 2,2);
                        $rebate += round($v['amount_player_1_banker_times'] * 2);
                        $rebatePlayer1 += round($v['amount_player_1_banker_times'] * 2);
                    }elseif($data['result_player_1'] == 10){
                        $winTotal -= round($v['amount_player_1_banker_times'] * 3,2);
                        $rebate += round($v['amount_player_1_banker_times'] * 3);
                        $rebatePlayer1 += round($v['amount_player_1_banker_times'] * 3);
                    }elseif($data['result_player_1'] == 11){
                        $winTotal -= round($v['amount_player_1_banker_times'] * 4,2);
                        $rebate += round($v['amount_player_1_banker_times'] * 4);
                        $rebatePlayer1 += round($v['amount_player_1_banker_times'] * 4);
                    }elseif($data['result_player_1'] == 12){
                        $winTotal -= round($v['amount_player_1_banker_times'] * 5,2);
                        $rebate += round($v['amount_player_1_banker_times'] * 5);
                        $rebatePlayer1 += round($v['amount_player_1_banker_times'] * 5);
                    }
                }
            }elseif($data['win_player_1'] == 0){
                if($v['amount_player_1_banker'] > 0){
                    $winTotal += round($v['amount_player_1_banker'] * $userInfo['price_n0_n6'],2) ;
                    $rebatePlayer1 += $v['amount_player_1_banker'];
                }
                if($v['amount_player_1_banker_times'] > 0){
                    if(0 <= $data['result_banker'] && $data['result_banker'] < 7){
                        $winTotal += round($v['amount_player_1_banker_times'] * $userInfo['price_n0_n6'],2);
                    }elseif(7 <= $data['result_banker'] && $data['result_banker'] <= 9){
                        $winTotal += round($v['amount_player_1_banker_times'] * $userInfo['price_n7_n9'],2);
                        $timesPlayer1 = $userInfo['price_n7_n9'];
                    }elseif($data['result_banker'] == 10){
                        $winTotal += round($v['amount_player_1_banker_times'] * $userInfo['price_nn'],2);
                        $timesPlayer1 = $userInfo['price_nn'];
                    }elseif($data['result_banker'] == 11){
                        $winTotal += round($v['amount_player_1_banker_times'] * $userInfo['price_5n'],2);
                        $timesPlayer1 = $userInfo['price_5n'];
                    }elseif($data['result_banker'] == 12){
                        $winTotal += round($v['amount_player_1_banker_times'] * $userInfo['price_bomb'],2);
                        $timesPlayer1 = $userInfo['price_bomb'];
                    }
                    $rebatePlayer1 += $v['amount_player_1_banker_times'];
                }
                if($v['amount_player_1'] > 0){
                    $winTotal -= $v['amount_player_1'];
                    $rebate += $v['amount_player_1'];
                    $rebatePlayer1 -= $v['amount_player_1'];
                }
                if($v['amount_player_1_times'] > 0){
                    if(0 <= $data['result_banker'] && $data['result_banker'] < 7){
                        $winTotal -= $v['amount_player_1_times'];
                        $rebate += $v['amount_player_1_times'];
                        $rebatePlayer1 -= $v['amount_player_1_times'];
                    }elseif(7 <= $data['result_banker'] && $data['result_banker'] <= 9){
                        $winTotal -= $v['amount_player_1_times'] * 2;
                        $winTotalActual -= $v['amount_player_1_times'] * 2;
                        $rebate += $v['amount_player_1_times'] * 2;
                        $rebatePlayer1 -= $v['amount_player_1_times'] * 2;
                    }elseif($data['result_banker'] == 10){
                        $winTotal -= $v['amount_player_1_times'] * 3;
                        $winTotalActual -= $v['amount_player_1_times'] * 3;
                        $rebate += $v['amount_player_1_times'] * 3;
                        $rebatePlayer1 -= $v['amount_player_1_times'] * 3;
                    }elseif($data['result_banker'] == 11){
                        $winTotal -= $v['amount_player_1_times'] * 4;
                        $winTotalActual -= $v['amount_player_1_times'] * 4;
                        $rebate += $v['amount_player_1_times'] * 4;
                        $rebatePlayer1 -= $v['amount_player_1_times'] * 4;
                    }elseif($data['result_banker'] == 12){
                        $winTotal -= $v['amount_player_1_times'] * 5;
                        $winTotalActual -= $v['amount_player_1_times'] * 5;
                        $rebate += $v['amount_player_1_times'] * 5;
                        $rebatePlayer1 -= $v['amount_player_1_times'] * 5;
                    }
                }
            }
            //闲2
            if($data['win_player_2'] == 1){
                if($v['amount_player_2'] > 0){
                    $winTotal += round($v['amount_player_2'] * $userInfo['price_n0_n6'],2);
                    $winTotalActual += round($v['amount_player_2'] * 1,2);
                    $rebatePlayer2 -= $v['amount_player_2'];
                }
                if($v['amount_player_2_times'] > 0){
                    if(0 <= $data['result_player_2'] && $data['result_player_2'] < 7){
                        $winTotal += round($v['amount_player_2_times'] * $userInfo['price_n0_n6'],2);
                        $winTotalActual += round($v['amount_player_2_times'] * 1,2);
                    }elseif(7 <= $data['result_player_2'] && $data['result_player_2'] <= 9){
                        $winTotal += round($v['amount_player_2_times'] * $userInfo['price_n7_n9'],2) ;
                        $winTotalActual += round($v['amount_player_2_times'] * 2,2);
                        $timesPlayer2 = $userInfo['price_n7_n9'];
                    }elseif($data['result_player_2'] == 10){
                        $winTotal += round($v['amount_player_2_times'] * $userInfo['price_nn'],2);
                        $winTotalActual += round($v['amount_player_2_times'] *  3,2);
                        $timesPlayer2 = $userInfo['price_nn'];
                    }elseif($data['result_player_2'] == 11){
                        $winTotal += round($v['amount_player_2_times'] * $userInfo['price_5n'],2);
                        $winTotalActual += round($v['amount_player_2_times'] * 4,2);
                        $timesPlayer2 = $userInfo['price_5n'];
                    }elseif($data['result_player_2'] == 12){
                        $winTotal += round($v['amount_player_2_times'] * $userInfo['price_bomb'],2);
                        $winTotalActual += round($v['amount_player_2_times'] * 5,2);
                        $timesPlayer2 = $userInfo['price_bomb'];
                    }
                    $rebatePlayer2 -= $v['amount_player_2_times'];
                }
                if($v['amount_player_2_banker'] > 0){
                    $winTotal -= $v['amount_player_2_banker'];
                    $winTotalActual -= $v['amount_player_2_banker'];
                    $rebate += $v['amount_player_2_banker'];
                    $rebatePlayer2 += $v['amount_player_2_banker'];
                }
                if($v['amount_player_2_banker_times'] > 0){
                    if(0 <= $data['result_player_2'] && $data['result_player_2'] < 7){
                        $winTotal -= $v['amount_player_2_banker_times'];
                        $rebate += $v['amount_player_2_banker_times'];
                        $rebatePlayer2 += $v['amount_player_2_banker_times'];
                    }elseif(7 <= $data['result_player_2'] && $data['result_player_2'] <= 9){
                        $winTotal -= round($v['amount_player_2_banker_times'] * 2,2);
                        $rebate += round($v['amount_player_2_banker_times'] * 2);
                        $rebatePlayer2 += round($v['amount_player_2_banker_times'] * 2);
                    }elseif($data['result_player_2'] == 10){
                        $winTotal -= round($v['amount_player_2_banker_times'] * 3,2);
                        $rebate += round($v['amount_player_2_banker_times'] * 3);
                        $rebatePlayer2 += round($v['amount_player_2_banker_times'] * 3);
                    }elseif($data['result_player_2'] == 11){
                        $winTotal -= round($v['amount_player_2_banker_times'] * 4,2);
                        $rebate += round($v['amount_player_2_banker_times'] * 4);
                        $rebatePlayer2 += round($v['amount_player_2_banker_times'] * 4);
                    }elseif($data['result_player_2'] == 12){
                        $winTotal -= round($v['amount_player_2_banker_times'] * 5,2);
                        $rebate += round($v['amount_player_2_banker_times'] * 5);
                        $rebatePlayer2 += round($v['amount_player_2_banker_times'] * 5);
                    }
                }
            }elseif($data['win_player_2'] == 0){
                if($v['amount_player_2_banker'] > 0){
                    $winTotal += round($v['amount_player_2_banker'] * $userInfo['price_n0_n6'],2) ;
                    $rebatePlayer2 += $v['amount_player_2_banker'];
                }
                if($v['amount_player_2_banker_times'] > 0){
                    if(0 <= $data['result_banker'] && $data['result_banker'] < 7){
                        $winTotal += round($v['amount_player_2_banker_times'] * $userInfo['price_n0_n6'],2);
                    }elseif(7 <= $data['result_banker'] && $data['result_banker'] <= 9){
                        $winTotal += round($v['amount_player_2_banker_times'] * $userInfo['price_n7_n9'],2);
                        $timesPlayer2 = $userInfo['price_n7_n9'];
                    }elseif($data['result_banker'] == 10){
                        $winTotal += round($v['amount_player_2_banker_times'] * $userInfo['price_nn'],2);
                        $timesPlayer2 = $userInfo['price_nn'];
                    }elseif($data['result_banker'] == 11){
                        $winTotal += round($v['amount_player_2_banker_times'] * $userInfo['price_5n'],2);
                        $timesPlayer2 = $userInfo['price_5n'];
                    }elseif($data['result_banker'] == 12){
                        $winTotal += round($v['amount_player_2_banker_times'] * $userInfo['price_bomb'],2);
                        $timesPlayer2 = $userInfo['price_bomb'];
                    }
                    $rebatePlayer2 += $v['amount_player_2_banker_times'];
                }
                if($v['amount_player_2'] > 0){
                    $winTotal -= $v['amount_player_2'];
                    $rebate += $v['amount_player_2'];
                    $rebatePlayer2 -= $v['amount_player_2'];
                }
                if($v['amount_player_2_times'] > 0){
                    if(0 <= $data['result_banker'] && $data['result_banker'] < 7){
                        $winTotal -= $v['amount_player_2_times'];
                        $rebate += $v['amount_player_2_times'];
                        $rebatePlayer2 -= $v['amount_player_2_times'];
                    }elseif(7 <= $data['result_banker'] && $data['result_banker'] <= 9){
                        $winTotal -= $v['amount_player_2_times'] * 2;
                        $winTotalActual -= $v['amount_player_2_times'] * 2;
                        $rebate +=  $v['amount_player_2_times'] * 2;
                        $rebatePlayer2 -=  $v['amount_player_2_times'] * 2;
                    }elseif($data['result_banker'] == 10){
                        $winTotal -= $v['amount_player_2_times'] * 3;
                        $winTotalActual -= $v['amount_player_2_times'] * 3;
                        $rebate += $v['amount_player_2_times'] * 3;
                        $rebatePlayer2 -= $v['amount_player_2_times'] * 3;
                    }elseif($data['result_banker'] == 11){
                        $winTotal -= $v['amount_player_2_times'] * 4;
                        $winTotalActual -= $v['amount_player_2_times'] * 4;
                        $rebate += $v['amount_player_2_times'] * 4;
                        $rebatePlayer2 -= $v['amount_player_2_times'] * 4;
                    }elseif($data['result_banker'] == 12){
                        $winTotal -= $v['amount_player_2_times'] * 5;
                        $winTotalActual -= $v['amount_player_2_times'] * 5;
                        $rebate += $v['amount_player_2_times'] * 5;
                        $rebatePlayer2 -= $v['amount_player_2_times'] * 5;
                    }
                }
            }
            //闲3
            if($data['win_player_3'] == 1){
                if($v['amount_player_3'] > 0){
                    $winTotal += round($v['amount_player_3'] * $userInfo['price_n0_n6'],2);
                    $winTotalActual += round($v['amount_player_3'] * 1,2);
                    $rebatePlayer3 -= $v['amount_player_3'];
                }
                if($v['amount_player_3_times'] > 0){
                    if(0 <= $data['result_player_3'] && $data['result_player_3'] < 7){
                        $winTotal += round($v['amount_player_3_times'] * $userInfo['price_n0_n6'],2);
                        $winTotalActual += round($v['amount_player_3_times'] * 1,2);
                    }elseif(7 <= $data['result_player_3'] && $data['result_player_3'] <= 9){
                        $winTotal += round($v['amount_player_3_times'] * $userInfo['price_n7_n9'],2) ;
                        $winTotalActual += round($v['amount_player_3_times'] * 2,2);
                        $timesPlayer3 = $userInfo['price_n7_n9'];
                    }elseif($data['result_player_3'] == 10){
                        $winTotal += round($v['amount_player_3_times'] * $userInfo['price_nn'],2);
                        $winTotalActual += round($v['amount_player_3_times'] *  3,2);
                        $timesPlayer3 = $userInfo['price_nn'];
                    }elseif($data['result_player_3'] == 11){
                        $winTotal += round($v['amount_player_3_times'] * $userInfo['price_5n'],2);
                        $winTotalActual += round($v['amount_player_3_times'] * 4,2);
                        $timesPlayer3 = $userInfo['price_5n'];
                    }elseif($data['result_player_3'] == 12){
                        $winTotal += round($v['amount_player_3_times'] * $userInfo['price_bomb'],2);
                        $winTotalActual += round($v['amount_player_3_times'] * 5,2);
                        $timesPlayer3 = $userInfo['price_bomb'];
                    }
                    $rebatePlayer3 -= $v['amount_player_3_times'];
                }
                if($v['amount_player_3_banker'] > 0){
                    $winTotal -= $v['amount_player_3_banker'];
                    $winTotalActual -= $v['amount_player_3_banker'];
                    $rebate += $v['amount_player_3_banker'];
                    $rebatePlayer3 += $v['amount_player_3_banker'];
                }
                if($v['amount_player_3_banker_times'] > 0){
                    if(0 <= $data['result_player_3'] && $data['result_player_3'] < 7){
                        $winTotal -= $v['amount_player_3_banker_times'];
                        $rebate += $v['amount_player_3_banker_times'];
                        $rebatePlayer3 += $v['amount_player_3_banker_times'];
                    }elseif(7 <= $data['result_player_3'] && $data['result_player_3'] <= 9){
                        $winTotal -= round($v['amount_player_3_banker_times'] * 2,2);
                        $rebate += round($v['amount_player_3_banker_times'] * 2);
                        $rebatePlayer3 += round($v['amount_player_3_banker_times'] * 2);
                    }elseif($data['result_player_3'] == 10){
                        $winTotal -= round($v['amount_player_3_banker_times'] * 3,2);
                        $rebate += round($v['amount_player_3_banker_times'] * 3);
                        $rebatePlayer3 += round($v['amount_player_3_banker_times'] * 3);
                    }elseif($data['result_player_3'] == 11){
                        $winTotal -= round($v['amount_player_3_banker_times'] * 4,2);
                        $rebate += round($v['amount_player_3_banker_times'] * 4);
                        $rebatePlayer3 += round($v['amount_player_3_banker_times'] * 4);
                    }elseif($data['result_player_3'] == 12){
                        $winTotal -= round($v['amount_player_3_banker_times'] * 5,2);
                        $rebate += round($v['amount_player_3_banker_times'] * 5);
                        $rebatePlayer3 += round($v['amount_player_3_banker_times'] * 5);
                    }
                }

            }elseif($data['win_player_3'] == 0){
                if($v['amount_player_3_banker'] > 0){
                    $winTotal += round($v['amount_player_3_banker'] * $userInfo['price_n0_n6'],2) ;
                    $rebatePlayer3 += $v['amount_player_3_banker'];
                }
                if($v['amount_player_3_banker_times'] > 0){
                    if(0 <= $data['result_banker'] && $data['result_banker'] < 7){
                        $winTotal += round($v['amount_player_3_banker_times'] * $userInfo['price_n0_n6'],2);
                    }elseif(7 <= $data['result_banker'] && $data['result_banker'] <= 9){
                        $winTotal += round($v['amount_player_3_banker_times'] * $userInfo['price_n7_n9'],2);
                        $timesPlayer3 = $userInfo['price_n7_n9'];
                    }elseif($data['result_banker'] == 10){
                        $winTotal += round($v['amount_player_3_banker_times'] * $userInfo['price_nn'],2);
                        $timesPlayer3 = $userInfo['price_nn'];
                    }elseif($data['result_banker'] == 11){
                        $winTotal += round($v['amount_player_3_banker_times'] * $userInfo['price_5n'],2);
                        $timesPlayer3 = $userInfo['price_5n'];
                    }elseif($data['result_banker'] == 12){
                        $winTotal += round($v['amount_player_3_banker_times'] * $userInfo['price_bomb'],2);
                        $timesPlayer3 = $userInfo['price_bomb'];
                    }
                    $rebatePlayer3 += $v['amount_player_3_banker_times'];
                }
                if($v['amount_player_3'] > 0){
                    $winTotal -= $v['amount_player_3'];
                    $rebate += $v['amount_player_3'];
                    $rebatePlayer3 -= $v['amount_player_3'];
                }
                if($v['amount_player_3_times'] > 0){
                    if(0 <= $data['result_banker'] && $data['result_banker'] < 7){
                        $winTotal -= $v['amount_player_3_times'];
                        $rebate += $v['amount_player_3_times'];
                        $rebatePlayer3 -= $v['amount_player_3_times'];
                    }elseif(7 <= $data['result_banker'] && $data['result_banker'] <= 9){
                        $winTotal -= $v['amount_player_3_times'] * 2;
                        $winTotalActual -= $v['amount_player_3_times'] * 2;
                        $rebate += $v['amount_player_3_times'] * 2;
                        $rebatePlayer3 -= $v['amount_player_3_times'] * 2;
                    }elseif($data['result_banker'] == 10){
                        $winTotal -= $v['amount_player_3_times'] * 3;
                        $winTotalActual -= $v['amount_player_3_times'] * 3;
                        $rebate += $v['amount_player_3_times'] * 3;
                        $rebatePlayer3 -= $v['amount_player_3_times'] * 3;
                    }elseif($data['result_banker'] == 11){
                        $winTotal -= $v['amount_player_3_times'] * 4;
                        $winTotalActual -= $v['amount_player_3_times'] * 4;
                        $rebate += $v['amount_player_3_times'] * 4;
                        $rebatePlayer3 -= $v['amount_player_3_times'] * 4;
                    }elseif($data['result_banker'] == 12){
                        $winTotal -= $v['amount_player_3_times'] * 5;
                        $winTotalActual -= $v['amount_player_3_times'] * 5;
                        $rebate += $v['amount_player_3_times'] * 5;
                        $rebatePlayer3 -= $v['amount_player_3_times'] * 5;
                    }
                }
            }

            // 双边洗码
            if($userInfo['type_xima'] == 1){
                $rebate = abs($rebatePlayer1) + abs($rebatePlayer2) + abs($rebatePlayer3);
            }

            //计算庄家输赢
            if($numberTabInfo['rob_banker_username']){
                if($userInfo['is_sw'] == 0){
                    $bankerWinTotal = $winTotalActual + $bankerWinTotal;
                }
            }
            $v['position_first'] = $numberTabInfo['position_first'];
            $betInfoItem = array_merge($v,$data);
            $betInfoItem['times_player_1'] = $timesPlayer1;
            $betInfoItem['times_player_2'] = $timesPlayer2;
            $betInfoItem['times_player_3'] = $timesPlayer3;
            $res = Bet::openingBet($tableInfo,$betInfoItem,$userInfo,$numberTabInfo,$amount,$winTotal,$rebate,$withholdAmount);
            if ($res['status']){
                $tableUser = app('swoole.table.user');
                $userSession = $tableUser->get((string) $v['user_id']);
                if ($userSession && is_array($userSession)){
                    $ws->setSender(0)->to($userSession['fd'])->emit('opening',[
                        'status' => true,
                        'table_id' => $tableInfo['id'],
                        'round' => [
                            'money' => $res['money'],
                            'win_total' => $winTotal,
                            'previous_number_tab_id' => $v['number_tab_id']
                        ]
                    ]);
                }
            }
        }

        /**
         * 计算抢庄
         */
        if($numberTabInfo['rob_banker_id'] > 0){
            $userInfo = User::get(intval($numberTabInfo['rob_banker_id']));
            //更新user表余额
            $winTotal = to_number($bankerWinTotal);
            if($winTotal > 0){
                $winTotal = $winTotal * 0.94;
            }
            $amount = 0;
            $rebate = abs($bankerWinTotal);
            $bankerBetInfo = array_merge($bankerBetInfo,$data);
            $res = Bet::openingBet($tableInfo,$bankerBetInfo,$userInfo,$numberTabInfo,$amount,$winTotal,$rebate);
            if ($res['status']){
                $tableUser = app('swoole.table.user');
                $userSession = $tableUser->get((string) $userInfo['id']);
                if ($userSession && is_array($userSession)){
                    $ws->setSender(0)->to($userSession['fd'])->emit('opening',[
                        'status' => true,
                        'table_id' => $tableInfo['id'],
                        'round' => [
                            'money' => $res['money'],
                            'previous_number_tab_id' => $bankerBetInfo['number_tab_id']
                        ]
                    ]);
                }
            }
        }
    }
}