<?php


namespace app\services\connect;


use app\models\table\Table;
use app\models\table\UserController;
use freedom\utils\SocketSession;
use think\Request;

/**
 * TODO 控制台登录服务
 * Class SpaceConnectService
 * @package app\services\connect
 */
class SpaceConnectService
{

    /**
     * TODO 控制台登录处理
     * @param Request $event
     * @return void
     */
    public static function doSpaceConnect(Request $event){
        /*
        $redis = Cache::store('redis')->handler();
        //设置哈希
        $redis->hset('123456','yzm','4566');
        //设置过期时间
        $redis->expire('123456',60);
        */
        $ws = app('\think\swoole\WebSocket');
        $tableId = intval($event->get('table_id'));
        $tableInfo = Table::get($tableId);
        $userId = intval($event->get('userid'));
        $loginToken = trim($event->get('login_token'));
        $username = trim($event->get('account'));
        $userController = UserController::get(['id' => $userId, 'username' => $username, 'login_token' => $loginToken]);
        if (!$tableInfo || !$userController){
            $ws->emit('onlineLogin',['status' => false, 'msg' => 'not_table_data']);
            //$ws->close();
            return;
        }
        $lastNumberTab = InitTableService::initTable($tableInfo);
        if($lastNumberTab){
            $round = array();
            $round['boot_id'] = $lastNumberTab['boot_id'];
            $round['boot_num'] = $lastNumberTab['boot_num'];
            $round['number_tab_id'] = $lastNumberTab['id'];
            $round['number_tab_number'] = $lastNumberTab['number'];
            $round['in_checkout'] = $tableInfo['in_checkout'];
            $round['number_tab_status'] = InitTableService::numberTabStatus($lastNumberTab);
            $round['show_card'] = GetCardService::getCard($tableInfo,$lastNumberTab);
            $ws->emit('onlineLogin',['status' => true, 'table_id' => $tableInfo['id'], 'round' => $round]);
            SocketSession::saveSocketSession(['table_id' => $tableInfo['id'], 'table_name' => $tableInfo['table_name']],'space');
        }else{
            $ws->emit('onlineLogin',['status' => false, 'table_id' => $tableInfo['id'], 'msg' => 'link_server_fail']);
            //$ws->close();
        }
    }
}