<?php
 /**
 *      [XCECP] (C) Colorful Network Inc.
 *      This is NOT a freeware, use is subject to license terms
 */
if(!defined("IN_QKWEB")){exit("Access Denied");}
class OnlinepayModel extends BaseModel{
	public $errString="";
	function getAllTypes(){
		$sql = "select * from qk_codemap where parentId=16 and iStatus=1";
		return $this->db->getAll($sql);
	}
	function del($id){
		$sql = "select * from qk_onlinepay where id='$id'";
		$payInfo = $this->db->getRow($sql);
		if(!$payInfo){$this->msg='未找到相关记录';return false;}
		//if($payInfo['iStatus']==14){$this->msg='成功记录不可删除';return false;}
		$sql = "delete from `qk_onlinepay` where id='$id' limit 1";
		return $this->db->query($sql);
	}
	function getInfo($params){
		if(!is_array($params)){
			if(!$params || !settype($params,"integer")){return false;}
			$wheres = " and id='$params'";
		}else{
			if(!$params['id'] && !$params['outTradeNo']){return false;}
			if($params['id'] && settype($params['id'],"integer")){$wheres .= " and id='".$params['id']."'";}
			if($params['outTradeNo']){$wheres .= " and outTradeNo='".$params['outTradeNo']."'";}
		}
		$sql = "select * from `qk_onlinepay` where 1=1 $wheres";
		$info = $this->db->getRow($sql);
		if(!$info){return false;}
		$info['status'] = $this->db->getOne("select name from `qk_onlinepay_status` where id='".$info['iStatus']."'");

		if($info['userId']){ $info['userInfo'] = $this->db->getRow("select * from `qk_user` where id='".$info['userId']."'"); }
		$info['paymentInterface']['className'] = '';
			$info['paymentInfo'] = $this->db->getRow("select name,typeId from `qk_payment` where id='".$info['paymentId']."'");
			$info['loginName'] = $this->db->getOne("select loginName from `qk_user` where id='".$info['userId']."'");
			$info['status'] = $this->db->getOne("select name from `qk_onlinepay_status` where id='".$info['iStatus']."'");
			$info['query'] = false;
			$typeId = $info['paymentInfo']['typeId'];$className='';
			if($typeId){
				$className = $this->db->getOne("select className from `qk_paytype` where id='".$typeId."'");
				$classFile = ROOT_PATH."plugins/pay/".$className.".class.php";
				if($className && file_exists($classFile)){
					$info['paymentInterface']['className'] = $className;
					require_once($classFile);$pay = new $className(); if($pay->action && $pay->action['query']===true){ $info['query'] = true; }
				}
			}

		return $info;
	}
	function add($param){
		return $this->query($param);
	}
	function query($param){
		$act = $param['act'];
		$id = $param['id'];
		$loginName = $param['loginName'];
		$amount = $param['amount'];
		$typeId = $param['typeId'];
		$about = $param['about'];
		$remark = $param['remark'];
		$orderId = $param['orderId'];
		$iStatus = $param['iStatus'];
		$effectiveTime = $param['effectiveTime'];
		if(!$effectiveTime || ($effectiveTime=strtotime($effectiveTime))==false){return false;}
		$outTradeNo = $param['outTradeNo'];
		if($act=="add"){
			$sql = "insert into `qk_onlinepay` set ";
			$sqlEnd = ",posttime=UNIX_TIMESTAMP()";
		}else if($act=="edit" && $id && settype($id,"integer")){
			$sql = "update `qk_onlinepay` set ";
			$sqlEnd = " where id='$id'";

		}
		load_class("user");
		$userId = UserModel::getId($loginName);
		if(!$userId){return false;}
		$sql .= " userId='$userId',amount='$amount',typeId='$typeId',about='$about',remark='$remark',iStatus='$iStatus'";
		$sql .= ",effectiveTime='$effectiveTime',outTradeNo='$outTradeNo'";
		$sql .= $sqlEnd;
		if($this->db->query($sql)){return true;}
		return false;
	}
}
?>
