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

namespace App\Models;
use DB;

class Lygame_betting_ogrbv extends BaseModel
{
    protected $table = "lygame_betting_ogrbv";
    public $timestamps = false;

    /**
     * 获取游戏投注记录
     * @param string $where
     * @param int $limit
     * @return mixed
     */
    protected function getlist($where='',$limit=15,$orby='GameStartTime',$sc="desc")
    {
        $data = $this->orderBy($orby,$sc);
        if(is_array($where) && count($where)>0){
            $data = $data->where($where);
        }
        $data=$data->paginate($limit);

        return $data->toArray();
    }

    //获取回水列表
    protected function getMoneyback($value = '', $type = 1, $limit = 10, $wheregame = '', $orwhere = '', $having = '')
    {
        //DB::connection()->enableQueryLog();
        $key = $this->getFeild($type);
        $data = $this->join ('account_detailed', $this->table.'.account_identity', '=', 'account_detailed.account_identity')->select('account_name','group_name','group_code',$this->table.'.account_identity','Accounts',DB::raw('sum("AllBet") as betting_money'),DB::raw('sum("CellScore") as ValidAmount'),DB::raw('sum("Profit") as WinLoseAmount'),DB::raw('count("GameID") as bet_count'))->where('type',1)->groupBy('Accounts','account_name','group_name','group_code',$this->table.'.account_identity');
        if (!empty($having)) {
            foreach ($having as $v) {
                $data = $data->havingRaw($v);
            }
        }
        if (!empty($wheregame)) {
            $data->whereIn('Accounts', $wheregame);
        }
        if (empty($value) || is_array($value)) {
            $where = $value;
        } else {
            $where[] = array($key, '=', $value);
        }

        $where[] = array('type', '=', 1);
        $data = $data->where($where);
        $data = $data->paginate($limit);
        if (!$data) {
            return -5030001202; //没有列表数据
        }
        return $data->toArray();
    }

    //反水更改狀态
    protected function updateBackWater($name, $timearea)
    {

        $res = $this->where('account_name', $name)->where($timearea);

        $res = $res->update(['type' => '2']);

        if (!$res) {
            return -3012564406; //反水失败
        }
        return 1;
    }

    /**
     * 获取指定数据
     * @param string $where
     * @return mixed
     */
    protected function getOneData($where='')
    {
        return $data = $this->where('GameID', $where)->first();
    }

    protected function getSum($where)
    {
        $arr = array();
        $ba = 0;
        $wa= 0;
        $va = 0;
        $co = 0;
        if(is_array($where) && count($where)>0)
        {
            //查询总投注金额
            $ba = $this->where($where)->sum('AllBet');
            //查询总派彩金额
            $wa = $this->where($where)->sum('Profit');
            //查询总有效投注金额
            $va = $this->where($where)->sum('CellScore');
            $re = $this->where($where)->sum('Revenue');
            //查询总条数
            $co = $this->where($where)->count();
        }

        $arr['bettingamount'] = ceil($ba) == $ba ? $ba.'.00' : round($ba ,2);
        $arr['winLoseamount'] = ceil($wa) == $wa ? $wa.'.00' : round($wa ,2);
        $arr['validamount'] = ceil($va) == $va ? $va.'.00' : round($va ,2);
        $arr['revenue'] = ceil($re) == $re ? $re.'.00' : round($re ,2);
        $arr['co'] = $co;

        return $arr;

    }
    
    //字段对应值
    private function getFeild($num)
    {
        $data = array(
            '1' => 'id',
            '2' => 'GameID',
            '3' => 'Accounts',
            '4' => 'ServerID',
            '5' => 'KindID',
            '6' => 'TableID',
            '7' => 'ChairID',
            '8' => 'UserCount',
            '9' => 'CardValue',
            '10' => 'CellScore',
            '11' => 'AllBet',
            '12' => 'Profit',
            '13' => 'Revenue',
            '14' => 'GameStartTime',
            '15' => 'GameEndTime',
            '16' => 'ChannelID',
            '17' => 'LineCode',
            '18' => 'type',
            '19' => 'lygame_betting_ogrbv.account_identity',
            '20' => 'account_name',
            '21' => 'group_name',
        );
        return $data[$num];
    }

