<?php
class shopuserpay extends netsDb {
	private $sysErrorClassID='0000';
	private $sysErrorFuncID=0;
	private $sysErrorID=0;
	
	public function shopuserpay($pConnection){
		$this->netsDb($pConnection);
	}
	public function Check(&$pTableDescriptAR,$pExceptionAR1,$pExceptionAR2){
		$this->sysErrorFuncID=$this->sysErrorClassID.'02';

		if(count($pExceptionAR1)==0){
			$pExceptionAR1=array();
		}
		 parent::Check($pTableDescriptAR,$pExceptionAR1,$pExceptionAR2);
		//
		$tmpSTR='shuptName';
		if(strlen($pTableDescriptAR[0]['Columns'][$tmpSTR]['Value'])<1 && !in_array($tmpSTR,$pExceptionAR2)){
			$this->sysLastError='提交内容错误:1.名称不能为空';
			return $this->sysErrorFuncID.'001';
		}
		
		return 0;
	}
	public function Order_Pay(&$pDescriptions,&$pURL){
		/*
生成订单
跳到支付接口
		*/
		$this->sysErrorFuncID=$this->sysErrorClassID.'02';
		$this->TransStart();
		//-- ------ #Add ------ --
		//#Get numCode
		$tmpSTR='shuptNumCode';
		$pDescriptions[0]['TableDescription']['SearchColumn']=$tmpSTR;
		$pDescriptions[0]['Columns'][$tmpSTR]['Value']=$this->sysFileConfig['NumCodeHead_UserOrder'].$this->sysSPLIT11.date('YmdHis');
		$this->numCode($pDescriptions[0]);
		//-- #Setting --
		$pDescriptions[0]['Columns']['shuptName']['Value']=$pDescriptions[0]['Columns']['shuptNumCode']['Value'];
		$pDescriptions[0]['Columns']['shuptCreateTime']['Value']=date($this->sysTimeFormat);
		$pDescriptions[0]['Columns']['shuptType']['Value']=0;
		$pDescriptions[0]['Columns']['shuptState_Pay']['Value']=0;
		$pDescriptions[0]['Columns']['shuptState_Get']['Value']=-1;
		//-- #Get user --
		$tmpSQL=' SELECT A.uitID,A.uitName
			FROM '.$this->sysTableHead.'netsuserinfo AS A
			INNER JOIN '.$this->sysTableHead.'netsuserinfocont AS B ON B.uictIndexID=A.uitID
			WHERE 1=1 AND A.uitName LIKE \'{Nets.Name}\'; ';
		$tmpSTR=str_replace('{Nets.Name}',$pDescriptions[1]['Columns']['shupctPayUsername']['Value'],$tmpSQL);
		$tmpData0=$this->Search($tmpSTR);
		if(count($tmpData0)==0){
			$this->TransRollback();
			$this->sysLastError='您填写的用户帐号并不存在，请确认后重新填写。';
			return $this->sysErrorFuncID.'002';
		}
		$pDescriptions[1]['Columns']['shupctPayUserid']['Value']=$tmpData0[0]['uitID'];
		$pDescriptions[1]['Columns']['shupctPayUsername']['Value']=$tmpData0[0]['uitName'];
		//-- #Add --
		$tmpINT=$this->Check($pDescriptions,array(),array());
		if($tmpINT!=0){
			$this->TransRollback();
			$this->sysLastError='充值单生成错误，请联系管理员。';
			return $tmpINT;
		}
		$pDescriptions[0]['TableDescription']['SearchColumn']='shuptNumCode,shuptID';
		$pDescriptions[1]['TableDescription']['SearchColumn']='shupctIndexID';
		$tmpINT=$this->add($pDescriptions);
		if($tmpINT!=0){
			$this->TransRollback();
			$this->sysLastError='充值单生成错误，请联系管理员。';
			return $tmpINT;
		}
		//-- ------ #Direct pay gateway ------ --
		$tmpSQL=' SELECT 
			B.shuptctWebpayRequestURL
			FROM '.$this->sysTableHead.'shopuserpaytype AS A
			INNER JOIN '.$this->sysTableHead.'shopuserpaytypecont AS B ON B.shuptctIndexID=A.shupttID
			WHERE 1=1 AND A.shupttID={Nets.ID} ';
		$tmpSTR=str_replace('{Nets.ID}',$pDescriptions[1]['Columns']['shupctPaytypeid']['Value'],$tmpSQL);
		$tmpData0=$this->Search($tmpSTR);
		if (count($tmpData0)==0) {
			$pURL=$this->sysFileConfig['PaySubmit_Default'];
		}else {
			$pURL=$tmpData0[0]['shuptctWebpayRequestURL'];
		}
		$pURL.='?NumCode='.$pDescriptions[0]['Columns']['shuptNumCode']['Value'];
		$this->sysLastMessage='用户充值单已提交成功！平台将为您跳转到支付网关，您本次充值的金额为：￥'.$pDescriptions[0]['Columns']['shuptTotalPrice']['Value'].'(人民币)。';
		
		$this->TransCommit();
		return 0;
	}
	public function Reorder_Pay(&$pDescriptions,&$pURL){
		/*
跳到支付接口
		*/
		$this->sysErrorFuncID=$this->sysErrorClassID.'02';
		$this->TransStart();
		//-- ------ #Get userpay ------ --
		//Update userpay
		$tmpSQL=' UPDATE '.$this->sysTableHead.'shopuserpaycont SET shupctPaytypeid={Nets.Typeid} WHERE 1=1 AND shupctIndexID={Nets.ID}; ';
		$tmpSTR=str_replace('{Nets.Typeid}',$pDescriptions[1]['Columns']['shupctPaytypeid']['Value'],$tmpSQL);
		$tmpSTR=str_replace('{Nets.ID}',$pDescriptions[0]['Columns']['shuptID']['Value'],$tmpSTR);
		$tmpINT=$this->Execute($tmpSTR);
		if($tmpINT!=0){
			$this->TransRollback();
			$this->sysLastError='更新用户充值单支付方式错误，请与管理员联系。';
			return $tmpINT;
		}
		//Get userpay
		$tmpSQL=' SELECT *
			FROM '.$this->sysTableHead.'shopuserpay AS A
			INNER JOIN '.$this->sysTableHead.'shopuserpaycont AS B ON B.shupctIndexID=A.shuptID
			WHERE 1=1 AND A.shuptID={Nets.ID}; ';
		$tmpSTR=str_replace('{Nets.ID}',$pDescriptions[0]['Columns']['shuptID']['Value'],$tmpSQL);
		$tmpData0=$this->Search($tmpSTR);
		if(count($tmpData0)==0){
			$this->TransRollback();
			$this->sysLastError='您提交的用户充值单并不存在，请确认后再提交。';
			return $this->sysErrorFuncID.'002';
		}
		$tmpINT=$this->getDescriptionFromData($pDescriptions,$tmpData0);
		//-- ------ #Direct pay gateway ------ --
		$tmpSQL=' SELECT 
			B.shuptctWebpayRequestURL
			FROM '.$this->sysTableHead.'shopuserpaytype AS A
			INNER JOIN '.$this->sysTableHead.'shopuserpaytypecont AS B ON B.shuptctIndexID=A.shupttID
			WHERE 1=1 AND A.shupttID={Nets.ID} ';
		$tmpSTR=str_replace('{Nets.ID}',$pDescriptions[1]['Columns']['shupctPaytypeid']['Value'],$tmpSQL);
		$tmpData0=$this->Search($tmpSTR);
		if (count($tmpData0)==0) {
			$pURL=$this->sysFileConfig['PaySubmit_Default'];
		}else {
			$pURL=$tmpData0[0]['shuptctWebpayRequestURL'];
		}
		$pURL.='?NumCode='.$pDescriptions[0]['Columns']['shuptNumCode']['Value'];
		$this->sysLastMessage='平台将为您跳转到支付网关，您本次充值的金额为：￥'.$pDescriptions[0]['Columns']['shuptTotalPrice']['Value'].'(人民币)。';

		$this->TransCommit();
		return 0;
	}
	public function cancelOrder_Pay(&$pDescriptions){
		/*
更新订单状态
		*/
		$this->sysErrorFuncID=$this->sysErrorClassID.'02';
		$this->TransStart();
		//-- ------ #Update order state ------ --
		//Get userpay
		$tmpSQL=' SELECT *
			FROM '.$this->sysTableHead.'shopuserpay AS A
			INNER JOIN '.$this->sysTableHead.'shopuserpaycont AS B ON B.shupctIndexID=A.shuptID
			WHERE 1=1 AND A.shuptNumCode=\'{Nets.NumCode}\'; ';
		$tmpSTR=str_replace('{Nets.NumCode}',$pDescriptions[0]['Columns']['shuptNumCode']['Value'],$tmpSQL);
		$tmpData0=$this->Search($tmpSTR);
		if(count($tmpData0)==0){
			$this->TransRollback();
			$this->sysLastError='您提交的用户充值单并不存在，请确认后再提交。';
			return $this->sysErrorFuncID.'002';
		}
		$tmpINT=$this->getDescriptionFromData($pDescriptions,$tmpData0);
		//Check state
		if($pDescriptions[0]['Columns']['shuptState_Pay']['Value']!=0){
			$this->TransRollback();
			$this->sysLastError='用户充值单已处理完毕，请到您的帐号里查询支付结果。';
			return $this->sysErrorFuncID.'002';
		}
		//Update
		$pDescriptions[0]['Columns']['shuptState_Pay']['Value']=2;
		$tmpSQL=' UPDATE shopuserpay SET shuptState_Pay={Nets.State} WHERE 1=1 AND shuptID={Nets.ID}; ';
		$tmpSTR=str_replace('{Nets.State}',$pDescriptions[0]['Columns']['shuptState_Pay']['Value'],$tmpSQL);
		$tmpSTR=str_replace('{Nets.ID}',$pDescriptions[0]['Columns']['shuptID']['Value'],$tmpSTR);
		$tmpINT=$this->Execute($tmpSTR);
		if($tmpINT!=0){
			$this->TransRollback();
			$this->sysLastError='更新用户充值单支付状态错误，请与管理员联系。';
			return $tmpINT;
		}
		
		$this->TransCommit();
		return 0;
	}
	public function confirmOrder_Pay(&$pDescriptions){
		/*
更新订单状态
给用户加资金
		*/
		$this->sysErrorFuncID=$this->sysErrorClassID.'02';
		$this->TransStart();
		//-- ------ #Update order state ------ --
		//Get userpay
		$tmpSQL=' SELECT *
			FROM '.$this->sysTableHead.'shopuserpay AS A
			INNER JOIN '.$this->sysTableHead.'shopuserpaycont AS B ON B.shupctIndexID=A.shuptID
			WHERE 1=1 AND A.shuptNumCode=\'{Nets.NumCode}\'; ';
		$tmpSTR=str_replace('{Nets.NumCode}',$pDescriptions[0]['Columns']['shuptNumCode']['Value'],$tmpSQL);
		$tmpData0=$this->Search($tmpSTR);
		if(count($tmpData0)==0){
			$this->TransRollback();
			$this->sysLastError='您提交的用户充值单并不存在，请确认后再提交。';
			return $this->sysErrorFuncID.'002';
		}
		$tmpINT=$this->getDescriptionFromData($pDescriptions,$tmpData0);
		//Check state
		if($pDescriptions[0]['Columns']['shuptState_Pay']['Value']!=0){
			$this->TransRollback();
			$this->sysLastError='用户充值单已处理完毕，请到您的帐号里查询支付结果。';
			return $this->sysErrorFuncID.'002';
		}
		//Update
		$pDescriptions[0]['Columns']['shuptState_Pay']['Value']=1;
		$tmpSQL=' UPDATE '.$this->sysTableHead.'shopuserpay SET shuptState_Pay={Nets.State} WHERE 1=1 AND shuptID={Nets.ID}; ';
		$tmpSTR=str_replace('{Nets.State}',$pDescriptions[0]['Columns']['shuptState_Pay']['Value'],$tmpSQL);
		$tmpSTR=str_replace('{Nets.ID}',$pDescriptions[0]['Columns']['shuptID']['Value'],$tmpSTR);
		$tmpINT=$this->Execute($tmpSTR);
		if($tmpINT!=0){
			$this->TransRollback();
			$this->sysLastError='更新用户充值单支付状态错误，请与管理员联系。';
			return $tmpINT;
		}
		//Update uitMoney
		$tmpSQL=' UPDATE '.$this->sysTableHead.'netsuserinfo SET uitMoney=uitMoney+{Nets.Money} WHERE 1=1 AND uitID={Nets.ID}; ';
		$tmpSTR=str_replace('{Nets.Money}',$pDescriptions[0]['Columns']['shuptTotalPrice']['Value'],$tmpSQL);
		$tmpSTR=str_replace('{Nets.ID}',$pDescriptions[0]['Columns']['shuptCreateBy']['Value'],$tmpSTR);
		$tmpINT=$this->Execute($tmpSTR);
		if($tmpINT!=0){
			$this->TransRollback();
			$this->sysLastError='增加用户资金错误，请与管理员联系。';
			return $tmpINT;
		}
		
		$this->TransCommit();
		return 0;
	}
	public function Order_Get(&$pDescriptions){
		/*
生成订单
减用户资金到提现单里
		*/
		$this->sysErrorFuncID=$this->sysErrorClassID.'02';
		$this->TransStart();
		//-- ------ #Add ------ --
		//#Get numCode
		$tmpSTR='shuptNumCode';
		$pDescriptions[0]['TableDescription']['SearchColumn']=$tmpSTR;
		$pDescriptions[0]['Columns'][$tmpSTR]['Value']=$this->sysFileConfig['NumCodeHead_UserOrder'].$this->sysSPLIT11.date('YmdHis');
		$this->numCode($pDescriptions[0]);
		//-- #Setting --
		$pDescriptions[0]['Columns']['shuptName']['Value']=$pDescriptions[0]['Columns']['shuptNumCode']['Value'];
		$pDescriptions[0]['Columns']['shuptCreateTime']['Value']=date($this->sysTimeFormat);
		$pDescriptions[0]['Columns']['shuptType']['Value']=1;
		$pDescriptions[0]['Columns']['shuptState_Pay']['Value']=-1;
		$pDescriptions[0]['Columns']['shuptState_Get']['Value']=0;
		//-- #Get user --
		$tmpSQL=' SELECT A.uitID,A.uitName
			FROM '.$this->sysTableHead.'netsuserinfo AS A
			INNER JOIN '.$this->sysTableHead.'netsuserinfocont AS B ON B.uictIndexID=A.uitID
			WHERE 1=1 AND A.uitName LIKE \'{Nets.Name}\'; ';
		$tmpSTR=str_replace('{Nets.Name}',$pDescriptions[1]['Columns']['shupctPayUsername']['Value'],$tmpSQL);
		$tmpData0=$this->Search($tmpSTR);
		if(count($tmpData0)==0){
			$this->TransRollback();
			$this->sysLastError='您填写的用户帐号并不存在，请确认后重新填写。';
			return $this->sysErrorFuncID.'002';
		}
		$pDescriptions[1]['Columns']['shupctPayUserid']['Value']=$tmpData0[0]['uitID'];
		$pDescriptions[1]['Columns']['shupctPayUsername']['Value']=$tmpData0[0]['uitName'];
		//-- #Add --
		$tmpINT=$this->Check($pDescriptions,array(),array());
		if($tmpINT!=0){
			$this->TransRollback();
			$this->sysLastError='提现单生成错误，请联系管理员。';
			return $tmpINT;
		}
		$pDescriptions[0]['TableDescription']['SearchColumn']='shuptNumCode,shuptID';
		$pDescriptions[1]['TableDescription']['SearchColumn']='shupctIndexID';
		$tmpINT=$this->add($pDescriptions);
		if($tmpINT!=0){
			$this->TransRollback();
			$this->sysLastError='充值单生成错误，请联系管理员。';
			return $tmpINT;
		}
		//-- ------ #Update uitMoney ------ --
		$tmpSQL=' UPDATE '.$this->sysTableHead.'netsuserinfo SET uitMoney=uitMoney-{Nets.Money} WHERE 1=1 AND uitID={Nets.ID}; ';
		$tmpSTR=str_replace('{Nets.Money}',$pDescriptions[0]['Columns']['shuptTotalPrice']['Value'],$tmpSQL);
		$tmpSTR=str_replace('{Nets.ID}',$pDescriptions[0]['Columns']['shuptCreateBy']['Value'],$tmpSTR);
		if(count($tmpData0)==0){
			$this->TransRollback();
			$this->sysLastError='冻结用户资金错误，请与管理员联系。';
			return $this->sysErrorFuncID.'002';
		}
		
		$this->TransCommit();
		return 0;
	}
	public function cancelOrder_Get(&$pDescriptions){
		/*
更新订单状态
返还提现单里的资金给用户
		*/
		$this->sysErrorFuncID=$this->sysErrorClassID.'02';
		$this->TransStart();
		//-- ------ #Update userpay state ------ --
		//Get userpay
		$tmpSQL=' SELECT *
			FROM '.$this->sysTableHead.'shopuserpay AS A
			INNER JOIN '.$this->sysTableHead.'shopuserpaycont AS B ON B.shupctIndexID=A.shuptID
			WHERE 1=1 AND A.shuptNumCode=\'{Nets.NumCode}\'; ';
		$tmpSTR=str_replace('{Nets.NumCode}',$pDescriptions[0]['Columns']['shuptNumCode']['Value'],$tmpSQL);
		$tmpData0=$this->Search($tmpSTR);
		if(count($tmpData0)==0){
			$this->TransRollback();
			$this->sysLastError='您提交的用户充值单并不存在，请确认后再提交。';
			return $this->sysErrorFuncID.'002';
		}
		$tmpINT=$this->getDescriptionFromData($pDescriptions,$tmpData0);
		//Check state
		if($pDescriptions[0]['Columns']['shuptState_Get']['Value']!=0){
			$this->TransRollback();
			$this->sysLastError='用户提现单已处理完毕或不存在，请确认后再申请提现。';
			return $this->sysErrorFuncID.'002';
		}
		//Update
		$pDescriptions[0]['Columns']['shuptState_Get']['Value']=3;
		$tmpSQL=' UPDATE '.$this->sysTableHead.'shopuserpay SET shuptState_Get={Nets.State} WHERE 1=1 AND shuptID={Nets.ID}; ';
		$tmpSTR=str_replace('{Nets.State}',$pDescriptions[0]['Columns']['shuptState_Get']['Value'],$tmpSQL);
		$tmpSTR=str_replace('{Nets.ID}',$pDescriptions[0]['Columns']['shuptID']['Value'],$tmpSTR);
		$tmpINT=$this->Execute($tmpSTR);
		if($tmpINT!=0){
			$this->TransRollback();
			$this->sysLastError='更新用户充值单支付状态错误，请与管理员联系。';
			return $tmpINT;
		}
		//-- ------ #Update uitMoney ------ --
		$tmpSQL=' UPDATE '.$this->sysTableHead.'netsuserinfo SET uitMoney=uitMoney+{Nets.Money} WHERE 1=1 AND uitID={Nets.ID}; ';
		$tmpSTR=str_replace('{Nets.Money}',$pDescriptions[0]['Columns']['shuptTotalPrice']['Value'],$tmpSQL);
		$tmpSTR=str_replace('{Nets.ID}',$pDescriptions[0]['Columns']['shuptCreateBy']['Value'],$tmpSTR);
		if(count($tmpData0)==0){
			$this->TransRollback();
			$this->sysLastError='返还用户资金错误，请与管理员联系。';
			return $this->sysErrorFuncID.'002';
		}
		
		$this->TransCommit();
		return 0;
	}
	public function payyetOrder_Get(&$pDescriptions){
		/*
更新订单状态
		*/
		$this->sysErrorFuncID=$this->sysErrorClassID.'02';
		$this->TransStart();
		//-- ------ #Update userpay state ------ --
		//Get userpay
		$tmpSQL=' SELECT *
			FROM '.$this->sysTableHead.'shopuserpay AS A
			INNER JOIN '.$this->sysTableHead.'shopuserpaycont AS B ON B.shupctIndexID=A.shuptID
			WHERE 1=1 AND A.shuptNumCode=\'{Nets.NumCode}\'; ';
		$tmpSTR=str_replace('{Nets.NumCode}',$pDescriptions[0]['Columns']['shuptNumCode']['Value'],$tmpSQL);
		$tmpData0=$this->Search($tmpSTR);
		if(count($tmpData0)==0){
			$this->TransRollback();
			$this->sysLastError='您提交的用户充值单并不存在，请确认后再提交。';
			return $this->sysErrorFuncID.'002';
		}
		$tmpINT=$this->getDescriptionFromData($pDescriptions,$tmpData0);
		//Check state
		if($pDescriptions[0]['Columns']['shuptState_Get']['Value']!=0){
			$this->TransRollback();
			$this->sysLastError='用户提现单已处理完毕，请到您的帐号里查询支付结果。';
			return $this->sysErrorFuncID.'002';
		}
		//Update
		$pDescriptions[0]['Columns']['shuptState_Get']['Value']=1;
		$tmpSQL=' UPDATE '.$this->sysTableHead.'shopuserpay SET shuptState_Get={Nets.State} WHERE 1=1 AND shuptID={Nets.ID}; ';
		$tmpSTR=str_replace('{Nets.State}',$pDescriptions[0]['Columns']['shuptState_Get']['Value'],$tmpSQL);
		$tmpSTR=str_replace('{Nets.ID}',$pDescriptions[0]['Columns']['shuptID']['Value'],$tmpSTR);
		$tmpINT=$this->Execute($tmpSTR);
		if($tmpINT!=0){
			$this->TransRollback();
			$this->sysLastError='更新用户充值单支付状态错误，请与管理员联系。';
			return $tmpINT;
		}
		
		$this->TransCommit();
		return 0;
	}
	public function confirmOrder_Get(&$pDescriptions){
		/*
更新订单状态
		*/
		$this->sysErrorFuncID=$this->sysErrorClassID.'02';
		$this->TransStart();
		//-- ------ #Update userpay state ------ --
		//Get userpay
		$tmpSQL=' SELECT *
			FROM '.$this->sysTableHead.'shopuserpay AS A
			INNER JOIN '.$this->sysTableHead.'shopuserpaycont AS B ON B.shupctIndexID=A.shuptID
			WHERE 1=1 AND A.shuptNumCode=\'{Nets.NumCode}\'; ';
		$tmpSTR=str_replace('{Nets.NumCode}',$pDescriptions[0]['Columns']['shuptNumCode']['Value'],$tmpSQL);
		$tmpData0=$this->Search($tmpSTR);
		if(count($tmpData0)==0){
			$this->TransRollback();
			$this->sysLastError='您提交的用户充值单并不存在，请确认后再提交。';
			return $this->sysErrorFuncID.'002';
		}
		$tmpINT=$this->getDescriptionFromData($pDescriptions,$tmpData0);
		//Check state
		if($pDescriptions[0]['Columns']['shuptState_Get']['Value']!=1){
			$this->TransRollback();
			$this->sysLastError='用户提现单未处理完毕，请等到平台支付后，才能确认。';
			return $this->sysErrorFuncID.'002';
		}
		//Update
		$pDescriptions[0]['Columns']['shuptState_Get']['Value']=2;
		$tmpSQL=' UPDATE '.$this->sysTableHead.'shopuserpay SET shuptState_Get={Nets.State} WHERE 1=1 AND shuptID={Nets.ID}; ';
		$tmpSTR=str_replace('{Nets.State}',$pDescriptions[0]['Columns']['shuptState_Get']['Value'],$tmpSQL);
		$tmpSTR=str_replace('{Nets.ID}',$pDescriptions[0]['Columns']['shuptID']['Value'],$tmpSTR);
		$tmpINT=$this->Execute($tmpSTR);
		if($tmpINT!=0){
			$this->TransRollback();
			$this->sysLastError='更新用户提现单支付状态错误，请与管理员联系。';
			return $tmpINT;
		}
		
		$this->TransCommit();
		return 0;
	}
	
}
?>