<?php
	require_once dirname(__FILE__) . "/dalBase.php";
	class line_gearingDAL extends dalBase {
		public $LGID;
		public $AccessToken;
		public $TargetType;
		public $Target;
		public $CreatedTime;
		public $CreatedUserID;
		public $CreatedIP;
		public $ModifiedTime;
		public $ModifiedUserID;
		public $ModifiedIP;

		public function loadRecord(): bool {
			$intoAry = [];
			$sql = "SELECT a.* FROM `line_gearing` a LEFT JOIN `user` b ON a.`CreatedUserID`=b.`UserID` LEFT JOIN `user` c ON a.`ModifiedUserID`=c.`UserID` WHERE a.`LGID`=:lGID";
			$intoAry[] = parent::addArray(":lGID", $this->LGID, "int");
			$reader = $this->data->executeReader($sql, $intoAry);

			if (!empty($reader)) {
				$this->LGID = $reader["LGID"];
				$this->AccessToken = $reader["AccessToken"];
				$this->TargetType = $reader["TargetType"];
				$this->Target = $reader["Target"];
				$this->CreatedTime = $reader["CreatedTime"];
				$this->CreatedUserID = $reader["CreatedUserID"];
				$this->CreatedIP = $reader["CreatedIP"];
				$this->ModifiedTime = $reader["ModifiedTime"];
				$this->ModifiedUserID = $reader["ModifiedUserID"];
				$this->ModifiedIP = $reader["ModifiedIP"];
				return true;
			} else {
				return false;
			}
		}
		public function insertRecord(): bool|int {
			$nowTime = time();
			$intoAry = [
				parent::addArray(":accessToken", $this->AccessToken, "str", 50),
				parent::addArray(":targetType", $this->TargetType, "int"),
				parent::addArray(":target", $this->Target, "str", 60),
				parent::addArray(":createdTime", $nowTime, "int"),
				parent::addArray(":createdUserID", $this->CreatedUserID, "int"),
				parent::addArray(":createdIP", $this->CreatedIP, "str", 50)
			];
			$sql = "INSERT INTO `line_gearing` (`AccessToken`, `TargetType`, `Target`, `CreatedTime`, `CreatedUserID`, `CreatedIP`, `ModifiedTime`, `ModifiedUserID`, `ModifiedIP`)".
				" VALUES (:accessToken, :targetType, :target, :createdTime, :createdUserID, :createdIP, 0, 0, '')";
			$this->data->executeNonQuery($sql, $intoAry, true);
			return (!$this->data->error) ? $this->data->getInsertID() : false;
		}
		public function updateRecord(): bool {
			$nowTime = time();
			$intoAry = [
				parent::addArray(":lGID", $this->LGID, "int"),
				parent::addArray(":accessToken", $this->AccessToken, "str", 50),
				parent::addArray(":targetType", $this->TargetType, "int"),
				parent::addArray(":target", $this->Target, "str", 60),
				parent::addArray(":modifiedTime", $nowTime, "int"),
				parent::addArray(":modifiedUserID", $this->ModifiedUserID, "int"),
				parent::addArray(":modifiedIP", $this->ModifiedIP, "str", 50)
			];
			$sql = "UPDATE `line_gearing` SET ".
				"`AccessToken`=:accessToken".
				",`TargetType`=:targetType".
				",`Target`=:target".
				",`ModifiedTime`=:modifiedTime".
				",`ModifiedUserID`=:modifiedUserID".
				",`ModifiedIP`=:modifiedIP".
				" WHERE `LGID`=:lGID";
			$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 `line_gearing` a";
			if ($filter !== "") $sql .= " WHERE $filter";
			$sql .= (!isset($order) || $order === "") ? " ORDER BY a.`CreatedTime` 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 `line_gearing` a";
			if ($filter !== "") $sql .= " WHERE $filter";
			return $this->data->executeScalar($sql, $intoAry);
		}
		
		//刪除
		public function deleteRecord(int $lGID): bool {
			$intoAry = [
				parent::addArray(":lGID", $lGID, "int")
			];
			$sql = "DELETE FROM `line_gearing` WHERE `LGID`=:lGID";
			$this->data->executeNonQuery($sql, $intoAry, true);
			return !$this->data->error;
		}

		//清空資料庫
		public function batTruncate(): bool {
			$sql = "TRUNCATE `line_gearing`";
			$this->data->executeNonQuery($sql, []);
			return !$this->data->error;
		}
	}
?>