<?php

/**
 * 账单信息
 * @author solu
 */
class Offer extends Model {
    protected $tableName = 'offer';
    protected $dbKey = 'dw_baccarat';

    const STATUS_PLAYING = 0; // 进行中
    const STATUS_CLOSE = 1; // 已结算
    const STATUS_FINISH = 2; // 已转账

    const LIST_RANK = 1; // 巨额奖金
    const LIST_PLAYER = 2; // 我的下注
    const LIST_ALL = 3; // 全部下注

    const REDIS_PRE_LIST_CACHE = 'baccarat:list_cache:';
    const REDIS_LIST_CACHE_TTL = 600;
    const MAX_LIST_COUNT = 100;

    /**
     * 获取投注玩家
     * @param $gameId
     * @return array
     */
    public function getGamePlayer($gameId) {
        $players = [];
        $list = $this->objTable->getAll(['game_id' => $gameId, 'state' => [self::STATUS_PLAYING, self::STATUS_CLOSE]], ['_field' => 'player, transfer_trx_id']);
        foreach ($list as $v) {
            $players[$v['player']] = $v['transfer_trx_id'];
        }

        return $players;
    }

    /**
     * 下注记录
     * @param $gameId
     * @return array
     */
    public function getCurrentList($gameId) {
        $list = $this->objTable->getAll(['game_id' => $gameId], [
            '_field' => 'id, player, offerall_int, offertype, offeramount_int, transfer_trx_id, update_time_int',
            '_sortKey' => 'id desc',
        ]);

        foreach ($list as $k => $v) {
            $v['id'] = intval($v['id']);
            $v['offerall'] = intval($v['offerall_int']);
            $v['offeramount'] = $v['offeramount_int'];
            $v['update_time_int'] = intval($v['update_time_int'] / 1000000);

            unset($v['offerall_int'], $v['offeramount_int']);
            $list[$k] = $v;
        }

        return $list ?: [];
    }

    /**
     * 下注列表
     * @author solu
     * @param $type
     * @param int $offset
     * @param int $limit
     * @param string $player
     * @param bool $cache
     * @param null|int $nextId
     * @return array
     */
    public function getList($type, $offset = 0, $limit = 25, $player = '', $cache = false, $nextId = null) {
        if ($offset + $limit > self::MAX_LIST_COUNT) {
            return [];
        }

        if (null !== $nextId) {
            $_limit = $limit;
        } else {
            $_limit = "{$offset}, {$limit}";
        }
        $where = [
            '_field' => 'id, game_id, player, offerall_int, offertype, offeramount_int, win_int, state, transfer_trx_id, result_trx_id, create_time, update_time_int',
            '_limit' => $_limit,
//            '_debug' => true,
        ];

        switch ($type) {
            case self::LIST_RANK:
                $where['_field'] .= ', ( CAST(win_int AS SIGNED) - CAST(offerall_int AS SIGNED) ) as income';
                $where['state'] = self::STATUS_FINISH;
                $where['_sortKey'] = 'income DESC';
                break;
            case self::LIST_PLAYER:
                $where['player'] = $player;
                $where['_sortKey'] = 'id DESC';
                if (null !== $nextId) {
                    $where['_where'] = "id<{$nextId}";
                }
                break;
            case self::LIST_ALL:
                $where['_sortKey'] = 'id DESC';
                if (null !== $nextId) {
                    $where['_where'] = "id<{$nextId}";
                }
                break;
        }

        $objRedis = null;
        $key = '';
        if ($cache) {
            $objRedis = dwRedis::init(Eos::REDIS_SERV);
            $key = self::getListCacheKey($where);
            $json = $objRedis->get($key);
            if ($json) {
                return json_decode($json, true);
            }
        }

        $list = $this->objTable->getAll($where);

        $players = array_column($list, 'player');
        $gameIds = array_unique(array_column($list, 'game_id'));

        $objAccount = new Account();
        $objGame = new Game();
        $vipLevels = $objAccount->getVipLevels($players);
        $gameInfos = $objGame->getGameRevealInfo($gameIds);

        foreach ($list as $k => $v) {
            $type == self::LIST_RANK && $v['rank'] = ($offset * $limit) + $k + 1;
            $v['vip_level'] = $vipLevels[$v['player']] ?: 0;
            $v['update_time'] = round($v['update_time_int'] / 1000000);
            if (0 == $v['result_block_num']) {
                $v['result_block_num'] = '';
            }
            $v['game_info'] = $gameInfos[$v['game_id']] ?: null;
            $list[$k] = $v;
        }

        if ($cache) {
            $objRedis->setex($key, self::REDIS_LIST_CACHE_TTL, json_encode($list));
        }

        return $list ?: [];
    }

    private static function getListCacheKey($where) {
        $where = http_build_query($where);
        return self::REDIS_PRE_LIST_CACHE . md5($where);
    }
}