<?php

/**
 * vip相关
 * User: liqx
 * Date: 2018/3/20
 * Time: 14:36
 */
class ModelNewvip
{

    const SUBVIPCONFKEY = 'SubVip';

    /**
     * 订阅会员特权配置key
     */
    private $subVipKeys = [
        'vipTurnTable',  //订阅会员每日转转盘次数
        'vipSignBet',    //订阅会员额外签到倍率
        'vipDailySpins', //订阅会员每日体力领取
        //'vipAttackCss',  //订阅会员炮台值
    ];

    /**
     * 设置VIP
     * @param $uid
     * @param int $expire
     * @param string $purchaseToken
     * @return bool
     * Created by: Owen
     * Created on: 2019/11/11 19:03
     */
    public function setVip($uid,$expire,$purchaseToken){
        if(empty($uid)){
            return false;
        }
        $tb  = otable::subscription();
        $sql = "SELECT * FROM {$tb} WHERE uid={$uid}";
        $ret = oo::commonOprDb('payment')->getOne($sql,MYSQLI_ASSOC);
        if(!empty($ret)){
            $sql = "UPDATE {$tb} SET `purchaseToken`='{$purchaseToken}' WHERE `uid`={$uid}";
        }else{
            $sql = "INSERT INTO {$tb} (uid,purchaseToken) VALUES ({$uid},'{$purchaseToken}')";
        }
        oo::commonOprDb('payment')->query($sql,false);
        $updateInfo['svip'] = 1;
        $updateInfo['svip_expire'] = $expire;
        oo::commonOprModel('member')->getUserInfo($uid, NULL, true,$updateInfo);
        oo::commonOprRedis('common')->delete(okeys::VipExperience($uid));
        oo::commonOprRedis('userinfo')->delete(okeys::IsSubscription($uid));
        return true;
    }

    /**
     * 设置体验Vip
     * @param $uid
     * @param $freeTime
     * @return mixed
     * created by owen
     * created at 22:12
     */
    public function setExperienceVip($uid,$freeTime){
        $experienceLv = self::getUserVipLv($uid,false,true);
        if($experienceLv <= self::getVipExperienceLv($uid)){
            return false;
        }
        $updateInfo['svip'] = 1;
        $updateInfo['svip_expire'] = $freeTime + time();
        $updateInfo['svipExperienceLv'] = $experienceLv;
        oo::commonOprModel('member')->getUserInfo($uid, NULL, true,$updateInfo);
        oo::commonOprRedis('common')->setex(okeys::VipExperience($uid),1,$freeTime);
        return ['vipInfo'=>self::checkVip($uid)];
    }

    /**
     * 设置免费Vip -- 测试用
     * @param $uid
     * @param $freeTime
     * @return mixed
     * created by owen
     * created at 22:12
     */
    public function setFreeVip($uid,$freeTime){
        $updateInfo['svip'] = 1;
        $updateInfo['svip_expire'] = $freeTime + time();
        oo::commonOprModel('member')->getUserInfo($uid, NULL, true,$updateInfo);
        return true;
    }

    /**
     * 获得已体验的最大等级
     * @param $uid
     * @return int
     * Created by: Owen
     * Created on: 2020/2/26 22:27
     */
    public function getVipExperienceLv($uid){
        $userInfo = oo::commonOprModel('member')->getUserInfo($uid);
        return intval($userInfo['vipExperienceLv']);
    }

