<?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;
use Illuminate\Support\Facades\DB;
use DB as DD;

class Money_buy_simplex extends Model {

    // 对象表
    protected $table = 'money_buy_simplex';
    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 moneyBuySimplex($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 $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 winLoseRecord($where, $between, $begin = '', $pageSize = '') {
        $select = ['money_time', 'gain_money', 'money', 'order_id'];
        $mnyBuyStrMdl = lm('Money_buy_str', "commons");
        $mnyBuyStrSql = $mnyBuyStrMdl->select($select)->where($where);
        $mnyBuySpxSql = $this
            ->select($select)
            ->where($where)
            ->unionAll($mnyBuyStrSql)
            ->whereBetween('money_time', $between)
            ->offset($begin)
            ->limit($pageSize)
            ->orderBy('money_time','DESC')
            ->get();
        foreach ($mnyBuySpxSql as $key => $value) {
            if ($value->gain_money >= $value->money) {
                $mnyBuySpxSql[$key]->money_type = 1;
                $mnyBuySpxSql[$key]->money = $value->gain_money - $value->money;
            } else {
                $mnyBuySpxSql[$key]->money = $value->money - $value->gain_money;
            }
        }

        return $mnyBuySpxSql;
    }

    /**
     * 盈亏总数
     *
     * @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 winLoseCount($where, $between) {
        $select = ['order_id'];
        $mnyBuyStrMdl = lm('Money_buy_str', "commons");
        $mnyBuyStrSql = $mnyBuyStrMdl->select($select)->where($where);
        $mnyBuySpxSql = $this
            ->select($select)
            ->where($where)
            ->unionAll($mnyBuyStrSql)
            ->whereBetween('money_time', $between)
            ->get();

        return count($mnyBuySpxSql);
    }
}
