<?php

namespace App\Models;
use Illuminate\Support\Facades\DB;
class Logfile extends BaseModel {
	protected $table = "logfile";
	public $timestamps = false;

	function log($list,$page,$where='') {
		$data = $this->join('account_detailed', 'logfile.account_identity', '=', 'account_detailed.account_identity')
			->select('account', 'add_time', 'type', 'ip', 'url', 'register_ip')
			->orderBy('add_time', 'desc')
			->paginate($list);
		if(!empty($where)&&is_array($where)){
			$data = $this->join('account_detailed', 'logfile.account_identity', '=', 'account_detailed.account_identity')
			->select('account', 'add_time', 'type', 'ip', 'url', 'register_ip')
			->where($where)
			->orderBy('add_time', 'desc')
			->paginate($list);
		}
		if (!$data) {
			return -2020032003; //没有用户登录日志
		}
		return $data->toArray();
	}
	
	//获取用户登录网址和注册网址
	function getUrl($account_identity){
		//DB::connection()->enableQueryLog();
		$sql = 'SELECT b.account,b.account_identity,a.maxtime,a.mintime,b.type,b.ip,b.url as last_url,c.url as register_url FROM (SELECT max(add_time) as maxtime,min(add_time) as mintime,account_identity from logfile group by account_identity) as a LEFT JOIN logfile as b ON b.add_time=a.maxtime and a.account_identity=b.account_identity LEFT JOIN logfile as c ON c.add_time=a.mintime and a.account_identity=c.account_identity WHERE b.account_identity IN '.$account_identity;
		
		$data = DB::select($sql);
		
		//$queries = DB::getQueryLog();
		//print_r($queries);		
		if (!$data < 0) {
			return -2020000102; //没有相关信息
		}
		$data = json_encode($data);
		$data = json_decode($data,1);
		return $data;
	}
	
	//统计当前用户在线数目
	function getUser(){
        $time=date('Y-m-d H:i:s',time()-30*60);

//        $time=1512357978;
        $data=$this->select(DB::raw('url as name,count(account) as value'))
            ->where('add_time','>',$time)
           // ->join('account_token as b','b.account_identity',$this->table.'.account_identity')
            ->groupBy('url')
            ->get();
        if(!$data){
            return -2020032103; //没有用户登录日志
        }
//        echo '<pre>';
//        print_r($data->toArray());
        return $data->toArray();
    }
    //在线人数
    function onlineUser($where){
        $data=$this->select((DB::raw('max("id"),url as name')))
            ->whereIn('account_identity',$where)
            ->groupBy('url')
            ->get();
//        echo '<pre>';
//        print_r($data->toArray());
        return $data->toArray();
    }

}

?>