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

/**
 * 异常统计
 */
class Abnormal extends MY_Controller
{
	public $postData = array('frombg' => 1, 'lid' => 1, 'sid' => 1);
    public function __construct()
    {
        parent::__construct();
        $config = array(
            'savePath' => 'Excel/uploads',             //存储文件夹
            'maxSize'  => 1024*5,                   //允许的文件最大尺寸，单位KB
            'allowFiles' => array('.xls','.xlsx')  //允许的文件格式
        );
        $this->load->library('uploader', $config);
        $this->load->library('PHPExcel');
        $this->load->library('PHPExcel/PHPExcel/IOFactory');
        $this->load->library('curl');
    }
    
    //异常金币体力统计
    public function moneyspins($sort = '',$level = 50,$money = 1000000000,$spins = 1000){
    	if( $this->env == 1 ){
			$url = $this->config->item('officialUrl');
		}else{
			$url = $this->config->item('localUrl');
			$this->postData['demo'] = 1;
		}

		$this->postData['mod'] = 'Abnormal';
		$this->postData['act'] = 'moneyspins';
		$this->postData['money'] = $money;
		$this->postData['spins'] = $spins;
		$this->postData['level'] = $level;
		$this->postData['sort'] = $sort;
		$this->postData['type'] = 1;
		$res = $this->curl->req($url, $this->postData);
		
		$retData = json_decode($res, TRUE);
		$data['data'] = $retData;
		$data['sort'] = $sort;
		$data['level'] = $level;
		$data['money'] = $money;
		$data['spins'] = $spins;
        $this->load->view('abnormal/moneyspins', $data);
    }
    
    //修改 异常金币体力统计 数据
    public function actionMoneyspins(){
    	if( $this->env == 1 ){
			$url = $this->config->item('officialUrl');
		}else{
			$url = $this->config->item('localUrl');
			$this->postData['demo'] = 1;
		}
        $postData = $this->input->post();
        $posts = $this->postData;

		$this->postData['sidList'] = json_encode([1,2]);
    	$this->postData['mod'] = 'Abnormal';
        $this->postData['act'] = 'actionMoneyspins';
        $this->postData['data'] = $postData['data'];
		$this->env == 2 && $this->postData['demo'] = 1;
        $retData = $this->curl->req($url, $this->postData);
		
        if($retData == 0){
            return $this->response(1, '操作失败');
        }else{
            return $this->response(0, '操作成功');
        }
    }
    
    //金币体力来源分布
    public function moneySpinsSource($uid = 0,$type = 'money'){
		
        if( $stime = get_cookie('stime') ){
        	$data['stime'] = $stime;
			$stime = strtotime($stime);
        }else{
            $stime = strtotime('-1 days');
        }
        if( $etime = get_cookie('etime') ){
        	$data['etime'] = $etime;
			$etime = strtotime($etime);
        }else{
            $etime = time();
        }
        
        if(empty($uid)){
        	$uid = get_cookie("uid");
        }
        
        foreach(['money','spins'] as $row){
	        if($row == 'money'){
	        	$addRet = oo::commonOprModel('statistics')->moneyAddType();//金币 来源
	        	$lowRet = oo::commonOprModel('statistics')->moneyLowType();//金币 消耗
	        }else{
	        	$addRet = oo::commonOprModel('statistics')->sipnAddType();//体力 来源
	        	$lowRet = oo::commonOprModel('statistics')->sipnLowType();//体力 消耗
	        }
	        
	    	$sday = date('Y-m-d H:i:s',$stime);
	    	$logRet = $dateTemp = [];
	    	for($i=0;$i<=$this->diffBetweenTwoDays(date('Y-m-d H:i:s',$stime),date('Y-m-d H:i:s',$etime));$i++){
	    		
	            $dateTemp[] = date('Ymd', strtotime("$sday + $i days"));
	            $tb = otable::Assetslog(date('Ymd', strtotime("$sday + $i days")), $uid);
	            $sql = " SELECT * FROM {$tb} WHERE uid = {$uid} AND type = '{$row}'";
	            $tempRet = oo::commonOprDb('log')->getAll($sql, MYSQLI_ASSOC);
	            $logRet = array_merge($logRet,$tempRet);
	        }
	        $addData = array();//总的
	        $lowData = array();
	        $addDataDay = array();//按天
	        $lowDataDay = array();
	    	foreach($logRet as $k => $v){
	    		foreach($addRet as $r_k =>$r_v){
	    			if($r_k == $v['rid'] && $v['type'] == $row && $v['crease'] == '+'){
	                    $addData[$r_k] += $v['num'];
	                }
	    		}
	    		foreach($lowRet as $r_k =>$r_v){
	    			if($r_k == $v['rid'] && $v['type'] == $row && $v['crease'] == '-'){
	                    $lowData[$r_k] += $v['num'];
	                }
	    		}
	    		foreach($dateTemp as $d_k =>$d_v){
	    			if(date("Ymd",$v['time']) == $d_v){
	    				foreach($addRet as $r_k =>$r_v){
			    			if($r_k == $v['rid'] && $v['type'] == $row && $v['crease'] == '+'){
			                    isset($addDataDay[$d_v][$r_k]) ? 0 : ($addDataDay[$d_v][$r_k] = 0);
			                    $addDataDay[$d_v][$r_k] += $v['num'];
			                }
			    		}
			    		foreach($lowRet as $r_k =>$r_v){
			    			if($r_k == $v['rid'] && $v['type'] == $row && $v['crease'] == '-'){
			                    $lowDataDay[$d_v][$r_k] += $v['num'];
			                }
			    		}
	    			}
	    		}
	    	}
	    	$data[$row]['addData'] = $addData;
	    	$data[$row]['addDataDay'] = $addDataDay;
	    	$data[$row]['addType'] = $addRet;
	    	$data[$row]['lowData'] = $lowData;
	    	$data[$row]['lowDataDay'] = $lowDataDay;
	    	$data[$row]['lowType'] = $lowRet;
	    	
        }
		
		$data['uid'] = $uid;
        $this->load->view('abnormal/moneySpinsSource', $data);
    }
    
