<?php


defined('BASEPATH') OR exit('No direct script access allowed');
/**
 * Created by liqx.
 * User: Administrator
 * Date: 2018/1/31
 * Time: 11:35
 */
class Notice extends MY_Controller
{
//['TH'=>"泰国",'DE'=>"德国",'BR'=>"巴西",'FR'=>"法国",'US'=>"美国",'IN'=>"印度",'RO'=>"罗马尼亚",'GB'=>"英国",'IT'=>"意大利",'ES'=>"西班牙",'BE'=>"比利时",'VN'=>"越南",'NO'=>"挪威"];
    public function __construct()
    {
        parent::__construct();
        $this->load->helper('url');
        $this->load->helper('cookie');
        $this->load->library('curl');
        $this->load->library('pagination');
        //请求地址
        $this->env = get_cookie($this->_wenenv);
        $this->_url = $this->config->item('officialUrl');

    }

    public function msgModel($type){
        $arr['buyVip'] = [
            "type"=>"buyVip",
            "content"=>[
                'ZH'=> '恭喜! <color=#fff220>{0}</color>成为了尊贵的<color=#5afffe>VIP</color>',
                'TW'=> '恭喜! <color=#fff220>{0}</color> 成為了尊貴的<color=#5afffe>VIP</color>',
                'EN'=> 'Congratulations! <color=#fff220>{0}</color> became the <color=#5afffe>VIP</color>!',
                'FR'=> 'Félicitations ! <color=#fff220>{0}</color> a devenu <color=#5afffe>VIP</color>!',
                'DE'=> 'Glückwunsch! <color=#fff220>{0}</color> wurde der <color=#5afffe>VIP</color>!',
            ],
            'conf'=>["status"=>1]
        ];
        $arr['getCard'] = [
            "type"=>"getCard",
            "content"=>[
                'ZH'=> '哇！<color=#fff220>{0}</color>获得了一张<color=#5afffe>小丑卡</color>！',
                'TW'=> '哇！<color=#fff220>{0}</color>獲得了一張<color=#5afffe>小丑卡</color>！',
                'EN'=> 'Amazing! <color=#fff220>{0}</color> got a <color=#5afffe>Joker Card</color>!',
                'FR'=> 'Parfait ! <color=#fff220>{0}</color> gagne une <color=#5afffe>Carte Joker</color> !',
                'DE'=> 'Toll! <color=#fff220>{0}</color> hat eine <color=#5afffe>Joker-Karte</color> bekommen!',
            ],
            'conf'=>["status"=>1]
        ];
        $arr['fulCardGroup'] = [
            "type"=>"fulCardGroup",
            "content"=>[
                'ZH'=> '恭喜！<color=#fff220>{0}</color>集齐了<color=#5afffe>{1}</color>卡组！',
                'TW'=> '恭喜！<color=#fff220>{0}</color>集齊了<color=#5afffe>{1}</color>卡組！',
                'EN'=> 'Congratulations! <color=#fff220>{0}</color> completed the <color=#5afffe>{1}</color> set!',
                'FR'=> 'Félicitations ! <color=#fff220>{0}</color> complète <color=#5afffe>{1}</color> set !',
                'DE'=> 'Glückwunsch! <color=#fff220>{0}</color> hat das Set <color=#5afffe>{1}</color> vervollständigt!',
            ],
            'conf'=>["status"=>1]
        ];
        $arr['getMaxSpins'] = [
            "type"=>"getMaxSpins",
            "content"=>[
                'ZH'=> '太棒了！<color=#fff220>{0}</color>在<color=#1fff2a>{2}</color>中获得了<color=#5afffe>{1}</color>体力！',
                'TW'=> '太棒了！<color=#fff220>{0}</color>在<color=#1fff2a>{2}</color>中獲得了<color=#5afffe>{1}</color>體力！',
                'EN'=> 'Awesome! <color=#fff220>{0}</color> won <color=#5afffe>{1}</color> spins in <color=#1fff2a>{2}</color>!',
                'FR'=> 'Bravo ! <color=#fff220>{0}</color> gagne <color=#5afffe>{1}</color> tours sur <color=#1fff2a>{2}</color>!',
                'DE'=> 'Bravo! <color=#fff220>{0}</color> hat <color=#5afffe>{1}</color> Drehungen im <color=#1fff2a>{2}</color> gewonnen!',
            ],
            'conf'=>["maxSpins"=>1000,"status"=>1]
        ];
        $arr['buyCrazyPass']=[
            "type"=>"buyCrazyPass",
            "content"=>[
                'ZH'=> '<color=#fff220>{0}</color> 购买了<color=#5afffe>疯狂通行证</color>',
                'TW'=> '<color=#fff220>{0}</color> 購買了<color=#5afffe>瘋狂通行證</color>',
                'EN'=> '<color=#fff220>{0}</color> purchased <color=#5afffe>Crazy Pass</color>',
                'FR'=> '<color=#fff220>{0}</color> a acheté le <color=#5afffe>Crazy Pass</color>',
                'DE'=> '<color=#fff220>{0}</color> hat <color=#5afffe>Verrückten Pass</color> gekauft',
            ],
            'conf'=>["status"=>1]
        ];
        return $arr[$type];
    }

