<?php
defined( 'IN_WEB') or die( 'Include Error!');
/**
 * 老虎机
 */
class Slot {
    private  $response = ['code'=>-1,'msg'=>'','data'=>[]]; //响应返回
    /**
     * 客户端展示水果中奖倍数
     * @param unknown $param [uid,levelId,multiple] uid  关卡id  倍数
     * @return string
     */
    public function getShowSlot($param)
    {
        if(empty($param['uid']) || $param['uid'] <= 0 || empty($param['levelId'])  || empty($param['multiple'])){
            $this->response['flag'] = -1;
            $this->response['msg'] = '参数错误';
            return json_encode($this->response);
        }
        $getShowSlot = oo::commonOprModel('slot')->getShowSlot(intval($param['uid']),intval($param['levelId']),intval($param['multiple']));
        return json_encode($getShowSlot);
    }
    /**
     * 老虎机上面的大转盘
         * uid当前用户uid，type【2是免费 3是收費 4是視頻 5Fb 6VIP】
     */
    public function slotTurntable($param){
        if(empty($param['uid']) || $param['uid'] <= 0 || empty($param['type'])){
            $this->response['flag'] = -1;
            $this->response['msg'] = '参数错误';
            return json_encode($this->response);
        }
        return self::actionTurntable1_2_7($param);
    }

    public function actionTurnTable1_2_7($param){
        $uid  = oo::functions()->uint( $param['uid']);
        $type = oo::functions()->uint( $param['type']);
        $aid = oo::functions()->uint( $param['aid']);
        if($type == 4 || $type == 5 || $type == 6){
            $time = oo::functions()->uint($param['time']);
            $sign = oo::functions()->escape($param['sign']);
            $serverSign = "{$uid}#2019$"."SUPERANT{$time}&^%";
            if($sign != md5($serverSign) && ($type == 4 || $type == 5 || $type == 6)){
                $this->response['flag'] = -1;
                $this->response['msg'] = '参数错误';
                return json_encode($this->response);
            }
            if($type == 4){
                $ret = oo::commonOprModel('advertisement')->getRemainCount($uid,$aid);
                if(empty($ret)){
                    return oo::response(-4,[],'广告未开启');
                }
                if($ret['remain']<=0){
                    return oo::response(-5,[],'次数不足');
                }
            }else if($type == 5){
                if(oo::commonOprRedis('common')->hGet(okeys::fbTurnCount(),$uid)){
                    return oo::response(-5,[],'次数不足');
                }
            }else if($type == 6){
                $temp = oo::commonOprModel('newvip')->checkVip($uid);
                if(!$temp || $temp['turnTableStatus'] == 1){
                    return oo::response(-5,[],'次数不足');
                }
            }

            oo::commonOprRedis('common')->delete(okeys::bigwheel($uid));
            if($type == 6){
                $type = 2;
                $data = oo::commonOprModel('slot')->slotTurntable($uid,$type,true);
                if($data['code'] == 1){
                    oo::commonOprRedis('common')->hSet(okeys::vipTurnTable(),$uid,1);
                }
            }else if($type == 5){
                $type = 2;
                $data = oo::commonOprModel('slot')->slotTurntable($uid,$type);
                if($data['code'] == 1){
                    oo::commonOprRedis('common')->hSet(okeys::fbTurnCount(),$uid,1);
                }
            }else if($type == 4){
                $type = 2;
                if($aid == 12){
                    $data = oo::commonOprModel('slot')->slotTurntable($uid,$type,true);
                }else{
                    $data = oo::commonOprModel('slot')->slotTurntable($uid,$type);
                }
                if($data['code'] == 1){
                    oo::commonOprModel('advertisement')->addCount($uid,$aid);
                }
            }
        }else{
            $data = oo::commonOprModel('slot')->slotTurntable($uid,$type);
        }
        return json_encode($data);
    }

  	/**
     * 体力转盘
     * @param $param
     * @return false|string
     * Created by: Owen
     * Created on: 2019/11/2 12:24
     */
    public function spinsTurnTable($param){
        if(empty($param['uid']) || $param['uid'] <= 0 || empty($param['type'])){
            $this->response['flag'] = -1;
            $this->response['msg'] = '参数错误';
            return json_encode($this->response);
        }
        $uid  = oo::functions()->uint( $param['uid']);
        $type = oo::functions()->uint( $param['type']);
        if($type == 1){
            $time = oo::functions()->uint($param['time']);
            $sign = oo::functions()->escape($param['sign']);
            $serverSign = "{$uid}#2019$"."SUPERANT{$time}&^%";
            if($sign == md5($serverSign)){
                $ret = oo::commonOprModel('advertisement')->getRemainCount($uid,13);
                if(empty($ret)){
                    return oo::response(-4,[],'广告未开启');
                }
                if($ret['remain']<=0){
                    return oo::response(-5,[],'次数不足');
                }

                $data = oo::commonOprModel('slot')->spinsTurnTable($uid,$type);
                if($data['code'] == 1){
                    oo::commonOprModel('advertisement')->addCount($uid,13);
                }
            }else{
                $this->response['flag'] = -1;
                $this->response['msg'] = '参数错误';
                return json_encode($this->response);
            }
        }else{
            $data = oo::commonOprModel('slot')->spinsTurnTable($uid,$type);
        }
        return json_encode($data);
    }
  
