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

namespace App\Models;
use DB;

class Aggame_betting_ogrbv extends BaseModel
{
    protected $table = "aggame_betting_ogrbv";
    public $timestamps = false;

    /**
     * 获取所有游戏记录
     * @param string $where
     * @param int $limit
     * @return mixed
     */
    public function getlist($where='',$limit=15)
    {
        $data = $this->orderBy('id','desc');

        if(is_array($where)&&count($where)>0){
            $data = $data->where($where);
        }

        $data=$data->paginate($limit);

        return $data->toArray();
    }

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

        if (empty($data)&&count($data)<=0) {
            return -20000;
        }else{
            return $data->toArray();
        }
    }

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

        if (empty($data)&&count($data)<=0) {
            return -20000;
        }else{
            return $data->toArray();
        }
    }

    /**
     * 获取最新一条数据
     */
    public function getLastData()
    {
        $res = $this->orderBy('tradeNo','desc')->limit(1)->first()->toArray();

        return $res;
    }

    public function agInsertData($data)
    {

        if(isset($data['betTime']) && isset($data['recalcuTime']))
        {
            $bettime = str_replace('/','-', $data['betTime']);
            $recalcuTime = strtotime(str_replace('/','-', $data['recalcuTime']));

            $data['betTime'] = strtotime("$bettime + 12hour");
            $data['recalcuTime'] = strtotime("$recalcuTime + 12hour");
            $data['recalcurawTime'] = $recalcuTime; //美东时间
            $data['rawTime'] = strtotime($bettime); //美东时间
        }
        else if(isset($data['SceneStartTime']) && isset($data['SceneEndTime']))
        {
            $bettime = str_replace('/','-', $data['SceneStartTime']);
            $recalcuTime = strtotime(str_replace('/','-', $data['SceneEndTime']));

            $data['betAmount'] = $data['Cost'];
            $data['validBetAmount'] = $data['Cost'];
            $data['netAmount'] = $data['transferAmount'];
            $data['netAmountBase'] = $data['Earn'];
            $data['netAmountBonus'] = $data['Jackpotcomm'];
            $data['gameCode'] = $data['Roomid'].'-'.$data['Roombet'];
            $data['beforeCredit'] = $data['previousAmount'];
            $data['loginIP'] = $data['IP'];
            $data['gameType'] = 'HUNTER';
            unset($data['SceneStartTime']);
            unset($data['SceneEndTime']);
            unset($data['ID']);
            unset($data['creationTime']);
            unset($data['Cost']);
            unset($data['Earn']);
            unset($data['Jackpotcomm']);
            unset($data['type']);
            unset($data['sceneId']);
            unset($data['Roomid']);
            unset($data['Roombet']);
            unset($data['exchangeRate']);
            unset($data['previousAmount']);
            unset($data['transferAmount']);
            unset($data['currentAmount']);
            unset($data['IP']);
            $data['betTime'] = strtotime("$bettime + 12hour");
            $data['recalcuTime'] = strtotime("$recalcuTime + 12hour");
            $data['recalcurawTime'] = $recalcuTime; //美东时间
            $data['rawTime'] = strtotime($bettime); //美东时间
        }

        $res = $this->insert($data);
        if(!$res){
            return -3020011322;
        }
        return $res;
    }

    //获取回水列表
    protected function getMoneyback($value = '', $type = 1, $limit = 10, $wheregame = '', $orwhere = '', $having = '')
    {
        DB::connection()->enableQueryLog();
        $key = $this->getFeild($type);
        $data = $this->join ('account_detailed', 'aggame_betting_ogrbv.account_identity', '=', 'account_detailed.account_identity')->select('account_name','group_name','group_code','aggame_betting_ogrbv.account_identity','playerName',DB::raw('sum("betAmount") as betting_money'),DB::raw('sum("validBetAmount") as ValidAmount'),DB::raw('sum("netAmount") as WinLoseAmount'),DB::raw('count("billNo") as bet_count'))->where('type',1)->groupBy('playerName','account_name','group_name','group_code','aggame_betting_ogrbv.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('water_status', '=', 1);
        $where[] = array('type', '=', 1);
        // $where[] = array('ResultType', '!=', 4);
        $data = $data->where($where);
        $data = $data->paginate($limit);
        if (!$data) {
            return -5030001202; //没有列表数据
        }
        return $data->toArray();
    }

    //字段对应值
    private function getFeild($num)
    {
        $data = array(
            '1' => 'id',
            '2' => 'dataType',
            '3' => 'billNo',
            '4' => 'playerName',
            '5' => 'agentCode',
            '6' => 'gameCode',
            '7' => 'netAmount',
            '8' => 'betTime',
            '9' => 'gameType',
            '10' => 'betAmount',
            '11' => 'validBetAmount',
            '12' => 'flag',
            '13' => 'playType',
            '14' => 'currency',
            '15' => 'tableCode',
            '16' => 'loginIP',
            '17' => 'recalcuTime',
            '18' => 'platformType',
            '19' => 'remark',
            '20' => 'round',
            '21' => 'result',
            '22' => 'beforeCredit',
            '23' => 'deviceType',
            '24' => 'type',
            '25' => 'account_identity',
            '26' => 'account_name',
            '27' => 'count_status',
        );
        return $data[$num];
    }

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

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

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

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

    public 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('betAmount');
            //查询总派彩金额
            $wa = $this->where($where)->sum('netAmount');
            //查询总有效投注金额
            $va = $this->where($where)->sum('validBetAmount');
            // $va = $this->where($where)->sum('Revenue');
            //查询总条数
            $co = $this->where($where)->count();
        }

        $arr['betAmount'] = ceil($ba) == $ba ? $ba.'.00' : round($ba ,2);
        $arr['netAmount'] = ceil($wa) == $wa ? $wa.'.00' : round($wa ,2);
        $arr['validBetAmount'] = ceil($va) == $va ? $va.'.00' : round($va ,2);
        //$arr['revenue'] = ceil($va) == $va ? $va.'.00' : round($va ,2);
        $arr['co'] = $co;

        return $arr;

    }
}