<?php
defined('IN_WEB') or die('Include Error!');

/**
* 活动相关
*/
class Activity
{
    private $_securityKey = 'ErtLPB^P%u9X^iMM';//校验码

	/**
	*	获取活动列表
	*/
	public function getList($param)
	{
		$data = oo::commonOprModel('activity')->getList($param);
		return json_encode($data);
	}

    /**
     * 获取随机礼物
     * @param $param
     * @return string
     */
	public function getLuckyDraw($param) {
        $uid = oo::functions()->uint($param['uid']);
        if(empty($uid)) {
            return $this->response(-1, '参数错误');
        }

        $data = oo::commonOprModel('activity')->getLuckyDraw($param);
        return $data;
    }


    /**
     * 进行抽奖
     * @param $param
     * @return json|string
     */
    public function updateLuckyDraw($param)
    {
        oo::logs()->debug3(['param'=>json_encode($param)], 'Activity.php');
        $uid = oo::functions()->uint($param['uid']);
        $times = oo::functions()->uint($param['times']);
        if(empty($uid) || empty($times)) {
            return $this->response(-1, '参数错误');
        }

        if(!$this->verify($param)) {
            return $this->response(-2, '参数校验不通过');
        }

        $data = oo::commonOprModel('activity')->updateLuckyDraw($param);
        return $data;
    }

    /**
     * 世界杯列表获取
     * @param $param
     * @return string
     */
    public function getWorldCupList($param)
    {
        $uid = oo::functions()->uint($param['uid']);
        if(empty($uid)) {
            return $this->response2(-1, '参数错误');
        }
        $data = oo::commonOprModel('worldcup')->getList($uid);
        if(empty($data)) {
            return $this->response2(-1, '参数错误');
        }
        return $this->response2(1, '', $data);
    }

    /**
     * 下注
     * @param $param
     * @return int
     */
    public function worldCupbet($param)
    {
        $uid = oo::functions()->uint($param['uid']);
        $countryId = oo::functions()->uint($param['countryId']);
        $money = oo::functions()->uint($param['money']);
        $betNum = oo::functions()->uint($param['betNum']);
        if($uid < 1 || $countryId < 1 || $money < 1) {
            return json_encode(oo::res(-1));
        }
        $ret = oo::commonOprModel('worldcup')->bet($uid, $countryId, $money, $betNum);
        return json_encode(oo::res($ret));
    }


    /**
     * 世界杯领奖
     * @param $param
     * @return int|json
     */
    public function worldCupReward($param)
    {
        $uid = oo::functions()->uint($param['uid']);
        if(empty($uid)) {
            return $this->response2(-1, '参数错误');
        }
        $ret = intval(oo::commonOprModel('worldcup')->reward($uid));
        if($ret <= 0) {
            return $this->response2($ret, '领奖失败');
        }
        return $this->response2(1, '领奖成功',['money' => $ret]);
    }


    /**
     * 获取玩牌局数和领奖次数
     * @param $param
     * @return string
     */
    public function getPlayPokerNum($param)
    {
        $uid = oo::functions()->uint($param['uid']);
        $blind = oo::functions()->uint($param['blind']);
        if(empty($uid) || empty($blind)) {
            return $this->response2(-1, '参数错误');
        }
        $data = oo::commonOprModel('activity')->getPlayPokerNum($uid, $blind);
        if(empty($data)) {
            return $this->response2(-2, '获取失败');
        }
        return $this->response2(1, '', $data);
    }

    /**
     * 领取玩牌活动奖励
     * @param $param
     * @return array
     */
    public function playPokerRwd($param)
    {
        $uid = oo::functions()->uint($param['uid']);
        $flag = oo::functions()->uint($param['flag']);
        $blind = oo::functions()->uint($param['blind']);
        $ret = oo::commonOprModel('activity')->playPokerRwd($uid, $flag, $blind);
        if($ret < 0) {
            $data = ['code'=>$ret, 'money'=>0];
        }else {
            $data = ['code'=>1, 'money'=>$ret['money'] ?? 0];
        }
        return json_encode($data);
    }

