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

    protected $table="game_type";
    public $timestamps = false;
    //获取游戏列表
    function getGameList(){
        $data=$this->select('id','name','table_name as gameCode')->where('table_name','!=',-1)->orderBy('hot','desc')->get();
        if(!$data){
            return -5040000522;//没有数据
        }
        return $data->toArray();
    }

    //获取代理业绩统计游戏列表
    protected function getNagentglist(){
        $data=$this->select('table_name')->where('table_name','!=',-1)->orderBy('hot','desc')->where('status',1)->get();
        if(!$data){
            return -5040000522;//没有数据
        }
        return $data->toArray();
    }

    protected function getGameLists() {
        return $this->getGameList();
    }
    //获取游戏关闭开启状态
    function getGameStatus($list=10,$where=''){
        $data=$this;
		if(!empty($where)&&is_array($where)){
			$data = $data->where($where);
		}
		$data = $data->paginate($list);
        if(!$data){
            return -5040000422;//没有数据
        }
        return $data->toArray();
    }

    //获取记入级差代理流水游戏列表
    protected function getNagentWatergame($type = 1){
        $data = $this->select('table_name')->where('table_name','<>',-1)->where('agent_is_water',1)->get();
        if(!$data || count($data->toArray())<1)return -5040000402;
        $resdata = array();
        $restr = '';
        foreach ($data->toArray() as $k => $v) {
            $resdata[] = $v['table_name'];
            $restr .= empty($restr)?"('" . $v['table_name'] . "'" : ",'" . $v['table_name'] . "'";
        }
        $restr .= ')';
        if($type==1){
            return $restr;
        }
        return $resdata;
    }

    //获取不记入级差代理流水游戏列表
    protected function getOutWatergame(){
        $data = $this->select('table_name')->where('table_name','<>',-1)->where('agent_is_water',2)->get();
        if(!$data || count($data->toArray())<1)return -5040000402;
        return $data->toArray();
    }

    //获取游戏列表
    function getGameAllList($list){
        if(empty($list))
        {
            $list = 10;
        }
        $data = DB::table('game_type AS gt')
            ->select("gt.id","gt.name","gt.closeinfo","gt.status","gt.close_status","gc.name AS cate_name","gt.extend","gt.suggest")
            ->leftjoin('game_category AS gc','gt.cate_id','=','gc.id')->orderby('gt.id','asc')
            ->paginate($list);
        if(!$data){
            return -5040000422;//没有数据
        }
      
        return $data->toArray();
    }

    //获取游戏玩法列表
    function getDescList($list){
        $data=$this->select('id','name','table_name')->where('table_name','!=',-1)->paginate($list);
        if(!$data){
            return -5040000622;//没有数据
        }
        return $data->toArray();
    }

    function closeGames($data,$id){
        $res=$this->where('id',$id)->update($data);
        if(!$res){
            return -5040000122;
        }
        return 1;
    }
    //关闭游戏
    function closeGame($data,$game){
        $res=$this->where('table_name',$game)->update($data);
        if(!$res){
            return -5040000122;
        }
        return 1;
    }
    //一条关闭信息
    function getCloseOne($game){
        $data=$this->where('id',$game)->first();
        if(!$data){
            return -5040000622;
        }
        return $data->toArray();
    }
    //更新关闭信息
    function updateOne($data,$id){
        $res=$this->where('id',$id)->update($data);
        if(!$res){
            return -5040000122;
        }
        return 1;
    }
    //添加
    function addGame($data){
        $data['identity']=UUID();
        $data['created']=date('Y-m-d');
        $res=$this->insert($data);
        if(!$res){
            return -5040001122;
        }
    }
    //检测游戏名字
    function checkCopyGame($name,$table_name){
        $data=$this->where('name',$name)->orWhere('table_name',$table_name)->first();
        if(!$data){
            return 1;
        }
        return 5040451122;
    }
    //获取游戏名及代码
    protected function getGameName(){
        $data=$this->select('name','table_name as game')
            ->where('status',1)
            ->where('table_name','!=',-1)
            ->where('table_name','!=','diceone')
            ->where('table_name','!=','guess')
            ->get();
        if(!$data){
            return -503232122;
        }
        return $data->toArray();

    }
    //获取下拉选择游戏
    protected function getGameType($type=1){
        $data=$this->select('name','table_name as gameCode')->where('table_name','!=',-1);
        if($type==1){
            $data = $data->where('table_name','!=','guess')
            ->where('table_name','!=','diceone')
            ->where('table_name','!=','sixlottery')
            ->where('table_name','!=','sfsixlottery')
            ->where('table_name','!=','dice')
            ->where('table_name','!=','hgxin');
        }
        $data = $data->orderBy('hot','desc')->get();
        if(!$data){
            return -5040000522;//没有数据
        }
        if($type==2){
            $ag = $this->AllowGamestart();
            foreach ($data as $k => $v) {
                if(!in_array($v['gameCode'], $ag))unset($data[$k]);
            }
        }
        return $data->toArray();
    }

    //起始期设置允许游戏列表
    protected function AllowGamestart(){
        return array('bjpk10','fc3d','gpxy28','xy28','gxkl10','jxkuai3','pl3','tjssc','tw28','xjp28','xy28');
    }

    //获取添加游戏起始号码游戏列表
    protected function getSetscList(){
        $sql = "select name,table_name from game_type where table_name<>'-1' and table_name NOT IN (select game from game_origin)";
        $data = DB::select ($sql);
        if (!$data < 0) {
            return -5040000522; //没有数据
        }
        $data = json_encode ($data);
        $data = json_decode ($data, 1);
        $ag = $this->AllowGamestart();
        foreach ($data as $k => $v) {
            if(!in_array($v['table_name'], $ag))unset($data[$k]);
        }
        return $data;
    }

    //获取游戏开奖周期秒数
    function getOpenTime($game_code)
    {
        $data=$this->select('table_name','open_time as cycle_time')->where('table_name',$game_code)->first();
        if(!$data){
            return -503232122;
        }
        return $data->toArray();
    }
}
