<?php
	require_once dirname(__FILE__)."/dalBase.php";
	class commentDAL extends dalBase{
		public $CommentID;
		public $CustomerID;
		public $ProductID;
		public $Stars;
		public $Content;
		public $Visible;
		public $CreatedIP;
		public $CreatedTime;
		//外部欄位
		public $CustomerName;

		public function loadRecord(){
			$sql = "SELECT a.*,b.`CustomerName` FROM `comment` a LEFT JOIN `customer` b ON a.`CustomerID`=b.`CustomerID` WHERE a.`CommentID`='".$this->CommentID."'";
			$reader = $this->data->executeReader($sql);

			$this->CommentID = $reader["CommentID"];
			$this->CustomerID = $reader["CustomerID"];
			$this->ProductID = $reader["ProductID"];
			$this->Stars = $reader["Stars"];
			$this->Content = $reader["Content"];
			$this->Visible = $reader["Visible"];
			$this->CreatedIP = $reader["CreatedIP"];
			$this->CreatedTime = $reader["CreatedTime"];
			//外部欄位
			$this->CustomerName = $reader["CustomerName"];

			if(!$this->data->error)
				return true;
			return false;
		}
		public function insertRecord(){
			$sql = "INSERT INTO `comment` (`CustomerID`, `ProductID`, `Stars`, `Content`, `Visible`, `CreatedIP`, `CreatedTime`)" .
				" VALUES (@CustomerID,@ProductID,@Stars,@Content,@Visible,@CreatedIP,@CreatedTime)";
			$sql = str_replace("@CustomerID","'".$this->CustomerID."'",$sql);
			$sql = str_replace("@ProductID","'".$this->ProductID."'",$sql);
			$sql = str_replace("@Stars","'".$this->Stars."'",$sql);
			$sql = str_replace("@Content","'".$this->Content."'",$sql);
			$sql = str_replace("@Visible","'".$this->Visible."'",$sql);
			$sql = str_replace("@CreatedIP","'".$this->CreatedIP."'",$sql);
			$sql = str_replace("@CreatedTime","'".date("Y-m-d H:i:s")."'",$sql);

			$this->data->executeNonQuery($sql);
			if(!$this->data->error)
				return $this->data->getInsertID();
			return false;
		}
		public function updateRecord(){
			$sql = "UPDATE `comment` SET " .
				" `CustomerID`=@CustomerID" .
				",`ProductID`=@ProductID" .
				",`Stars`=@Stars" .
				",`Content`=@Content" .
				",`Visible`=@Visible" .
				" WHERE `CommentID`=@CommentID";
			$sql = str_replace("@CommentID","'".$this->CommentID."'",$sql);
			$sql = str_replace("@CustomerID","'".$this->CustomerID."'",$sql);
			$sql = str_replace("@ProductID","'".$this->ProductID."'",$sql);
			$sql = str_replace("@Stars","'".$this->Stars."'",$sql);
			$sql = str_replace("@Content","'".$this->Content."'",$sql);
			$sql = str_replace("@Visible","'".$this->Visible."'",$sql);

			$this->data->executeNonQuery($sql);
			if(!$this->data->error)
				return true;
			return false;
		}
		//查詢
		public function executeRecordset($filter="",$orderExp="",$start=0,$length=-1){
			$sql = "SELECT a.*,b.`CustomerName` FROM `comment` a LEFT JOIN `customer` b ON a.`CustomerID`=b.`CustomerID`";
			if(trim($filter) != "")
				$sql .= " WHERE ".$filter;
			if(trim($orderExp) != "")
				$sql .= " ORDER BY ".$orderExp;
			else
				$sql .= " ORDER BY a.`CreatedTime`";

			return $this->data->executeRecordset($sql,$start,$length);
		}
		//查詢計數
		public function countRecordset($filter){
			$sql = "SELECT COUNT(*) FROM `comment` a";
			if($filter != "")
				$sql .= " WHERE ".$filter;

			return $this->data->executeScalar($sql);
		}
		//刪除
		public function deleteRecord($commentID){
			$sql = "DELETE FROM `comment` WHERE `CommentID`='".$commentID."'";
			$this->data->executeNonQuery($sql);

			if(!$this->data->error)
				return true;
			return false;
		}
		//批量刪除，傳入參數為用逗號分隔的id镩
		public function batDelete($commentIDs){
			$sql = "DELETE FROM `comment` WHERE `CommentID` IN (".$commentIDs.")";
			$this->data->executeNonQuery($sql,true);

			if(!$this->data->error)
				return true;
			return false;
		}
	}
?>