<?php
	require_once dirname(__FILE__)."/dalBase.php";
	class productDAL extends dalBase{
		public $ProductID;
		public $CategoryID;
		public $ProductName;
		public $ProductNO;
		public $OldPrice;
		public $Price;
		public $IconImage;
		public $Image1;
		public $Image2;
		public $Image3;
		public $Image4;
		public $Introduction;
		public $Title1;
		public $Title2;
		public $Title3;
		public $Description1;
		public $Description2;
		public $Description3;
		public $ViewCount;
		public $IsIndex;
		public $Visible;
		public $OrderNO;
		public $WebTitle;
		public $WebKeyword;
		public $WebDescription;
		public $CreatedTime;
		public $CreatedUserID;
		public $ModifiedTime;
		public $ModifiedUserID;
		//外部欄位
		public $CategoryName;
		public $CategoryVisible;
		public $ParentID;
		public $ParentName;
		public $ParentVisible;

		public function loadRecord(){
			$sql = "SELECT a.*,b.`CategoryName`,b.`Visible` AS `CategoryVisible`,b.`ParentID`,c.`CategoryName` AS `ParentName`,c.`Visible` AS `ParentVisible` FROM `product` a LEFT JOIN `category` b ON a.`CategoryID`=b.`CategoryID` LEFT JOIN `category` c ON b.`ParentID`=c.`CategoryID` WHERE a.`ProductID`='".$this->ProductID."'";
			$reader = $this->data->executeReader($sql);

			$this->ProductID = $reader["ProductID"];
			$this->CategoryID = $reader["CategoryID"];
			$this->ProductName = $reader["ProductName"];
			$this->ProductNO = $reader["ProductNO"];
			$this->OldPrice = $reader["OldPrice"];
			$this->Price = $reader["Price"];
			$this->IconImage = $reader["IconImage"];
			$this->Image1 = $reader["Image1"];
			$this->Image2 = $reader["Image2"];
			$this->Image3 = $reader["Image3"];
			$this->Image4 = $reader["Image4"];
			$this->Introduction = $reader["Introduction"];
			$this->Title1 = $reader["Title1"];
			$this->Title2 = $reader["Title2"];
			$this->Title3 = $reader["Title3"];
			$this->Description1 = $reader["Description1"];
			$this->Description2 = $reader["Description2"];
			$this->Description3 = $reader["Description3"];
			$this->ViewCount = $reader["ViewCount"];
			$this->IsIndex = $reader["IsIndex"];
			$this->Visible = $reader["Visible"];
			$this->OrderNO = $reader["OrderNO"];
			$this->WebTitle = $reader["WebTitle"];
			$this->WebKeyword = $reader["WebKeyword"];
			$this->WebDescription = $reader["WebDescription"];
			$this->CreatedTime = $reader["CreatedTime"];
			$this->CreatedUserID = $reader["CreatedUserID"];
			$this->ModifiedTime = $reader["ModifiedTime"];
			$this->ModifiedUserID = $reader["ModifiedUserID"];
			$this->CategoryName = $reader["CategoryName"];
			$this->CategoryVisible = $reader["CategoryVisible"];
			$this->ParentID = $reader["ParentID"];
			$this->ParentName = $reader["ParentName"];
			$this->ParentVisible = $reader["ParentVisible"];

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

		public function insertRecord(){
			$sql = "INSERT INTO `product` (`CategoryID`, `ProductName`, `ProductNO`, `OldPrice`, `Price`, `IconImage`, `Image1`, `Image2`, `Image3`, `Image4`, `Introduction`, `Title1`, `Title2`, `Title3`, `Description1`, `Description2`, `Description3`, `ViewCount`, `IsIndex`, `Visible`, `OrderNO`, `WebTitle`, `WebKeyword`, `WebDescription`, `CreatedTime`, `CreatedUserID`, `ModifiedTime`, `ModifiedUserID`)" .
			" VALUES " .
"(@CategoryID,@ProductName,@ProductNO,@OldPrice,@Price,@IconImage,@Image1,@Image2,@Image3,@Image4,@Introduction,@Title1,@Title2,@Title3,@Description1,@Description2,@Description3,@ViewCount,@IsIndex,@Visible,@OrderNO,@WebTitle,@WebKeyword,@WebDescription,@CreatedTime,@CreatedUserID,@ModifiedTime,@ModifiedUserID)";
			$sql = str_replace("@CategoryID","'".$this->CategoryID."'",$sql);
			$sql = str_replace("@ProductName","'".$this->ProductName."'",$sql);
			$sql = str_replace("@ProductNO","'".$this->ProductNO."'",$sql);
			$sql = str_replace("@OldPrice","'".$this->OldPrice."'",$sql);
			$sql = str_replace("@Price","'".$this->Price."'",$sql);
			$sql = str_replace("@IconImage","'".$this->IconImage."'",$sql);
			$sql = str_replace("@Image1","'".$this->Image1."'",$sql);
			$sql = str_replace("@Image2","'".$this->Image2."'",$sql);
			$sql = str_replace("@Image3","'".$this->Image3."'",$sql);
			$sql = str_replace("@Image4","'".$this->Image4."'",$sql);
			$sql = str_replace("@Introduction","'".$this->Introduction."'",$sql);
			$sql = str_replace("@Title1","'".$this->Title1."'",$sql);
			$sql = str_replace("@Title2","'".$this->Title2."'",$sql);
			$sql = str_replace("@Title3","'".$this->Title3."'",$sql);
			$sql = str_replace("@Description1","'".$this->Description1."'",$sql);
			$sql = str_replace("@Description2","'".$this->Description2."'",$sql);
			$sql = str_replace("@Description3","'".$this->Description3."'",$sql);
			$sql = str_replace("@ViewCount","'".$this->ViewCount."'",$sql);
			$sql = str_replace("@IsIndex","'".$this->IsIndex."'",$sql);
			$sql = str_replace("@Visible","'".$this->Visible."'",$sql);
			$sql = str_replace("@OrderNO","'".$this->OrderNO."'",$sql);
			$sql = str_replace("@WebTitle","'".$this->WebTitle."'",$sql);
			$sql = str_replace("@WebKeyword","'".$this->WebKeyword."'",$sql);
			$sql = str_replace("@WebDescription","'".$this->WebDescription."'",$sql);
			$sql = str_replace("@CreatedTime","'".date("Y-m-d H:i:s")."'",$sql);
			$sql = str_replace("@CreatedUserID","'".$this->CreatedUserID."'",$sql);
			$sql = str_replace("@ModifiedTime","'".date("Y-m-d H:i:s")."'",$sql);
			$sql = str_replace("@ModifiedUserID","'".$this->ModifiedUserID."'",$sql);
			$this->data->executeNonQuery($sql, true);
			$insertID = $this->data->getInsertID();

			if(!$this->data->error)
				return $insertID;
			return false;
		}
		public function updateRecord(){
			$sql = "UPDATE `product` SET " .
				"`CategoryID`=@CategoryID" .
				",`ProductName`=@ProductName" .
				",`ProductNO`=@ProductNO" .
				",`OldPrice`=@OldPrice" .
				",`Price`=@Price" .
				",`IconImage`=@IconImage" .
				",`Image1`=@Image1" .
				",`Image2`=@Image2" .
				",`Image3`=@Image3" .
				",`Image4`=@Image4" .
				",`Introduction`=@Introduction" .
				",`Title1`=@Title1" .
				",`Title2`=@Title2" .
				",`Title3`=@Title3" .
				",`Description1`=@Description1" .
				",`Description2`=@Description2" .
				",`Description3`=@Description3" .
				",`ViewCount`=@ViewCount" .
				",`IsIndex`=@IsIndex" .
				",`Visible`=@Visible" .
				",`OrderNO`=@OrderNO" .
				",`WebTitle`=@WebTitle" .
				",`WebKeyword`=@WebKeyword" .
				",`WebDescription`=@WebDescription" .
				",`ModifiedTime`=@ModifiedTime" .
				",`ModifiedUserID`=@ModifiedUserID" .
				" WHERE `ProductID`=@ProductID";
			$sql = str_replace("@ProductID","'".$this->ProductID."'",$sql);
			$sql = str_replace("@CategoryID","'".$this->CategoryID."'",$sql);
			$sql = str_replace("@ProductName","'".$this->ProductName."'",$sql);
			$sql = str_replace("@ProductNO","'".$this->ProductNO."'",$sql);
			$sql = str_replace("@OldPrice","'".$this->OldPrice."'",$sql);
			$sql = str_replace("@Price","'".$this->Price."'",$sql);
			$sql = str_replace("@IconImage","'".$this->IconImage."'",$sql);
			$sql = str_replace("@Image1","'".$this->Image1."'",$sql);
			$sql = str_replace("@Image2","'".$this->Image2."'",$sql);
			$sql = str_replace("@Image3","'".$this->Image3."'",$sql);
			$sql = str_replace("@Image4","'".$this->Image4."'",$sql);
			$sql = str_replace("@Introduction","'".$this->Introduction."'",$sql);
			$sql = str_replace("@Title1","'".$this->Title1."'",$sql);
			$sql = str_replace("@Title2","'".$this->Title2."'",$sql);
			$sql = str_replace("@Title3","'".$this->Title3."'",$sql);
			$sql = str_replace("@Description1","'".$this->Description1."'",$sql);
			$sql = str_replace("@Description2","'".$this->Description2."'",$sql);
			$sql = str_replace("@Description3","'".$this->Description3."'",$sql);
			$sql = str_replace("@ViewCount","'".$this->ViewCount."'",$sql);
			$sql = str_replace("@IsIndex","'".$this->IsIndex."'",$sql);
			$sql = str_replace("@Visible","'".$this->Visible."'",$sql);
			$sql = str_replace("@OrderNO","'".$this->OrderNO."'",$sql);
			$sql = str_replace("@WebTitle","'".$this->WebTitle."'",$sql);
			$sql = str_replace("@WebKeyword","'".$this->WebKeyword."'",$sql);
			$sql = str_replace("@WebDescription","'".$this->WebDescription."'",$sql);
			$sql = str_replace("@ModifiedTime","'".date("Y-m-d H:i:s")."'",$sql);
			$sql = str_replace("@ModifiedUserID","'".$this->ModifiedUserID."'",$sql);

			$this->data->executeNonQuery($sql,true);
			if(!$this->data->error)
				return true;
			return false;
		}

		//查詢
		public function executeRecordset($filter="", $orderExp="",$start=0,$length=-1){
			$sql = "SELECT a.*,b.`CategoryName`,b.`Visible` AS `CategoryVisible`,b.`ParentID`,c.`CategoryName` AS `ParentName`,c.`Visible` AS `ParentVisible`  FROM `product` a LEFT JOIN `category` b ON a.`CategoryID`=b.`CategoryID` LEFT JOIN `category` c ON b.`ParentID`=c.`CategoryID`";
			if(trim($filter) != "")
				$sql .= " WHERE ".$filter;
			if(trim($orderExp) != "")
				$sql .= " ORDER BY ".$orderExp;
			else
				$sql .= " ORDER BY a.`OrderNO`";

			return $this->data->executeRecordset($sql,$start,$length);
		}
		//刪除
		public function deleteRecord($productID){
			$sql = "DELETE FROM `product` WHERE `ProductID`='".$productID."'";
			$this->data->executeNonQuery($sql,true);

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

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

		//批量修改產品啟用狀態
		public function batUpdateVisible($productIDs,$value="1"){
			$sql = "UPDATE `product` SET `Visible`='".$value."' WHERE `ProductID` IN (".$productIDs.")";
			$this->data->executeNonQuery($sql);
			if(!$this->data->error)
				return true;
			return false;
		}
		//查詢計數
		public function countRecordset($filter){
			$sql = "SELECT COUNT(*) FROM `product` a LEFT JOIN `category` b ON a.`CategoryID`=b.`CategoryID` LEFT JOIN `category` c ON b.`ParentID`=c.`CategoryID`";
			if($filter != "")
				$sql .= " WHERE ".$filter;

			return $this->data->executeScalar($sql);
		}
		//點擊計數遞增
		public function click(){
			$sql = "UPDATE `product` SET `ViewCount`=`ViewCount`+1 WHERE `ProductID`='".$this->ProductID."'";
			$this->data->executeNonQuery($sql);
			if(!$this->data->error)
				return true;
			return false;
		}
	}
?>