<?php
/**
 * Report.php
 * Created by: Owen
 * Created on: 2020/3/27 15:39
 *
 */
defined('IN_WEB') or die('Include Error!');
class Report{
    /**
     * 聊天举报
     * @param $param
     * @return string
     * Created by: Owen
     * Created on: 2020/3/27 12:29
     */
    public function chat($param){
        $uid = oo::functions()->uint($param['uid']);
        $touid = oo::functions()->uint($param['touid']);
        $content = oo::functions()->escape($param['content']);
        if(empty($uid) || empty($touid) || empty($content)){
            return oo::response(-1);
        }
        $tb = otable::chatReport();
        $time = time();
        $sql = "INSERT INTO {$tb} VALUES(NULL, {$uid}, {$touid},'{$time}','{$content}')";
        oo::commonOprDb('common')->query($sql);
        return oo::response(1,[],'上报成功');
    }

    /**
     * 客户端日记
     * @return string
     * Created by: hss
     * Created on: 2020/3/13 16:28
     */
    public function clientlog($param){
    	$uid = oo::functions()->uint($param['uid']);
    	$ret = oo::commonOprModel('report')->clientlog($param['data'],$uid);
        return json_encode(['code'=>1,'length'=>$ret]);
    }

    /**
     * 事件上报
     * @param $param
     * @return false|string
     * Created by: Owen
     * Created on: 2020/3/27 19:48
     */
    public function event($param){
        $uid = oo::functions()->uint($param['uid']);
        if(empty($uid) || empty($param['data'])){
            return oo::response(-1);
        }
		
		$param['data'] = json_decode($param['data'],true);
		if(is_array($param['data'])){
			$step = $param['data'][0];
		}else{
			$step = $param['data'];
		}	
        $sgsid = oo::getDefinedSid($uid);
        switch ($param['event_id']){
            case 10002:
                $count = intval(oo::commonOprRedis('Statistics')->hGet(okeys::Report($param['event_id'],$sgsid),$uid)) + 1;
                $ret   = oo::commonOprRedis('Statistics')->hSet(okeys::Report($param['event_id'],$sgsid),$uid.'-'.$step,$count);
                break;
            case 10003:
                $count = intval(oo::commonOprRedis('Statistics')->hGet(okeys::Report($param['event_id'],$sgsid),$uid)) + 1;
                $ret   = oo::commonOprRedis('Statistics')->hSet(okeys::Report($param['event_id'],$sgsid),$uid.'-'.$step,$count);
                break;
            case 10012:
                $ret = oo::commonOprRedis('Statistics')->hSet(okeys::Report($param['event_id'],$sgsid),$uid,$step);
                break;
            default:
                $ret = oo::commonOprRedis('Statistics')->hSet(okeys::Report($param['event_id']),$uid,$step);
                if(in_array($param['event_id'],[10005,10006,10007])){
                	//新手步骤统计 总数
                	oo::commonOprRedis('Statistics')->hIncrBy(okeys::noviceStepsSta(),$step,1);
                	//新手步骤统计 FB用户
                	if($param['lid'] == 2){
                		oo::commonOprRedis('Statistics')->hIncrBy(okeys::noviceStepsSta(1),$step,1);
                	}
                }
                if(in_array($param['event_id'],[10008,10009,10010,10011])){
                	//支付上报
                	$sid = intval($param['sid']);
                	oo::commonOprModel('report')->payReport($sid,$step,$param['event_id']);
                }
                break;
        }
        if($ret)
            return oo::response(1);
        return oo::response(-1);
    }

    /**
     * 异常上报
     * @param $param
     * @return false|string
     * Created by: Owen
     * Created on: 2019/8/29 15:15
     * Description:
     */
    public function error($param){
        $uid = oo::functions()->uint($param['uid']);
        oo::logs()->debug4(['time'=>time(),'uid'=>$uid,'data'=>$param['data']],$uid.'.log','error/'.date("Ymd").'/');
        return json_encode(['code'=>1]);
    }
}
