<?php
	require_once dirname(__FILE__) . "/dalBase.php";
	class message_typeDAL extends dalBase {
		public $TypeID;
		public $TypeName;
		public $Visible;
		public $OrderNO;
		public $IsM;
		public $IsU;

		public function loadRecord(): bool {
			$intoAry = [];
			$sql = "SELECT a.* FROM `message_type` a WHERE a.`TypeID`=:typeID";
			$intoAry[] = parent::addArray(":typeID", $this->TypeID, "int");
			$reader = $this->data->executeReader($sql, $intoAry);

			if (!empty($reader)) {
				$this->TypeID = $reader["TypeID"];
				$this->TypeName = $reader["TypeName"];
				$this->Visible = $reader["Visible"];
				$this->OrderNO = $reader["OrderNO"];
				$this->IsM = $reader["IsM"];
				$this->IsU = $reader["IsU"];
				return true;
			} else {
				return false;
			}
		}

		//新增
		public function insertRecord(): bool|int {
			$intoAry = [
				parent::addArray(":typeName", $this->TypeName, "str", 50),
				parent::addArray(":visible", $this->Visible, "int"),
				parent::addArray(":orderNO", $this->OrderNO, "int"),
				parent::addArray(":isM", $this->IsM, "int"),
				parent::addArray(":isU", $this->IsU, "int")
			];
			$sql = "INSERT INTO `message_type` (`TypeName`, `Visible`, `OrderNO`, `IsM`, `IsU`) VALUES (:typeName, :visible, :orderNO, :isM, :isU)";
			$this->data->executeNonQuery($sql, $intoAry, true);
			return (!$this->data->error) ? $this->data->getInsertID() : false;
		}

		//修改
		public function updateRecord(): bool {
			$intoAry = [
				parent::addArray(":typeID", $this->TypeID, "int"),
				parent::addArray(":typeName", $this->TypeName, "str", 50),
				parent::addArray(":visible", $this->Visible, "int"),
				parent::addArray(":orderNO", $this->OrderNO, "int"),
				parent::addArray(":isM", $this->IsM, "int"),
				parent::addArray(":isU", $this->IsU, "int")
			];
			$sql = "UPDATE `message_type` SET ".
				"`TypeName`=:typeName".
				",`Visible`=:visible".
				",`OrderNO`=:orderNO".
				",`IsM`=:isM".
				",`IsU`=:isU".
				" WHERE `TypeID`=:typeID";
			$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 `message_type` a";
			if ($filter !== "") $sql .= " WHERE $filter";
			$sql .= (!isset($order) || $order === "") ? " ORDER BY a.`OrderNO`,a.`TypeID`" : " 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 `message_type` a";
			if ($filter !== "") $sql .= " WHERE $filter";
			return $this->data->executeScalar($sql, $intoAry);
		}

		//刪除
		public function deleteRecord(int $typeID = 0): bool {
			$intoAry = [
				parent::addArray(":typeID", $typeID, "int")
			];
			$sql = "DELETE FROM `message_type` WHERE `TypeID`=:typeID";
			$this->data->executeNonQuery($sql, $intoAry, true);
			return !$this->data->error;
		}
	}
?>