    /**
     * 获取玩牌活动的配置
     * @return string
     */
    public function getPlayPokerConfig()
    {
        $activityConfig = oo::commonOprModel('activity')->playPokerConfig() ?? ['checkExpire'=>0, 'rewardArr'=>[]];
        $data = $activityConfig;
        return $this->response2(1, '', $data);
    }


    /**
     * 奥马哈领奖
     * @param $param
     * @return string
     */
    public function playOmahaRwd($param)
    {
        $uid = oo::functions()->uint($param['uid']);
        $playNum = oo::functions()->uint($param['playNum']);
        $ret = oo::commonOprModel('activity')->omahaRwd($uid, $playNum);
        if($ret < 0) {
            $data = ['code'=>$ret, 'speaker'=>0];
        }else {
            $data = ['code'=>1, $ret ?? ['speaker' => 0]];
        }
        return json_encode($data);

    }


    /**
     * 奥马哈玩牌喇叭兑换筹码
     * @param $param
     * @return string
     */
//    public function omahaExchange($param)
//    {
//        $uid = oo::functions()->uint($param['uid']);
//        $type = oo::functions()->uint($param['type']);
//        $ret = oo::commonOprModel('activity')->omahaExchange($uid, $type);
//
//
//        if($ret < 0) {
//            $msg = '';
//            if($ret == -3){
//                $msg = oo::getLang('act_exchange_errmsg') ?? oo::getLangByKey('act_exchange_errmsg', 'en');
//            }
//            $data = ['code'=>$ret, 'data' => [], 'msg'=>$msg];
//        }else {
//            $dataArr = [
//                [
//                    'rewardType' => 1, // 1 筹码 2钻石 3道具
//                    'rewardNum' => $ret['money'] ?? 0,
//                ],
//                [
//                    'rewardType' => 2, // 1 筹码 2钻石 3道具
//                    'rewardNum' => $ret['diamonds'] ?? 0,
//                ],
//                [
//                    'rewardType' => 3, // 1 筹码 2钻石 3道具
//                    'rewardNum' => $ret['props'] ?? 0,
//                ],
//            ];
//            $data = ['code'=>1, 'data'=>$dataArr, 'msg' => ''];
//        }
//        return json_encode($data);
//
//    }


    /**
     * 赠送好友筹码
     * @param $param
     * @return string
     */
    public function sendFriendMoney($param)
    {
        $data =['code' => -1, 'data' =>['toName' => '', 'shakeTimes' => 0] ] ;
        $uid = oo::functions()->uint($param['uid']);
        $fid = oo::functions()->uint($param['fid']);
        $money = oo::functions()->uint($param['money']);
        if($money < 70000){
            return json_encode($data);
        }

        $ret = oo::commonOprModel('activity')->sendMoneyToFriendInAct($uid, $fid, $money);

        return json_encode($ret);
    }


    /**
     * 获取赠送筹码的排行
     * @return string
     */
    public function getSendMoneyRank()
    {
        $data = [];
        $rankKey = okeys::userSendRankInAct();
        $ret = oo::commonOprRedis('common')->zRevRange($rankKey, 0, 20, 1);
        if(!empty($ret)) {
            $i = 0;
            foreach ($ret as $key => $money){
                $uidStr = $key;
                $uidList = explode('_', $uidStr);
                $uidFrom = $uidList[0];
                $uidTo = $uidList[1];
                $nickF = $nickT = '';
                if(!empty($uidFrom)){
                    $infoF = oo::commonOprModel('member')->getUserInfo($uidFrom);
                    $nickF = $infoF['nick'] ?? '';
                }
                if(!empty($uidTo)){
                    $infoT = oo::commonOprModel('member')->getUserInfo($uidTo);
                    $nickT = $infoT['nick'] ?? '';
                }

                $data[$i]['fromName'] = $nickF;
                $data[$i]['toName'] = $nickT;
                $data[$i]['sendChips'] = $money;
                $i++;
            }
        }
        return json_encode(['data'=>$data]);

    }




