<?php


namespace app\services\bet;


use app\models\bet\Bet;
use app\models\process\NumberTab;
use app\models\user\User;

/**
 * TODO CancelBet处理类
 * Class CancelBetService
 * @package app\services\bet
 */
class CancelBetService
{
    /**
     * TODO 处理cancelBet
     * @param array $event
     * @param array $tableInfo
     * @return array
     */
    public static function cancelBet(array $event, array $tableInfo): array{
        $numberTabId = intval($event['number_tab_id']);
        $numberTabInfo = NumberTab::getByTableIdOrderByIdDesc($tableInfo);
        if (!$numberTabInfo || $numberTabId != $numberTabInfo['id']) return ['status' => false, 'msg' => 'cannot_cancel_bet'];
        $time = time();
        if ($numberTabInfo['bet_status'] != 1 || $time > ($numberTabInfo['bet_start_time'] + $tableInfo['wait_time'])) return ['status' => false, 'msg' => 'cannot_cancel_bet'];
        $userInfo = User::get(intval($event['user_id']));
        if (!$userInfo) return ['status' => false, 'msg' => 'cannot_cancel_bet'];
        $prevBetInfo = Bet::getPrevBetInfo($userInfo['id'],$numberTabInfo['id']);
        if(!$prevBetInfo) return ['status' => false, 'msg' => 'not_user_bet'];
        return Bet::cancelBet($tableInfo,$numberTabInfo,$userInfo,$prevBetInfo);

    }
}