<?php
/**
 * 用户信息相关
 */
defined( 'IN_WEB') or die( 'Include Error!');

include_once dirname(__FILE__) . "/Base.php";


class Abnormal extends  Base{
	
	function secToTime($sec)
    {
        $sec = round($sec/60);
        if ($sec >= 60){
            $hour = floor($sec/60);
            $min = $sec % 60;
            $res = $hour.' 小时 ';
            $min != 0  &&  $res .= $min.' 分钟';
        }
        else{
            $res = $sec.' 分钟';
        }
        return $res;
    }
	//金币体力异常
	public function moneyspins($param){
        return;
		$sort = $param['sort'];
		$money = oo::functions()->uint($param['money']);
		$spins = oo::functions()->uint($param['spins']);
		$level = oo::functions()->uint($param['level']);
		$type = oo::functions()->uint($param['type']);
		if(empty($money)){
			$money = 10000000;
		}
		if(empty($spins)){
			$spins = 1000;
		}
		if(empty($level)){
			$level = 50;
		}
		if(empty($type)){
			$typeSql = 'AND';
		}else{
			$typeSql = 'OR';
		}
		
		$blackUidArr = oo::commonOprModel("config")->payWhiteList();
        $blackUidSql = '';
        if(!empty($blackUidArr)) {
            $blackUidStr = implode(',', $blackUidArr);
            $blackUidSql = " AND mid NOT IN ({$blackUidStr}) ";
        }
        
		$tb = otable::gh_gameserver(1);
		$group = '';
		if($sort == 'money'){
			$group = 'GROUP BY money DESC';
		}elseif($sort == 'spins'){
			$group = 'GROUP BY spins DESC';
		}elseif($sort == 'star'){
			$group = 'GROUP BY star DESC';
		}else{
			$group = 'GROUP BY spins DESC,money DESC,star DESC';
		}
		$sql = "SELECT mid as uid,levelId,star,money,spins FROM {$tb} WHERE (spins >= '{$spins}' OR money >= '{$money}') {$typeSql} levelId > '{$level}' {$blackUidSql} {$group}";
		$ret = oo::commonOprDb('common')->getAll($sql, 1);
		$uids = [];
		foreach($ret as $k => $v){
			$uid = $v['uid'];
			$uids[] = $uid;
			$userinfo = oo::commonOprModel('member')->getUserInfo($uid);//玩家基本信息
			
			$ret[$k]['nick'] = $userinfo['nick'];
			$ret[$k]['status'] = $userinfo['status'];
			$ret[$k]['createtime'] = $userinfo['createtime'];
			$ret[$k]['lasttime'] = $userinfo['lasttime'];
			$ret[$k]['vip'] = $userinfo['vip'];
			$ret[$k]['isVistor'] = oo::commonOprModel('member')->isVistor($uid);
			$ghtable = otable::statistics($uid);
            $updateSrv = "SELECT * FROM {$ghtable} WHERE uid={$uid}";
			$loginTime = oo::commonOprDb('ghgames')->getOne($updateSrv,1);
            $ret[$k]['loginTime'] = $this->secToTime($loginTime['loginTime']);
            $payment  = otable::payment($uid);
            $sql = "SELECT sum(pusd) as sum FROM {$payment} WHERE uid = {$uid} AND status = 2";
            $paymentRet = oo::commonOprDb('payment')->getOne($sql,MYSQLI_ASSOC);
            $ret[$k]['sumMoney'] = $paymentRet['sum'];
            
            $ret[$k]['chatLock']= oo::commonOprRedis('Userinfo')->get(okeys::chatLock($uid));
            if($ret[$k]['sumMoney'] > 1){
            	unset($ret[$k]);
            }
		}
		
		if(!empty($uids)) {
            $uidstr = implode(',', $uids);
            $tb = otable::ab_moneyspins();
            $sql = "SELECT * FROM {$tb} WHERE uid in($uidstr)";
            $moneyspins = oo::commonOprDb('common')->getAll($sql, 1);
            $reason = [];
            foreach($moneyspins as $k => $v){
            	$reason[$v['uid']] = $v['reason'];
            }
            foreach($ret as $k => $v){
            	$ret[$k]['reason'] = $reason[$v['uid']];
            }
        }
		
		return json_encode($ret);
	}
	
	/**
     * 修改 异常金币体力统计 数据
     * @return string
     * Created by: hss
     * Created on: 2020/3/12 16:56
     */
    public function actionMoneyspins($param){
        return;
    	$data = json_decode($param['data']);
        $tb = otable::ab_moneyspins();
        foreach ($data as $row){
        	//先判断有这个用户的数据
        	$uid = $row->uid;
        	$sql = "SELECT COUNT(*) as count FROM {$tb} WHERE uid = {$uid}";
        	$ret = oo::commonOprDb('ghgames')->getOne($sql,1);
        	
        	if($ret['count']){
        		//更新
        		$sql = "UPDATE {$tb} SET ";
	            $sql .= "reason='{$row->data->reason}' WHERE uid={$uid}";
	            $rs = oo::commonOprDb('statistics')->query($sql);
        	}else{
        		//插入
        		$sql = "INSERT INTO {$tb} (uid,reason) VALUES (";
	            $sql .= "'{$uid}','{$row->data->reason}')";
	            $rs = oo::commonOprDb('statistics')->query($sql);
        	}
        }

        return $rs ? $rs : 0;
    }
	