    /**
     * 判断VIP状态
     * @param $uid
     * @return bool|mixed
     * Created by: Owen
     * Created on: 2019/11/11 19:03
     * PS:vip 头像框 1,4,5，
     */
    public function checkVip($uid){
        if(empty($uid)){
            return false;
        }
        $userInfo = oo::commonOprModel('member')->getUserInfo($uid);
        if(empty($userInfo)){
            return false;
        }
        if($userInfo['vip_expire'] > time()){ //订阅未到期
            $ret['expire'] = intval($userInfo['vip_expire']);
            $config = self::getUserVipLv($uid,true,false,155);
            if(!empty($config)){
                $vipLv = intval($userInfo['vipLv']);
                $curPoint = intval($config['curPoint']);
                if($curPoint != 0 && $vipLv == 0){
                    $updateInfo['svipLv'] = $config['lv'];
                    $vipLv = intval($config['lv']);
                }
                if($userInfo['vipExperienceLv'] != $vipLv){
                    $updateInfo['svipExperienceLv'] = $vipLv;
                }
                $ret['level']     = $vipLv;
                $ret['curPoint']  = $curPoint;
                $ret['isFreeing'] = intval(oo::commonOprRedis('common')->get(okeys::VipExperience($uid)));

                //1.8.5以后vip特权转转盘增加次数取消,改为vip订阅的特权
                //$ret['tpStatus']  = max($config['turnTable']['num'] - intval(oo::commonOprRedis('common')->hGet(okeys::vipTurnTable(),$uid)),0);
                $ret['tpStatus']  = 0;

                $avatarBox = json_decode($userInfo['avatarBox'],true)??[];
                if($config['avatarBox'] == 5){
                    $vipAvatarBox = array_merge($avatarBox,[1,4,5,6,8]);
                }else if($config['avatarBox'] == 4){
                    $vipAvatarBox = array_merge($avatarBox,[1,4,5,6]);
                }else if($config['avatarBox'] == 3){
                    $vipAvatarBox = array_merge($avatarBox,[1,4,5]);
                }else if($config['avatarBox'] == 2){
                    $vipAvatarBox = array_merge($avatarBox,[1,4]);
                }else if($config['avatarBox'] == 1){
                    $vipAvatarBox = array_merge($avatarBox,[1]);
                }
                $vipAvatarBox = array_unique($vipAvatarBox);
                if(count($vipAvatarBox) != count($avatarBox)){
                    sort($vipAvatarBox);
                    $updateInfo['savatarBox'] = json_encode($vipAvatarBox,JSON_NUMERIC_CHECK);
                    oo::commonOprModel('member')->getUserInfo($uid, NULL, TRUE,$updateInfo);
                }
            }else{
                $ret['level'] = 0;
                $ret['curPoint'] = 0;
                //1.8.5以后vip特权转转盘增加次数取消,改为vip订阅的特权
                //$ret['tpStatus'] = max(3 - intval(oo::commonOprRedis('common')->hGet(okeys::vipTurnTable(),$uid)),0);
                $ret['tpStatus']  = 0;
            }
            return $ret;
        }else{
            if($userInfo['vip']==0){
                return false;
            }
            //vip过期清除所有avatabox
            //$userAvatarBox = (array)json_decode($userInfo['avatarBox'],true);
            //$flag = false;
            //if(!empty($userAvatarBox)){
            //    foreach ($userAvatarBox as $key=>$row){
            //        if(in_array($row,[1,4,5])){
            //            unset($userAvatarBox[$key]);
            //            $flag = true;
            //        }
            //    }
            //}
            //if(!empty($userAvatarBox)){
            //    $userAvatarBox = [];
            //    sort($userAvatarBox);
            //    $userAvatarBox = array_unique($userAvatarBox);
            //    $userAvatarBox = json_encode($userAvatarBox);
            //    $updateInfo['savatarBox'] = $userAvatarBox;
            //    $updateInfo['savatarBox'] = $userAvatarBox;
            //}
            $updateInfo['savatarBox'] = '[]';
            $updateInfo['savatar'] = 0;
            $updateInfo['svip'] = 0;
            oo::commonOprModel('member')->getUserInfo($uid, NULL, TRUE,$updateInfo);
            oo::commonOprModel('ta')->setEventData($uid,'user_set',["is_member"=>0]);
            return false;
        }
    }

    /**
     * 判断订阅会员状态
     * @param $uid
     * @return bool|mixed
     * Created by: Owen
     * Created on: 2019/11/11 19:03
     * PS:vip 头像框 1,4,5，
     */
    public function checkSubVip($uid) {
        $userInfo = oo::commonOprModel('member')->getUserInfo($uid);
        if(empty($userInfo['vip_expire']) || $userInfo['vip_expire'] < time()){
            return false;
        }
        return $this->getUserSubVipInfo($uid);
    }

    /**
     * 1.14版本后只有订阅玩家才是vip
     * @param int $uid
     * @param string $version
     * @return box|mixed
     */
    public function checkNewVip($uid, $version = '') {
        return $this->checkSubVip($uid) ? true : false;
    }

