<?php
defined( 'IN_WEB') or die( 'Include Error!');
/**
 * 奖励
 */
class Reward {
    /**
     * 七日签到
     * @param $param
     * @return string
     * Created by: Owen
     * Created on: 2019/12/5 20:00
     */
    public function signIn($param){
        $uid = oo::functions()->uint($param['uid']);
        if(empty($uid)){
            return oo::response(-1);
        }
        return oo::commonOprModel('member')->getSignInReward2($uid,true);
    }

    /**
     * 系统补偿
     * @param $param
     * @return string
     * Created by: Owen
     * Created on: 2019/12/5 20:00
     */
    public function sysCompensate($param){
        $uid = oo::functions()->uint($param['uid']);
        if(empty($uid)){
            return oo::response(-1);
        }
        $config = oo::commonOprModel('config')->getSysCompensateConfig();
        if(empty($config)){
            return oo::response(-1);
        }

        if($config['endTime'] + 600 < time()){
            return oo::response(-3,[],'过期');
        }

        $times = oo::commonOprRedis('User')->get(okeys::sysCompensate($config['id'],$uid));
        if($times){
            return oo::response(-2,[],'已经领取过');
        }

        if(!empty($config['levelId'])){
            $levelInfo = oo::commonOprModel('member')->getUserLevel($uid);
            if(empty($levelInfo['levelId']) || $levelInfo['levelId'] != $config['levelId']){
                return oo::response(-2,[],'当前无补偿奖励可领取');
            }
        }

        $reward = oo::commonOprRedis('common')->hGet(okeys::sysCompensateConfig(),'reward');
        $reward = json_decode($reward,true);
        $reward = array_filter($reward);
        oo::commonOprModel('member')->optProperty($uid, $reward, '+',997, '系统补偿');
        $ts = $config['endTime']-time()+1000;
        oo::commonOprRedis('User')->setex(okeys::sysCompensate($config['id'],$uid),1,$ts);
		
		//系统补偿领取率
        oo::commonOprModel('statistics')->sysCompensateSta($uid);
		
        return oo::response(1,$reward);
    }


    /**
     * 分享奖励
     * @param $param
     * @return string
     * Created by: Owen
     * Created on: 2019/11/29 20:19
     */
    public function shareReward($param){
        $uid = oo::functions()->uint($param['uid']);
        $rewardId = oo::functions()->escape($param['rewardId']);
        $timestamp = oo::functions()->escape($param['timestamp']);
        $rewardData = oo::commonOprRedis('common')->get(okeys::ShareRewardIdData($rewardId));
        if(!empty($rewardData)){
            $rewardData = json_decode($rewardData,true);
            $config = oo::commonOprModel('config')->getShareConfig($rewardData['type']);
            $times = intval(oo::commonOprRedis('commom')->get(okeys::userShareTimes($uid,$rewardData['type'])));
            if($rewardData['uid'] == $uid){
                if($times >= $config['maxTimes']){
                    return oo::response(-2,[],'分享次数上限');
                }else{
                    oo::commonOprRedis('common')->setex(okeys::userShareTimes($uid,$rewardData['type']),$times+1,oo::todayDeadline());
                    if($config['selfType'] == 1){
                        $type = "money";
                    }else if($config['selfType'] == 2){
                        $type = 'spins';
                    }
                    oo::commonOprModel('member')->optProperty($uid,array($type=>intval($config['selfReward'])),'+',105,'分享奖励');
                    
					//每日分享次数统计
                    oo::commonOprModel('reward')->todayShareStatistics($rewardData['type'],$config,'selfType');
					
					return oo::response(1,[$type=>intval($config['selfReward'])]);
                }
            }else{
                if(isset($rewardData['recTimestamp'])){
                    if(count($rewardData['recTimestamp']) >= $times || in_array($timestamp,$rewardData['recTimestamp'])){
                        return oo::response(-3,[],'分享领取上限');
                    }
                }
                if($config['otherType'] == 1){
                    $type = 'money';
                }else if($config['otherType'] == 2){
                    $type = "spins";
                }
                oo::commonOprModel('member')->optProperty($uid,array($type=>intval($config['otherReward'])),'+',105,'分享奖励');
                $rewardData['recTimestamp'][] = $timestamp;
                oo::commonOprRedis('common')->setex(okeys::ShareRewardIdData($rewardId),json_encode($rewardData),oo::todayDeadline());
                
				//每日分享次数统计
                oo::commonOprModel('reward')->todayShareStatistics($rewardData['type'],$config,'otherType');
				
				return oo::response(1,[$type=>intval($config['otherReward'])]);
            }
        }else{
            return oo::response(-1,[],'分享过期');
        }
    }

