<?php
/**
*------Create thems Model------
*------SCWPHP  Version 1.0.0------
*------Dev Model Jions------
*------Create Time 2017-06-13 18:45:24------
*/
namespace App\Commons\Model;

use \System\Model;

class Money_buy_match extends Model {

    protected $table = 'money_buy_match';

    /**
     * 投注记录
     *
     * @access public
     * @param mixed $select 查询字段
     * @param mixed $where 查询条件
     * @param mixed $orWhere 关联关系      [[['batch_id', '191'], ['batch_id', '186']],[['id', '55'], ['id', '59']]];
     * @param mixed $begin 起始查询位置
     * @param mixed $pageSize 分页大小
     * @param mixed $orderBy 排序字段
     * @return array JsonString
     */
    public function moneyBuyMatch($select, $where = '', $orWhere = '', $begin = '', $pageSize = '', $orderBy = ['batch_id' => 'desc']) {
        // 查询字段
        $result = $this -> select($select)
        ->join('st_odds_code', function ($join) {
            $join->on('money_buy_match.odds_code', '=','st_odds_code.odds_code')->on('money_buy_match.game_code', '=','st_odds_code.game_code');
        });
        // 查询条件
        if (!empty($where)) {
            $result = $result -> where($where);
        }
        // 循环获取or查询
        if (!empty($orWhere)) {
            foreach ($orWhere as $value) {
                $result = $result -> where(function($query) use ($value) {
                    foreach ($value as $k => $v) {
                        if ($k == 0) {
                            $query = $query -> where([$v[0] => $v[1]]);
                        } else {
                            $query = $query -> orWhere([$v[0] => $v[1]]);
                        }
                    }
                });
            }
        }
        // 查询起始
        if (strlen($begin)) {
            $result = $result -> offset($begin);
        }

        // 分页大小
        if (strlen($pageSize)) {
            $result = $result -> limit($pageSize);
        }
        // 循环排序规则
        foreach ($orderBy as $key => $value) {
            $result = $result -> orderBy($key, $value);
        }
        // 获取数据
        $result = $result -> get();

        return $result;
    }

    /**
     * 投注记录
     *
     * @access public
     * @param mixed $select 查询字段
     * @param mixed $where 查询条件
     * @param mixed $orWhere 关联关系      [[['batch_id', '191'], ['batch_id', '186']],[['id', '55'], ['id', '59']]];
     * @param mixed $begin 起始查询位置
     * @param mixed $pageSize 分页大小
     * @param mixed $orderBy 排序字段
     * @return array JsonString
     */
    public function moneyBuyMatchAll($select, $where = '', $orWhere = '', $begin = '', $pageSize = '', $orderBy = ['batch_id' => 'desc']) {
        // 查询字段
        $result = $this -> select($select);
        // 查询条件
        if (!empty($where)) {
            $result = $result -> where($where);
        }
        // 循环获取or查询
        if (!empty($orWhere)) {
            foreach ($orWhere as $value) {
                $result = $result -> where(function($query) use ($value) {
                    foreach ($value as $k => $v) {
                        if ($k == 0) {
                            $query = $query -> where([$v[0] => $v[1]]);
                        } else {
                            $query = $query -> orWhere([$v[0] => $v[1]]);
                        }
                    }
                });
            }
        }
        // 查询起始
        if (strlen($begin)) {
            $result = $result -> offset($begin);
        }

        // 分页大小
        if (strlen($pageSize)) {
            $result = $result -> limit($pageSize);
        }
        // 循环排序规则
        foreach ($orderBy as $key => $value) {
            $result = $result -> orderBy($key, $value);
        }
        // 获取数据
        $result = $result -> get();

        return $result;
    }

    /**
     * 投注记录
     *
     * @access public
     * @param mixed $where 查询条件
     * @param mixed $orWhere 关联关系      [[['batch_id', '191'], ['batch_id', '186']],[['id', '55'], ['id', '59']]];
     * @return array JsonString
     */
    public function moneyBuyMatchTotal($where = '', $orWhere = '') {
        // 查询字段
        $result = $this;
        // 查询条件
        if (!empty($where)) {
            $result = $result -> where($where);
        }
        // 循环获取or查询
        if (!empty($orWhere)) {
            foreach ($orWhere as $value) {
                $result = $result -> where(function($query) use ($value) {
                    foreach ($value as $k => $v) {
                        if ($k == 0) {
                            $query = $query -> where([$v[0] => $v[1]]);
                        } else {
                            $query = $query -> orWhere([$v[0] => $v[1]]);
                        }
                    }
                });
            }
        }
        // 获取数据
        $result = $result -> count();

        return $result;
    }

    public function  getByDetail($Detailid){
            $ret = $this->where('money_buy_detail_id' ,$Detailid)->find();
            return $ret;
    }

    public  function  getByTypeMatch($type,$matchId){
          $rets = $this->where(['game_code'=>$type,'match_id'=>$matchId])->find();
          return $rets;
    }

    //查找某个订单下是否还有未处理完的订单情况
    public function    FindByTypeOrderResult($type,$OrderId,$result=0){
            if (is_string($result)){
                $ret = $this->where(['game_code'=>$type,'order_id'=>$OrderId,'result'=>$result])->find();
            }else{
                $ret = $this->where(['game_code'=>$type,'order_id'=>$OrderId])->whereIn('result',$result)->find();
            }
            return $ret;
    }

    public function  getByBatchId($batch_id){
            $ret = $this->where('batch_id',$batch_id)->find();
            return $ret;
    }




}