<?php
declare (strict_types = 1);

namespace app\listener\scan;

use app\models\table\Table;
use app\services\scan\ChangeNnService;
use freedom\utils\SocketSession;
use think\swoole\Websocket;

class NnChange
{
    /**
     * 事件监听处理
     *
     * @return void
     */
    public function handle(array $event, Websocket $ws)
    {
        if (!isset($event['table_id']) || empty(intval($event['table_id']))){
            $ws->emit('sendScanResult',['status' => false, 'msg' => "not_table_data"]);
        }
        $tableInfo = Table::get(intval($event['table_id']));
        if ($tableInfo['scanner_type'] == 1) {
            $serviceRes = ChangeNnService::doScanNnChange($event,$tableInfo);
        } else {
            $serviceRes = ChangeNnService::doScanNnSbChange($event,$tableInfo);
        }
        if ($serviceRes['status']){
            $ws->to(SocketSession::HOUSE_NAME)->emit('sendScanResult',['status' => true, 'table_id' => $tableInfo['id'], 'round' => $serviceRes['data'], 'isChanged' => true]);
        }else{
            $ws->emit('sendScanChangeNnResult',['status' => false, 'table_id' => $tableInfo['id'], 'msg' => $serviceRes['msg']]);
        }
    }
}
