<?php
	require_once dirname(__FILE__) . "/dalBase.php";
	class paypageDAL extends dalBase {
		public $ID;
		public $PayerName;
		public $TimeStamp;
		public $MerchantOrderNo;
		public $Amt;
		public $ItemDesc;
		public $ExpireTime;
		public $Email;
		public $OrderComment;
		public $Status;
		public $ReStatus;
		public $ReMessage;
		public $TradeNo;
		public $PaymentType;
		public $PayTime;
		public $PayIP;
		public $CreatedTime;
		public $CreatedIP;
		public $ReplyTime;
		public $ReplyIP;

		public function loadRecord(): bool {
			$intoAry = [];
			$sql = "SELECT a.* FROM `paypage` a WHERE ";
			if ($this->MerchantOrderNo !== null) {
				$sql .= "a.`MerchantOrderNo`=:merchantOrderNo";
				$intoAry[] = parent::addArray(":merchantOrderNo", $this->MerchantOrderNo, "str", 30);
			} else if ($this->ID !== null) {
				$sql .= "a.`ID`=:id";
				$intoAry[] = parent::addArray(":id", $this->ID, "int");
			} else {
				$sql .= "1 = 2";
			}
			$reader = $this->data->executeReader($sql, $intoAry);

			if (!empty($reader)) {
				$this->ID = $reader["ID"];
				$this->PayerName = $reader["PayerName"];
				$this->TimeStamp = $reader["TimeStamp"];
				$this->MerchantOrderNo = $reader["MerchantOrderNo"];
				$this->Amt = $reader["Amt"];
				$this->ItemDesc = $reader["ItemDesc"];
				$this->ExpireTime = $reader["ExpireTime"];
				$this->Email = $reader["Email"];
				$this->OrderComment = $reader["OrderComment"];
				$this->Status = $reader["Status"];
				$this->ReStatus = $reader["ReStatus"];
				$this->ReMessage = $reader["ReMessage"];
				$this->TradeNo = $reader["TradeNo"];
				$this->PaymentType = $reader["PaymentType"];
				$this->PayTime = $reader["PayTime"];
				$this->PayIP = $reader["PayIP"];
				$this->CreatedTime = $reader["CreatedTime"];
				$this->CreatedIP = $reader["CreatedIP"];
				$this->ReplyTime = $reader["ReplyTime"];
				$this->ReplyIP = $reader["ReplyIP"];
				return true;
			} else {
				return false;
			}
		}

		public function insertRecord(): bool|int {
			$intoAry = [
				parent::addArray(":payerName", $this->PayerName, "str", 50),
				parent::addArray(":timeStamp", $this->TimeStamp, "str", 50),
				parent::addArray(":merchantOrderNo", $this->MerchantOrderNo, "str", 30),
				parent::addArray(":amt", $this->Amt, "int"),
				parent::addArray(":itemDesc", $this->ItemDesc, "str", 50),
				parent::addArray(":expireTime", $this->ExpireTime, "int"),
				parent::addArray(":email", $this->Email, "str", 50),
				parent::addArray(":orderComment", $this->OrderComment, "str", 300),
				parent::addArray(":createdIP", $this->CreatedIP, "str", 50)
			];
			$sql = "INSERT INTO `paypage` (`PayerName`, `TimeStamp`, `MerchantOrderNo`, `Amt`, `ItemDesc`, `ExpireTime`, `Email`, `OrderComment`, `Status`, `CreatedTime`, `CreatedIP`)".
				" VALUES (:payerName, :timeStamp, :merchantOrderNo, :amt, :itemDesc, :expireTime, :email, :orderComment, 0, UNIX_TIMESTAMP(), :createdIP)";
			$this->data->executeNonQuery($sql, $intoAry, true);
			return (!$this->data->error) ? $this->data->getInsertID() : false;
		}

		public function replyRecord(): bool {
			$intoAry = [
				parent::addArray(":id", $this->ID, "int"),
				parent::addArray(":status", $this->Status, "int"),
				parent::addArray(":reStatus", $this->ReStatus, "str", 10),
				parent::addArray(":reMessage", $this->ReMessage, "str", 50),
				parent::addArray(":tradeNo", $this->TradeNo, "str", 20),
				parent::addArray(":paymentType", $this->PaymentType, "str", 20),
				parent::addArray(":payTime", $this->PayTime, "str"),
				parent::addArray(":payIP", $this->PayIP, "str", 15),
				parent::addArray(":replyIP", $this->ReplyIP, "str", 50)
			];
			$sql = "UPDATE `paypage` SET ".
				"`Status`=:status".
				",`ReStatus`=:reStatus".
				",`ReMessage`=:reMessage".
				",`TradeNo`=:tradeNo".
				",`PaymentType`=:paymentType".
				",`PayTime`=:payTime".
				",`PayIP`=:payIP".
				",`ReplyTime`=UNIX_TIMESTAMP()".
				",`ReplyIP`=:replyIP".
				" WHERE `ID`=:id";
			$this->data->executeNonQuery($sql, $intoAry, true);
			return !$this->data->error;
		}

		//查詢
		public function executeRecordset(array $intoAry = [], string $filter = "", string $order = "", int $start = 0, int $length = -1): array {
			$sql = "SELECT a.* FROM `paypage` a";
			if ($filter !== "") $sql .= " WHERE $filter";
			$sql .= (!isset($order) || $order === "") ? " ORDER BY a.`ID` DESC" : " ORDER BY $order";
			$length = is_int($length) ? $length : -1;
			return $this->data->executeRecordset($sql, $intoAry, $start, $length);
		}
		//查詢記數
		public function countRecordset(array $intoAry = [], string $filter = ""): int {
			$sql = "SELECT COUNT(*) FROM `paypage` a";
			if ($filter !== "") $sql .= " WHERE $filter";
			return $this->data->executeScalar($sql, $intoAry);
		}
		//逾期處理
		public function overRecord(): bool {
			$sql = "UPDATE `paypage` SET `Status`=-2 WHERE `Status`=0 AND `ExpireTime`<UNIX_TIMESTAMP()";
			$this->data->executeNonQuery($sql, [], true);
			return !$this->data->error;
		}
	}
?>