<?php
defined( 'IN_WEB') or die( 'Include Error!');
/**
 * 奖励
 */
class Reward {

    /**
     * 金蛋收集活动
     * @param $param
     * @return false|string
     * Created by: Owen
     * Created on: 2020/3/27 11:05
     */
    public function symbol($param){
        $uid = oo::functions()->uint($param['uid']);
        $version = oo::commonOprModel('user')->getUserVersion($uid);
        $do = '';
        if(oo::compareVersion($version,'1.7.7')){
            if(oo::commonOprModel('activitynew')->activityLock($uid,'super_collect')){
                $do = 'super_collect';
            }
        }
        if(!$do){
            $ret = ['code'=>0,'msg'=>"没有可领奖的活动"];
        }else{
            $ret = oo::commonOprModel('activitynew')->receiveReward($uid,$do);
            $ret = json_encode($ret,JSON_NUMERIC_CHECK);
        }
        return $ret;
    }

    /**
     * 广告奖励
     * @param $param
     * @return string
     * Created by: Owen
     * Created on: 2020/3/27 12:10
     */
    public function advertisement($param){
        $uid = oo::functions()->uint($param['uid']);
        $id  = oo::functions()->uint($param['id']);
        $time  = oo::functions()->uint($param['time']);
        $sign= oo::functions()->escape($param['sign']);
        if(empty($uid) || empty($id)|| empty($sign)){
            return oo::response(-1,[],'参数错误');
        }
        $redisSign = oo::commonOprRedis('common')->get(okeys::lastRedisSign($uid));
        $serverSign = "{$uid}#2019$"."SUPERANT{$time}&^%";
        if($sign != md5($serverSign) || $sign == $redisSign){
            return oo::response(-2,[],$sign.'签名异常'.$redisSign.'签名异常'.$serverSign);
        }
        $ret = oo::commonOprModel('advertisement')->getRemainCount($uid,$id);
        if(empty($ret)){
            return oo::response(-3,[],'广告未开启');
        }
        if($ret['remain']<=0){
            return oo::response(-4,[],'次数不足');
        }
        if(!empty($ret['reward']) && $ret['reward']['specialReward'] == 0){
            oo::commonOprModel('member')->optProperty($uid,$ret['reward']['reward'],'+',108,"广告获得:视频Id-{$id}");
            oo::commonOprModel('advertisement')->addCount($uid,$id);
            oo::commonOprRedis('common')->setex(okeys::lastRedisSign($uid),$sign,3600);
            $userAssets  = oo::commonOprModel('member')->getUserAssetsInfo($uid);
            return oo::response(1,['money'=>$userAssets['money'],'spins'=>$userAssets['spins']],'奖励发放成功');
        }else{
            switch ($id){
                case 11:
                    $chatNum = intval(oo::commonOprRedis('common')->hGet(okeys::chatMaxNum(),$uid));
                    oo::commonOprRedis('common')->hSet(okeys::chatMaxNum(),$uid,$chatNum-5);
                    break;
            }
            oo::commonOprModel('advertisement')->addCount($uid,$id);
            return oo::response(1);
        }
    }

    /**
     * 卡组奖励
     * @param $param
     * @return false|string
     * Created by: Owen
     * Created on: 2020/3/27 12:11
     */
    public function cardGroup($param){
        $uid = oo::functions()->uint($param['uid']);
        $gid = oo::functions()->uint($param['gid']);
        if( empty($uid) ){
            $data = array('code' => -1,'desc'=>'参数错误！');
            return json_encode($data);
        }
        //卡组领奖uid+gid加锁
        if(!funs::concurrentLock($uid.$gid,'cardGroup')){
            return oo::response(-1,[],'频繁操作');
        }
        if($gid > 100000){//已弃用不考虑
            $gid  = $gid - 100000;
            $data = oo::commonOprModel('card')->rewardExclusiveGroup($uid,$gid);
        }else{
            $data = oo::commonOprModel('card')->cardReward($uid,$gid);
        }
        if(!$data){
            return oo::response(-1,[]);
        }else{
            return oo::response(1,$data);
        }
    }

    /**
     * 卡组进度
     * @param $param
     * @return false|string
     * Created by: Owen
     * Created on: 2020/3/27 12:17
     */
    public function cardProgress($param){
        $uid = oo::functions()->uint($param['uid']);
        if(empty($uid)){
            return oo::response(-1,[],"参数错误");
        }
        $data = oo::commonOprModel('card')->progressReward($uid);
        if(!$data){
            return oo::response(-1,[]);
        }else{
            return oo::response(1,$data);
        }
    }

    /**
     * 兑换码
     * @param $param
     * @return false|string
     * Created by: Owen
     * Created on: 2020/3/27 15:13
     */
    function exchange($param) {
        $uid  = oo::functions()->uint($param['uid']);
        $code = oo::functions()->escape($param['code']);
        if (empty($uid) || empty($code) ) {
            return oo::response(-1,[],"参数错误");
        }
        $data = oo::commonOprModel('exchangecode')->exchange($code, $uid);
        if($data['code'] == -3){
            $userInfo = oo::commonOprModel('member')->getUserInfo($code);
            if($userInfo){
                return oo::commonOprModel('invite')->inviteUrlRecord($uid,$code);
            }
        }
        return json_encode($data,JSON_NUMERIC_CHECK);
    }

