<?php
/**
 * Created by PhpStorm.
 * User: ikeke
 * Date: 2018/12/6
 * Time: 19:38
 */

namespace App\Models;

use DB;

class Hjgame_betting_ogrbv extends BaseModel
{
	protected $table      = "hjgame_betting_ogrbv";
	public    $timestamps = false;
	
	/**
	 * 获取游戏投注记录
	 * @param string $where
	 * @param int    $limit
	 * @return mixed
	 */
	public function getlist($where = '', $limit = 15, $orby = 'id', $sc = "desc"){
		$data = $this->orderBy($orby, $sc);
		if (is_array($where) && count($where)>0) {
			$data = $data->where($where);
		}
		$data = $data->paginate($limit);
		
		return $data? $data->toArray():[];
	}
	
	protected function getLastTime(){
		$data	=	$this->orderBy('id','desc')->first(['bet_time']);
		return	$data?date('Y-m-d H:i:s',$data->toArray()['bet_time']):'';
	}
	
	/**
	 * 获取指定数据
	 * @param string $where
	 * @return mixed
	 */
	protected function getOneData($where = ''){
		return $data = $this->where('record_id', $where)->first();
	}
	
	public function getSum($where){
		$arr = [];
		$ba  = 0;
		$wa  = 0;
		$va  = 0;
		$co  = 0;
		if (is_array($where) && count($where)>0) {
			//查询总投注金额
			$ba = $this->where($where)->sum('bet_amount');
			//查询总派彩金额
			$wa = $this->where($where)->where('profit','>','0')->sum('profit');
			//查询总有效投注金额
			$va = $this->where($where)->where('state','1')->sum('bet_amount');

			
			//查询总条数
			$co = $this->where($where)->count();
		}
		
		$arr['bettingamount'] = ceil($ba) == $ba ? $ba : round($ba, 2);
		$arr['winLoseamount'] = ceil($wa) == $wa ? $wa : round($wa, 2);
		$arr['validamount']   = ceil($va) == $va ? $va : round($va, 2);
		$arr['revenue']   = 0;
		$arr['co']            = $co;
		
		return $arr;
		
	}
	
	//获取回水列表
	protected function getMoneyback($value = '', $type = 1, $limit = 10, $wheregame = '', $orwhere = '', $having = '')
	{
		DB::connection()->enableQueryLog();
		
		$data = $this->select('account_name','account_identity',"username as Accounts",DB::raw('sum("bet_amount") as betting_money'),DB::raw('sum("profit") as WinLoseAmount'),DB::raw('sum("bet_amount") as ValidAmount'),DB::raw('count("id") as bet_count'))->where('state',1)->groupBy('username','account_name','account_identity');
		if (!empty($having)) {
			foreach ($having as $v) {
				$data = $data->havingRaw($v);
			}
		}

		if (empty($value) || is_array($value)) {
			$where = $value;
		} else {
			$where[] = array( 'id', $value);
		}
		$where[] = array('type', 	 1);
		$data = $data->where($where);
		$data = $data->paginate($limit);
		
		if (!$data) {
			return -5030001202; //没有列表数据
		}
		
		return $data->toArray();
	}
	
	
	//反水更改狀
	public function updateBackWater($name, $timearea)
	{
	
		$res = $this->where('account_name', $name)->where($timearea);
		
		$res = $res->update(['type' => '2']);
		
		if (!$res) {
			return -3012564406; //反水失败
		}
		return 1;
	}
	//添加游戏注单信息
	protected function AddGameBet($betinfo){
		$users	=	$indata	=	[];
		$i=$j=0;
		foreach ($betinfo as $k => $v) {
			if(!empty($v['bet_time'])){
				$v['bet_time'] = strtotime($v['bet_time']);
			}
			if(!empty($v['draw_time'])){
				$v['draw_time'] = strtotime($v['draw_time']);
			}

			if (!array_key_exists($v['username'], $users)) {
				//获取本站用户名
				$loainfo = \App\Models\Oggame_user::select('lo_name')->where('rp_name', $v['username'])->first();

				//获取本站用户ID
				if (!empty($loainfo->lo_name)){
					$aid = \App\Models\Account::select('identity')->where('account', $loainfo->lo_name)->first();

					if (!empty($aid->identity)){
						$users[$v['username']] = ['identity' => $aid->identity, 'name' => $loainfo->lo_name];
						$indata = [
							'oder_id'	=>md5(json_encode($v)),
							'account_identity' =>$users[$v['username']]['identity'],
							'account_name' => $users[$v['username']]['name'],
						];
						
						$indata	=	(array_merge($indata,$v));
						
						try{
							$res = $this->insert($indata);
							if($res)++$i;
						}catch (\Exception $e){
							if(strpos($e->getMessage(),'Unique violation')!==false){continue;}else{
							dd($e);
							}
						}
					}
				}
				
			}

		}

		return $i;
	}
	
	
}
