<?php
namespace App\Models;

use DB;

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

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

    public function getLastCreateAt()
    {
        $creatAt = $this->select('CreateAt')->orderBy('CreateAt','desc')->first();
        return $creatAt;
    }

    //数据插入
    public function addBettingInfo($orderid,$data)
    {
        return $this->updateOrCreate(['OrderID'=>$orderid],$data);
    }


    public function checkBetLog($orderId)
    {
        return $this->select('OrderID')->where('OrderID','=',$orderId)->first();
    }

    public function  getTotal()
    {
        $data['count'] = $this->count(); //总条数
        $data['totalMoney'] = number_format($this->sum('BetAmount'),2); //总投注额
        $data['Money'] = number_format($this->sum('Money'),2); //总派彩(输赢)
        $data['effectMoney'] = number_format($this->where('Status','!=','None')->where('Status','!=','Revoke')->sum('BetAmount'),2);   //总有效投注
        return $data;
    }


    //获取回水列表
    protected function getMoneyback($value = '', $type = 1, $limit = 10, $wheregame = '', $orwhere = '', $having = '')
    {
        DB::connection()->enableQueryLog();

        $data = $this->select('account_name','account_identity',"UserName as Accounts",DB::raw('sum("BetAmount") as betting_money'),DB::raw('sum("Money") as WinLoseAmount'),DB::raw('sum("BetAmount") as ValidAmount'),DB::raw('count("id") as bet_count'))->where('Status','!=','None')->where('Status','!=','Revoke')->groupBy('UserName','account_name','account_identity');
        if (!empty($having)) {
            foreach ($having as $v) {
                $data = $data->havingRaw($v);
            }
        }

        if (empty($value) || is_array($value)) {
            $where = $value;
        } else {
            $where[] = array( 'id', $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;
    }
}
