<?php
defined( 'IN_WEB') or die( 'Include Error!');
/**
 * 老虎机
 */
class Slot {

    /**
     * 新版大转盘
     * @param $param $param['type'] 2普通 3支付 4老广告 5聚合广告 6vip
     * @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 == 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 != 6){
                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 == 5){
                $config = oo::commonOprModel('Advertisement')->getAggregateConfig2($uid);
                $config = array_column($config,null,'id');
                $config = $config[5];
                if(empty($config)){
                    return oo::response(-2,[],'奖励配置为空');
                }
                if($config['remain'] < -1 || $config['status'] == 0){
                    return oo::response(-3,[],'奖励数量不足');
                }
            }else if($type == 6){
                //1.8.5取消vip大转盘特权,改为vip订阅特权
                if(!oo::compareVersion(oo::commonOprModel('user')->getUserVersion($uid),'1.8.5')){
                    $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);
                }else{
                    $subVip = oo::commonOprModel('newvip')->checkSubVip($uid);
                    if(empty($subVip)){
                        return oo::response(-6,[],'非vip');
                    }
                    $confNum = empty($subVip['vipTurnTable']) ? 0 : $subVip['vipTurnTable'];
                    $curNum = empty($subVip['vipTurnTableNum']) ? $subVip['vipTurnTableNum'] : 0;
                    if($curNum >= $confNum){
                        return oo::response(-6,[],'vip转盘剩余次数不足');
                    }
                    return oo::commonOprModel('newvip')->subVipTurnTable($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,$type);
            $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,$type);
        }
        //转盘参与率 统计
        oo::commonOprModel('statistics')->turnTableInSta($uid);
        return $data;
    }

}
