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

use \System\Model;

class Money_record extends Model {

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

    /**
     * 盈亏记录
     *
     * @access public
     * @param mixed $moneyRecordSelect 查询参数
     * @param mixed $moneyRecordWhere 查询条件
     * @param mixed $moneyRecordBetween 查询条件区间
     * @param mixed $begin 起始查询位置
     * @param mixed $pageSize 分页大小
     * @param mixed $orderBy 排序字段
     * @return array JsonString
     */
    public function moneyRecord($moneyRecordSelect, $moneyRecordWhere, $moneyRecordBetween, $begin, $pageSize, $orderBy = 'money_time') {
        $result = $this
            -> select($moneyRecordSelect)
            -> where($moneyRecordWhere)
            -> whereBetween('money_time', $moneyRecordBetween)
            -> offset($begin)
            -> limit($pageSize)
            -> orderBy($orderBy)
            -> get();

        return $result;
    }

    /**
     * 盈亏记录总数
     *
     * @access public
     * @param mixed $moneyRecordWhere 查询条件
     * @param mixed $moneyRecordBetween 查询条件区间
     * @return array JsonString
     */
    public function moneyRecordTotal($moneyRecordWhere, $moneyRecordBetween) {
        $result = $this
            -> where($moneyRecordWhere)
            -> whereBetween('money_time', $moneyRecordBetween)
            -> count();

        return $result;
    }

    /**
     * 盈亏记录
     *
     * @access public
     * @param mixed $moneyRecordSelect 查询参数
     * @param mixed $moneyRecordWhere 查询条件
     * @param mixed $moneyRecordBetween 查询条件区间
     * @param mixed $orWhere or查询
     * @return array JsonString
     */
    public function moneyRecordAll($moneyRecordSelect, $moneyRecordWhere, $moneyRecordBetween, $orWhere=[]) {
        $result = $this
            -> select($moneyRecordSelect)
            -> where($moneyRecordWhere);
        if ($moneyRecordBetween) {
            $result = $result-> whereBetween('money_time', $moneyRecordBetween);
        }
        if ($orWhere) {
            // 循环获取or查询.
            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->get()->toArray();

        return $result;
    }
}
