<?php
	require_once dirname(__FILE__) . "/dalBase.php";
	class copy_product_logDAL extends dalBase {
		public $ProductID;
		public $CopyProductID;
		public $CopyUserID;
		public $CopyIP;
		public $CopyTime;
		//外部欄位
		public $ProductName;
		public $ProductIconImage;
		public $CopyName;
		public $CopyIconImage;
		public $LoginName;
		public $UserName;

		public function insertRecord(): bool|int {
			$intoAry = [
				parent::addArray(":productID", $this->ProductID, "int"),
				parent::addArray(":copyProductID", $this->CopyProductID, "int"),
				parent::addArray(":copyUserID", $this->CopyUserID, "int"),
				parent::addArray(":copyIP", $this->CopyIP, "str", 50),
				parent::addArray(":copyTime", $this->CopyTime, "int")
			];
			$sql = "INSERT INTO `copy_product_log` (`ProductID`, `CopyProductID`, `CopyUserID`, `CopyIP`, `CopyTime`) VALUES (:productID, :copyProductID, :copyUserID, :copyIP, :copyTime)";
			$this->data->executeNonQuery($sql, $intoAry, true);
			return (!$this->data->error) ? $this->data->getInsertID() : false;
		}
		//查詢
		public function executeRecordset(array $intoAry = [], string $filter = "", string $order = "", int $start = 0, int $length = -1): array {
			$sql = "SELECT a.*,b.`ProductName`,b.`IconImage` AS `ProductIconImage`".
				",c.`ProductName` AS `CopyName`,c.`IconImage` AS `CopyIconImage`".
				",d.`LoginName`,d.`UserName` FROM `copy_product_log` a".
				" LEFT JOIN `product` b ON a.`ProductID`=b.`ProductID`".
				" LEFT JOIN `product` c ON a.`CopyProductID`=c.`ProductID`".
				" LEFT JOIN `user` d ON a.`CopyUserID`=d.`UserID`";
			if ($filter !== "") $sql .= " WHERE $filter";
			$sql .= (!isset($order) || $order === "") ? " ORDER BY a.`CopyTime` 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 `copy_product_log` a".
				" LEFT JOIN `product` b ON a.`ProductID`=b.`ProductID`".
				" LEFT JOIN `product` c ON a.`CopyProductID`=c.`ProductID`".
				" LEFT JOIN `user` d ON a.`CopyUserID`=d.`UserID`";
			if ($filter !== "") $sql .= " WHERE $filter";
			return $this->data->executeScalar($sql, $intoAry);
		}
	}
?>