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

namespace App\Models;
use DB;

class Bbingame_betting_ogrbv extends BaseModel
{
    protected $table = "bbingame_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='')
    {
        return $data = $this->where('VendorId', $where)->first();
    }

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

    public function obInsertData($data)
    {
        $data['AddTime'] = strtotime(str_replace('/','-', $data['AddTime']));
        $res = $this->insert($data);
        if(!$res)
        {
            return -3020011322;
        }
    }

    //获取回水列表
	protected function getMoneyback($value = '', $type = 1, $limit = 10, $wheregame = '', $orwhere = '', $having = '')
    {
        DB::connection()->enableQueryLog();
        $key = $this->getFeild($type);
        $data = $this->select('account_name','account_identity',"Accounts",DB::raw('sum("AllBet") as betting_money'),DB::raw('sum("Profit") as WinLoseAmount'),DB::raw('sum("CellScore") as ValidAmount'),DB::raw('count("GameID") as bet_count'))->where('type',1)->groupBy('Accounts','account_name','account_identity');
        if (!empty($having)) {
            foreach ($having as $v) {
                $data = $data->havingRaw($v);
            }
        }
        if (!empty($wheregame)) {
            $data->whereIn('UserName', $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' => 'GameID',
            '3' => 'WagersID',
            '4' => 'Accounts',
            '5' => 'GameStartTime',
            '6' => 'RoundNo',
            '7' => 'GameType',
            '8' => 'GameTypes',
            '9' => 'gamekind',
            '10' => 'WagerDetail',
            '11' => 'TableID',
            '12' => 'ExchangeRate',
            '13' => 'ResultType',
            '14' => 'Result',
            '15' => 'Card',
            '16' => 'AllBet',
            '17' => 'Profit',
            '18' => 'CellScore',
            '19' => 'Revenue',
            '20' => 'type',
            '21' => 'OrderDate',
            '22' => 'account_identity',
            '23' => 'account_name',
        );
        return $data[$num];
    }

    //反水更改狀
    public 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('BettingAmount');
            //查询总派彩金额
            $wa = $this->where($where)->sum('WinLoseAmount');
            //查询总有效投注金额
            $va = $this->where($where)->sum('ValidAmount');
            //查询总条数
            $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['co'] = $co;

        return $arr;

    }
}
