<?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 Sumday
 * @package app\models\process
 */
class Sumday extends BaseModel
{
    /**
     * 数据表主键
     * @var string
     */
    protected $pk = 'id';
    /**
     * 模型名称
     * @var string
     */
    protected $name = 'sumday';

    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 $tableInfo
     * @return array
     */
    public static function addByTable(array $tableInfo): array
    {
        $day = date('Y-m-d',time());
        $insert = array(
            'game_id' => $tableInfo['game_id'],
            'game_name' => $tableInfo['game_name'],
            'table_id' => $tableInfo['id'],
            'table_name' => $tableInfo['table_name'],
            'create_time' => time(),
            'day' => $day,
            'start_time' => time()
        );
        $newSumDay = self::create($insert);
        return $newSumDay->toArray();
    }

    /**
     * TODO 日结
     * @param array $tableInfo
     * @return array
     */
    public static function resetBoot(array $tableInfo): array
    {
        $sumdayInfo = self::getByTableIdOrderByIdDesc($tableInfo);
        $time = time();
        $day = date('Y-m-d',$time);
        if($day == $sumdayInfo['day']){
            return ['status' => false, 'msg' => 'in_top_sumday'];
        }
        $bootInfo = Boot::getByTableIdOrderByIdDesc($tableInfo);
        $numberTabInfo = NumberTab::getByBootIdOrderByIdDesc($bootInfo);
        if (!$numberTabInfo){
            return ['status' => false, 'msg' => 'reset_boot_fail'];
        }
        if($numberTabInfo['bet_status'] != 0){
            return ['status' => false, 'msg' => 'reset_boot_false'];
        }
        Db::startTrans();
        try {
            //更新现在的sumday
            $updateLastSumdayRes = self::where(['id' => $sumdayInfo['id']])->update(['is_checkout' => 1, 'end_time' => $time]);
            $createSumdayRes = self::create([
                'game_id' => $tableInfo['game_id'],
                'game_name' => $tableInfo['game_name'],
                'table_id' => $tableInfo['id'],
                'table_name' => $tableInfo['table_name'],
                'create_time' => $time,
                'day' => $day,
                'start_time' => $time
            ]);
            $createSumdayRes = $createSumdayRes->toArray();
            $createBootRes = Boot::addBySumday($createSumdayRes);
            $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' => 'reset_boot_fail'];
        }

    }
}