    public function index(){
        foreach (['buyVip','getCard','fulCardGroup','getMaxSpins','buyCrazyPass'] as $type ){
            $this->setNoticeMsg($this->msgModel($type));
        }
        $list = $this->getAllNoticeMsg();
        echo "<pre>";
        var_dump($list);
//        $this->load->view('activity/index', $list);
    }

    /**
     * Notes:设置跑马灯消息配置
     * User: wsc
     * Time: 2021/1/20 15:28
     * @param $data
     * @return mixed
     */
    public function setNoticeMsg($data){
        $type = $data['type'];
        $tb = otable::noticeMsg();
        $saveData['type'] = $type;
        $saveData['conf'] = json_encode($data['conf'],256);
        $saveData['content'] = str_replace("\/", "/", json_encode($data['content'],256));
        if(!empty($this->getNoticeMsgByType($type))){
            unset($saveData['type']);
            $sql = funs::db_updateSQL($tb,$saveData," type='{$type}'");
        }else{
            $sql = funs::db_insertSQL($tb,$saveData);
        }

        $res = oo::commonOprDb('games')->query($sql);
//        echo ($sql);var_dump($res);echo "<br>";
        if($res){
            $this->getNoticeMsgByType($type,true);
        }
        return $res;
    }

    /**
     * Notes:通过类型获取跑马灯配置
     * User: wsc
     * Time: 2021/1/20 15:28
     * @param $type
     * @param bool $db
     * @return array|mixed
     */
    public function getNoticeMsgByType($type,$db=false){
        $key=okeys::noticeMsg();
        $info = oo::commonOprRedis('games')->hGet($key,$type);
        if(empty($info)||$db){
            $tb = otable::noticeMsg();
            $sql = "SELECT * FROM {$tb} where type ='{$type}'";
            $data = oo::commonOprDb('slavedb')->getOne($sql,1);
            if(!empty($data)){
                $arr['type'] = $data['type'];
                $arr['conf']=json_decode($data['conf'],true);
                $arr['content']=json_decode($data['content'],true);
                $info = str_replace("\/", "/", json_encode($arr,256));
                oo::commonOprRedis('games')->hSet($key,$type,$info);
            }
        }
        return !empty($info)?json_decode($info,true):[];
    }

    /**
     * Notes:获取所有跑马灯消息配置
     * User: wsc
     * Time: 2021/1/20 15:29
     * @param bool $db
     * @return array
     */
    public function getAllNoticeMsg($db=false){
        $key=okeys::noticeMsg();
        $list = oo::commonOprRedis('games')->hGetAll($key);
        if(empty($list)||$db){
            $tb = otable::noticeMsg();
            $sql = "SELECT * FROM {$tb}";
            $data = oo::commonOprDb('slavedb')->getAll($sql,1);
            if(!empty($data)){
                foreach ($data as $v){
                    $v['conf']=json_decode($v['conf'],true);
                    $v['content']=json_decode($v['content'],true);
                    $list[$v['type']] = str_replace("\/", "/", json_encode($v,256));
                }
                oo::commonOprRedis('games')->hMset($key,$list);
            }
        }
        $retData = !empty($list)?funs::getArrFromJsonArr($list):[];
        $filename = "NoticeMsg".time().".json";
        $res =$this->SaveJson($retData,$filename);
        if($res){
            oo::commonOprModel('json')->setStaticConfig("NoticeMsg",$filename);
        }
        return $retData;
    }


}