    /**
     * 建筑大师奖励
     * @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);
        }
        $aid = oo::functions()->uint($param['aid']);
        if(!$aid){
            return oo::response(-1);
        }
        $ret = oo::commonOprModel('activitynew')->receiveBuildMasterReward($uid,$aid);
        $ret =json_encode($ret);
        return $ret;
    }

    /**
     * 排行榜活动进度奖励
     * @param $param
     * @return string
     * Created by: Owen
     * Created on: 2020/3/27 19:51
     */
    public function acRankProgress($param){
        $uid = oo::functions()->uint($param['uid']);
        if (empty($uid)) {
            return oo::response(-1);
        }
        $ret = oo::commonOprModel('activitynew')->receiveReward($uid,'rank');
        $ret =json_encode($ret);
        return $ret;
    }

    /**
     * 排行榜活动奖励
     * @param $param
     * @return string
     * Created by: Owen
     * Created on: 2020/3/27 19:53
     */
    public function acRank($param){
        $uid = oo::functions()->uint($param['uid']);
        if (empty($uid)) {
            return oo::response(-1);
        }
        if(!funs::concurrentLock($uid,'acrank',2)){
            return oo::response(-1);
        }
        $aid = oo::functions()->uint($param['aid']);
        if(empty($aid)){
            return oo::response(-1);
        }
        $ret = oo::commonOprModel('activitynew')->receiveRankReward($uid,$aid);
        $ret = json_encode($ret);
        return $ret;
    }

    /**
     * 矿工游戏活动
     * @param $param
     * @return string
     * Created by: Owen
     * Created on: 2020/3/27 19:54
     */
    public function miningGame($param){
        $uid = oo::functions()->uint($param['uid']);
        $mineral = oo::functions()->escape($param['mineral']);
        $levelId = oo::functions()->escape($param['levelId']);
        if (empty($uid) || empty($mineral)) {
            return oo::response(-1);
        }
        return oo::commonOprModel('mining')->getMiningGameReward2($uid,$mineral,$levelId);
    }

    /**
     * vip 每周奖励,1.8.5版本后改为订阅会员每日领取体力
     * @param $param
     * @return string
     * Created by: Owen
     * Created on: 2020/3/27 19:54
     */
    public function vipWeek($param){
        $uid = oo::functions()->uint($param['uid']);
        if (empty($uid)) {
            return oo::response(-1);
        }
        $version = oo::commonOprModel('user')->getUserVersion($uid);
        if(!oo::compareVersion($version,"1.8.5")){
            return oo::commonOprModel('newvip')->getUserVipWeekReward2($uid);
        }
        $subVip = oo::commonOprModel('newvip')->checkSubVip($uid);
        if(empty($subVip)){
            return oo::response(errorCode::CONDITION_NOT_RNOUGH);
        }
        $got = (int)oo::commonOprRedis('user')->hGet(okeys::userSubVipDaily($uid), 'vipDailySpins');
        if(!empty($got)){
            return oo::response(errorCode::OVER_RECEIVE_REWARD);
        }
        return oo::commonOprModel('newvip')->gainSubVipDailyReward($uid);
    }

    /**
     * 节日收集进度奖励
     * @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: 2020/1/8 17:50
     */
    public function invite($param){
        $uid   = oo::functions()->uint($param['uid']);
        $toUid = $param['toUid'];
        if (empty($uid) || empty($toUid)) {
            return oo::response(-1);
        }
        $toUidArr = [];
        if(oo::compareVersion(oo::commonOprModel('user')->getUserVersion($uid),'1.7.8')){
            $toUidArr = explode(',',$toUid);
        }else{
            $toUidArr[] = $toUid;
        }
        return oo::commonOprModel('invite')->getInviteRewardArr($uid,$toUidArr);

    }

