<?php
/**
 *------Create thems Controller------
 *------SCWPHP  Version 1.0.0------
 *------Dev Model Jions------
 *------Create Time 2017-06-21 07:25:34------
 */

namespace App\Api\Controller;

//ini_set('display_errors', 1);

//error_reporting(E_ALL);
use Biz\Game\GameQuery;

define('SETTINGS_REMOTE_KEY', '14e1b600b1fd579f47433b88e8d85291');

class Settings extends BaseController {

    function init() {
    }


    function Query() {
        $game = isset($_GET['game']) && !empty($_GET['game']) ? $_GET['game'] : null;
        $playtype = isset($_GET['playtype']) && !empty($_GET['playtype']) ? $_GET['playtype'] : null;
        if (!$game) {
            Render('', -3216); //查询游戏设置，游戏名不能为空
        }
        $key = empty($playtype) ? $game : $game . $playtype;
        if (C()->get('cache')->has('odds-' . $key)) {
            $items = C()->get('cache')->get('odds-' . $key);

        } else {
            $cls = "\\Biz\\Game\\Settings\\" . ucfirst($game) . "Settings";
            $settings = new $cls();
            $items = $settings->get($playtype);
            // dump($items);
            if ($items < 0) {
                Render('', $items); //查询游戏设置，游戏名不能为空
            }
            C()->get('cache')->set('odds-' . $key, $items, 60);
        }

        Render($items);
    }


    //更新缓存
    public function odds() {

        $clear = isset($_REQUEST['clear']) && ($_REQUEST['clear'] == '-99999') ? 1 : 0;
//        $odds=C()->get('cache')->get('odds-list');
        $lastTime = C()->get('cache')->get('odds-lastUpdateTime');
        $times = $this->getUpdateTime();

        if (empty($lastTime) || $clear) {
            $this->_buildOdds();
            // header('location:/Cache/odds.json');
            //Render($odds,1,strtotime($lastTime) .'='.$times);

        }
        if (empty($times)) {
            $times = strtotime($lastTime);
        }
        if (strtotime($lastTime) < $times) {
            $this->_buildOdds();
            //返回所有最新赔率               
        }
        // header('location:/Cache/odds.json');
        $json = file_get_contents(ROOT_PATH . '/Cache/odds.json');
        file_put_contents(ROOT_PATH . '/Cache/odds2.json',$json);
        if (!$json) {
            $this->_buildOdds();
            $json = file_get_contents(ROOT_PATH . '/Cache/odds.json');
        }
        $data=json_decode($json,1);
        Render($data, 1, strtotime($lastTime) . '+' . $times);

    }


    /**
     * 得到系统更新的赔率修改时间
     *
     * @return void
     */
    private function getUpdateTime() {
        $time = lm('Setinfo', 'commons')->getType(1911);
        return strtotime($time);
    }

    /**
     * 生成赔率根据数据
     *
     * @param [type] $oddsList 存储赔率生成结果数组
     * @param [type] $game 游戏名称
     * @param [type] $table_data  指定游戏的数据表的赔率记录信息
     * @return void
     */
    private function _build_odds(&$oddsList, $game, $table_data) {
        $cls = "\\Biz\\Game\\Settings\\" . ucfirst($game) . "Settings";
        if (!class_exists($cls)) {
            return -1;
        }
        $settings = new $cls(0);
        $settings->setOddsSettings($table_data);
        $items = $settings->get('');
        if ($items < 0) {
            return -2;
        }
        $oddsList[] = array('game_type' => $game, 'odds' => $items);
    }

    private function _buildOdds() {
        //本地缓存和服务器比较
        $allOdds = lm('Game_odds', 'Commons')->get(['game_type', 'odds', 'update_time']);
        if (!$allOdds) {
            Render('', -7029); //赔率数据不存在
        }
        $oddsList = array();

        foreach ($allOdds as $odds_item) {
            $this->_build_odds($oddsList, $odds_item->game_type, $odds_item);
        }

        $time = date('Y-m-d H:i:s', time());
        C()->get('cache')->set('odds-lastUpdateTime', $time, 31536000); //最后更新时间
        C()->get('cache')->set('odds-list', $oddsList, 31536000);
        file_put_contents(ROOT_PATH . '/Cache/odds.json', json_encode($oddsList));
        return $oddsList;
    }
}
