<?php
defined( 'IN_WEB') or die( 'Include Error!');
class Card{
    /**
     * @param $param
     * @return false|string
     * Created by: Cassie Lee
     * Created on: 2019/5/9 20:19
     * Description:获取卡片信息
     */
    public function cardInfo($param)
    {
        $uid = oo::functions()->uint($param['uid']);
        if( empty($uid)  ){
            $data = array('code' => -1,'desc'=>'参数错误！');
            return json_encode($data);
        }
        $data['code'] = 1;
        $data['data']['card'] = oo::commonOprModel('card')->getInfo($uid);
        $data['data']['exclusiveCard'] = oo::commonOprModel('card')->getExclusiveInfo($uid);
        $data['data']['groupReward'] = oo::commonOprModel('card')->getRewardProgress($uid);
        $data['data']['grouCompletepNum'] = oo::commonOprModel('card')->getCardProgress($uid);
        $data['data']['exclusiveReward'] = oo::commonOprModel('card')->getExclusiveGroupEd($uid);
        $data['data']['msg'] = '成功';
        return json_encode($data);
    }

    /**
     * 卡组进度
     * @param $param
     * @return string
     * Created by: Owen
     * Created on: 2019/11/6 12:24
     */
    public function cardGroupProgress($param){
        $uid = oo::functions()->uint($param['uid']);
        if( empty($uid)  ){
            return oo::response(-1,[],'参数错误');
        }
        return oo::response(1,[
            'groupReward'     =>oo::commonOprModel('card')->getRewardProgress($uid),
          	'exclusiveCard'   =>oo::commonOprModel('card')->getExclusiveInfo($uid),
            'grouCompletepNum'=>oo::commonOprModel('card')->getCardProgress($uid),
            'exclusiveReward' =>oo::commonOprModel('card')->getExclusiveGroupEd($uid)
        ]);
    }

    public function getFriendList($param)
    {
        $uid = oo::functions()->uint($param['uid']);
        if( empty($uid) ){
            $data = array('code' => -1,'desc'=>'参数错误！');
            return json_encode($data);
        }
        $data = oo::commonOprModel('card')->getFriendList($uid);
        return json_encode($data);
    }

    /**
     * @param $param
     * @return false|string
     * Created by: Cassie Lee
     * Created on: 2019/5/10 16:33
     * Description:赠送好友卡片
     */
    public function sendTo($param)
    {
        $uid  = oo::functions()->uint($param['uid']);
        if(isset($param['pos'])){
            $pos = $param['pos'];
        }else if(isset($param['index'])){
            $pos = oo::functions()->uint($param['index']);
        }
        if(isset($param['cgid'])){
            $cgid = $param['cgid'];
        }else if(isset($param['group'])){
            $cgid = oo::functions()->uint($param['group']);
        }
        if(isset($param['suid'])){
            $sendToUid = oo::functions()->uint($param['suid']);
        }else if(isset($param['toUid'])){
            $sendToUid = oo::functions()->uint($param['toUid']);
        }
        if( empty($uid)   || empty($sendToUid)   ){
            $data = array('code' => -1,'desc'=>'参数错误！');
            return json_encode($data);
        }
        $time = oo::functions()->uint($param['time']);
        $sign = $param['sign'];
        $number = 1;
        $serverSign = md5($uid.'#2019$'.$cgid.$pos.'superant'.$sendToUid.$time.'&^%');
//        $redisSign = oo::commonOprCache('memcache')->get('CARDSENTO:'.$uid);
        $redisSign = oo::commonOprRedis('common')->get('CARDSENTO:'.$uid);
        if($sign != $serverSign || $redisSign == $sign){
            return json_encode(['code'=>-1]);
        }
        oo::commonOprRedis('common')->setex('CARDSENTO:'.$uid,$sign,86400);
        if(isset($param['rewardId'])){
            $rewardData = oo::commonOprRedis('common')->get(okeys::ShareRewardIdData($param['rewardId']));
            if(!empty($rewardData)){
                if(isset($rewardData['recTimestamp'])){
                    if(in_array($param['timestamp'],$rewardData['recTimestamp'])){
                        return oo::response(-3,[],'分享领取上限');
                    }
                }
            }else{
                return oo::response(-1,[],'分享过期');
            }
            $toUidFriends = oo::commonOprModel('friend')->getUserAllFriend($sendToUid);
            if(!in_array($uid,$toUidFriends)){
                return oo::response(-2,[],'对方不是你的好友');
            }
        }
      
        $data = oo::commonOprModel('card')->sendTo($uid,$cgid,$pos,$sendToUid,$number);
        if($data['code'] == 1){
            oo::commonOprModel('Workerman')->push($sendToUid,ocmd::$redPoint,['type'=>5]);
            $uidInfo = oo::commonOprModel('member')->getUserInfo($uid);
            oo::commonOprModel('push')->pushNews($sendToUid, 'push.title.gift', 'push.content.gift', [$uidInfo['nick']],'gift');
        }
        return json_encode($data);
    }

    public function cardReward($param){
        $uid = oo::functions()->uint($param['uid']);
        $gid = oo::functions()->uint($param['gid']);
        $groupType = oo::functions()->uint($param['groupType']);
        if( empty($uid) ){
            $data = array('code' => -1,'desc'=>'参数错误！');
            return json_encode($data);
        }
        if(!empty($groupType) && $groupType == 2){
            $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);
        }
    }

    public function progressReward($param){
        $uid      = oo::functions()->uint($param['uid']);
        if( empty($uid) ){
            $data = array('code' => -1,'desc'=>'参数错误！');
            return json_encode($data);
        }
        $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: Cassie Lee
     * Created on: 2019/5/14 10:46
     * Description:索要卡片
     */
    public function askFor($param)
    {
        $uid = oo::functions()->uint($param['uid']);
        $askForUid = oo::functions()->uint($param['auid']);//需要改
        $number = oo::functions()->uint($param['num']);
        if( empty($uid)  || empty($param['ciid']) || empty($askForUid)){
            $data = array('code' => -1,'desc'=>'参数错误！');
            return json_encode($data);
        }
        $data = oo::commonOprModel('card')->askFor($uid,$param['ciid'],$askForUid,$number);
        return json_encode($data);
    }

    /**
     * @param $param
     * @return false|string
     * Created by: Cassie Lee
     * Created on: 2019/5/14 18:45
     * Description:获取宝箱卡片
     */
    public function getCard($param){
        $uid = oo::functions()->uint($param['uid']);
        $type = 3;
        if(empty($uid)){
            $data = array('code' => -1,'desc'=>'参数错误！');
            return json_encode($data);
        }
        $data = oo::commonOprModel('card')->getCardByType($uid,$type);
        $data = array('code' => 1,'msg'=>'领取成功','data'=>$data);
        return json_encode($data);
    }

    /**
     * 获得卡片信息-1.3.7 改版
     * @param $param
     * @return false|string
     * Created by: Owen
     * Created on: 2020/3/24 19:11
     */
    public function info($param){
        $uid = oo::functions()->uint($param['uid']);
        if(empty($uid)){
            return oo::response(-1);
        }
        return oo::commonOprModel('card')->info($uid,$uid);
    }
}
?>