    //添加游戏注单信息
    protected function AddGameBet($betinfo){
        if(empty($betinfo) || empty($betinfo['GameID']))return -50630200102;
        $hasgid = $this->select('GameID')->whereIn('GameID',$betinfo['GameID'])->get();
        $hsinfo = array();
        if($hasgid && count($hasgid->toArray())>0){
            foreach ($hasgid->toArray() as $k => $v) {
                $hsinfo[] = $v['GameID'];
            }
        }
        $indata = array();
        foreach ($betinfo['GameID'] as $k => $v) {
            if(in_array($v, $hsinfo))continue;
            $account = explode('_', $betinfo['Accounts'][$k]);
            $rp_name = str_replace($account[0].'_', '', $betinfo['Accounts'][$k]);
            if(empty($account[2]))return -50630200202;
            //获取本站用户名
            $loainfo = \App\Models\Oggame_user::select('lo_name')->where('rp_name',$rp_name)->where('game_type','lygame')->first();
            if(!$loainfo || empty($loainfo->lo_name))return -50630201302;
            //获取本站用户ID
            $aid = \App\Models\Account::select('identity')->where('account',$loainfo->lo_name)->first();
            if(!$aid || empty($aid->identity))return -50630200302;
            $indata[$k] = array(
                'GameID' => $v,
                'Accounts' => $betinfo['Accounts'][$k],
                'ServerID' => $betinfo['ServerID'][$k],
                'KindID' => $betinfo['KindID'][$k],
                'TableID' => $betinfo['TableID'][$k],
                'ChairID' => $betinfo['ChairID'][$k],
                'UserCount' => $betinfo['UserCount'][$k],
                'CellScore' => $betinfo['CellScore'][$k],
                'AllBet' => $betinfo['AllBet'][$k],
                'Profit' => $betinfo['Profit'][$k],
                'Revenue' => $betinfo['Revenue'][$k],
                'GameStartTime' => strtotime($betinfo['GameStartTime'][$k]),
                'GameEndTime' => strtotime($betinfo['GameEndTime'][$k]),
                'CardValue' => $betinfo['CardValue'][$k],
                'ChannelID' => $betinfo['ChannelID'][$k],
                'LineCode' => $betinfo['LineCode'][$k],
                'account_identity' => $aid->identity,
                'account_name' => $loainfo->lo_name,
            );
        }
        if(!empty($indata)){
            $res = $this->insert($indata);
            if($res<0)return -50630200402;
        }
        return 1;
    }

    //接口地址获取
    protected function transferGame($agent,$data,$account,$money=0,$orderid='',$KindID=0,$time=1)
    {
        $ip= GETIP();
        $s = $data['s'];
        $robj = '\App\Models\Rgame_setting';
        $timestamp = str_pad($robj::getMillisecond(),13,0);//时间戳
        $jdata = json_decode($data['extend'], true);
        $DESKey = $jdata['desKey'];//'139B4CCEF28033C9';
        $lineCode=$jdata['lineCode'];
        $prams = 's='.$s;
        if ($s==0){
            $prams .= '&account='.$account.'&money='.$money.'&orderid='.$orderid.'&ip='.$ip.'&lineCode='.$lineCode.'&KindID='.$KindID;
        }elseif ($s==1 || $s==5 || $s==7 || $s==8){
            $prams .= '&account='.$account;
        }else if($s==2||$s==3){
            if($data['game_type'] == 'lygame'){
                $prams .= '&account='.$account.'&money='.$money.'&orderid='.$orderid;
            }else{
                $prams .= '&account='.$account.'&orderid='.$orderid.'&money='.$money;
            }
        }else if($s==4){
            $prams .= '&orderid='.$orderid;
        }else if($s==6)
        {
            $startTime =  $timestamp-60*$time*1000;
            $endTime = $timestamp;
            $prams .= '&startTime='.$startTime.'&endTime='.$endTime;
        }
        $params = urlencode($robj::openssl_desEncode($prams,$DESKey));//参数加密字符串
        $key = md5($agent.$timestamp.$jdata['md5key']);

        $param = [
            'agent' => $agent,
            'timestamp' => $timestamp,
            'param' => $params,
            'key' => $key,
        ];

        if ($s == 6)
        {
            $url = $jdata['host2'].'?agent='.$agent.'&timestamp='.$timestamp.'&param='.$params.'&key='.$key;
            return $url;
        }

        $url = $jdata['host1'].'?agent='.$agent.'&timestamp='.$timestamp.'&param='.$params.'&key='.$key;
        return $url;
    }
}