<?php
class reportModel extends CI_Model
{
	/**
     * 客户端日记
     * @return string
     * Created by: hss
     * Created on: 2020/3/27 20:05
     */
    public function clientlog($data = "",$uid = 0){
    	$month = date('Ym');
    	
	    $tb = otable::clientlog($month);
    	if(empty($data)){
			$isTB = oo::commonOprRedis('Statistics')->get(okeys::clientlog());
			if(empty($isTB)){
				//判断表是否存在
	    		$database = oo::$config['dbindex'] . "api_statistics";
	    		
	    		$sql = "select count(*) as count from information_schema.TABLES t where t.TABLE_SCHEMA ='{$database}' and t.TABLE_NAME ='t_clientlog_{$month}'";
	    		
	    		$ret = oo::commonOprDb('statistics')->getOne($sql,1);
	    		
	    		if(empty($ret['count'])){
	    			$sql = "CREATE TABLE {$tb}  (
					  `id` int(11) NOT NULL AUTO_INCREMENT,
					  `uid` int(11) NULL DEFAULT 0,
					  `time` int(11) NULL DEFAULT 0,
					  `filename` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT '',
					  PRIMARY KEY (`id`) USING BTREE
					) ENGINE = InnoDB AUTO_INCREMENT = 1 CHARACTER SET = utf8mb4 COLLATE = utf8mb4_general_ci ROW_FORMAT = Dynamic;";
					
					$rs = oo::commonOprDb('statistics')->query($sql);
					
					if($rs){
						oo::commonOprRedis('Statistics')->setex(okeys::clientlog(),1,86400);
					}
	    		}else{
	    			oo::commonOprRedis('Statistics')->setex(okeys::clientlog(),1,86400);
	    		}
			}
    	}else{
	    	$time = time();
	    	$filename = $uid."_".date("Ymd")."_".substr(time(),-5,5).'.log';
	    	$filepath = 'clientLog/'.date("Ymd").'/'.$uid.'/';
	        $ret = oo::logs()->debug4($data,$filename,$filepath);
	        if($ret){
	        	//插入数据库
	        	$sql = "INSERT INTO {$tb} (uid,time,filename) VALUES (";
	            $sql .= "'{$uid}','{$time}','{$filename}')";
	            $rs = oo::commonOprDb('statistics')->query($sql);
	        }
	        return $ret;
    	}
    }
  	/**
     * 支付上报
     * @return string
     * Created by: hss
     * Created on: 2020/6/11 10:11
     */
    public function payReport($sid = 1,$gid = "",$event_id = 10008){
    	oo::commonOprRedis("Statistics")->hIncrBy(okeys::payReport($sid,$event_id),$gid,1);
    }
    
    /**
     * 埋点统计
     * @return string
     * Created by: hss
     * Created on: 2020/9/21 15:04
     */
    public function buryingPoint($param){
    	$uid = $param['commonData']['uid'];
    	if(isset($param['commonData']['is_guest'])){
    		$is_guest = strtoupper($param['commonData']['is_guest']) == "FACEBOOK"?0:1;
    	}else{
    		$is_guest = 1;
    	}
    	
    	$data = [
            'uid'					=>$uid,
            'event_id'				=>$param['event_id'],
            'is_guest'				=>$is_guest,
            'register_time'			=>$param['commonData']['register_time'],
            'total_recharge'		=>0,
            'total_score'			=>$param['commonData']['total_score'],
            'first_pay_time'		=>0,
            'last_pay_time'			=>0,
            'friend_num'			=>0,
            'ad_channel'			=>0,
            'ad_type'				=>0,
            'level'					=>$param['commonData']['level'],
            'star'					=>$param['commonData']['star'],
            'spins'					=>$param['commonData']['spin'],
            'money'					=>$param['commonData']['coin'],
            'building'				=>$param['commonData']['building'],
            'is_fix'				=>$param['commonData']['is_fix'],
            'time'					=>time(),
            'z0'					=>isset($param['data'][0])?$param['data'][0]:0,
            'z1'					=>isset($param['data'][1])?$param['data'][1]:0,
            'z2'					=>isset($param['data'][2])?$param['data'][2]:0,
            'z3'					=>isset($param['data'][3])?$param['data'][3]:0,
            'z4'					=>isset($param['data'][4])?$param['data'][4]:0,
            'z5'					=>isset($param['data'][5])?$param['data'][5]:0,
            'z6'					=>isset($param['data'][6])?$param['data'][6]:0,
            'z7'					=>isset($param['data'][7])?$param['data'][7]:0,
            'z8'					=>isset($param['data'][8])?$param['data'][8]:0,
            'z9'					=>isset($param['data'][9])?$param['data'][9]:0,
            'z10'					=>isset($param['data'][10])?$param['data'][10]:0,
            'z11'					=>isset($param['data'][11])?$param['data'][11]:0,
            'z12'					=>isset($param['data'][12])?$param['data'][12]:0,
            'z13'					=>isset($param['data'][13])?$param['data'][13]:0,
            'z14'					=>isset($param['data'][14])?$param['data'][14]:0,
            'z15'					=>isset($param['data'][15])?$param['data'][15]:0,
            'z16'					=>isset($param['data'][16])?$param['data'][16]:0,
            'z17'					=>isset($param['data'][17])?$param['data'][17]:0,
            'z18'					=>isset($param['data'][18])?$param['data'][18]:0,
            'z19'					=>isset($param['data'][19])?$param['data'][19]:0,
        ];
        
        $data['total_recharge'] = oo::commonOprModel('payment')->getUserTotalPayNum($uid);//充值总额
        $data['first_pay_time'] = oo::commonOprModel('payment')->getUserFirstPayTime($uid);//首次充值时间
        $data['last_pay_time'] = oo::commonOprModel('payment')->getUserLastPayTime($uid);//最后充值时间
        $data['friend_num'] = count(oo::commonOprModel('friend')->getUserAllFriend($uid));//好友数量
        $data['ad_type'] = oo::commonOprModel('member')->getUserAdjustGroup($uid);//广告类型
        $data['ad_channel'] = oo::commonOprModel('member')->getUserAdjustChannel($uid);//广告渠道
        
        if($param['event_id'] == 'order_finish' || $param['event_id'] == '10009'){
        	$aid = $param['data'][18];
        	$data['z14'] = oo::commonOprModel('payment')->getUserPayTimes($uid);//是第几次完成充值
        	$data['z15'] = empty($aid)?0:(oo::commonOprRedis('games')->get(okeys::userBindActRid($uid,$aid)))
;//收集活动数值(第几套活动数值)
        	$data['z16'] = oo::commonOprModel('payment')->getUserUpPayTime($uid);//上次支付时间
        	$data['z17'] = oo::commonOprModel('member')->getUserLoginTime($uid);//当前累计游戏时长
        }
        
        $key = okeys::buryingPointLog(date("Ymd"));
        oo::commonOprRedis('Statistics')->lPush($key,json_encode($data));//流水入队
    }
}
