<?php
defined('IN_WEB') or die('Include Error!');

/**
* 活动相关
*/
class Activity{
    /**
     * 加入排行榜活动房间
     * @param $param
     * @return string
     * Created by: Owen
     * Created on: 2020/3/6 15:07
     */
    public function joinRankRoom($param){
        $uid = oo::functions()->uint($param['uid']);
        if(empty($uid)){
            return oo::response(-1);
        }

        /*
        $version = oo::commonOprModel('user')->getUserVersion($uid);
        $type = "rank";
        if(oo::compareVersion($version,'1.13.0')){
            $aid = oo::functions()->uint($param['aid']);
            $actInfo = oo::commonOprModel('activitynew')->getActivity($aid);
            if(empty($actInfo) || $actInfo["is_online"]!=1 || !in_array($actInfo["type"],["rank","rank2"])){
                $aid = 0;
            }else{
                $type = $actInfo["type"];
            }
        }else{
            $aid = oo::commonOprModel('activitynew')->activityLock($uid,'rank');
        }
        */

        $aid = oo::commonOprModel('activitynew')->activityLock($uid,'rank');

        if($aid){
            if(oo::commonOprModel('activitynew')->addToRankRoom($uid,$aid)){
                $data['rankData'] = oo::commonOprModel('activitynew')->getRankData($uid,$aid);
                $data['selfData'] = oo::commonOprModel('activitynew')->getUserRankData($uid,$aid);
                $ret = oo::response(1,$data);
            }else{
                $ret = oo::response(-2,[],"未达标");
            }
        }else{
            $ret = oo::response(-2,[],"未开始");
        }
        return $ret;
    }

    //加入排行榜活动房间 无进度
    public function joinRankRoomNoPro($param){
        $uid = oo::functions()->uint($param['uid']);
        if(empty($uid)){
            return oo::response(-1);
        }

        $aid = oo::commonOprModel('activitynew')->activityLock($uid,'rank2');

        if($aid){
            if(oo::commonOprModel('activitynew')->addToRankRoom($uid,$aid)){
                $data['rankData'] = oo::commonOprModel('activitynew')->getRankData($uid,$aid);
                $data['selfData'] = oo::commonOprModel('activitynew')->getUserRankData($uid,$aid,'rank2');
                $ret = oo::response(1,$data);
            }else{
                $ret = oo::response(-2,[],"未达标");
            }
        }else{
            $ret = oo::response(-2,[],"未开始");
        }
        return $ret;
    }

    /**
     * 当前排行榜数据
     * @param $param
     * @return string
     * Created by: Owen
     * Created on: 2020/3/6 15:19
     */
    public function getCurRankData($param){
        $uid = oo::functions()->uint($param['uid']);
        $aid = oo::commonOprModel('activitynew')->activityLock($uid,'rank');
        $activity13['rankData'] = oo::commonOprModel('activitynew')->getRankData($uid,$aid);
        return oo::response(1,$activity13,"",true);
    }


    //当前排行榜数据 无进度
    public function getCurRankDataNoPro($param){
        $uid = oo::functions()->uint($param['uid']);
        $aid = oo::commonOprModel('activitynew')->activityLock($uid,'rank2');
        $activity13['rankData'] = oo::commonOprModel('activitynew')->getRankData($uid,$aid,'rank2');
        return oo::response(1,$activity13,"",true);
    }

    /**
     * 获取宝藏活动预设结果
     * @param $param
     * @return string
     * Created by: Owen
     * Created on: 2020/7/28 15:37
     */
    public function getUserTreasurePreset($param){
        $uid = oo::functions()->uint($param['uid']);
        $id  = oo::functions()->uint($param['id']);
        if(empty($uid)){
            return oo::response(-1);
        }
        $version = oo::commonOprModel('user')->getUserVersion($uid);
        if(oo::compareVersion($version,'1.7.2')){
            $data['preset'] = oo::commonOprModel('activitynew')->getUserTreasureSlotResult($uid,$id);
        }else{
            $data['preset'] = oo::commonOprModel('activity')->getUserTreasureSlotResult($uid,$id);
        }
        return oo::response(1,$data,'',true);
    }