    /**
     * 校验合法性
     * @param $param
     * @return bool
     */
    private function verify($param)
    {
        $uid = oo::functions()->uint($param['uid']);
        $key = oo::functions()->escape($param['key']);
        $chips = oo::functions()->uint($param['chips']);
        $props = oo::functions()->uint($param['props']);

        $sign = md5($uid.$chips.$props.$this->_securityKey);
        oo::logs()->debug3('sign:'.$sign);
        return $sign == $key ? true : false;
    }

    /**
     * 统一输出返回json内容
     *
     * @access public
     * @date   2016-08-16
     * @param int    $ret  状态码,0成功
     * @param string $msg  错误信息
     * @param array  $data 返回数据内容
     * @return json;
     */
    public function response($ret = 0, $msg = '', array $data = array())
    {
        $result = [
            'ret'  => $ret,
            'msg'  => $msg,
            'data' => $data,
        ];

        return json_encode($result);
    }

    /**
     * 统一输出返回json内容
     *
     * @access public
     * @date   2016-08-16
     * @param int    $ret  状态码,0成功
     * @param string $msg  错误信息
     * @param array  $data 返回数据内容
     * @return json;
     */
    public function response2($ret = 0, $msg = '', array $data = array())
    {
        $result = [
            'code'  => $ret,
            'msg'  => $msg,
            'data' => $data,
        ];

        return json_encode($result);
    }
  
  	public function getSymbol($param){
        $uid = oo::functions()->uint($param['uid']);
        $ret = oo::commonOprModel('activity')->getSymbol($uid);
        return json_encode(['code'=>1,'data'=>$ret],JSON_NUMERIC_CHECK);
    }

    public function upSymbol($param){
        $uid = oo::functions()->uint($param['uid']);
        oo::commonOprModel('activity')->upSymbol($uid,$param['count']);
      	return json_encode(['code'=>1],JSON_NUMERIC_CHECK);
    }

    public function rewardSymbol($param){
        $uid = oo::functions()->uint($param['uid']);
        $ret = oo::commonOprModel('activity')->rewardSymbol($uid);
        return json_encode($ret,JSON_NUMERIC_CHECK);
    }
  
  	public function rewardSymbolArray($param){
        $uid = oo::functions()->uint($param['uid']);
        while (true){
            $ret = oo::commonOprModel('activity')->rewardSymbol($uid);
            if($ret['code'] == 1){
                $out[] = $ret;
            }else{
                $out[] = $ret;
                return json_encode($ret,JSON_NUMERIC_CHECK);
            }
        }
    }
  
  	public function activityData($param){
        $uid = oo::functions()->uint($param['uid']);
        $id = oo::functions()->uint($param['id']);
        $Activity = oo::commonOprRedis('common')->hGetAll(okeys::Activity());
        if(empty($Activity)){
            return [];
        }
        foreach ($Activity as $key=>$row){
            $temp = explode('#',$key);
            if($temp[0] == $id){
                $out = json_decode($row,true);
                $out['id'] = $id;
                $out['start_time'] = $temp[1] ;
                $out['end_time'] = $temp[2] ;
                $out['openlv'] = $temp[3] ;
                return oo::response(1,$out);
            }
        }
        return true;
    }

    /**
     * 加入排行榜活动房间
     * @param $param
     * @return string
     * Created by: Owen
     * Created on: 2020/3/6 15:07
     */
    public function addToRankRoom($param){
        $uid = oo::functions()->uint($param['uid']);
        if(empty($uid)){
            return oo::response(-1);
        }
        return oo::commonOprModel('activity')->addToRankRoom($uid);
    }

    /**
     * 当前排行榜数据
     * @param $param
     * @return string
     * Created by: Owen
     * Created on: 2020/3/6 15:19
     */
    public function getCurRankData($param){
        $uid = oo::functions()->uint($param['uid']);
        $activity13['rankData'] = oo::commonOprModel('activity')->getAllUserRankByUid($uid);
        return oo::response(1,$activity13,"",true);
    }
}