	/**
     * 金币体力来源分布
     * @return string
     * Created by: hss
     * Created on: 2020/3/12 16:56
     */
    public function moneySpinsSource($param){
        return;
    	$start_time = microtime();
    	$stime = oo::functions()->uint($param['stime']);
        $etime = oo::functions()->uint($param['etime']);
        $uid = oo::functions()->uint($param['uid']);
        $type = oo::functions()->escape($param['type']);
        if (empty($stime)){
            $stime = strtotime('-7 days');
        }
        if (empty($etime)){
            $etime = time();
        }
        $tb = otable::Assetslog($uid);
        if($type == 'money'){
        	//金币 来源
        	$addRet = oo::commonOprModel('member')->moneyAddType();
        	$addRet['betSteal'] = '倍率偷取';
        	$addRet['steal'] = '偷到';
        	
        	//金币 消耗
        	$lowRet = oo::commonOprModel('member')->moneyLowType();
        	
        	$sql = " SELECT * FROM {$tb} WHERE uid={$uid} AND type = 'money' AND `time` >= '{$stime}' AND `time` <= '{$etime}'";
        }else{
        	//体力 来源
        	$addRet = oo::commonOprModel('member')->sipnAddType();
        	
        	//体力 消耗
        	$lowRet = oo::commonOprModel('member')->sipnLowType();
        	
        	$sql = " SELECT * FROM {$tb} WHERE uid={$uid} AND type = 'spins' AND `time` >= '{$stime}' AND `time` <= '{$etime}'";
        }
        $logRet = oo::commonOprDb('common')->getAll($sql, MYSQLI_ASSOC);
    	
    	$sday = date('Y-m-d H:i:s',$stime);
    	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"));
        }
        $addData = $lowData =array();//总的
        $addDataDay = $lowDataDay = array();//按天
    	foreach($logRet as $k => $v){
    		foreach($addRet as $r_k =>$r_v){
    			if(strpos($v['reason'],$r_v) !== false){
                    $addData[$r_k] += $v['num'];
                }
    		}
    		foreach($lowRet as $r_k =>$r_v){
    			if(strpos($v['reason'],$r_v) !== false){
                    $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(strpos($v['reason'],$r_v) !== false){
		                    $addDataDay[$d_v][$r_k] += $v['num'];
		                }
		    		}
		    		foreach($lowRet as $r_k =>$r_v){
		    			if(strpos($v['reason'],$r_v) !== false){
		                    $lowDataDay[$d_v][$r_k] += $v['num'];
		                }
		    		}
    			}
    		}
    	}
        
        return json_encode(['addData'=>$addData,'lowData'=>$lowData,'addType'=>$addRet,'lowType'=>$lowRet,'addDataDay'=>$addDataDay,'lowDataDay'=>$lowDataDay,'resultTime'=>microtime() - $start_time]); 
    }
	
	/**
     * 玩家升级曲线 & 星星曲线
     * @return string
     * Created by: hss
     * Created on: 2020/4/16 10:42
     */
    public function upStarsLine($param){
        $uid = oo::functions()->uint($param['uid']);
        $type = oo::functions()->escape($param['type']);
        $type = empty($type) ? 'up' : $type;

        if($type == "up"){
            //升级曲线
            $tb = otable::userlevel($uid);
            $sql = "SELECT levelId,createTime FROM {$tb} WHERE uid = {$uid} ORDER by levelId ASC";
            $ret = oo::commonOprDb('userlevel')->getAll($sql, MYSQLI_ASSOC);
            $time = $num = [];
            foreach($ret as $k => $v){
                $time[] = $v['createTime'];
                $num[] = $v['levelId'];
            }
            return json_encode(['time'=>$time,'num'=>$num]);
        }else{
            //星星曲线
            $userinfo = oo::commonOprModel('member')->getUserInfo($uid);//玩家基本信息
            $tb = otable::Assetslog($uid);
            $sql = "SELECT * FROM {$tb} WHERE uid = {$uid} AND type = 'star'";
            $ret = oo::commonOprDb('common')->getAll($sql, MYSQLI_ASSOC);
            $time = $num = [];
            $time[] = $userinfo['createtime'];
            $num[] = "1";
            foreach($ret as $k => $v){
                $time[] = $v['time'];
                $num[] = $v['last_num'];
            }
            return json_encode(['time'=>$time,'num'=>$num]);
        }
    }
    
    public function diffBetweenTwoDays ($day1, $day2){
        $second1 = strtotime($day1);
        $second2 = strtotime($day2);

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