<?php
defined('IN_WEB') or die( 'Include Error!');
/**
 *   日记事件上报
 */

class ModelReport {

    /**
     * 客户端日记
     * @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,oo::redisRandomExpire(86400));
					}
	    		}else{
	    			oo::commonOprRedis('Statistics')->setex(okeys::clientlog(),1,oo::redisRandomExpire(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);
    }
}