    /**
     * 获取 VIP 转盘状态
     * @param $uid
     * @return mixed
     * Created by: Owen
     * Created on: 2019/11/11 19:03
     */
    public function vipTurnTableStatus($uid){
        if($ret = self::checkVip($uid)){
            if($ret['turnTableStatus']){
                return false;
            }else{
                return true;
            }
        }
        return false;
    }

    /**
     * 设置 VIP 转盘状态
     * @param $uid
     * @return mixed
     * Created by: Owen
     * Created on: 2019/11/11 19:03
     */
    public function vipSetTurnTableStatus($uid){
        return oo::commonOprRedis('common')->hSet(okeys::vipTurnTable(),$uid,1);
    }

    public function getUserCurPoint($uid){
        $userInfo = oo::commonOprModel('member')->getUserInfo($uid);
        $vipPoint = intval($userInfo['vipPoint']);
        if($vipPoint == -1){
            $totalPayNum = oo::commonOprModel('payment')->getUserTotalPayNum($uid);
            $vipPoint = intval($totalPayNum*100);
            if($vipPoint != 0 && $vipPoint<100){
                $vipPoint = 150;
            }
            $updateInfo['svipPoint'] = $vipPoint;
            oo::commonOprModel('member')->getUserInfo($uid, NULL, true,$updateInfo);
        }
        return $vipPoint;
    }

    /**
     * 更新当前积分
     * @param $uid
     * @param $num
     * Created by: Owen
     * Created on: 2020/3/5 19:43
     */
    public function updateUserCurPoint($uid,$num){
        $curPoint = self::getUserCurPoint($uid);
        $updateInfo['svipPoint'] = $curPoint+$num;
        oo::commonOprModel('member')->getUserInfo($uid, NULL, true,$updateInfo);
    }

    /**
     * 用户vip等级
     * @param $uid
     * @param bool $returnConfig
     * @param bool $fromCache
     * @param string $version
     * @return array|mixed
     * created by owen
     * created at 14:37
     */
    public function getUserVipLv($uid,$returnConfig = false,$fromCache = false,$version = ''){
        if($fromCache){
            $userInfo = oo::commonOprModel('member')->getUserInfo($uid);
            return $userInfo['vipLv'];
        }
        $curPoint = self::getUserCurPoint($uid);
        if($curPoint == 0 && $returnConfig){
            return [];
        }else if($curPoint == 0 && !$returnConfig){
            return 0;
        }
        $config = self::getVipLvConfig($curPoint,$version);
        if($returnConfig){
            $config['curPoint'] = $curPoint;
            return $config;
        }else{
            return intval($config['lv']);
        }
    }

    /**
     * vip 配置
     * @param int $integration
     * @param mixed $version
     * @return array
     * created by owen
     * created at 14:35
     */
    public function getVipLvConfig($integration = 0,$version = ''){
        $cacheKey = okeys::VipLvConfig($version);
        $cacheJson = oo::commonOprRedis('config')->hGetAll($cacheKey);
        $cache =funs::getArrFromJsonArr($cacheJson);
        if($cache){
            array_multisort(array_column($cache,'lv'),SORT_ASC,$cache);
            if($integration){
                $return = [];
                foreach ($cache as $key=>$row){
                    if($row['integration'] <= $integration){
                        $return = $row;
                    }
                }
                return $return;
            }else{
                return $cache;
            }
        }
        $tb  = otable::VipLvConfig($version);
        $sql = "SELECT * FROM {$tb} WHERE 1";
        $ret = oo::commonOprDb('config')->getAll($sql,MYSQLI_ASSOC);
        foreach ($ret as $key=>$row){
            $ret[$key]['turnTable']           = json_decode($row['turnTable'],true);
            $ret[$key]['weekRewardChest']     = json_decode($row['weekRewardChest'],true);
            $ret[$key]['weekRewardBless']     = json_decode($row['weekRewardBless'],true);
            $ret[$key]['weekRewardLuckydraw'] = json_decode($row['weekRewardLuckydraw'],true);
            $cacheJson[$key] = json_encode($ret[$key]);
        }
        array_multisort(array_column($ret,'lv'),SORT_ASC,$ret);
        oo::commonOprRedis('config')->hMset($cacheKey,$cacheJson);
        oo::commonOprRedis('config')->expire($cacheKey,oo::redisRandomExpire(7*86400));
        if($integration){
            $return = [];
            foreach ($ret as $key=>$row){
                if($row['integration'] < $integration){
                    $return = $row;
                }
            }
            return $return;
        }else{
            return $ret;
        }
    }