    //玩家升级曲线 & 玩家星星曲线
    public function upStarsLine($uid = '200716',$type = 'up'){
    	if( $this->env == 1 ){
			$url = $this->config->item('officialUrl');
		}else{
			$url = $this->config->item('localUrl');
			$this->postData['demo'] = 1;
		}
		
		if($type == "up"){
			$this->postData['sidList'] = json_encode([1,2]);
	    	$this->postData['mod'] = 'Abnormal';
	        $this->postData['act'] = 'upStarsLine';
	        $this->postData['uid'] = $uid;
	        $this->postData['type'] = $type;
			$this->env == 2 && $this->postData['demo'] = 1;
	        $retData = $this->curl->req($url, $this->postData);
	        
			$retData = json_decode($retData, TRUE);
		}else{
			if( $stime = get_cookie('stime') ){
	            $data['stime'] = $stime;
	            $dateArr['stime'] = strtotime($stime.' 0:0:1');
	        }else{
	            $dateArr['stime'] = strtotime('-7 days 0:0:1');
	            $data['stime'] = date('Y-m-d', $dateArr['stime']);
	        }
	        if( $etime = get_cookie('etime') ){
	            $data['etime'] = $etime;
	            $dateArr['etime'] = strtotime($etime.' 0:0:1');
	        }else{
	            $dateArr['etime'] = strtotime('-1 days 0:0:1');
	            $data['etime'] = date('Y-m-d', $dateArr['etime']);
	        }
			
			$retData = ['num'=>[],'time'=>[]];
			for($i=0;$i<=$this->diffBetweenTwoDays($data['stime'],$etime);$i++){
	            $date = date('Ymd', strtotime($data['stime']." + $i days"));
	            $tb = otable::Assetslog($date, $uid);
	            $sql = "SELECT * FROM {$tb} WHERE uid = {$uid} AND type = 'star'";
	            $ret = oo::commonOprDb('log')->getAll($sql,1);
	            foreach($ret as $k => $v){
	            	$retData['time'][] = $v['time'];
	            	$retData['num'][] = $v['last_num'];
	            }
	        }
		}
		
		$startTime = 0;
		$endTime = end($retData['time']);
		
		//if($type == 'up'){
			$time = $num = [];
			foreach($retData['num'] as $k => $v){
				if($k == 0){
					$startTime = $retData['time'][$k];
				}
				$num[$k] = date("Ymd",$retData['time'][$k])."(".$v.")";
				$time[$k] = round(($retData['time'][$k] - $startTime) / 86400,2);
			}
			$data['time'] = $time;
			$data['num'] = $num;
		//}
		
		$data['type'] = $retData['type'];
		$data['uid'] = $uid;
		
		//玩家升级曲线 从注册到最后一个等级天数之间的等级
		$stime = date("Y-m-d",$startTime);
		$etime = date("Y-m-d",$endTime);
		for($i=0;$i<=$this->diffBetweenTwoDays($stime,$etime);$i++){
            $dateTemp[] = date('Ymd', strtotime("$stime + $i days"));
        }
        
        $dayData = [];
        $levelId = 1;
        foreach($dateTemp as $k => $v){
        	$time = strtotime($v);
        	
        	foreach($retData['num'] as $tk => $tv){
        		$tempTime = strtotime(date("Y-m-d",$retData['time'][$tk]));
        		if($time == $tempTime && $tv > $levelId){
        			$levelId = $retData['num'][$tk];
        			//break;
        		}
        	}
        	$dayData[] = $levelId;
        }

		$data['dateTemp'] = $dateTemp;
		$data['dayData'] = $dayData;
		
		if($type == 'up'){
			$this->load->view('abnormal/upStarsLine', $data);
		}else{
			$this->load->view('abnormal/upStarsLine2', $data);
		}
        
    }
    
    public function diffBetweenTwoDays ($day1, $day2){
        $second1 = strtotime($day1);
        $second2 = strtotime($day2);

        if ($second1 < $second2) {
            $tmp = $second2;
            $second2 = $second1;
            $second1 = $tmp;
        }
        return ($second1 - $second2) / 86400;
    }
}
