<?php
defined('BASEPATH') OR exit('No direct script access allowed');

/**
* 上报方法
*/
class Report extends CI_Controller
{
	
	public function __construct()
	{
		parent::__construct();
		//$this->load->library('Redis');
		$this->load->helper('url');
		$this->load->helper('cookie');
		$this->load->model('okeys');
		$this->load->model('otable');
		$this->load->model('oo');
		
		$this->output->set_header("Access-Control-Allow-Origin: * ");
	}

	/**
     * 事件上报
     * @param $param
     * @return false|string
     * Created by: HSS
     * Created on: 2020/6/20 12:25
     */
    public function event(){
    	header('Access-Control-Allow-Origin: *');
    	$this->output->set_header("Access-Control-Allow-Origin: * ");
        $ip = $this->input->ip_address();
    	
    	$param = $this->input->post();
    	if(!empty($param['param'])){
    		$str = $param['param'];
			$sign = substr($str,0,1).substr($str,2,strlen($str));
			$sign = substr($sign,0,floor((strlen($sign)-16)/2)-1).substr($sign,floor((strlen($sign)-16)/2)+15,strlen($sign));
			$sign = base64_decode($sign);
			$sign = trim($sign,'&');
			
			$sign = explode('&',$sign);
			foreach ($sign as $row){
			    $temp = explode('=',$row,2);
			    $param[$temp[0]] = $temp[1];
			    $param[$temp[0]] = $temp[1];
			    $param[$temp[0]] = $temp[1];
			}
    	}

        //临时处理客户端上报event_id参数是数组的问题
        if(!empty($param['event_id']) && !is_numeric($param['event_id'])){
            $tmp = (array)json_decode($param['event_id'], true);
            $param['event_id'] = $tmp ? $tmp[0] : '';
        }
        if(empty($param['event_id'])){
            die(json_encode(['code'=>1])); //事件id不为空
        }
        //漏斗事件上报
        oo::commonOprModel('funnel')->funnelReport((array)$param, $ip);

        if(oo::commonOprModel('funnel')->isFunnelReport($param['event_id'])){
            die(json_encode(['code'=>1])); //直接上报elk
        }

        $uid = $param["uid"];
        if(empty($param["data"])){
            die(json_encode(['code'=>-1,"msg"=>"data null"]));
        }
		$param['data'] = json_decode($param['data'],true);
		$param['commonData'] = json_decode($param['commonData'],true);
        if(is_array($param['data'])){
        	$temp = $param['data'];
			$step = $temp[0];
            if(is_array($step)){
                $step = $step[0];
            }
            if(is_array($param['data'][0])){
                $param['data'][0] = $param['data'][0][0];
            }
		}else{
			$step = $param['data'];
		}
		
		if(in_array($param["event_id"],[10002,10003]) && empty($uid)){
    		die(json_encode(['code'=>-1,"msg"=>"uid null"]));
    	}
		
        $sid = intval($param['sid']);
        if(empty($sid)){
        	$sid = 1;
        }
 
        switch ($param["event_id"]){
            case 10002:
                $count = intval(oo::commonOprRedis("Statistics")->hGet(okeys::Report($param['event_id']),$uid)) + 1;
                $ret = oo::commonOprRedis("Statistics")->hSet(okeys::Report($param['event_id']),$uid.'-'.$step,$count);
                break;
            case 10003:
                $count = intval(oo::commonOprRedis("Statistics")->hGet(okeys::Report($param['event_id']),$uid)) + 1;
                $ret = oo::commonOprRedis("Statistics")->hSet(okeys::Report($param['event_id']),$uid.'-'.$step,$count);
                break;
            case 10012:
                $ret = oo::commonOprRedis("Statistics")->hSet(okeys::Report($param['event_id'],$sid),$uid,$step);
                break;
            case 10013:
            	$ret = oo::commonOprRedis("Statistics")->hIncrBy(okeys::NetworkReport($sid),$param['data'][0]."-".$param['data'][1],1);
            	break;
            case 10014:
            	$ret = oo::commonOprRedis("Statistics")->hIncrBy(okeys::LoginEventSta($sid),$param['data'][0],1);
            	break;
            case 10015:
            	$ret = oo::commonOprRedis("Statistics")->hIncrBy(okeys::GiftEventSta($sid,$param['data'][1]),$param['data'][0],1);
            	break;
            case 10016:
            	$ret = oo::commonOprRedis("Statistics")->hIncrBy(okeys::inviteEventSta($sid,$param['data'][1]),$param['data'][0],1);
            	break;
            case 'invite_friend':
            	oo::commonOprModel('report')->buryingPoint($param);
            	break;
            case 'invite_friend_success':
            	oo::commonOprModel('report')->buryingPoint($param);
            	break;
            case 10017:
                //分享事件
                $type = $param['data'][0];//FB MESSAGE
                $point = $param['data'][1];//六个分享点
                $event = $param['data'][2];//事件
                $ret = oo::commonOprRedis("Statistics")->hIncrBy(okeys::shareEventSta($sid,$type,$event),$point,1);
                break;
            case 10018:
            	$ret = oo::commonOprRedis("Statistics")->hIncrBy(okeys::activityClickSta(),$param['data'][0],1);
            	break;
            case 10019:
                //矿工小游戏
                oo::commonOprModel('report')->buryingPoint($param);
                break;
            case 10020:
                //好友召回 按钮点击
                //1点击召回按钮数量 2点击消息数量 3成功召回数量
                $toUid = $param['data'][0];//被召回人
//                $uid = $uid;//召回人
                $ret = oo::commonOprRedis("Statistics")->hIncrBy(okeys::friendRecallSta($param['data'][1]),$uid,1);
                break;
            default:
                $ret = oo::commonOprRedis("Statistics")->hSet(okeys::Report($param['event_id']),$uid,$step);
                if(in_array($param['event_id'],[10005,10006,10007])){
                	if($param['event_id'] == 10007){
                		$step += 8;
                	}
                	//新手步骤统计 总数
                	oo::commonOprRedis("Statistics")->hIncrBy(okeys::noviceStepsSta(),$step,1);
                	//新手步骤统计 FB用户 lid 1是FB 2是游客
                	if($param['lid'] == 1){
                		oo::commonOprRedis("Statistics")->hIncrBy(okeys::noviceStepsSta(1),$step,1);
                	}
                }
                if(in_array($param['event_id'],[10008,10009,10010,10011])){
                	//支付上报 10008支付上报 10009支付完成
                	oo::commonOprModel('report')->payReport($sid,$step,$param['event_id']);
                	$ret = 1;
                	
                	if(!empty($param['is_new_report'])){
                		//新统计
                		oo::commonOprModel('report')->buryingPoint($param);
                	}
                }
                break;
        }
        if($ret)
            die(json_encode(['code'=>1]));
        die(json_encode(['code'=>-1]));
    }
    