    /**
     * 更新礼包
     * @param $param
     * @return string
     * Created by: Owen
     * Created on: 2019/12/5 19:59
     */
  	public function updateGift($param){
        $uid = oo::functions()->uint($param['uid']);
        if(empty($uid)){
            return oo::response(-1);
        }
        if(oo::commonOprModel('config')->getVersionGiftStatus($uid)){
            return oo::response(-2,[],'已领取奖励');
        }
        $version = oo::commonOprModel('user')->getUserVersion($uid);
        $config  = oo::commonOprModel('config')->getVersionConfig();
        if($config['gu_isReward'] == 0){
            return oo::response(-3,[],'无奖励版本');
        }
        $reward = json_decode($config['gu_reward'],true);
        $tb  = otable::versionGitfLog($uid);
        $key = okeys::versionGitfLog($uid,$version);
        oo::commonOprModel('member')->optProperty($uid,$reward,'+',104,'更新礼包:'.$version);
        oo::commonOprRedis('User')->setex($key,1,3*24*60*60);
        $time = time();
        $sql  = " INSERT INTO {$tb}(id, uid, version, time, reward) VALUES( NULL,{$uid},'{$version}','{$time}','{$config['reward']}')";
        oo::commonOprDb('common')->query($sql);
		
		//更新公告领取率
        oo::commonOprModel('statistics')->updateVersionSta($uid);
		
        return oo::response(1,$reward,'领取成功',true);
    }

    /**
     * 偷打活动奖励
     * @param $param
     * @return string
     * Created by: Owen
     * Created on: 2019/12/5 19:59
     */
  	public function stealAttack($param){
        $uid = oo::functions()->uint($param['uid']);
        if(empty($uid)){
            return oo::response(-1);
        }
        return oo::commonOprModel('activity')->rewardStealAttack($uid);
    }

    /**
     * 大转盘倍率领取
     * @param $param
     * @return string
     * Created by: Owen
     * Created on: 2019/12/5 19:59
     */
    public function turnTableBetReward($param){
        $uid = oo::functions()->uint($param['uid']);
        if(empty($uid)){
            return oo::response(-1);
        }

        $ret = oo::commonOprModel('advertisement')->getRemainCount($uid,13);
        if(empty($ret)){
            return oo::response(-4,[],'广告未开启');
        }else if($ret['remain']<=0){
            return oo::response(-5,[],'次数不足');
        }

        $data = oo::commonOprModel('slot')->getTurntableBetReward($uid);
        $code = json_decode($data,true);
        if($code['code'] == 1){
            oo::commonOprModel('advertisement')->addCount($uid,13);
        }
        return $data;
    }

    public function setSpinMonthly($param){
        $uid = oo::functions()->uint($param['uid']);
        if(empty($uid)){
            return oo::response(-1);
        }
        $type = 1;
        $endTime = strtotime("23:59:59") + 29*24*60*60;
        $expire  = $endTime - time();
//        ocache::t_userinfo($uid)->set(okeys::SpinMonthly($uid,$type),160,$expire);
        oo::commonOprRedis('usercache')->setex(okeys::SpinMonthly($uid,$type),160,$expire);
        return oo::response(1);
    }


    /**
     * 体力月卡
     * @param $param
     * @return string
     * Created by: Owen
     * Created on: 2019/12/10 11:58
     */
    public function spinMonthly($param){
        $uid = oo::functions()->uint($param['uid']);
        if(empty($uid)){
            return oo::response(-1);
        }
        $gid = oo::functions()->uint($param['gid']);
        $gid = empty($gid)?1001:$gid;
        $reward = oo::commonOprModel('payment')->getSpinMonthly($uid,$gid);
        if(empty($reward)){
            return oo::response(-2,[],'请先购买');
        }
//        $today = ocache::t_userinfo($uid)->get(okeys::SpinMonthlyDaily($uid,$gid));//过度-上线后删除
//        if(!$today){//过度-上线后删除
            $today = oo::commonOprRedis('usercache')->get(okeys::SpinMonthlyDaily($uid,$gid));
//        }//过度-上线后删除
        if(!empty($today)){
            return oo::response(-3,[],'已领取过');
        }
//        ocache::t_userinfo($uid)->set(okeys::SpinMonthlyDaily($uid,$gid),1,oo::todayDeadline());
        oo::commonOprRedis('usercache')->setex(okeys::SpinMonthlyDaily($uid,$gid),1,oo::todayDeadline());
        $reward = json_decode($reward,true);
        oo::commonOprModel('member')->optProperty($uid,array('spins'=>$reward['num']),'+',330,'体力月卡');
        return oo::response(1,['num'=>$reward],'');
    }

