<?php
namespace Biz\Money;
/**
 * 资金操作相关类
 */
class Money {

	/**
	 * 资金明细表
	 * @var string
	 */
	private $money_table = 'fund_detailed';
	/**
	 * 用户详情表
	 * @var string
	 */
	private $account_detailed = 'account_detailed';
	/**
	 * 用户银行绑定记录表
	 * @var string
	 */
	private $bank_table = 'account_bank';
	/**
	 * 用户表
	 * @var string
	 */
	private $account_table = 'account';
	/**
	 * 交易密码表
	 * @var string
	 */
	private $pay_password = 'pay_password';
	/**
	 * 资金明细表
	 * @var string
	 */
	private $money_details = 'money_details';
	/**
	 * 资金记录修改
	 * @param
	 * string $account_identity 用户id
	 * @param  string $money            金额
	 * @param  订单号 $order_id         订单号
	 * @param  int    $type             交易类型
	 * @return [type]                   [description]
	 */
	public function insertMoney($account_identity, $money, $order_id, $type, $game_no = '',$UUID,$account=array())
	{
		//_beginTransaction();
		//获取用户信息
		if(empty($account)){
			return '-4020';
		}
		//查询余额
		$getAccount = $this->getAccount($account_identity);
		//验证流水金额与余额是否正常
		$checkWater = $this->checkWater($account_identity,$getAccount['available_cash']);
		if($checkWater != 1){
			return $checkWater;
		}
		//获取money_type
		$m_type = $this->getMtype($type);
		//判断加减金额
		if ($m_type == 1) {
			$nowMoney = $getAccount['available_cash'] + $money;
            $nowCash = $getAccount['cash'] + $money;
		} else {
			$nowMoney = $getAccount['available_cash'] - $money;
            $nowCash = $getAccount['cash'] - $money;
		}
		//更新用户余额
		$updateAccountMoney = $this->updateAccountMoney($account_identity,$nowMoney, $nowCash);
		if($updateAccountMoney != 1){
			return $updateAccountMoney;
		}else {
			//插入资金详情
			$insertDetail = $this->insertDetail($account_identity, $order_id, $money, $nowMoney, $type,$UUID,$account);
			if($insertDetail !=1){
				return $insertDetail;
			}
		}
		return '1';
	}

	/**
	 * 更新用户金额
	 * @param  [type] $account_identity 用户唯一ID
	 * @param  [type] $nowMoney  操作后可提现金额
	 * @param  [type] $nowCash  操作后总金额
	 */
	public function updateAccountMoney($account_identity, $nowMoney,$nowCash)
	{
		if(empty($account_identity)||empty($nowMoney)||empty($nowCash)){
			return '-2255';
		}
		//更新用户余额
		$res = lm($this->account_detailed, 'Commons')
			->where('account_identity',$account_identity)
		    ->update([
				'cash'=> $nowCash,
				'available_cash' => $nowMoney
			]);
		if(!$res){
			return '-7024';
		}
		return '1';
	}

	/**
	 * 插入资金详情
	 * @param  [type] $account_identity    用户唯一id
	 * @param  [type] $order_id   交易id
	 * @param  [type] $money      交易金额
	 * @param  [type] $nowMoney   剩余金额
	 * @param  [type] $type       交易类型的数字表示
	 * @return [type]             [description]
	 */
	public function insertDetail($account_identity, $order_id, $money, $nowMoney, $type,$UUID,$account)
	{
		//获取用户信息
		if(empty($account)){
			return '-4020';
		}
		//获取money_type
		$m_type = $this->getMtype($type);
        $trade_desc = $this->getDesc($account['account'], $money, $order_id, $type);
		$data2 = array(
			'info_identity' => $UUID,
			'trade_id' => $order_id,
			'account_name' => $account['account'],
			'account_identity' => $account_identity,
			'money' => $money,
			'money_time' => date('Y-m-d H:i:s', time()),
			'money_type' => $m_type,
			'money_cash' => $nowMoney,
			//'status' => $status,
			'trade_type' => $type,
			'trade_desc' => $trade_desc,
		);
		$res3 = lm($this->money_details,'commons')->insert($data2);
		if ($res3) {
			return '1';
		}
		return '-7025';
	}

	/**
	 * 获取用户余额信息
	 * @param  [type] $account_identity 用户唯一id
	 * @return [type]                   用户资金相关数组
	 */
	public function getAccount($account_identity) {
		$res = lm($this->account_detailed, 'Commons')
			->select('available_cash', 'cash', 'frozen_cash', 'account_identity')
			->where('account_identity', $account_identity)
			->first();
		if(!$res){
			Render('', '2115',lang('Errors','Api')->get('error-2115'));
		}
		$res->toarray();
		return $res;
	}

	/**
	 * 验证账户金额是否足够
	 * @param  [type] $money          操作所需金额
	 * @param  [type] $available_cash 账户可用金额
	 * @return [type]                 [description]
	 */
	public function verifyMoney($money, $available_cash) {
		if ($available_cash > $money) {
			return true;
		} else {
			return false;
		}
	}

