<?php
	require_once dirname(__FILE__)."/dalBase.php";
	class customerDAL extends dalBase{
		public $CustomerID;
		public $CustomerLevel;
		public $FacebookID;
		public $LoginName;
		public $LoginPassword;
		public $CustomerName;
		public $Mobile;
		public $LineID;
		public $CreatedIP;
		public $CreatedTime;

		public function loadRecord(){
			$sql = "SELECT a.* FROM `customer` a WHERE a.`CustomerID`='".$this->CustomerID."'";
			$reader = $this->data->executeReader($sql);

			$this->CustomerID = $reader["CustomerID"];
			$this->CustomerLevel = $reader["CustomerLevel"];
			$this->FacebookID = $reader["FacebookID"];
			$this->LoginName = $reader["LoginName"];
			$this->LoginPassword = $reader["LoginPassword"];
			$this->CustomerName = $reader["CustomerName"];
			$this->Mobile = $reader["Mobile"];
			$this->LineID = $reader["LineID"];
			$this->CreatedIP = $reader["CreatedIP"];
			$this->CreatedTime = $reader["CreatedTime"];

			if(!$this->data->error)
				return true;
			return false;
		}
		public function insertRecord(){
			$sql = "INSERT INTO `customer` (`CustomerLevel`, `FacebookID`, `LoginName`, `LoginPassword`, `CustomerName`, `Mobile`, `LineID`, `CreatedIP`, `CreatedTime`)" .
				" VALUES (@CustomerLevel,@FacebookID,@LoginName,@LoginPassword,@CustomerName,@Mobile,@LineID,@CreatedIP,@CreatedTime)";
			$sql = str_replace("@CustomerLevel","'".$this->CustomerLevel."'",$sql);
			$sql = str_replace("@FacebookID","'".$this->FacebookID."'",$sql);
			$sql = str_replace("@LoginName","'".$this->LoginName."'",$sql);
			$sql = str_replace("@LoginPassword","'".$this->LoginPassword."'",$sql);
			$sql = str_replace("@CustomerName","'".$this->CustomerName."'",$sql);
			$sql = str_replace("@Mobile","'".$this->Mobile."'",$sql);
			$sql = str_replace("@LineID","'".$this->LineID."'",$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 `customer` SET " .
				" `CustomerLevel`=@CustomerLevel" .
				",`FacebookID`=@FacebookID" .
				",`LoginName`=@LoginName" .
				",`LoginPassword`=@LoginPassword" .
				",`CustomerName`=@CustomerName" .
				",`Mobile`=@Mobile" .
				",`LineID`=@LineID" .
				" WHERE `CustomerID`=@CustomerID";
			$sql = str_replace("@CustomerID","'".$this->CustomerID."'",$sql);
			$sql = str_replace("@CustomerLevel","'".$this->CustomerLevel."'",$sql);
			$sql = str_replace("@FacebookID","'".$this->FacebookID."'",$sql);
			$sql = str_replace("@LoginName","'".$this->LoginName."'",$sql);
			$sql = str_replace("@LoginPassword","'".$this->LoginPassword."'",$sql);
			$sql = str_replace("@CustomerName","'".$this->CustomerName."'",$sql);
			$sql = str_replace("@Mobile","'".$this->Mobile."'",$sql);
			$sql = str_replace("@LineID","'".$this->LineID."'",$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.* FROM `customer` a";
			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 `customer` a";
			if($filter != "")
				$sql .= " WHERE ".$filter;

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

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

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