    /**
     * 节日收集进度奖励
     * @param $param
     * @return string
     * Created by: Owen
     * Created on: 2019/12/10 17:07
     */
    public function holidayCollect($param){
        $uid = oo::functions()->uint($param['uid']);
        $stage = oo::functions()->uint($param['stage']);
        if(empty($uid) || empty($stage)){
            return oo::response(-1);
        }
        return oo::commonOprModel('activity')->rewardHolidayCollect($uid,$stage);
    }

    /**
     * 建筑大师奖励
     * @param $param
     * @return string
     * Created by: Owen
     * Created on: 2019/12/17 16:16
     */
    public function buildMaster($param){
        $uid = oo::functions()->uint($param['uid']);
        if (empty($uid)) {
            return oo::response(-1);
        }
        return oo::commonOprModel('activity')->rewardBuildMaster($uid);
    }

    /**
     * 使用背包道具
     * @param $param
     * @return string
     * Created by: Owen
     * Created on: 2019/12/23 10:47
     */
    public function props($param){
        $uid = oo::functions()->uint($param['uid']);
        $id  = oo::functions()->uint($param['id']);
        $num = max(intval($param['num']),1);
        if (empty($uid) || empty($id)) {
            return oo::response(-1);
        }

        $argc = [];
        $bless  = oo::functions()->escape($param['bless']);
        if($bless){
            if(count(explode('-',$bless))>= 2){
                $argc['bless'] = 2;
            }else {
                $argc['bless'] = 1;
            }
        }
        return oo::commonOprModel('props')->useUserProps($uid,$id,$num,$argc);
    }

    /**
     * 夺宝进度奖励
     * @param $param
     * @return string
     * Created by: Owen
     * Created on: 2020/1/8 16:58
     */
    public function luckyProgress($param){
        $uid = oo::functions()->uint($param['uid']);
        $progress = oo::functions()->uint($param['progress']);
        if (empty($uid) || empty($progress)) {
            return oo::response(-1);
        }
        return oo::commonOprModel('luckydraw')->rewardProgress($uid,$progress);
    }

    /**
     * 领取邀请奖励
     * @param $param
     * @return string
     * Created by: Owen
     * Created on: 2020/1/8 17:50
     */
    public function invite($param){
        $uid = oo::functions()->uint($param['uid']);
        $toUid = oo::functions()->uint($param['toUid']);
        if (empty($uid) || empty($toUid)) {
            return oo::response(-1);
        }
        return oo::response(1,[],'');
    }

    public function getVipWeekReward($param){
        $uid = oo::functions()->uint($param['uid']);
        if (empty($uid)) {
            return oo::response(-1);
        }
        return oo::commonOprModel('newvip')->getUserVipWeekReward($uid);
    }

    public function getAcRankReward($param){
        $uid = oo::functions()->uint($param['uid']);
        if (empty($uid)) {
            return oo::response(-1);
        }
        return oo::commonOprModel('activity')->rewardRank($uid);
    }

    public function getAcRankProgressReward($param){
        $uid = oo::functions()->uint($param['uid']);
        if (empty($uid)) {
            return oo::response(-1);
        }
        return oo::commonOprModel('activity')->rewardRankProgress($uid);
    }

    public function getMiningGameReward($param){
        $uid = oo::functions()->uint($param['uid']);
        $mineral = oo::functions()->escape($param['mineral']);
        if (empty($uid) || empty($mineral)) {
            return oo::response(-1);
        }
        return oo::commonOprModel('mining')->getMiningGameReward($uid,$mineral);
    }

    /**
     * 领取签到进度奖励
     * @param $param
     * @return string
     * Created by: Owen
     * Created on: 2020/4/24 16:39
     */
    public function signInProgress($param){
        $uid = oo::functions()->uint($param['uid']);
        $progress = oo::functions()->uint($param['progress']);
        if (empty($uid) || empty($progress)) {
            return oo::response(-1);
        }
        return oo::commonOprModel('member')->getSignInProgressReward($uid,$progress);
    }

    /**
     * 最大关卡奖励
     * @param $param
     * @return string
     * Created by: Owen
     * Created on: 2020/4/24 18:49
     */
    public function maxLevel($param){
        $uid = oo::functions()->uint($param['uid']);
        if (empty($uid)) {
            return oo::response(-1);
        }
        return oo::commonOprModel('level')->getMaxLevelReward($uid);
    }
}
