<?php


namespace app\services\process;


use app\models\process\NumberTab;
use app\services\connect\InitTableService;
use freedom\utils\SocketSession;

/**
 * TODO 作废
 * Class ResetNumberTabService
 * @package app\services\process
 */
class ResetNumberTabService
{
    /**
     * TODO 处理作废
     * @param array $tableInfo
     * @return void
     */
    public static function resetNumberTabService(array $tableInfo){
        $ws = app('\think\swoole\WebSocket');
        $numberTabInfo = NumberTab::getByTableIdOrderByIdDesc($tableInfo);
        if (!$numberTabInfo){
            $ws->emit('resetNumberTab', ['status' => false, 'msg' => 'not_number_tab_data']);
            return;
        }
        if($numberTabInfo['bet_status'] == 1 || $numberTabInfo['bet_status'] == 2 || ($numberTabInfo['bet_status'] == 0 || $numberTabInfo['rob_status'] == 1) || ($numberTabInfo['bet_status'] == 0 || $numberTabInfo['rob_status'] == 2)){
            //事务处理
            $res = NumberTab::resetNumberTab($numberTabInfo);
            if (!$res){
                $ws->emit('resetNumberTab', ['status' => false, 'msg' => 'reset_number_fail']);
                return;
            }
            $numberTabInfo['bet_status'] = 0;
            $ws->to(SocketSession::HOUSE_NAME)->emit('resetNumberTab',[
                'status' => true,
                'table_id' => $tableInfo['id'],
                'round' => [
                    'boot_id' => $numberTabInfo['boot_id'],
                    'boot_num' => $numberTabInfo['boot_num'],
                    'number_tab_id' => $numberTabInfo['id'],
                    'number_tab_number' => $numberTabInfo['number'],
                    'in_checkout' => $tableInfo['in_checkout'],
                    'number_tab_status' => InitTableService::numberTabStatus($numberTabInfo)
                ]
            ]);
        }else{
            $ws->emit('resetNumberTab', ['status' => false, 'msg' => 'reset_number_fail']);
        }
    }
}