    /**
     * 夺宝进度奖励
     * @param $param
     * @return string
     * Created by: Owen
     * Created on: 2020/1/8 16:58
     */
    public function luckyDrawProgress($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: 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);
        }
        $bless  = oo::functions()->escape($param['bless']); // bless：1卡組，2卡片
        return oo::commonOprModel('props')->useUserProps($uid,$id,$num,['bless'=>$bless]);
    }

    /**
     * 分享奖励
     * @param $param
     * @return string
     * Created by: Owen
     * Created on: 2019/11/29 20:19
     */
    public function share($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)) {
            return oo::response(-1,[],'分享过期');
        }
        $rewardData = json_decode($rewardData,true);
        $version = oo::commonOprModel('user')->getUserVersion($uid);
        if(oo::compareVersion($version,'1.10')){
            $shareKey   = 'base_key';//统一一个key了
            if(oo::commonOprRedis('common')->get(okeys::ShareIntervalTime($uid,$shareKey))){
                return oo::response(-2,[],'分享冷却');
            }
            $config = oo::commonOprModel('config')->getShareBaseConfig();
            //邀请奖励单独走原来的逻辑
            if(strpos($rewardId, 'INVITE_FRIEND') !== false){
                $shareKey = 'invite_key';
                $inviteConf = oo::commonOprModel('config')->getShareConfig('invite', 2);
                if(oo::compareVersion($version,'1.13')){
                    $config['reward'] = empty($inviteConf['reward']) ? [] : $inviteConf['reward']; //v1.13取消每日20体力奖励
                }else{
                    $config['reward'] = empty($inviteConf['reward']) ? [['propsId' => 15, 'num' => 20]] : $inviteConf['reward'];
                }
                $config['maxTimes'] = empty($inviteConf['maxTimes']) ? 1 : $inviteConf['maxTimes'];
                $config['intervalTime'] = empty($inviteConf['maxTimes']) ? 86400 : $inviteConf['intervalTime'];
            }
        }else if(oo::compareVersion($version,'1.7.4')){
            $shareKey   = $rewardData['type'];
            if(oo::commonOprRedis('common')->get(okeys::ShareIntervalTime($uid,$shareKey))){
                return oo::response(-2,[],'分享冷却');
            }
            $config = oo::commonOprModel('config')->getShareConfig($shareKey,true);
        }else{
            $shareKey   = $rewardData['type'];
            $config = oo::commonOprModel('config')->getShareConfig($shareKey);
        }
        $times = intval(oo::commonOprRedis('common')->get(okeys::userShareTimes($uid,$shareKey)));
        if($rewardData['uid'] == $uid){
            if($times >= $config['maxTimes']){
                return oo::response(-2,[],'分享次数上限');
            }else{
                oo::commonOprRedis('common')->setex(okeys::userShareTimes($uid,$shareKey),$times+1,oo::todayDeadline());
                $reward = [];
                if(oo::compareVersion($version,'1.7.4')){
                    foreach($config['reward'] as $row){
                        $res = oo::commonOprModel("member")->sendReward($uid,$row['propsId'],$row['num'],105,'分享奖励');//返回最终值
                        switch ($row['propsId']){
                            case 14:
                                $reward['money'] = $row['num'];
                                break;
                            case 15:
                                $reward['spins'] = $row['num'];
                                break;
                            case 22:
                                $reward['diamond'] = $row['num'];
                                break;
                        }
                    }
                    //分享冷却
                    $config['intervalTime'] && oo::commonOprRedis('common')->setex(okeys::ShareIntervalTime($uid,$shareKey),1,$config['intervalTime']);
                    $reward['expire'] = $config['intervalTime'] + time();
                }else{
                    switch ($config['selfType']){
                        case 1:
                            $reward = ['money' => intval($config['selfReward'])];
                            break;
                        case 2:
                            $reward = ['spins' => intval($config['selfReward'])];
                            break;
                    }
                    oo::commonOprModel('member')->optProperty($uid,$reward,'+',105,'分享奖励');
                }
                if(isset($reward['money'])){
                    $reward['coins'] = $reward['money'];
                }
                if(!oo::compareVersion($version,'1.10')){
                    oo::commonOprModel('reward')->todayShareStatistics($shareKey,$config,'selfType'); //每日分享次数统计
                }
                /**
                 * 通行证任务10 -- 分享游戏
                 */
                oo::commonOprModel('activitynew')->addTrafficPermitNum($uid,10);
                //召回-分享任务
//                if(oo::compareVersion($version,'1.8.4')){
//                    oo::commonOprModel('callback')->doTask($uid, 5, 1);
//                }
                return oo::response(1,$reward);
            }
        }else if(!oo::compareVersion($version,'1.7.4')){
            if(isset($rewardData['recTimestamp'])){
                if(count($rewardData['recTimestamp']) >= $times || in_array($timestamp,$rewardData['recTimestamp'])){
                    return oo::response(-3,[],'分享领取上限');
                }
            }
            if($config['otherType'] == 1){
                $reward = [
                    'money' => intval($config['otherReward'])
                ];
            }else if($config['otherType'] == 2){
                $reward = [
                    'spins' => intval($config['otherReward'])
                ];
            }else{
                return oo::response(-1,[],'分享无效');
            }
            oo::commonOprModel('member')->optProperty($uid,$reward,'+',105,'分享奖励');
            $rewardData['recTimestamp'][] = $timestamp;
            oo::commonOprRedis('common')->setex(okeys::ShareRewardIdData($rewardId),json_encode($rewardData),oo::todayDeadline());
            //每日分享次数统计
            oo::commonOprModel('reward')->todayShareStatistics($shareKey,$config,'otherType');
            if(isset($reward['money'])){
                $reward['coins'] = $reward['money'];
            }
            return oo::response(1,$reward);
        }else{
            return oo::response(-1,[],'分享无效');
        }
    }

    /**
     * 七日签到
     * @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);
    }

    /**
     * 偷打活动奖励
     * @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);
        }
        $aid = oo::commonOprModel('activitynew')->activityLock($uid,'steal');
        $do = $aid?'steal':'attack';
        $ret = oo::commonOprModel('activitynew')->receiveReward($uid,$do);
        $ret = json_encode($ret);
        return $ret;
    }

    /**
     * 大转盘倍率领取
     * @param $param
     * @return string
     * Created by: Owen
     * Created on: 2019/12/5 19:59
     */
    public function turnTableBet($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;
    }

    /**
     * 更新礼包
     * @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($uid);
        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 false|string
     * Created by: Owen
     * Created on: 2020/3/27 20:03
     */
    public function achieve($param){
        $uid = oo::functions()->uint($param['uid']);
        $aid = oo::functions()->uint($param['aid']);
        if(empty($uid) || empty($aid)){
            return oo::response(-1);
        }
        $data = oo::commonOprModel('achievement')->getAchieveReward($uid,$aid);
        return json_encode($data);
    }

    /**
     * 每日任务进度奖励
     * @param $param
     * @return string
     * Created by: Owen
     * Created on: 2020/3/27 20:06
     */
    public function taskProgress($param){
        $uid = oo::functions()->uint($param['uid']);
        $progress = oo::functions()->uint($param['progress']);
        $data = oo::commonOprModel('task')->getTaskProgressReward($uid, $progress);
        if($data){
            return oo::response(1,$data,'',true);
        }
        return oo::response(-2,[],"目标未达成");
    }

    /**
     * 回归奖励
     * @param $param
     * @return string
     * Created by: Owen
     * Created on: 2020/3/27 20:28
     */
    public function recall($param){
        $uid = oo::functions()->uint($param['uid']);
        if(empty($uid)){
            return oo::response(-1);
        }

        $recallData = oo::commonOprRedis('common')->get(okeys::recall($uid));
        if($recallData){
            $recallData = json_decode($recallData,true);
            $spins = 0;
            $totalCoins = 0;
            foreach ($recallData as $row){
                $coins = oo::commonOprModel('goods')->getGoodCoins($uid,oo::commonOprModel('config')->gidToCoins($row['reward']['coins']['gid']));
                $totalCoins += $coins * $row['reward']['coins']['bet'];
                $spins      += $row['reward']['spins'];
            }
            $ret = oo::commonOprModel('member')->optProperty($uid,['money' => $totalCoins ,'spins' => $spins],'+',103,'回归赠送');
            if($ret){
                $protectedTime = 600;
                oo::commonOprModel('member')->setUserStealProtect($uid,$protectedTime);
                $userAssetsInfo = oo::commonOprModel('member')->getUserAssetsInfo($uid);
                return oo::response(1,[
                    'curAssetsInfo' => [
                        'coins' => $userAssetsInfo['money'],
                        'spins' => $userAssetsInfo['spins']
                    ]
                ],"领取成功",true);
            }
            return oo::response(-2);
        }
        return oo::response(-1);
    }

    /**
     * 系统补偿
     * @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')->getSysCompensateConfig2($uid);
        if(empty($config)){
            return oo::response(-1);
        }
        oo::commonOprRedis('common')->hDel(okeys::UserSysCompen($config['id']),$uid);
        oo::commonOprRedis('common')->setex(okeys::UserSysCompenStatus($uid,$config['id']),1,max($config['endTime'] - time(),0) + 600);
        oo::commonOprModel('member')->optProperty($uid, $config['reward'], '+',997, '系统补偿:补偿ID'.$config['id']);
        //系统补偿领取率
        oo::commonOprModel('statistics')->sysCompensateSta($uid);
        $config['reward']['coins'] = $config['reward']['money'];
        return oo::response(1,$config['reward'],'',true);
    }

    /**
     * 每日任务领奖
     * @param $param
     * @return false|string
     * Created by: Owen
     * Created on: 2020/3/28 11:27
     */
    public function task($param) {
        $uid = oo::functions()->uint($param['uid']);
        $taskType = oo::functions()->escape($param['taskType']);
        $versionFlag = false;
        if (empty($uid)) {
            return oo::response(-1);
        }
        if(empty($taskType)){
            $id       = oo::functions()->uint($param['id']);
            $taskType = oo::commonOprModel('task')->taskMap($id);
            $versionFlag = true;
        }
        $data = oo::commonOprModel('task')->reward($uid, $taskType,$versionFlag);
        return json_encode($data,JSON_NUMERIC_CHECK);
    }

    /**
     * 领取签到进度奖励
     * @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);
    }

    /**
     * Fb 绑定奖励
     * @param $param
     * @return string
     * Created by: Owen
     * Created on: 2020/4/24 18:49
     */
    public function fbBind($param){
        $uid = oo::functions()->uint($param['uid']);
        if (empty($uid)) {
            oo::commonOprModel('statistics')->visitorToFbSta(-1,$uid);
            return oo::response(-1);
        }
        if(!oo::commonOprRedis('common')->get(okeys::FbBindReward($uid))){
            oo::commonOprModel('statistics')->visitorToFbSta(-2,$uid);
            return oo::response(-2);
        }
        $reward = [
            'money' => intval(oo::commonOprModel('readconfig')->getCon("Game","fbBindCoins")),
            'spins' => intval(oo::commonOprModel('readconfig')->getCon("Game","fbBindSpins")),
        ];
        $ret = oo::commonOprModel('member')->optProperty($uid, $reward, '+',109,'fb绑定');
        if($ret){
            oo::commonOprModel('statistics')->visitorToFbSta(1,$uid);
            oo::commonOprRedis('common')->delete(okeys::FbBindReward($uid));
        }
        return oo::response(-3);
    }

    /**
     * 星星兑换
     * @param $param
     * @return string
     * Created by: Owen
     * Created on: 2020/6/10 15:36
     */
    public function starExchange($param){
        $uid   = oo::functions()->uint($param['uid']);
        $propsId  = oo::functions()->uint($param['propsId']);
        $cards = oo::functions()->escape($param['cards']);
        if (empty($uid) || empty($cards) || empty($propsId)) {
            return oo::response(-1);
        }
        if(!funs::concurrentLock($uid,'successTwo',6)){
            return oo::response(-500,[],'请求频繁');
        }

        return oo::commonOprModel('card')->starExchange($uid,$propsId,$cards);
    }

    /**
     * 聚合广告
     * @param $param
     * @return string
     * Created by: Owen
     * Created on: 2020/7/14 17:39
     */
    public function aggregate($param){
        $uid  = oo::functions()->uint($param['uid']);
        $id   = oo::functions()->uint($param['id']);
        $time = oo::functions()->uint($param['time']);
        $sign = oo::functions()->escape($param['sign']);
        $petId= oo::functions()->uint($param['petId']);
        if(empty($uid) || empty($id)|| empty($sign)){
            return oo::response(-1,[],'参数错误');
        }
        //广告领奖设置15秒并发锁
        if(!funs::concurrentLock($uid,'aggregate', 15)){
            return oo::response(-1,[],'频繁请求');
        }
        $redisSign = oo::commonOprRedis('common')->get(okeys::lastRedisSign($uid));
        $serverSign = "{$uid}#2019$"."SUPERANT{$time}&^%";
        if($sign != md5($serverSign) || $sign == $redisSign){
            return oo::response(-2,[],$sign.'签名异常'.$redisSign.'签名异常'.$serverSign);
        }
        return oo::commonOprModel('Advertisement')->rewardAggregate($uid,$id,['petId'=>$petId]);
    }

    /**
     * 领取宝藏活动任务奖励
     * @param $param
     * @return string
     * Created by: Owen
     * Created on: 2020/7/27 20:16
     */
    public function treasureTask($param){
        $uid = oo::functions()->uint($param['uid']);
        if(empty($uid)){
            return oo::response(-1,[],'参数错误');
        }
        $version = oo::commonOprModel('user')->getUserVersion($uid);
        if(oo::compareVersion($version,'1.7.2')){
            return oo::commonOprModel('activitynew')->getTreasureTaskReward($uid);
        }else{
            return oo::commonOprModel('activity')->getTreasureTaskReward($uid);
        }
    }

    /**
     * 领取宝藏活动入门奖励
     * @param $param
     * @return string
     * Created by: Owen
     * Created on: 2020/7/28 16:51
     */
    public function getTreasureGift($param){
        $uid = oo::functions()->uint($param['uid']);
        if(empty($uid)){
            return oo::response(-1,[],'参数错误');
        }
        $version = oo::commonOprModel('user')->getUserVersion($uid);
        if(oo::compareVersion($version,'1.7.2')) {
            return oo::commonOprModel('activitynew')->getTreasureGift($uid);
        }else{
            return oo::commonOprModel('activity')->getTreasureGift($uid);
        }

    }

    /**
     * 分享链奖励
     * @param $param
     * @return string
     * Created by: Owen
     * Created on: 2020/8/5 9:37
     */
    public function shareLink($param){
        $uid = oo::functions()->uint($param['uid']);
        $id  = oo::functions()->uint($param['id']);
        if(empty($uid) || empty($id)){
            return oo::response(-1,[],'参数错误');
        }
        return oo::commonOprModel('member')->getShareLinkReward($uid,$id);
    }

    /**
     * Notes:领取邀请好友奖励（活动）
     * User: wsc
     * Time: 2020/9/14 16:00
     * @param $param
     * @return false|string
     */
    public function getInviteReward($param){
        $uid = oo::functions()->uint($param['uid']);
        $aid  = oo::functions()->uint($param['aid']);
        $stage = oo::functions()->uint($param['stage']);
        if(!funs::concurrentLock($uid,'reward_getInviteReward')){
            return oo::response(-1,[],'频繁操作');
        }
        if(empty($uid) || empty($aid)||empty($stage)){
            return oo::response(-1,[],'参数错误');
        }
        return json_encode(oo::commonOprModel('activitynew')->receiveInviteReward($uid,$aid,$stage));
    }

    /**
     * 领取邀请好友进度奖励（常规功能）
     * @param $param
     * @return string
     * Created by: Owen
     * Created on: 2020/9/21 10:53
     */
    public function receiveInviteStageReward($param){
        $uid = oo::functions()->uint($param['uid']);
        $stage = oo::functions()->uint($param['stage']);
        if(!funs::concurrentLock($uid,'reward_receiveInviteStageReward')){
            return oo::response(-1,[],'频繁操作');
        }
        if(empty($uid) ||empty($stage)){
            return oo::response(-1,[],'参数错误');
        }
        $ret = oo::commonOprModel('invite')->receiveInviteStageReward($uid,$stage);
        return $ret;
    }

    /**
     * 上报邀请链接 A 邀请 B，B上报接口
     * @param $param
     * @return string
     * Created by: Owen
     * Created on: 2020/8/26 12:29
     */
    public function inviteUrlRecord($param){
        $uid  = oo::functions()->uint($param['uid']);
        $code = oo::functions()->escape($param['code']);
        if(empty($uid) || empty($code)){
            return oo::response(-1,[],'参数错误');
        }
        $toUid = base64_decode($code);
        if(empty($toUid) || $uid == $toUid){
            return oo::response(-1,[],'参数错误');
        }
        oo::commonOprRedis("statistics")->hIncrBy(okeys::friendRecallSta(2),$uid,1);//好友召回统计
        return oo::commonOprModel('invite')->inviteUrlRecord($uid,$toUid);
    }

    /**
     * 上报邀请链接
     * @param $param
     * @return string
     * Created by: Owen
     * Created on: 2020/8/26 12:29
     */
    public function inviteUrlReward($param){
        return oo::response(1,[]);
    }

    /**
     * Notes:领取累充奖励
     * User: wsc
     * Time: 2020/10/13 12:17
     * @param $param
     * @return string
     */
    public function receiveSumPayActReward($param){
        $uid = oo::functions()->uint($param['uid']);
        $stage = oo::functions()->uint($param['stage']);
        if(!funs::concurrentLock($uid,'receiveSumPayActReward')){
            return oo::response(-1,[],'频繁操作');
        }
        if(empty($uid) ||empty($stage)){
            return oo::response(-1,[],'参数错误');
        }
        $aid = oo::commonOprModel('activitynew')->activityLock($uid,'accumulated_pay');
        if(!$aid){
            return oo::response(-1,[],'当前活动已结束 ');
        }
        $ret = oo::commonOprModel('activitynew')->receiveSumPayActReward($uid,$aid,$stage);
        return json_encode($ret);
    }

    /**
     * Notes:公共进度活动领奖(体力大师)
     * User: wsc
     * Time: 2021/2/5 15:03
     * @param $param
     * @return false|string
     */
    public function receiveCommonRateReward($param){
        $uid = oo::functions()->uint($param['uid']);
        $stage = oo::functions()->uint($param['stage']);
        $type = $param['type'];
        if(!funs::concurrentLock($uid,'receiveCommonRateReward')){
            return oo::response(errorCode::API_FREQUENT_REQUESTS,[],'频繁操作');
        }
        if(empty($uid) ||empty($stage)||empty($type)||!in_array($type,['spins_master'])){
            return oo::response(errorCode::REQUEST_PARAM_ERROR,[],'参数错误');
        }
        $aid = oo::commonOprModel('activitynew')->activityLock($uid,$type);
        if(!$aid){
            return oo::response(errorCode::NOT_FOUND_ACTIVITY,[],'当前活动已结束 ');
        }
        $ret = oo::commonOprModel('activitynew')->receiveCommonRateReward($uid,$aid,$type,$stage);
        return json_encode($ret);
    }
    /**
     * 领取通行证奖励
     * @param $param
     * @return string
     * Created by: Owen
     * Created on: 2020/10/21 15:28
     */
    public function trafficPermit($param){
        $uid  = oo::functions()->uint($param['uid']);
        $levelId = oo::functions()->uint($param['levelId']);
        $type = oo::functions()->uint($param['type']);
        if(!funs::concurrentLock($uid,'trafficPermit')){
            return oo::response(-1,[],'频繁操作');
        }
        if(empty($uid) || empty($type)){
            return oo::response(-1,[],'参数错误');
        }
        return oo::commonOprModel('activitynew')->getTrafficPermitReward($uid,$levelId,$type);
    }

    /**
     * 一键领取通行证奖励
     * @param $param
     * @return string
     * Created by: Richard
     * Created on: 2021/04/21 14:27
     */
    public function allTrafficPermitReward($param){
        $uid  = oo::functions()->uint($param['uid']);
        if(!funs::concurrentLock($uid,'alltrafficPermit')){
            return oo::response(-1,[],'频繁操作');
        }
        if(empty($uid)){
            return oo::response(-1,[],'参数错误');
        }
        return oo::commonOprModel('activitynew')->allTrafficPermitReward($uid);
    }

    /**
     * 矿工小游戏阶段奖励
     * @param $param
     * @return string
     * Created by: Owen
     * Created on: 2020/11/5 10:06
     */
    public function gameStage($param){
        $uid  = oo::functions()->uint($param['uid']);
        $levelId = oo::functions()->uint($param['levelId']);
        if(empty($uid) || empty($levelId)){
            return oo::response(-1,[],'参数错误');
        }
        if(!funs::concurrentLock($uid,'gameStage')){
            return oo::response(-1,[],'频繁操作');
        }
        return oo::commonOprModel('mining')->gameStageReward($uid,$levelId);
    }

    /**
     * 领取奖励
     * @param $param
     * @return string
     * Created by: Owen
     * Created on: 2020/12/3 9:58
     */
    public function userGift($param){
        $uid = oo::functions()->uint($param['uid']);
        $id  = oo::functions()->uint($param['id']);
        if(empty($uid) || empty($id)){
            return oo::response(-1,[],'参数错误');
        }
        if(!funs::concurrentLock($uid,'userGift')){
            return oo::response(-1,[],'频繁操作');
        }
        return oo::commonOprModel('member')->rewardUserGift($uid,$id);
    }

    /**
     * 领取体力存钱罐奖励,修改于21/07/22
     * @param $param
     * @return string
     * Created by: Owen
     * Created on: 2020/12/5 14:22
     */
    public function safeBox($param){
        $uid         = oo::functions()->uint($param['uid']);
        $progressId  = $param['progressId']??1;
        if(empty($uid) || empty($progressId)){
            return oo::response(-1,[],'参数错误');
        }
        if(!funs::concurrentLock($uid,'safeBox')){
            return oo::response(-1,[],'频繁操作');
        }
        return oo::commonOprModel('activitynew')->rewardSteelSafe($uid,$progressId);
    }

    /**
     * 金币老虎机 1.7.8 新接口
     * @param $param
     * @return mixed
     * Created by: Owen
     * Created on: 2020/12/31 14:27
     */
    public function userTreasure($param){
        $uid   = oo::functions()->uint($param['uid']);
        $coins = oo::functions()->uint($param['bet']);
        $version = oo::commonOprModel('user')->getUserVersion($uid);
        if(oo::compareVersion($version,'1.8.1')){
            return oo::response(-1,[],'old api');
        }
        return oo::commonOprModel('activitynew')->treasureSlotReward($uid,$coins);
    }

    /**
     * 商店横条
     * @param $param
     * @return string
     * Created by: Owen
     * Created on: 2021/1/21 21:06
     */
    public function freeBar($param){
        $uid = oo::functions()->uint($param['uid']);
        if( empty($uid) ){
            return oo::response(-1,[],'参数错误');
        }
        return oo::commonOprModel('goods')->rewardFreeBar($uid);
    }

    /**
     * 基金活动奖励
     * @param $param
     * @return string
     * Created by: Owen
     * Created on: 2021/1/21 21:06
     */
    public function fund($param){
        $uid      = oo::functions()->uint($param['uid']);
        $repairId = oo::functions()->uint($param['repairId']);
        if( empty($uid) ){
            return oo::response(-1,[],'参数错误');
        }
        return oo::commonOprModel('activitynew')->rewardFund($uid,$repairId);
    }

    /**
     * Notes:四选一活动-领奖离开
     * User: wsc
     * Time: 2021/3/10 12:30
     * @param $param
     * @return string
     */
    public function rewardEasterEgg($param){
        $uid  = oo::functions()->uint($param['uid']);
        if(empty($uid)){
            return oo::response(errorCode::REQUEST_PARAM_ERROR,[],'参数错误');
        }
        if(!funs::concurrentLock($uid,'rewardEasterEgg')){
            return oo::response(errorCode::API_FREQUENT_REQUESTS,[],'频繁操作');
        }
        $aid = oo::commonOprModel('activitynew')->activityLock($uid,'easter_egg');
        if(!$aid){
            return  oo::response(errorCode::NOT_FOUND_ACTIVITY,[]);
        }
        return oo::commonOprModel('activitynew')->rewardEasterEgg($uid);
    }

    /**
     * 通行证分享上报
     * @param $param
     * @return string
     * Created by: Owen
     * Created on: 2021/3/11 17:17
     */
    public function trafficPermitShare($param){
        $uid  = oo::functions()->uint($param['uid']);
        if(empty($uid)){
            return oo::response(errorCode::REQUEST_PARAM_ERROR,[],'参数错误');
        }
        if(!funs::concurrentLock($uid,'trafficPermitShare',60)){
            return oo::response(errorCode::API_FREQUENT_REQUESTS,[],'频繁操作');
        }
        oo::commonOprModel('activitynew')->addTrafficPermitNum($uid,10);//通行证任务10 分享游戏
        return oo::response(1);
    }

    /**
     * 邀请者A领取受邀请者B绑定FB奖励
     * @param $param
     * @return string
     * Created by: Owen
     * Created on: 2021/3/12 15:09
     */
    public function inviteeBindFb($param){
        $uid = oo::functions()->uint($param['uid']);
        if (empty($uid)) {
            return oo::response(errorCode::REQUEST_PARAM_ERROR, [], '参数错误');
        }
        if (!funs::concurrentLock($uid, 'inviteeBindFb', 60)) {
            return oo::response(errorCode::API_FREQUENT_REQUESTS, [], '频繁操作');
        }
        return oo::commonOprModel('invite')->rewardInviteeBindFb($uid);
    }

    /**
     * 受邀请者绑定FB奖励额外奖励(---弃用)
     * @param $param
     * @return string
     * Created by: Owen
     * Created on: 2021/3/23 17:47
     */
    public function inviteeBindFbExtra($param){
        $uid = oo::functions()->uint($param['uid']);
        if (empty($uid)) {
            return oo::response(errorCode::REQUEST_PARAM_ERROR, [], '参数错误');
        }
        if (!funs::concurrentLock($uid, 'inviteeBindFbExtra', 60)) {
            return oo::response(errorCode::API_FREQUENT_REQUESTS, [], '频繁操作');
        }
        return oo::commonOprModel('invite')->RewardInviteeBindFbExtra($uid);
    }

    /**
     * 领取好友回归积分
     * @param array $param
     */
    public function receiveCallbackSuccScore($param) {
        $uid = oo::functions()->uint($param['uid']);
        $fid = oo::functions()->uint($param['fid']);
        if(empty($uid)||empty($fid) || $uid == $fid){
            return oo::response(errorCode::REQUEST_PARAM_ERROR);
        }

        if(!funs::concurrentLock($uid,'reward_callback',2)){
            return oo::response(errorCode::API_FREQUENT_REQUESTS,[],'频繁操作');
        }

        $ret = oo::commonOprModel('callback')->receiveBindScore($uid,$fid);
        return $ret;
    }

    /**
     * Notes:领取任务积分
     * User: wsc
     * Time: 2021/3/17 18:19
     * @param $param
     * @return string
     */
    public function receiveCallBackTaskScore($param){
        $uid = oo::functions()->uint($param['uid']);
        $tid = oo::functions()->uint($param['tid']);
        if(empty($uid)||empty($tid)){
            return oo::response(errorCode::REQUEST_PARAM_ERROR);
        }
        if(!funs::concurrentLock($uid,'receiveCallBackTaskScore',2)){
            return oo::response(errorCode::API_FREQUENT_REQUESTS,[],'频繁操作');
        }
        $ret = oo::commonOprModel('callback')->receiveTaskScore($uid,$tid);
        return $ret;
    }

    /**
     * Notes:领取召回进度奖励
     * User: wsc
     * Time: 2021/3/17 18:34
     * @param $param  $param['type'] 1发起者领奖 2被招回者领奖 $param['stage']要领取的阶段
     * @return string
     */
    public function receiveCallBackRateReward($param){
        $uid = oo::functions()->uint($param['uid']);
        $type= oo::functions()->uint($param['type']);
        $stage= oo::functions()->uint($param['stage']);
        if(empty($uid)||empty($type)||empty($stage)){
            return oo::response(errorCode::REQUEST_PARAM_ERROR);
        }
        if(!funs::concurrentLock($uid,'receiveCallBackTaskScore',2)){
            return oo::response(errorCode::API_FREQUENT_REQUESTS,[],'频繁操作');
        }
        $ret = oo::commonOprModel('callback')->receiveRateReward($uid,$stage,$type);
        return $ret;
    }

    public function receiveItemReward($param){
        $uid = oo::functions()->uint($param['uid']);
        $toUid = oo::functions()->uint($param['toUid']);
        $place= oo::functions()->uint($param['place']);
        if(empty($uid)||empty($toUid)||empty($place)){
            return oo::response(errorCode::REQUEST_PARAM_ERROR);
        }
        if(!funs::concurrentLock($uid,'receiveItemReward',2)){
            return oo::response(errorCode::API_FREQUENT_REQUESTS,[],'频繁操作');
        }
        $ret = oo::commonOprModel('activitynew')->sendItemReward($uid,$toUid,$place);
        return $ret;
    }

    /**
     * 领取更新公告奖励
     */
    public function announcementReward($param) {
        $uid = oo::functions()->uint($param['uid']);
        if(empty($uid)){
            return oo::response(errorCode::REQUEST_PARAM_ERROR);
        }

        $rewardInfo = oo::commonOprModel('notice')->getNowAnnouncementNew($uid);
        if(empty($rewardInfo['reward']) || empty($rewardInfo['reward']['props'])){
            return oo::response(errorCode::NONE_REWARD); //没有奖励可领取
        }
        $version = oo::commonOprModel('user')->getUserVersion($uid);
        if(empty($rewardInfo['version']) && !oo::compareVersion($version, $rewardInfo['version'])){
            return oo::response(errorCode::CONDITION_NOT_RNOUGH); //未升级到新版本
        }
        if(!empty($rewardInfo['got'])){
            return oo::response(errorCode::OVER_RECEIVE_REWARD); //已领取
        }
        $key = okeys::announcementReward($uid);
        $ttl = oo::commonOprRedis('userinfo')->ttl($key);
        //设置已领取
        $ret = oo::commonOprRedis('userinfo')->setex($key, json_encode(['got' => 1, 'version' => $version]), $ttl+24*3600);
        if(!$ret){
            return oo::response(errorCode::SYSTEM_ERROR); //系统异常
        }
        $reward = $rewardInfo['reward']['props'];
        foreach ($reward as $row){
            oo::commonOprModel('member')->sendReward($uid,$row['propsId'],$row['num'],151,'公告更新奖励',[],$row);
        }
        return oo::response(errorCode::SUCCESS,$rewardInfo['reward']);
    }

    /**
     * Notes:1+1礼包免费奖励
     * User: hss
     * Time: 2021/8/9 12:10
     * @param $param
     * @return string
     */
    public function onePlusGiftReward($param){
        $uid = oo::functions()->uint($param['uid']);
        if(empty($uid)){
            return oo::response(errorCode::REQUEST_PARAM_ERROR);
        }
        if(!funs::concurrentLock($uid,'onePlusGiftReward',2)){
            return oo::response(errorCode::API_FREQUENT_REQUESTS,[],'频繁操作');
        }
        $aid = oo::commonOprModel('activitynew')->activityLock($uid,'one_plus_gift');
        if(!$aid){
            return oo::response(-1,[],'当前活动已结束 ');
        }
        $ret = oo::commonOprModel('activitynew')->onePlusGiftReward($uid,$aid);
        return $ret;
    }
}