    /**
     * 用户宝藏活动配置
     * @param $param
     * @return string
     * Created by: Owen
     * Created on: 2020/7/28 16:06
     */
    public function getUserTreasureData($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')){
            $aid = oo::commonOprModel('activitynew')->activityLock($uid,"treasure");
            if(!$aid){
                return oo::response(-2,[],'活动未开启');
            }
            $config = oo::commonOprModel('activitynew')->getUserTreasureData($aid,$uid,true);
        }else{
            $config = oo::commonOprModel('activity')->getUserTreasureData($uid,true);
        }
        return oo::response(1,$config,'',true);
    }

    /**
     * Notes:活动报名
     * User: wsc
     * Time: 2020/9/14 19:02
     * @param $param
     * @return string
     */
    public function applyActivity($param){
        $uid = oo::functions()->uint($param['uid']);
        $aid = oo::functions()->uint($param['aid']);
        if(empty($uid)||empty($aid)){
            return oo::response(-1);
        }
        $res = oo::commonOprModel('activitynew')->applyActivity($uid,$aid);
        $ret = $res?oo::response(1,[]):oo::response(0,[]);
        return $ret;
    }

    /**
     * Notes:返回活动数据
     * User: wsc
     * Time: 2020/9/14 19:35
     * @param $param
     * @return string
     */
    public function getActivity($param){
        $uid = oo::functions()->uint($param['uid']);
        $aid = oo::functions()->uint($param['aid']);
        if(empty($uid)||empty($aid)){
            return oo::response(-1);
        }
        $actInfo= oo::commonOprModel('activitynew')->getActivityData($uid,$aid);
        $act_type = oo::commonOprModel('activitynew')->act_type;
        $rcid = $act_type[$actInfo['activityData']['actType']]['rcid'];
        $data =[
            'id'   => $rcid,
            'sort' => $rcid,
            'type' => oo::commonOprModel('activitynew')->isShow($rcid),
            'data' => $actInfo
        ];
        return oo::response(1,$data);
    }

    /**
     * Notes:通过活动类型id(acid)获取活动详情
     * User: wsc
     * Time: 2020/12/8 18:05
     * @param $param
     * @return string
     */
    public function getActivityByActId($param){
        $uid = oo::functions()->uint($param['uid']);
        $rcid = oo::functions()->uint($param['rcid']);
        if(empty($uid)||empty($rcid)){
            return oo::response(-1);
        }
        $data =[];
        $act_type = oo::commonOprModel('activitynew')->act_type;
        $type ='';
        foreach ($act_type as $k => $v){
            if(!empty($v['rcid'])&&$v['rcid']==$rcid){
                $type = $k;
                break;
            }
        }
        if($type){
            $aid = oo::commonOprModel('activitynew')->activityLock($uid,$type);
            if($aid){
                $actInfo = oo::commonOprModel('activitynew')->getActivityData($uid,$aid);
                $data =[
                    'id'   => $rcid,
                    'sort' => $rcid,
                    'type' => oo::commonOprModel('activitynew')->isShow($rcid),
                    'data' => $actInfo
                ];
            }
        }
        if(empty($data)){
            return oo::response(errorCode::NOT_FOUND_ACTIVITY,[],"not find activity");
        }
        return oo::response(1,$data);
    }
    /**
     * 刷新宝藏活动保护时间
     * @param $param
     * @return string
     * Created by: Owen
     * Created on: 2020/11/3 15:48
     */
    public function refreshTreasureProtectTime($param){
        $uid = oo::functions()->uint($param['uid']);
        if(empty($uid)){
            return oo::response(-1);
        }
        return oo::commonOprModel('activitynew')->RefreshTreasureProtectTime($uid);
    }

    /**
     * Notes:获取排行榜结算信息
     * User: wsc
     * Time: 2020/12/3 10:21
     * @param $param
     * @return string
     */
    public function getRankData($param){
        $uid = oo::functions()->uint($param['uid']);
        if(empty($uid)){
            return oo::response(-1);
        }
        $rankReward =oo::commonOprModel('activitynew')->getUserCheckRankReward($uid);
        $data =[];
        if($rankReward){
            $actinfo = oo::commonOprModel('activitynew')->getActivity($rankReward['aid']);
            $rankReward['activityName'] = $actinfo['imgName'];
            $rankReward['rankData'] = oo::commonOprModel('activitynew')->getRankData($uid,$rankReward['aid']);
            $rankReward['rankReward'] = array_values(oo::commonOprModel('activitynew')->getRankEndRewardConf($rankReward['aid'],$uid));
            $data = $rankReward;
        }
        return oo::response(1,$data);
    }

    /**
     * Notes:四选一活动-敲蛋
     * User: wsc
     * Time: 2021/3/10 12:12
     * @param $param
     * @return string
     */
    public function strikeEgg($param){
        $uid = oo::functions()->uint($param['uid']);
        $n   = oo::functions()->uint($param['stage'])??0;//指定敲哪个蛋
        if(empty($uid)){
            return oo::response(errorCode::REQUEST_PARAM_ERROR,[],'参数错误');
        }
        if(!funs::concurrentLock($uid,'strikeEgg')){
            return oo::response(errorCode::API_FREQUENT_REQUESTS,[],'频繁操作');
        }
        $ret = oo::commonOprModel('activitynew')->strikeEgg($uid,$n);
        return $ret;
    }

    /**
     * Notes:四选一活动-复活
     * User: wsc
     * Time: 2021/3/10 12:14
     * @param $param
     * @return string
     */
    public function easterGameAgain($param){
        $uid = oo::functions()->uint($param['uid']);
        if(empty($uid)){
            return oo::response(errorCode::REQUEST_PARAM_ERROR,[],'参数错误');
        }
        $ret = oo::commonOprModel('activitynew')->easterGame($uid);
        return $ret;
    }

    /**
     * Notes:四选一活动-放弃离开
     * User: wsc
     * Time: 2021/3/10 12:24
     * @param $param
     * @return string
     */
    public function overEasterEgg($param){
        $uid = oo::functions()->uint($param['uid']);
        if(empty($uid)){
            return oo::response(errorCode::REQUEST_PARAM_ERROR,[],'参数错误');
        }
        $ret = oo::commonOprModel('activitynew')->overEasterEgg($uid);
        return $ret;
    }

    /**
     * Notes:四选一活动获取当前礼包
     * User: wsc
     * Time: 2021/3/10 12:26
     * @param $param
     * @return string
     */
    public function getEasterEggGiftBag($param){
        $uid = oo::functions()->uint($param['uid']);
        if(empty($uid)){
            return oo::response(errorCode::REQUEST_PARAM_ERROR,[],'参数错误');
        }
        $data = oo::commonOprModel('activitynew')->getEasterGiftBag($uid);
        return oo::response(1,$data);
    }

    //=============================好友召回活动=============================

    /**
     * 召回好友上报
     */    
    public function callbackFriend($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(!oo::commonOprModel('friend')->isFriend($uid, $fid)){
            return oo::response(errorCode::NOT_MY_FRIEND);
        }
        $activity = oo::commonOprModel('callback')->getCurrentActivity($uid);
        if(empty($activity) || time() > $activity['reward_time']){ //活动不存在或已进入领奖期
            return oo::response(errorCode::NOT_FOUND_ACTIVITY);
        }
        //添加召回记录
        oo::commonOprModel('callback')->callbackFriend($uid, $fid, $activity);
        return oo::response(errorCode::SUCCESS);
    }

    /**
     * 设置成功召回我的好友
     */
    public function setSuccCallbackFriend($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,'setSuccCallbackFriend')){
            return oo::response(errorCode::API_FREQUENT_REQUESTS,[],'频繁操作');
        }
        if($uid==$fid){
            return oo::response(errorCode::CONDITION_NOT_RNOUGH,[],'条件不足');
        }
        if(!oo::commonOprModel('friend')->isFriend($uid, $fid)){
            return oo::response(errorCode::NOT_MY_FRIEND);
        }
        $activity = oo::commonOprModel('callback')->getCurrentActivity($fid);
        if(empty($activity) || time() > $activity['reward_time']){ //活动不存在或已进入领奖期
            return oo::response(errorCode::NOT_FOUND_ACTIVITY);
        }
//        $res = oo::commonOprRedis('activity')->hKeys(okeys::activityBeCallBackFriend($activity['id'],$uid));
//        if(!in_array($fid, $res)){
//            return oo::response(errorCode::NOT_CALLBACK_FRIEND);
//        }
        $ret = oo::commonOprModel('callback')->setCallbackSuccess($uid, $fid, $activity);
        return $ret;
    }

    /**
     * Notes:设置雷区
     * User: wsc
     * Time: 2021/4/7 16:25
     * @param $param
     * @return string
     */
    public function setMinePlace($param){
        $uid = oo::functions()->uint($param['uid']);
        $place = oo::functions()->uint($param['place']);
        if(empty($uid)||empty($place)){
            return oo::response(errorCode::REQUEST_PARAM_ERROR);
        }
        if(!funs::concurrentLock($uid,'setMinePrace')){
            return oo::response(errorCode::API_FREQUENT_REQUESTS,[],'频繁操作');
        }
        $retData = oo::commonOprModel('activitynew')->buryItem($uid,$place);
        if(!is_array($retData)){
            return oo::response(errorCode::NOT_FOUND_ACTIVITY,[],'埋雷类型活动不存在');
        }
        return oo::response(1,$retData);
    }
}
