<?php
namespace App\Models;
use DB;

class Money_take extends BaseModel {
	protected $table = "money_take";
	public $timestamps = false;

	//获取提现列表
	function getTakelist($value = '', $type = 1, $limit = 10, $sort = 6, $ads = 'desc',$jointable,$grade='') {
		//DB::connection()->enableQueryLog();
		$key = $this->getFeild($type);
		$sort = is_integer($sort) ? 'money_take.'.$this->getFeild($sort) : 'money_take.'.$sort;
		$data = $this->orderby($sort, $ads);
		if (!empty($value)) {
			if (is_array($value)) {
				$data = $data->where($value);
			} else {
				$data = $data->where($key, $value);
			}
		}
		if (!empty($jointable) && ($jointable == 'account_detailed')) {
			$data->join('account_detailed', 'account_detailed.account_identity', 'money_take.account_identity');
			$data->where('grade',$grade);
		}
		$data = $data->paginate($limit);
		//$queries = DB::getQueryLog();
		//print_r($queries);
		if (!$data) {
			return -3020100102; //没有列表数据
		}
		return $data->toArray();
	}

	//获取提现订单详情
	function getOrderDetails($value, $type = 1, $jointable = '', $columnn = '', $columnw = '', $clumn = 1) {
		$key = $this->getFeild($type);
		if ($clumn == 1) {
			$data = $this->where($key, $value);
		} elseif ($clumn == 2) {
			$data = $this->select('account_name as account', 'order_id as trade_id', 'bank_info as bank_name', 'bank_user as account_name', 'bank_no as bank_number', 'bank_address', 'apply_time as money_time', 'money', 'status', 'reason')->where($key, $value);
		}
		$data = $data->first();
		if (!$data) {
			return -3020100302; //没有订单信息
		}
		return $data->toArray();
	}

	//获取提现记录条数
	function getCountnum($value = '', $type = 10) {
		$key = $this->getFeild($type);
		if (!empty($value) && is_array($value)) {
			$data = $this->where($value);
		} else if (!empty($value)) {
			$data = $this->where($key, $value);
		}
		$data = $data->count();
		return $data;
	}

	//获取提现统计
	function getTotal($where = '', $type = 1, $list = 10, $sort = 6, $ads = 'desc', $page = 1, $having = '') {
		$sort = is_integer($sort) ? $this->getFeild($sort) : $sort;
		$data = $this->select(DB::raw('account_name,money_take.account_identity,count("order_id") as betnum,sum("money") as betmoney,money_take.status,max("name") as name,max("apply_time") as end_time,min("apply_time") as star_time'))->join('account_detailed', 'money_take.account_identity', 'account_detailed.account_identity')->groupBy('account_name')->groupBy('money_take.account_identity')->groupBy('status');

		if (!empty($where) && is_array($where) && !empty($having) && is_array($having)) {
			$data = $data->where($where);
			foreach ($having as $v) {
				$data = $data->havingRaw($v);
			}
		} elseif (!empty($having) && is_array($having)) {
			foreach ($having as $v) {
				$data = $data->havingRaw($v);
			}
		} elseif (!empty($where) && is_array($where)) {
			$data = $data->where($where);
		}

		$data = $data->orderby('star_time', 'desc')->paginate($list);
		if (!$data) {
			return -3020100402; //没有订单统计信息
		} else {
			$data = $data->toArray();
			$status = trans('status.money_take.status');
			foreach ($data['data'] as $k => $v) {
				if ($v['status'] == 1 || $v['status'] == 2) {
					$data['data'][$k]['status'] = $status[$v['status']];
				} else {
					$data['data'][$k]['status'] = $status[3];
				}
				$data['data'][$k]['statuss'] = $v['status'];
			}
			return $data;
		}
	}

	//获取提现统计详情
	function getTotalDetails($value, $type = 1, $list = 10, $page = 1, $sort = 6, $ads = 'desc') {
		$key = $this->getFeild($type);
		$sort = is_integer($sort) ? $this->getFeild($sort) : $sort;

		$data = $this->orderby($sort, $ads);
		if (!empty($value) && is_array($value)) {
			//提现统计详情，条件会员、状态，关联用户详情表
			$data = $data->select('account_name as account', 'order_id as trade_id', 'bank_info as bank_name', 'bank_user as account_name', 'bank_no as bank_number', 'bank_address', 'apply_time as money_time', 'money', 'money_cash', 'money_cash', 'money_take.status', 'reason', 'name', 'money_cash as old_money')->join('account_detailed', 'money_take.account_identity', 'account_detailed.account_identity')->join('money_details', 'money_take.order_id', 'money_details.trade_id')->where($value);
		} else {
			$data = $data->where($key, $value);
		}
		$data = $data->paginate($list);

		if (!$data) {
			return -3020100502; //没有订单信息
		}

		$data = $data->toArray();

		return $data;
	}

	//字段对应值
	private function getFeild($num) {
		$data = array(
			'1' => 'id',
			'2' => 'info_identity',
			'3' => 'order_id',
			'4' => 'account_name',
			'5' => 'account_identity',
			'6' => 'apply_time',
			'7' => 'pass_time',
			'8' => 'apply_date',
			'9' => 'sysetem_user',
			'10' => 'status',
		);
		return $data[$num];
	}
	//总提现数
	function getCount($account_id) {
		$count = $this->where('account_identity', $account_id)->where('status',1)->count();
		return $count;
	}
	//今日提现总数
	function gettoDayCount($account_id) {
		$today = date('Y-m-d', time());
		$count = $this->where('account_identity', $account_id)->where('apply_date', $today)->where('status', 1)->count();
		return $count;
	}
	//修改更新数据
	function updateInfo($data, $oid) {
		$res = $this->where('order_id', $oid)->update($data);
		if (!$res) {
			return -3020102202;
		}
		return 1;
	}

	//提现金额汇总统计
	function takeMoneyCount($where = '') {
		$data = array();
		$data['all_money'] = $this->where('status', '<>', 2)->sum('money');
		$data['success_money'] = $this->where('status', 1)->sum('money');
		// $data['fail_money'] = $this->where('status',2)->sum('money');
		$data['service_money'] = 0;
		$data['handing'] = $this->where('status', '0')->sum('money');
		if (!empty($where) && is_array($where)) {
			$data['all_money'] = $this->where($where)->where('status', '<>', 2)->sum('money');
			$data['success_money'] = $this->where($where)->where('status', 1)->sum('money');
			// $data['fail_money'] = $this->where($where)->where('status',2)->sum('money');
			$data['handing'] = $this->where($where)->where('status', '0')->sum('money');
		}

		return $data;
	}

	//最后一个提款
	function getLast($account_id) {
		$time = $this->where('account_identity', $account_id)->where('status', 1)->first();
		if (!$time) {
			return -3020102302;
		}
		return $time->toArray();
	}
	//一段时间内的提现人数
	function countUser($timearea){
	    $data=$this->select('account_name')->whereBetween('apply_time', $timearea)->groupBy('account_name')->get();
	    if(!$data){
	        return 0;
        }
        $data=$data->toArray();
	    return count($data);
    }
    //检测订单是否已经操作
    function checkInfo($order_id){
	    $data=$this->where('order_id',$order_id)->where('status',0)->first();
	    if(!$data){
	        return -5051272522;
        }
        return 1;
    }
    //获取历史成功提现
    protected function getTakeHistory($limit){
        $data=$this->where('status',1)->paginate($limit);
        return $data->toArray();
    }
}