<?php
namespace App\Models;
class Money_return extends BaseModel {
	protected $table = "money_return";
	public $timestamps = false;

	//获取回水列表
	function getReturnlist($value = '', $type = 1, $page = 10, $sort = 6, $ads = 'desc') {
		$key = $this->getFeild($type);
		$sort = is_integer($sort) ? $this->getFeild($sort) : $sort;
		$data = $this->select('id','info_identity','order_id','account_name','account_identity','money as return_money','total_money','status','sysetem_user','processing_time','ratio','reason');
		if (is_array($value)) {
			$data = $data->where($value);
		} else if(!empty($value)){
			$data = $data->where($key, $value);
		}
		$data = $data->orderby($sort, $ads)->paginate($page);

		if (!$data) {
			return -3020200102; //没有列表数据
		}
		return $data->toArray();
	}

	//获取回水订单详情
	function getOrderDetails($value, $type = 1, $jointable = '', $columnn = '', $columnw = '') {
		$key = $this->getFeild($type);
		if (empty($jointable)) {
			$data = $this->where($key, $value)->first();
		} elseif ($jointable == 'account_bank') {
			$data = $this->select('money_return.account_name as account', 'order_id as trade_id', 'bank_name', 'account_bank.account_name', 'bank_number', 'bank_address', 'processing_time as money_time', 'money', 'money_return.status', 'reason')->where($key, $value)->join($jointable, $this->table . '.' . $columnn, $jointable . '.' . $columnw)->first();
		} else {
			$data = $this->where($key, $value)->join($jointable, $this->table . '.' . $columnn, $jointable . '.' . $columnw)->first();
		}
		if (!$data) {
			return -30202000302; //没有订单信息
		}
		return $data->toArray();
	}

	//字段对应值
	private function getFeild($num) {
		$data = array(
			'1' => 'id',
			'2' => 'info_identity',
			'3' => 'order_id',
			'4' => 'account_name',
			'5' => 'account_identity',
			'6' => 'processing_time',
			'7' => 'sysetem_user',
			'8' => 'status',
		);
		return $data[$num];
	}
	//插入回水记录
    function returnMoney($data){
	    $res=$this->insert($data);
	    if(!$res){
	        return -5010262202;//反水失败
        }
        return 1;
    }
	
	//回水金额汇总统计
	function returnMoneyCount($where=''){
		$data = array();
		$data['all_money'] = $this->sum('money');
		$data['all_lossmoney'] = $this->sum('total_money');
		if(!empty($where)&&is_array($where)){
			$data['all_money'] = $this->where($where)->sum('money');
			$data['all_lossmoney'] = $this->where($where)->sum('total_money');
		}
		
		return $data;
	}
	//统计时间段内的回水任谁
	function countUser($timearea){
        $data = $this->select('account_name')->whereBetween('processing_time', $timearea)->groupBy('account_name')->get();
        if (!$data) {
            return 0;
        }
        $data = $data->toArray();
        return count($data);
    }

}