    /**
     * 获取vip订阅会员单个配置值
     * @param string $key
     * @return int
     */
    public function getSubVipConfByKey($key) {
        return (int)oo::commonOprModel('readconfig')->getCon(self::SUBVIPCONFKEY, $key);
    }

    /**
     * 获取订阅会员特权配置
     * @return array
     */
    public function getSubVipConf() {
        $keyArr = [];
        foreach($this->subVipKeys as $key){
            $keyArr[] = self::SUBVIPCONFKEY."-".$key;
        }

        $tb = otable::basicConfig();
        $tmp = oo::commonOprRedis('config')->hMget($tb, $keyArr);
        $arr = [];
        foreach($tmp as $k => $v){
            $key = str_replace(self::SUBVIPCONFKEY."-", "", $k);
            $arr[$key] = empty($v) ? oo::commonOprModel('readconfig')->getCon(self::SUBVIPCONFKEY, $key) : $v;
        }
        return $arr;
    }

    /**
     * 获取玩家vip订阅会员信息
     */
    public function getUserSubVipInfo($uid) {
        $res                        = $this->getSubVipConf();
        $dailyInfo                  = (array)oo::commonOprRedis('common')->hGetAll(okeys::userSubVipDaily($uid));
        $res['vipTurnTableNum']     = isset($dailyInfo['vipTurnTable']) ? (int)$dailyInfo['vipTurnTable'] : 0;
        $res['vipDailySpinsStatus'] = isset($dailyInfo['vipDailySpins']) ? 1 : 0;
        return $res;
    }

    /**
     * 获取vip周奖励
     * @param $uid
     * @return string
     * created by owen
     * created at 15:21
     */
    public function getUserVipWeekReward($uid){
        if(oo::commonOprRedis('common')->get(okeys::VipExperience($uid))){
            return oo::response(-4,[],"体验VIp,不可领取");
        }
        if(!self::checkVip($uid)){
            return oo::response(-3,[],"非VIp,不可领取");
        }
        $cache = oo::commonOprRedis('common')->get(okeys::UserVipWeekReward($uid));
        if(intval($cache) == 1){
            return oo::response(-2,[],"本周已领取过奖励");
        }
        $config = self::getUserVipLv($uid,true);
        /*
         * 体力
         */
        $add = [];
        if($config['weekRewardSpins'] > 0){
            $add['spins'] = $config['weekRewardSpins'];
        }
        if($config['weekRewardShield']>0){
            $userAssetsInfo = oo::commonOprModel('member')->getUserAssetsInfo($uid);
            $shieldUntilFull = $config['shieldGroove'] + 3 - $userAssetsInfo['shield'];
            if($shieldUntilFull >= $config['weekRewardShield']){
                $add['shield'] = $config['weekRewardShield'];
            }else{
                $turnToSpins = $config['weekRewardShield'] - $shieldUntilFull;
                $add['shield'] = $shieldUntilFull;
                $add['spins']  = intval($add['spins']) + $turnToSpins;
            }
        }
        if(!empty($add)){
            oo::commonOprModel('member')->optProperty($uid, $add, '+',352,'vip每周奖励');
        }
        if(!empty($config['weekRewardChest']) && $config['weekRewardChest']['propsId'] != 0 && $config['weekRewardChest']['num'] != 0){
            $data = $config['weekRewardChest'];
            oo::commonOprModel('props')->addUserProps($uid,$data['propsId'],$data['num'],0,352);
        }
        if(!empty($config['weekRewardBless']) && $config['weekRewardBless']['propsId'] != 0 && $config['weekRewardBless']['num'] != 0){
            $data = $config['weekRewardBless'];
            oo::commonOprModel('props')->addUserProps($uid,$data['propsId'],$data['num'],0,352);
        }
        if(!empty($config['weekRewardLuckydraw']) && $config['weekRewardLuckydraw']['propsId'] != 0 && $config['weekRewardLuckydraw']['num'] != 0){
            $data = $config['weekRewardLuckydraw'];
            oo::commonOprModel('props')->addUserProps($uid,$data['propsId'],$data['num'],0,352);
        }
        oo::commonOprRedis('common')->setex(okeys::UserVipWeekReward($uid),1,oo::weekDeadline());
        $userAssetsInfo = oo::commonOprModel('member')->getUserAssetsInfo($uid);
        return oo::response(1,[
            'spins' => $userAssetsInfo['spins'],
            'shield' => $userAssetsInfo['shield'],
            'rewardStatus' => 3,
        ],"",true);
    }

