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

namespace App\Models;
use DB;

class Ogsports_betting_ogrbv extends BaseModel
{
    protected $table = "ogsports_betting_ogrbv";
    public $timestamps = false;
    protected $guarded = [];

    /**
     * 获取所有游戏记录
     * @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('bettingcode', $where)->first();
    }

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

        return $res;
    }

    public function obInsertData($data)
    {   
        $bettingdate = preg_match('/\d+/', $data['bettingdate'],$bettingda);
        $settledate = preg_match('/\d+/', $data['settledate'],$settleda);
        $data['bettingdate'] = $bettingda[0];
        $data['settledate'] = $settleda[0];
        $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','membername',DB::raw('sum("bettingamount") as betting_money'),DB::raw('sum("validbet") as ValidAmount'),DB::raw('sum("winloseamount") as WinLoseAmount'),DB::raw('count("bettingcode") as bet_count'))->where('type',1)->groupBy('membername','account_name','account_identity');
        if (!empty($having)) {
            foreach ($having as $v) {
                $data = $data->havingRaw($v);
            }
        }
        if (!empty($wheregame)) {
            $data->whereIn('membername', $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('status', '!=', '');
        $data = $data->where($where);
        $data = $data->paginate($limit);
        if (!$data) {
            return -5030001202; //没有列表数据
        }
        return $data->toArray();
    }

    //字段对应值
    private function getFeild($num)
    {
        $data = array(
            '1' => 'id',
            '2' => 'gameprovider',
            '3' => 'membername',
            '4' => 'gamename',
            '5' => 'bettingcode',
            '6' => 'bettingdate',
            '7' => 'gameid',
            '8' => 'roundno',
            '9' => 'result',
            '10' => 'bet',
            '11' => 'winloseresult',
            '12' => 'bettingamount',
            '13' => 'validbet',
            '14' => 'winloseamount',
            '15' => 'balance',
            '16' => 'currency',
            '17' => 'handicap',
            '18' => 'status',
            '19' => 'gamecategory',
            '20' => 'settledate',
            '21' => 'remark',
            '22' => 'type',
            '23' => 'account_identity',
            '24' => 'account_name',
            '25' => '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)
        {
            //查询总有效投注金额
            $va = $this->where($where)->sum('bettingamount');
            $va = $this->where($where)->sum('validbet');
            $va = $this->where($where)->sum('winloseamount');
            //查询总条数
            $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['validbet'] = ceil($va) == $va ? $va.'.00' : round($va ,2);
        // $arr['revenue'] = ceil($va) == $va ? $va.'.00' : round($va ,2);
        $arr['co'] = $co;

        return $arr;

    }
}