    /**
     * 客户端日记
     * @return string
     * Created by: hss
     * Created on: 2020/3/13 16:28
     */
    public function clientlog(){

    	$param = $this->input->post();
    	$uid = oo::functions()->uint($param['uid']);

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

    /**
     * 客户端错误日志文件上传
     * @param $param
     * @return false|string
     * Created by: Xwy
     * Created on: 2021/12/01 10:30
     * Description:
     */
    public function clientErrorLogFiles(){
        header('Access-Control-Allow-Origin: *');
        $this->output->set_header("Access-Control-Allow-Origin: * ");
        $time = time();
        $param = $this->input->post();
        oo::logs()->debug3(['time'=>$time,'dateTime'=>date('YmdHis', $time),'param'=>$param],'clientErrorLogFiles.log');
        $uid = oo::functions()->uint($param['uid']);
        $config = array(
            'savePath' => 'deBUG/clientErrorLogFiles/'.date('Ymd', $time).'/'.$uid,             //存储文件夹
//            'savePath' => 'public/game/clientErrorLogFiles/'.$uid.'/'.date('YmdHis', $time),             //存储文件夹
            'maxSize'  => 5 * 1024 * 1024,                 //允许的文件最大尺寸，单位KB
            'allowFiles' => array('.zip')  //允许的文件格式
        );
        $this->load->library('uploader', $config);
        $file = $this->uploader->getFileInfo();
        // 1 保存成功 2 zip 文件为空 删除成功 3 删除失败
        $code = 1;
        if($file['type'] == '.zip' && $file['size'] == 22){
            $delRes = unlink($file['url']);
            $code = $delRes ? 2 : 3;
        }
        echo json_encode(['code'=>$code]);
    }

}
