<?php
namespace App\Models;
use DB;
class GamePrize extends BaseModel
{

    protected $table="game_prize";
    public $timestamps = false;
    //获取游戏目录
    function getgame(){
        $data=$this->select('game_name')->groupBy('game_name')->get();
		if(!$data){
            return -5030002822;//没有数据
		}
		$data = $data->toArray();
		foreach ($data as $k=>$v){
		    $data[$k]['name']=trans('common.'.$v['game_name']);
        }
        return $data;
    }
    //获取游戏历史数据
    function getMsg($where,$list=20,$fild='open_time',$order='desc'){
        $data=$this->where($where)->orderBy($fild,$order)->paginate($list);
        if(!$data){
            return -5030002822;//没有数据
        }
        $data=$data->toArray();
        foreach ($data['data'] as $k=>$v){
            $data['data'][$k]['name']=trans('common.'.$v['game_name']);
        }
        return $data;
    }
    protected function getMsgs($where,$game,$list=20,$fild='open_time',$order='desc'){
        //DB::connection()->enableQueryLog();
        $gamemodel = '\App\Models\Game' . ucfirst($game);
        $data = $gamemodel::where($where)->orderBy($fild,$order)->paginate($list);
        //$queries = DB::getQueryLog();
        //print_r($queries);
        if(!$data){
            return -5030002822;//没有数据
        }
        $data=$data->toArray();
        foreach ($data['data'] as $k=>$v){
            $data['data'][$k]['name']=trans('common.'.$game);
        }
        return $data;
    }
    //根据id获取该条数据信息
    function getMsgByID($id){
        $data=$this->where('id',$id)->first();
        if(!$data){
            return -5030002922;//没有数据
        }
        return $data->toArray();
    }
    //修改信息
    function editMsgByID($data,$id){
        $res=$this->where('id',$id)->update($data);
        if(!$res){
            return -5030003022;//修改失败
        }
        return 1;
    }
    //插入数据
    function insetMsg($data){
        if($this->checkInfo($data['game_name'],$data['info_no'])<0){
            $res=$this->where('game_name',$data['game_name'])->where('info_no',$data['info_no'])->update($data);
        }else{
            $res=$this->insert($data);
        }

        if(!$res){
            return -5030003122;//添加失败
        }
        return 1;
    }
    //获取游戏最新期数
    function getInfoNo($game){
        $info_no=$this->select('info_no')->where('game_name',$game)->orderBy('info_no','desc')->first();
        if(!$info_no){
            return -5030113122;//没有期号
        }
        return $info_no['info_no']+1;
    }
    //检测期号是都存在
    protected function checkInfo($game,$info_no){
        $info=$this->where('game_name',$game)->where('info_no',$info_no)->first();
        if(!$info){
            return 1;
        }
        return -5030114122;
    }

}
