<?php


namespace app\models\process;


use app\models\table\Table;
use app\services\connect\InitTableService;
use freedom\basic\BaseModel;
use freedom\traits\ModelTrait;
use think\facade\Db;

/**
 * TODO 靴Model
 * Class Boot
 * @package app\models\process
 */
class Boot extends BaseModel
{
    /**
     * 数据表主键
     * @var string
     */
    protected $pk = 'id';
    /**
     * 模型名称
     * @var string
     */
    protected $name = 'boot';

    use ModelTrait;

    /**
     * TODO 根据桌子获取最后一靴
     * @param array $tableInfo
     * @return array
     */
    public static function getByTableIdOrderByIdDesc(array $tableInfo): array
    {
        $find = self::where(['table_id' => $tableInfo['id']])->order('id DESC')->find();
        if ($find) return $find->toArray();
        else return [];
    }

    /**
     * TODO 根据日结创建新一局
     * @param array $sumdayInfo
     * @return array
     */
    public static function addBySumday(array $sumdayInfo): array
    {
        $insert = array(
            'table_id' => $sumdayInfo['table_id'],
            'table_name' => $sumdayInfo['table_name'],
            'game_id' => $sumdayInfo['game_id'],
            'game_name' => $sumdayInfo['game_name'],
            'sumday_id' => $sumdayInfo['id'],
            'day' => $sumdayInfo['day'],
            'boot_num' => 1,
            'create_time' => time()
        );
        $newBoot = self::create($insert);
        return $newBoot->toArray();
    }

    /**
     * TODO 换靴
     * @param array $tableInfo
     * @return array
     */
    public static function changeBoot(array $tableInfo): array
    {
        $bootInfo = self::getByTableIdOrderByIdDesc($tableInfo);
        $numberTabInfo = NumberTab::getByBootIdOrderByIdDesc($bootInfo);
        if (!$numberTabInfo){
            return ['status' => false, 'msg' => 'change_boot_fail'];
        }
        if($numberTabInfo['bet_status'] != 0){
            return ['status' => false, 'msg' => 'change_boot_false'];
        }
        try {
            $insertBoot = [
                'table_id' => $bootInfo['table_id'],
                'table_name' => $bootInfo['table_name'],
                'game_id' => $bootInfo['game_id'],
                'game_name' => $bootInfo['game_name'],
                'sumday_id' => $bootInfo['sumday_id'],
                'day' => $bootInfo['day'],
                'boot_num' => $bootInfo['boot_num']+1,
                'create_time' => time()
            ];
            $createBootRes = self::create($insertBoot);
            $createBootRes = $createBootRes->toArray();
            $createNumberTabRes = NumberTab::addByBoot($createBootRes);
            WaybillRemind::where(['table_id' => $tableInfo['id']])->delete();
            if ($tableInfo['in_checkout'] > 0){
                Table::where(['id' => $tableInfo['id']])->update(['in_checkout' => 0]);
            }
            Db::commit();
            return [
                'status' => true,
                'data' => [
                    'boot_id' => $createBootRes['id'],
                    'boot_num' => $createBootRes['boot_num'],
                    'number_tab_id' => $createNumberTabRes['id'],
                    'number_tab_number' => $createNumberTabRes['number'],
                    'in_checkout' => 0,
                    'number_tab_status' => InitTableService::numberTabStatus($createNumberTabRes)
                ]
            ];
        } catch (\Exception $e) {
            Db::rollback();
            return ['status' => false, 'msg' => 'change_boot_fail'];
        }

    }
}