	/**
	 * 检测是否设置交易密码
	 * @param [type] $account_identity 用户id
	 */
	public function checkPayPwd($account_identity='')
	{
		//$account_identity = isset($_SESSION['uinfo']['account_identity']) ? $_SESSION['uinfo']['account_identity'] : '';

		if (empty($account_identity)) {
			Render('', '-2001', lang()->get('user not login'));
		}
		//检查用户是否已经设置密码
		$judge = lm($this->pay_password, 'commons')->where(['account_identity' => $account_identity])->first();
		if ($judge) {
			return '1';
		} else {
			return '-1024';
		}
	}

	/**
	 * 检测资金流水情况
	 * @param $account_identity
	 * @param $cash
	 */
	public function checkWater($account_identity, $NowCash)
	{
		//查询最后一条资金余额
		$last_data = lm($this->money_details, 'commons')->where('account_identity', $account_identity)->orderBy('id', 'desc')->first();
		if (empty($last_data)) {
			return -500147;
		}
		$last_data = $last_data->toArray();
		if ($last_data['money_cash'] != $NowCash) {
			return -2255;
		}
		return 1;
	}

	/**
	 * 验证交易密码
	 * @param  [type] $account_identity 用户唯一id
	 * @param  [type] $pwd              密码
	 * @return [type]                   true or false
	 */
	public function verifyPayPwd($account_identity,$pwd) {
		$enc = lm($this->pay_password,'commons')->select('encryption')->where('account_identity', $account_identity)->first()->toArray();
		$VerPwd = md5(md5($enc["encryption"] . $pwd));
		$res = lm($this->pay_password,'commons')->where(['account_identity' => $account_identity, 'pay_password' => $VerPwd])->first();
		if ($res) {
			return true;
		} else {
			return false;
		}
	}
	/**
	 * 反水
	 * @param  string $account_identity 用户唯一ID
	 * @param  string  $account 用户名
	 * @param  string  $bet_money 投注总金额
	 */
	public function fanshui($account_identity,$account,$bet_money,$order_id){

		//查询是否可以反水
		$account_info = lm('Account','Api')->where([['identity',$account_identity],['account',$account]])->first();
		if(!$account_info){
			Render('', '51030',lang('Errors','Api')->get('error-51030'));
		}
		//查询反水比例
		$Setinfo = lm('SetInfo','Api')->where([['infoname','fanshui'],['status',1]])->first();
		if(empty($Setinfo)){
			Render('', '51034',lang('Errors','Api')->get('error-51034'));
		}
		$account_info->toArray();
		$Setinfo->toArray();
		if($account_info['fanshui'] == 1){
			//执行插入金额详情与用户余额加反水
//			$order_id = OrderID(); //订单ID
			$UUID = UUID(); //信息ID
			$type = 7; //反水
			$bet_money  = $bet_money * $Setinfo['infocontent']; //计算反水额
			$res = $this->insertMoney($account_identity, $bet_money, $order_id, $type, $game_no = '',$UUID,$account_info);
			if($res != 1){
				Render('', $res,lang('Errors','Api')->get('error'.$res));
			}
		}
	}


	/**
	 * 获取描述信息
	 * @param  string $account_name 用户名
	 * @param  string $money        金额
	 * @param  string $order_id     订单号
	 * @param  int 	$type         操作类型
	 * @return [type]               [description]
	 */
	public function getDesc($account_name, $money, $order_id, $type)
	{
		switch ($type) {
			case 1:
				return $account_name . '投注' . $money . '元。' . '订单号' . $order_id;
				break;
			case 2:
				return $account_name . '追号' . $money . '元。' . '订单号' . $order_id;
				break;
			case 3:
				return $account_name . '撤单' . '订单号' . $order_id;
				break;
			case 4:
				return $account_name . '中奖' . $money . '元。' . '订单号' . $order_id;
				break;
			case 5:
				return $account_name . '申请提现' . $money . '元。' . '订单号' . $order_id;
				break;
			case 6:
				return $account_name . '充值' . $money . '元。' . '订单号' . $order_id;
				break;
			case 7:
				return $account_name . '反水' . $money . '元。' . '订单号' . $order_id;
				break;
			case 8:
				return $account_name . '回水' . $money . '元。' . '订单号' . $order_id;
				break;
			case 9:
				return $account_name . '管理员扣款' . $money . '元。' . '订单号' . $order_id;
				break;
			case 10:
				return $account_name . '佣金提成' . $money . '元。' . '订单号' . $order_id;
				break;
			default:
				return '';
				break;
		}
	}
	/**
	 * 返回money_type
	 * @param  [type] $type 类型
	 * @return [type]       [description]
	 */
	public function getMtype($type)
	{
		$arr = array(
			'1' => 2,
			'2' => 2,
			'3' => 1,
			'4' => 1,
			'5' => 2,
			'6' => 1,
			'7' => 1,
			'8' => 1,
			'9' => 2,
			'10' => 1,
			'11' => 2,
			'15' => 2,
		);
		return $arr[$type];
	}
}
?>