    /**
     * 大转盘 - 确定领奖
     */

    public function confirmTurntable($param){
        if(empty($param['uid']) || $param['uid'] <= 0){
            $this->response['flag'] = -1;
            $this->response['msg'] = '参数错误';
            return json_encode($this->response);
        }
        $uid  = oo::functions()->uint( $param['uid']);
        $data = oo::commonOprModel('slot')->confirmTurntable($uid);
        return json_encode($data);
    }

    /**
     * 大转盘状态
     * Created by: Owen
     * Created on: 2019/6/11 11:57
     * Description:
     */
    public function turntableStatus($param){
        if(empty($param['uid']) || $param['uid'] <= 0){
            $this->response['msg'] = '参数错误';
            return json_encode($this->response);
        }
        $uid  = oo::functions()->uint($param['uid']);
        $bigwheel_key = okeys::bigwheel($uid);
        $bigwheel = oo::commonOprRedis('common')->get($bigwheel_key);
        if(empty($bigwheel)){
            return json_encode([
                'code'=>1,
                'msg'=>'spin',
                'data'=>[
                    'tpStatus'=>1,
                    'time'=>0,
                ]

            ]);
        }else{
            $data = explode('#',$bigwheel);
            if(count($data) ==3){
                return json_encode([
                    'code'=>1,
                    'msg'=>'collect',
                    'data'=>[
                        'tpStatus'=>2,
                        'time'=>0,
                    ]
                ]);
            }else{
                // 判断是否有支付状态下的大转盘
                $payWheel = oo::commonOprRedis('common')->hGetAll(okeys::bigwheelPay($uid));
                if(!empty($payWheel)){
                    return json_encode([
                        'code'=>1,
                        'msg'=>'pay',
                        'data'=>[
                            'tpStatus'=>4,
                            'time'=>0
                        ]
                    ]);
                }
                return json_encode([
                    'code'=>1,
                    'msg'=>'waiting',
                    'data'=>[
                        'tpStatus'=>3,
                        'time'=>oo::commonOprRedis('common')->ttl($bigwheel_key)
                    ]
                ]);
            }
        }
    }

    /**
     * 新版大转盘
     * @param $param
     * @return string
     * Created by: Owen
     * Created on: 2019/12/5 19:58
     */
    public function turnTable($param){
        $uid  = oo::functions()->uint($param['uid']);
        $type = oo::functions()->uint( $param['type']);
        $aid = oo::functions()->uint( $param['aid']);
        if($type == 4 || $type == 6){
            $time = oo::functions()->uint($param['time']);
            $sign = oo::functions()->escape($param['sign']);
            $serverSign = "{$uid}#2019$"."SUPERANT{$time}&^%";
            if($sign != md5($serverSign)){
                return oo::response(-1,[],'参数错误');
            }
            if($type == 4){
                $ret = oo::commonOprModel('advertisement')->getRemainCount($uid,$aid);
                if(empty($ret)){
                    return oo::response(-4,[],'广告未开启');
                }
                if($ret['remain']<=0){
                    return oo::response(-5,[],'次数不足');
                }
            }else if($type == 6){
                $vip = oo::commonOprModel('newvip')->checkVip($uid);
                if(!$vip){
                    return oo::response(-6,[],'非vip');
                }
                if($vip['tpStatus'] <= 0){
                    return oo::response(-6,[],'vip转盘剩余次数不足');
                }
                return oo::commonOprModel('newvip')->vipTurnTable($uid);
            }
            $flag = true;
            if(oo::commonOprRedis('common')->get(okeys::bigwheel($uid))){
                $flag = false;
                oo::commonOprRedis('common')->delete(okeys::bigwheel($uid));
            }
            $data = oo::commonOprModel('slot')->actionTurnTable($uid,2);
            $code = json_decode($data,true);
            if($code['code'] == 1){
                if($flag){
                    $code['data']['tpCountdown'] = 0;
                    $data = json_encode($code);
                    oo::commonOprRedis('common')->delete(okeys::bigwheel($uid));
                }
                if($type == 4){
                    oo::commonOprModel('advertisement')->addCount($uid,$aid);
                }else if($type == 6){
                    oo::commonOprRedis('common')->hIncrBy(okeys::vipTurnTable(),$uid,1);
                }
            }
        }else{
            $data = oo::commonOprModel('slot')->actionTurnTable($uid,$type);
        }
		
		//转盘参与率 统计
        oo::commonOprModel('statistics')->turnTableInSta($uid);
		
        return $data;
    }

}