    /**
     * 获取vip每日奖励
     * @param $uid
     * @return string
     * created by owen
     * created at 15:21
     */
    public function getUserVipWeekReward2($uid){
        if(!self::checkNewVip($uid)){
            return oo::response(-3,[],"非VIp,不可领取");
        }
        $cache = oo::commonOprRedis('common')->get(okeys::UserVipWeekReward($uid));
        if(intval($cache) == 1){
            return oo::response(-2,[],"本日已领取过奖励");
        }
        $config = self::getUserVipLv($uid,true,false,155);
        /*
         * 体力
         */
        $add = [];
        //1.8.5版本取消vip每日体力奖励特权
        //if($config['weekRewardSpins'] > 0){
        //    $add['spins'] = $config['weekRewardSpins'];
        //}
        if($config['weekRewardShield']>0){
            $shield = oo::commonOprModel('member')->getUserLevelUpLimit($uid,'shield');
            $userAssetsInfo = oo::commonOprModel('member')->getUserAssetsInfo($uid);
            $shieldUntilFull = max($config['shieldGroove'] + $shield - $userAssetsInfo['shield'],0);
            if($shieldUntilFull >= $config['weekRewardShield']){
                $add['shield'] = $config['weekRewardShield'];
            }else{
                $turnToSpins = $config['weekRewardShield'] - $shieldUntilFull;
                $add['shield'] = $shieldUntilFull;
                $add['spins']  = intval($add['spins']) + $turnToSpins;
            }
        }
        if(!empty($add)){
            oo::commonOprModel('member')->optProperty($uid, $add, '+',351,'vip每日奖励');
        }
        if(!empty($config['weekRewardChest']) && $config['weekRewardChest']['propsId'] != 0 && $config['weekRewardChest']['num'] != 0){
            $data = $config['weekRewardChest'];
            oo::commonOprModel('props')->addUserProps($uid,$data['propsId'],$data['num'],0,351);
        }
        if(!empty($config['weekRewardBless']) && $config['weekRewardBless']['propsId'] != 0 && $config['weekRewardBless']['num'] != 0){
            $data = $config['weekRewardBless'];
            oo::commonOprModel('props')->addUserProps($uid,$data['propsId'],$data['num'],0,351);
        }
        if(!empty($config['weekRewardLuckydraw']) && $config['weekRewardLuckydraw']['propsId'] != 0 && $config['weekRewardLuckydraw']['num'] != 0){
            $data = $config['weekRewardLuckydraw'];
            oo::commonOprModel('props')->addUserProps($uid,$data['propsId'],$data['num'],0,351);
        }

        oo::commonOprRedis('common')->setex(okeys::UserVipWeekReward($uid),1,oo::todayDeadline());
        $userAssetsInfo = oo::commonOprModel('member')->getUserAssetsInfo($uid);
        return oo::response(1,[
            'spins' => $userAssetsInfo['spins'],
            'shield' => $userAssetsInfo['shield'],
            'rewardStatus' => 3,
            'vipPoint'     => $this->getUserCurPoint($uid),
        ],"",true);
    }

    /**
     * 1.8.5版本vip订阅会员领取每日奖励
     * @param $uid int
     * @return array
     */
    public function gainSubVipDailyReward($uid) {
        oo::commonOprRedis('user')->hIncrBy(okeys::userSubVipDaily($uid), 'vipDailySpins', 1);
        if(oo::commonOprRedis('user')->ttl(okeys::userSubVipDaily($uid)) == -1){
            oo::commonOprRedis('user')->expire(okeys::userSubVipDaily($uid), oo::todayDeadline());
        }
        $spinsNum = $this->getSubVipConfByKey('vipDailySpins');
        $spinsNum = $spinsNum < 1 ? 1 : $spinsNum;
        oo::commonOprModel('member')->optProperty($uid, ['spins' => $spinsNum], '+',351,'vip订阅会员每日奖励');
        $userAssetsInfo = oo::commonOprModel('member')->getUserAssetsInfo($uid);
        return oo::response(1,[
            'spins' => $userAssetsInfo['spins'],
            'rewardStatus' => 3,
        ],"",true);
    }

