<?php


namespace app\models\card;


use freedom\basic\BaseModel;
use freedom\traits\ModelTrait;

/**
 * TODO 百家乐牌数据
 * Class Card
 * @package app\models\table
 */
class Card extends BaseModel
{
    /**
     * 数据表主键
     * @var string
     */
    protected $pk = 'id';
    /**
     * 模型名称
     * @var string
     */
    protected $name = 'card';

    use ModelTrait;

    /**
     * 牌数据保存
     * @param int $numberTabId
     * @param array $cardInfo
     * @return bool
     */
    public static function saveAndUpdateCard(int $numberTabId, int $position, int $card):bool
    {
        $update = [];
        switch ($position){
            case 11:
                $update['player_1'] = $card;
                break;
            case 12:
                $update['player_2'] = $card;
                break;
            case 13:
                $update['player_3'] = $card;
                break;
            case 21:
                $update['banker_1'] = $card;
                break;
            case 22:
                $update['banker_2'] = $card;
                break;
            case 23:
                $update['banker_3'] = $card;
                break;
        }
        if (empty($update)) return false;
        $card = self::where(['number_tab_id' => $numberTabId])->find();
        if ($card){
            $res = self::where(['id' => $card['id']])->update($update);
        } else {
            $insert = [
                'number_tab_id' => $numberTabId,
                'create_time' => time()
            ];
            $insert = array_merge($insert, $update);
            $res = self::create($insert);
        }
        return (bool)$res;
    }

    /**
     * 获取卡牌
     * @param int $numberTabId
     * @return array
     */
    public static function getCard(int $numberTabId): array
    {
        $card = self::where(['number_tab_id' => $numberTabId])->field('banker_1,banker_2,banker_3,player_1,player_2,player_3')->find();
        if ($card) {
            return $card->toArray();
        } else {
            return [];
        }
    }
}