<?php

namespace Cas\Module;

use KIF\Data\Convert;
use KIF\Data\ResultWrapper;
use KIF\Db\Transaction;
use KIF\Cache\Memcached;
use Cas\Dao\LotteryEventsLog;
use Cas\Dao\LotteryUserParticipateLog;
use KIF\Url;
use KIF\Cookie;
use Cas\Dao\LotteryDeliveryChannels;
use KIF\Core\Request;
use KIF\Core\Config;

/**
 * 计数类
 *
 * @author lihuanchun
 *        
 */
class Count {
	private $keyName;
	private $objDLotteryDeliveryChannels;
	
	/**
	 * 初始化
	 */
	public function __construct() {
		$this->keyName = 'FromKey';
		$this->objDLotteryDeliveryChannels = new LotteryDeliveryChannels ();
	}
	
	/**
	 * 用文件记录用户访问活动日志 & 来源
	 * 后期用脚本跑出当天结果 PV UV
	 * 
	 * @param int $event_id       
	 * @param int $platform_id	
	 */
	public function setPassLog( $event_id,$platform_id) {
		$objDLotteryDeliveryChannels = new LotteryDeliveryChannels();
		$from_id = Request::g('from');
		if($from_id){
			Cookie::set('cas_from', $from_id,60*60*24*29,'.'.$_SERVER['HTTP_HOST']);
		}else{
			$from_id = Cookie::get('cas_from');
		}
		if($from_id){
			$data = $objDLotteryDeliveryChannels -> get($from_id);
			
			if($data['events_id'] == $event_id ){
				$objDLotteryDeliveryChannels -> addOnePV($from_id);
			}
		}else{
			$objDLotteryDeliveryChannels ->addDefaultEventOnePV(Request::schemeDomain(),$event_id,$platform_id);
		}
		
		
		
		
		$info = array();
		$info['ip'] = Request::ip();
		$info['user_agent'] = $_SERVER['HTTP_USER_AGENT'];
		$info['event_id'] = $event_id;
		$info['time'] = time();
		$logStr = serialize($info);
		
		$this->writePassLog($logStr,'pass_log');
	}
	
	/**
	 * 渠道计数 操作
	 * PV IP 记录
	 * 所有页面请求此方法
	 */
	public function DeliveryChannelsCount() {
		// 1.判断当前url中是否有from参数
		$id = Request::g ( 'from' );
		if (! $id) {
			// 2. 判断是否有渠道Cookie
			$id = Cookie::get ( $this->keyNam );
		}
		
		if ($id) {
			// 3. 更具渠道ID增加PV
			$this->objDLotteryDeliveryChannels->addOnePV ( $id );
			Cookie::set ( $this->keyName, $id, 60 * 60 * 24 * 10 ,'.'.$_SERVER['HTTP_HOST']);
		}
	}
	/**
	 * 写日志
	 *
	 * @param string $log_message
	 *        	日志信息
	 * @param string $log_type
	 *        	日志类型
	 */
	public static function writePassLog($log_message, $log_type = 'log') {
		$log_size = 1024 * 1024 * 5; // 1024 * 1024 * 5 = 5MB
		$dir =Config::getInstance ()->get ( 'Log_Path' ).DIRECTORY_SEPARATOR.date('Y',time()).DIRECTORY_SEPARATOR;
		if (! is_dir ( $dir )) {
			if (! mkdir ( $dir )) {
				return false;
			}
		}
		if (! is_writable ( $dir )){
			exit ( 'LOG_PATH is not writeable !' );
		}
		
		$dir .= date('m',time()).DIRECTORY_SEPARATOR;
		if (! is_dir ( $dir )) {
			if (! mkdir ( $dir )) {
				return false;
			}
		}
		if (! is_writable ( $dir )){
			exit ( 'LOG_PATH is not writeable !' );
		}
		
		$s_now_time = date ( '[Y-m-d H:i:s]' );
		$log_now_day = date ( 'Y_m_d' );
		// 根据类型设置日志目标位置
		$log_path = $dir;
		switch ($log_type) {
			case 'debug' :
				$log_path .= 'Out_' . $log_now_day . '.log';
				break;
			case 'error' :
				$log_path .= 'Err_' . $log_now_day . '.log';
				break;
			case 'pass_log' :
				$log_path .= 'Pass_log_' . $log_now_day . '.log';
				break;
			default :
				$log_path .= 'Log_' . $log_now_day . '.log';
				break;
		}
		// 检测日志文件大小, 超过配置大小则重命名
		if (file_exists ( $log_path ) && $log_size <= filesize ( $log_path )) {
			$s_file_name = substr ( basename ( $log_path ), 0, strrpos ( basename ( $log_path ), '.log' ) ) . '_' . time () . '.log';
			rename ( $log_path, dirname ( $log_path ) . DS . $s_file_name );
			
		}
		// 写日志, 返回成功与否
		return error_log ( "$log_message\n", 3, $log_path );
	}
}