    public function getVipInfo($uid, $version = ''){
        $isVip = self::checkVip($uid);
        $vipData = [];return $vipData;
        ///////////////////////1.14.1还原原vip业务///////////////////////
        if($isVip){
            $vipData = $isVip;
            $vipData['rewardStatus']      = self::getVipRewardStatus($uid,true);
            $vipData['pointRewardStatus'] = intval(oo::commonOprRedis('User')->get(okeys::VipPointWeekReward($uid)));
            $vipData['isSubscribed']      = self::getVipIsSubscribed($uid);
            $vipData['countdown']         = max($vipData['expire'] - time() , 0);
            $vipData['openLv']            = intval(oo::commonOprModel('readconfig')->getCon('vip','openLv'));//开放等级
            $vipData['openPoint']         = intval(oo::commonOprModel('readconfig')->getCon('vip','openPoint'));//开放积分
            $vipData['subVipInfo']        = $this->getUserSubVipInfo($uid);
            //给客户端的状态值,0:未订阅, 2-可领取  3-已领取
            $vipData['subVipInfo']['vipDailySpinsStatus'] = empty($vipData['subVipInfo']['vipDailySpinsStatus']) ? 2 : 3;
        }else{
            $userInfo = oo::commonOprModel('member')->getUserInfo($uid);
            $vipData = [
                'level'        => self::getUserVipLv($uid,false,false,155),
                'expire'       => intval($userInfo['vip_expire']),
                'countdown'    => 0,
                'tpStatus'     => 0,
                'rewardStatus' => self::getVipRewardStatus($uid,false),
                'pointRewardStatus' => intval(oo::commonOprRedis('User')->get(okeys::VipPointWeekReward($uid))),
                'curPoint'  => self::getUserCurPoint($uid),
                'isFreeing' => 0,
                'isSubscribed' => self::getVipIsSubscribed($uid),
                'openLv'    => intval(oo::commonOprModel('readconfig')->getCon('vip','openLv')),
                'openPoint' => intval(oo::commonOprModel('readconfig')->getCon('vip','openPoint')),
                'subVipInfo' => $this->getSubVipConf(),
            ];
            $vipData['subVipInfo']['vipDailySpinsStatus'] = 0; //给客户端的状态值,0:未订阅, 2-可领取  3-已领取
        }
        return $vipData;
    }

    public function getVipRewardStatus($uid,$isVip = false){
        if($isVip){
            // 判断是否已领取当周奖励
            if(oo::commonOprRedis('common')->get(okeys::UserVipWeekReward($uid))){
                return 3;
            }else{
                return 2;
            }
        }else{
            return 0;
            if(self::getUserVipLv($uid,false,true) > self::getVipExperienceLv($uid)){
                return 1;
            }else{
                return 0;
            }
        }
    }

    public function getVipIsSubscribed($uid){
        $cacheKey = okeys::IsSubscription($uid);
        $cache = oo::commonOprRedis('userinfo')->get($cacheKey);
        if(!empty($cache)){
            return $cache;
        }
        $tb  = otable::subscription();
        $sql = "SELECT * FROM {$tb} WHERE uid={$uid}";
        $ret = oo::commonOprDb('payment')->getOne($sql,MYSQLI_ASSOC);
        if(!empty($ret)){
            oo::commonOprRedis('userinfo')->setex($cacheKey,1,7*24*60*60);
            return 1;
        }
        oo::commonOprRedis('userinfo')->setex($cacheKey,0,7*24*60*60);
        return 0;
    }

    public function updateVipLv($uid){
        $config = self::getUserVipLv($uid,true,false,155);
        $updateInfo['svipLv'] = intval($config['lv']);
        oo::commonOprModel('member')->getUserInfo($uid, NULL, true,$updateInfo);
        return true;
    }

