<?php
namespace App\Models;
class GameMoney extends BaseModel
{

    protected $table="game_money";
    public $timestamps = false;

    protected function getbetMoney($list=20,$where=''){
        return $this->getGamebetMoney($list,$where);
    }
    //获取游戏单注最大金额信息列表
    function getGamebetMoney($list=20,$where=''){
        $data=$this->select('id','game_type','big_money','small_money','commission_money','commission_rate','grade');
		if(!empty($where)&&is_array($where)){
			$data = $data->where($where);
		}else{
			$data = $data->where('game_type','!=',-1);
		}
		$data = $data->paginate($list);
        if(!$data){
            return -5040000401;//没有数据
        }
        $data=$data->toArray();
        foreach ($data['data'] as $k=>$v){
            $data['data'][$k]['name']=trans('common.'.$v['game_type']);
        }
        return $data;
    }
    //设置游戏单注投注金额等信息
    function setMoneyLimit($data,$game){
        if(($has=$this->has_game($game))<0){
            return $has;
        }
        $res=$this->where('game_type',$game)->update($data);
        if(!$res){
            return -5040000722;
        }
        return 1;
    }
    //检查是否存在此游戏
    function has_game($game,$grade=0){
        $res=$this->where('game_type',$game)->where('grade',$grade)->first();
        if(!$res){
            return -5040000822;//不存在的游戏
        }
        return 1;
    }
    //
    function getBetOne($id){
      $data=$this->where('id',$id)->first();
      if(!$data){
          return -5040000822;
      }
      $data=$data->toArray();
      $data['name']=trans('common.'.$data['game_type']);
      return $data;
    }
    function updateOne($data,$id){
        $res=$this->where('id',$id)->update($data);
        if(!$res){
            return -5040001101;//失败
        }
        return 1;
    }
    //添加数据
    protected function add($data){
        if($this->has_game($data['game_type'],$data['grade'])>0){
            return -50400001922;//已存在该游戏
        }
        if(!isset($data['identity'])){
            $data['identity']=UUID();
        }

        $res=$this->insert($data);
        if(!$res){
            return -50400002922;//添加失败
        }
        return 1;
    }
}
