<?php
/**
*------Create thems Model------
*------SCWPHP  Version 1.0.0------
*------Dev Model Jions------
*------Create Time 2019-04-15 16:05:26------
*/
namespace App\Commons\Model;

use \System\Model;

class Money_buy_str extends Model {

    // 对象表
    protected $table = 'money_buy_str';
    public $timestamps = false;

    /**
     * 投注记录
     *
     * @access public
     * @param mixed $select 查询字段
     * @param mixed $where 查询条件
     * @param mixed $between 查询条件区间     ['money_time' => [$startTime, $endTime], ...]
     * @param mixed $begin 起始查询位置
     * @param mixed $pageSize 分页大小
     * @param mixed $toArray 是否专为数组
     * @param mixed $orderBy 排序字段
     * @param mixed $join 关联关系      ['article' => 'oney_buy.info_identity = article.identity', ...]
     * @return array JsonString
     */
    public function moneyBuyStr($select, $where = '', $between = '', $begin = '', $pageSize = '', $toArray = 0, $orderBy = ['money_time' => 'desc'], $join = '') {
        // 查询字段
        $result = $this -> select($select);
        // 循环关联
        if (!empty($join)) {
            foreach ($join as $key => $value) {
                $result = $result -> join($key, $value);
            }
        }
        // 查询条件
        if (!empty($where)) {
            $result = $result -> where($where);
        }
        // 循环获取查询区间
        if (!empty($between)) {
            foreach ($between as $key => $value) {
                $result = $result -> whereBetween($key, $value);
            }
        }
        // 查询起始
        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();
        if ($toArray) {
            $result = $result -> toArray();
        }

        return $result;
    }

    /**
     * 投注记录总数
     *
     * @access public
     * @param mixed $where 查询条件
     * @param mixed $between 查询条件区间
     * @return array JsonString
     */
    public function moneyBuyStrTotal($where, $between) {
        $result = $this;
        // 查询条件
        if (!empty($where)) {
            $result = $result -> where($where);
        }
        // 循环获取查询区间
        if (!empty($between)) {
            foreach ($between as $key => $value) {
                $result = $result -> whereBetween($key, $value);
            }
        }
        // 获取数据
        $result = $result -> count();

        return $result;
    }

    /**
     * 投注记录联合查询
     *
     * @access public
     * @param mixed $select 查询字段
     * @param mixed $moneyBuyWhere 查询条件
     * @param mixed $orderBy 排序字段
     * @return array JsonString
     */
    public function moneyBuyStrUnion($select, $moneyBuyWhere, $orderBy = 'money_time') {
        if (empty($moneyBuyWhere)) {
            return;
        }
        $count = count($moneyBuyWhere);
        $result = [];
        $unionAll = [];
        foreach ($moneyBuyWhere as $key => $value) {
            if ($key === 0) {
                $unionAll[0] = $this
                    -> select($select)
                    -> join('st_bet_str','money_buy_str.bet_str_id','=','st_bet_str.id')
                    -> where(['order_id' => $value]);
            } elseif ($key + 1 === $count) {
                $result = $this
                    -> select($select)
                    -> join('st_bet_str','money_buy_str.bet_str_id','=','st_bet_str.id')
                    -> where(['order_id' => $value])
                    -> unionAll($unionAll[$key - 1])
                    -> orderBy($orderBy)
                    -> get();
            } else {
                $unionAll[$key] = $this
                    -> select($select)
                    -> join('st_bet_str','money_buy_str.bet_str_id','=','st_bet_str.id')
                    -> unionAll($unionAll[$key - 1])
                    -> where(['order_id' => $value]);
            }
        }

        return $result;
    }

    /**
     *
     * @access public
     * @param mixed $batch_id 批次ID
     * @return array JsonString
     *
     * */
     public  function   getByBatchId($batch_id){
         $result = $this->where(['batch_id',$batch_id])->get();
         return $result ;
     }
}
