<?php
/**
 * User: MikeMei
 */
defined('IN_WEB') or die('Include Error!');
class Chat{
    /**
     * 领取礼物
     * @param $param
     */
    public function giftRec($param){
        $uid = oo::functions()->uint($param['uid']);
        $giftId = oo::functions()->uint($param['giftId']);
        $gift = oo::commonOprRedis('common')->get(okeys::ChatGift($giftId));
        if(empty($gift)){
            return json_encode(['code'=>-1,'msg'=>'gift error']);
        }
        $gift = json_decode($gift,true);

        // 判断参与人是否能领 -- 礼物ID限制
        $userGiftId = oo::commonOprRedis('common')->get(okeys::ChatGiftId($uid,$giftId));
        if($userGiftId != false){
            return json_encode(['code'=>-1,'msg'=>'已领取过礼物']);
        }

        // 判断参与人是否能领 -- 数量限制
        $userGift = oo::commonOprRedis('common')->get(okeys::ChatGiftRec($uid,$gift['gift'].'#participate'));
        $money = 50000;
        $spins = 1;

        if(empty($userGift)){
            if($gift['gift'] == 1){
                oo::commonOprModel('member')->optProperty($uid,array('money'=>$money),'+',106,'聊天礼物');
            }else if($gift['gift'] == 2){
                oo::commonOprModel('member')->optProperty($uid,array('spins'=>$spins),'+',106,'聊天礼物');
            }
            oo::commonOprRedis('common')->set(okeys::ChatGiftRec($uid,$gift['gift'].'#participate'),1);
            oo::commonOprRedis('common')->expireAt(okeys::ChatGiftRec($uid,$gift['gift'].'#participate'),strtotime('23:59:59'));
        }else{
            if($gift['gift'] == 1){
                if(intval($userGift)>10){
                    return json_decode(['code'=>-2,'msg'=>'gift error']);
                }else{
                    oo::commonOprModel('member')->optProperty($uid,array('money'=>$money),'+',106,'聊天礼物');
                }
            }else if($gift['gift'] == 2){
                if(intval($userGift)>10){
                    return json_decode(['code'=>-2,'msg'=>'gift error']);
                }else{
                    oo::commonOprModel('member')->optProperty($uid,array('spins'=>$spins),'+',106,'聊天礼物');
                }
            }else{
                return json_decode(['code'=>-2,'msg'=>'gift error']);
            }
            oo::commonOprRedis('common')->set(okeys::ChatGiftRec($uid,$gift['gift'].'#participate'),intval($userGift)+1);
            oo::commonOprRedis('common')->expireAt(okeys::ChatGiftRec($uid,$gift['gift'].'#participate'),strtotime('23:59:59'));
        }
      	/*
        // 判断发起人是否能领
        $userGift = oo::commonOprRedis('common')->get(okeys::ChatGiftRec($gift['uid'],$gift['gift'].'#initiate'));
        if(empty($userGift)){
            if($gift['gift'] == 1){
                oo::commonOprModel('member')->optProperty($gift['uid'],array('money'=>$money),'+','聊天礼物');
            }else if($gift['gift'] == 2){
                oo::commonOprModel('member')->optProperty($gift['uid'],array('spins'=>$spins),'+','聊天礼物');
            }
            oo::commonOprRedis('common')->set(okeys::ChatGiftRec($gift['uid'],$gift['gift'].'#initiate'),$spins);
            oo::commonOprRedis('common')->expireAt(okeys::ChatGiftRec($gift['uid'],$gift['gift'].'#initiate'),strtotime('23:59:59'));
        }else{
            if(intval($userGift)<10){
                if($gift['gift'] == 1){
                    oo::commonOprModel('member')->optProperty($gift['uid'],array('money'=>$money),'+','聊天礼物');
                }else if($gift['gift'] == 2){
                    oo::commonOprModel('member')->optProperty($gift['uid'],array('spins'=>$spins),'+','聊天礼物');
                }
                oo::commonOprRedis('common')->set(okeys::ChatGiftRec($gift['uid'],$gift['gift'].'#initiate'),intval($userGift)+1);
                oo::commonOprRedis('common')->expireAt(okeys::ChatGiftRec($gift['uid'],$gift['gift'].'#initiate'),strtotime('23:59:59'));
            }
        }
		*/
        $newNum = $gift['num']+1;
        $sData = [
            'num'=>$newNum,
            'maxNum'=>$gift['maxNum'],
            'gift'=>$gift['gift'],
            'uid'=>$gift['uid'],
            'giftId'=>$giftId,
        ];

        $worldMessage = oo::commonOprRedis('common')->lGetRange(okeys::Chat('WORLD'),0,-1);
        foreach ($worldMessage as $key=>$row){
            $temp = json_decode($row,true);
            if($temp['type'] == 3 && $temp['giftId'] == $giftId){
                $temp['num'] = $newNum;
                oo::commonOprRedis('common')->lSet(okeys::Chat('WORLD'),$key,json_encode($temp));
            }
        }

        if($newNum>=10){
            oo::commonOprRedis('common')->delete(okeys::ChatGift($giftId));
        }else{
            $ttl = oo::commonOprRedis('common')->ttl(okeys::ChatGift($giftId));
            oo::commonOprRedis('common')->setex(okeys::ChatGift($giftId),$sData,$ttl);
        }
        oo::commonOprRedis('common')->setex(okeys::ChatGiftId($uid,$giftId),1,24*60*60);
        oo::commonOprModel('workerman')->pushGroup('WORLD',array_search('Chat_giftRec_message',ocmd::$Cmd),$sData);
		
		//聊天的人均金币体力领取次数统计
        oo::commonOprModel('statistics')->chatGiftSta($gift['gift']==1?'money':'spins');
		
        return json_encode(['code'=>1,'data'=>[
            'type'=>$gift['gift'],
            'num'=>($gift['gift'] == 1)?$money:$spins,
        ]]);
    }
  
  	public function report($param){
        $uid = oo::functions()->uint($param['uid']);
        $touid = oo::functions()->uint($param['touid']);
        $content = oo::functions()->escape($param['content']);
        $tb = otable::chatReport();
        $time = time();
        $sql = "INSERT INTO {$tb} VALUES(NULL, {$uid}, {$touid},'{$time}','{$content}')";
        oo::commonOprDb('common')->query($sql);
        return oo::response(1,[],'上报成功');
    }
}
