<?php

namespace App\Models;
use Illuminate\Support\Facades\DB;

/**
 *
 * @property mixed account_identity
 */
class Account_detailed extends BaseModel {
	protected $table = "account_detailed";
	public $timestamps = false;
	//由identity获取用户信息
	function getInfoBy($u_id) {
		$data = $this->where('account_identity', $u_id)->first();
		if (!$data) {
			return -5040012022;
		}
		return $data->toArray();
	}
	protected function getInfoBys($u_id){
	    return $this->getInfoBy($u_id);
    }
	
	function getUinfo(){
		DB::connection()->enableQueryLog();
		$data = $this->select('account_identity')->whereNull('register_url')->limit(1000)->offset(0)->get();
		// $queries = DB::getQueryLog();
		// print_r($queries);
		if (!$data) {
			return -5040012022;
		}
		return $data->toArray();
	}
	
	function updateInfo($data, $uid) {
		$res = $this->where('account_identity', $uid)->update($data);
		if (!$res) {
			return -5040012022;
		}
		return 1;
	}
	function updateInfos($data, $id) {
		$res = $this->where('id', $id)->update($data);
		if (!$res) {
			return -7010101202; //更新失败
		}
		return 1;
	}
	function addMoney($account_id,$money){
	    $res1=$this->where('account_identity',$account_id)->increment('available_cash',$money);
	    if(!$res1){
            return -5040022022;//增加约失败
        }
	    $res2=$this->where('account_identity',$account_id)->increment('cash',$money);
	    if(!$res2){
            return -5040022022;//增加约失败
        }
        return 1;
    }
    function costMoney($account_id,$money){
        $res1=$this->where('account_identity',$account_id)->decrement('available_cash',$money);
        if(!$res1){
            return -5040022022;//增加约失败
        }
        $res2=$this->where('account_identity',$account_id)->decrement('cash',$money);
        if(!$res2){
            return -5040022022;//增加约失败
        }
        return 1;
    }

    //统计在线用户
    function getOnlineUser(){
        $time=time();
        $timeZone=[date('Y-m-d H:i:s',$time-300),date('Y-m-d H:i:s')];
        $data=$this->select(DB::Raw('count(id) as value,last_url as name'))
            ->where('statuss',1)->whereNotNull('last_url')
            //->whereBetween('last_time',$timeZone)
            ->groupBy('last_url')
            ->get();
        if(!$data){
            return -2020032103;
        }
        return $data->toArray();
    }
    function getCashArray($id_array){
        $data=$this->whereIn('account_identity',$id_array)->get();
        if(!$data){
            return -5040012522;
        }
        $data=$data->toArray();
        $account_cash_array=array();
        foreach ($data as $k=>$v){
            $account_cash_array[$v['account_identity']]=$v['cash'];
        }
        return $account_cash_array;
    }

    //在线
    function Accountas($where){
        if (!empty($where)){
            $data=$this->rightJoin('account','account.identity',$this->table.'.account_identity')
                ->where('account_detailed'.'.'.'statuss',$where);
            $data=$data ->paginate();
        }else{
            $data=$this->rightJoin('account','account.identity',$this->table.'.account_identity');
            $data=$data ->paginate();
        }

        if (!$data){
            return -502030154202;
        }
        return $data->toArray();
    }
    //改变会员标签至1
    protected function changeToleve($group_codes,$data){
//        DB::connection()->enableQueryLog();
        $res=$this->where('group_code','like',"%{$group_codes}%")->update($data);
//         $queries = DB::getQueryLog();
//         print_r($queries);exit;

        return 1;
    }


}

?>