    public function vipTurnTable($uid){
        $config = self::getUserVipLv($uid,true);
        $memberAssets  = oo::commonOprModel('member')->getUserAssetsInfo($uid);
        $levelId = 1;
        if(!empty($memberAssets) && !empty($memberAssets['levelId'])){
            $levelId = $memberAssets['levelId'];
        }
        if($config['turnTable']['id'] == 601){
            $trun_arr = oo::commonOprModel('slot')->getSlotTurnConfig($levelId);
        }else if(in_array($config['turnTable']['id'],[602,603])){
            $trun_arr = oo::commonOprModel('slot')->getTurnConfigPlusConfig($levelId);
        }

        $arr = [];
        foreach ($trun_arr as $val) {
            $arr[$val['id']] = floatval($val['v']);
        }
        $rid = oo::commonOprModel('slot')->get_rand($arr,true); // 中奖id
        if(($rid-1) >= 0){
            $res['rid'] = $rid - 1;
        }else{
            $res['rid'] = 0;
        }
        $protecte = false;
        $tpdown = oo::todayDeadline();

        if($config['turnTable']['id'] == 601){
            $res['prize'] = intval($trun_arr[$res['rid']]['prize']);
            $res['type']  = 1;
            $res['betRewardTime'] = 10;
            $res['tpCountdown'] = $tpdown;
            $protecte = true;
            oo::commonOprModel('member')->optProperty($uid,array('money'=>$res['prize']),'+',41,'大转盘摇奖');
            oo::commonOprRedis('usercache')->setex(okeys::TurntableBetReward($uid),$res['prize'],180);
        }else if(in_array($config['turnTable']['id'],[602,603])){
            $res['prize'] = intval($trun_arr[$res['rid']]['num']);
            $res['type'] = intval($trun_arr[$res['rid']]['type']);
            $res['betRewardTime'] = 0;
            $res['tpCountdown'] = $tpdown;
            if($res['type'] == 1){
                $type = 'money';
                $protecte = true;
                $res['prize'] = ($config['turnTable']['id']==603)?intval($res['prize'])*10:intval($res['prize']);
            }else if($res['type'] == 2){
                $type = "spins";
            }
            oo::commonOprModel('member')->optProperty($uid,array($type=>$res['prize']),'+',42,'大转盘摇奖');
        }
        oo::commonOprRedis('common')->hIncrBy(okeys::vipTurnTable($config['turnTable']['id']),$uid,1);
        /**
         * 偷保护
         */
        if($protecte){
            oo::commonOprModel('member')->setUserStealProtect($uid,intval(oo::commonOprModel('readconfig')->getCon('basic','payStealProtectTime')) * 60);
        }
        $memberAssets  = oo::commonOprModel('member')->getUserAssetsInfo($uid);
        $res['lastMoney'] = $memberAssets['money'];
        $res['lastSpins'] = $memberAssets['spins'];
        //数数事件-点击水果机
        $ssEventData =[
            "fruit_coin"=>$res['prize'],
            "fruit_result"=>$rid,
            "fruit_type"=>3
        ];
        oo::commonOprModel('ta')->setEventData($uid,'tap_fruit',$ssEventData);
        return oo::response(1,$res,'',true);
    }

    /**
     * 1.8.5版本订阅会员每日免费转转盘
     * @param $uid int
     * @return string
     */
    public function subVipTurnTable($uid){
        $tpdown = oo::todayDeadline();
        //更新每天转盘次数
        $ret = oo::commonOprRedis('user')->hIncrBy(okeys::userSubVipDaily($uid), 'vipTurnTable', 1);
        if(oo::commonOprRedis('user')->ttl(okeys::userSubVipDaily($uid)) == -1){
            oo::commonOprRedis('user')->expire(okeys::userSubVipDaily($uid), $tpdown);
        }
        $res = oo::commonOprModel('slot')->actionTurnTable($uid, 6, 2);
        $res = $res ? (array)json_decode($res, true) : [];
        if(!empty($res['data'])){
            $res['data']['vipTurnTableNum'] = $ret;
        }
        return json_encode($res, JSON_NUMERIC_CHECK);
    }

    /**
     * 奖励配置
     * @param $uid
     * @param string $form
     * @return int|mixed
     * Created by: Owen
     * Created on: 2020/11/21 11:41
     */
    public function vipRewardConf($uid,$form = ""){
        if(!self::checkNewVip($uid)){
            return 0;
        }
        $config = self::getUserVipLv($uid,true,false,155);
        return round($config[$form]/100,2);
    }
}
