<?php
require_once '../database/SqlHelper.class.php';
require_once '../phpbean/Teacher.class.php';

/*教师信息管理业务层类*/
class TeacherService {
	/*添加教师信息*/
	function AddTeacher(Teacher $teacher) {
		$teacher_teacherNumber = $teacher->getTeacherNumber();
		$teacher_teacherName = $teacher->getTeacherName();
		$teacher_teacherPassword = $teacher->getTeacherPassword();
		$teacher_teacherSex = $teacher->getTeacherSex();
		$teacher_teacherBirthday = $teacher->getTeacherBirthday();
		$teacher_teacherArriveDate = $teacher->getTeacherArriveDate();
		$teacher_teacherCardNumber = $teacher->getTeacherCardNumber();
		$teacher_teacherPhone = $teacher->getTeacherPhone();
		$teacher_teacherPhoto = $teacher->getTeacherPhoto();
		$teacher_teacherAddress = $teacher->getTeacherAddress();
		$teacher_teacherMemo = $teacher->getTeacherMemo();

		// 构建sql语句
		$sql = "insert into t_Teacher(teacherNumber,teacherName,teacherPassword,teacherSex,teacherBirthday,teacherArriveDate,teacherCardNumber,teacherPhone,teacherPhoto,teacherAddress,teacherMemo) values ('$teacher_teacherNumber','$teacher_teacherName','$teacher_teacherPassword','$teacher_teacherSex','$teacher_teacherBirthday','$teacher_teacherArriveDate','$teacher_teacherCardNumber','$teacher_teacherPhone','$teacher_teacherPhoto','$teacher_teacherAddress','$teacher_teacherMemo')";
		//调用数据层执行添加
		$sqlHelper = new SqlHelper ();
		$res = $sqlHelper->execute_dml ( $sql );
		$sqlHelper->close_connect ();
		return $res;
	}

	/*更新教师信息信息*/
	function UpdateTeacher(Teacher $teacher) {
		$teacher_teacherNumber = $teacher->getTeacherNumber();
		$teacher_teacherName = $teacher->getTeacherName();
		$teacher_teacherPassword = $teacher->getTeacherPassword();
		$teacher_teacherSex = $teacher->getTeacherSex();
		$teacher_teacherBirthday = $teacher->getTeacherBirthday();
		$teacher_teacherArriveDate = $teacher->getTeacherArriveDate();
		$teacher_teacherCardNumber = $teacher->getTeacherCardNumber();
		$teacher_teacherPhone = $teacher->getTeacherPhone();
		$teacher_teacherPhoto = $teacher->getTeacherPhoto();
		$teacher_teacherAddress = $teacher->getTeacherAddress();
		$teacher_teacherMemo = $teacher->getTeacherMemo();
		$sql = "update t_Teacher set ";
		$sql = $sql."teacherName='$teacher_teacherName'";
		$sql = $sql.",teacherPassword='$teacher_teacherPassword'";
		$sql = $sql.",teacherSex='$teacher_teacherSex'";
		$sql = $sql.",teacherBirthday='$teacher_teacherBirthday'";
		$sql = $sql.",teacherArriveDate='$teacher_teacherArriveDate'";
		$sql = $sql.",teacherCardNumber='$teacher_teacherCardNumber'";
		$sql = $sql.",teacherPhone='$teacher_teacherPhone'";
		$sql = $sql.",teacherPhoto='$teacher_teacherPhoto'";
		$sql = $sql.",teacherAddress='$teacher_teacherAddress'";
		$sql = $sql.",teacherMemo='$teacher_teacherMemo'";
		$sql = $sql." where teacherNumber='$teacher_teacherNumber'";
		//调用数据层执行更新操作
		$sqlHelper = new SqlHelper (); 
		$res = $sqlHelper->execute_dml ( $sql );
		$sqlHelper->close_connect ();
		return $res;
	}

	// 根据教师编号获取教师信息
	function GetTeacher($teacherNumber) {
		$sql = "select * from t_Teacher where teacherNumber='$teacherNumber'";
		$sqlHelper = new SqlHelper ();
		$arr = $sqlHelper->execute_dql2 ( $sql );
		$sqlHelper->close_connect ();
		// 二次封装,$arr=>Teacher对象实例
		$teacher = new Teacher();
		if(count($arr)>0) {
			$teacher->setTeacherNumber($arr [0] ['teacherNumber']);
			$teacher->setTeacherName($arr [0] ['teacherName']);
			$teacher->setTeacherPassword($arr [0] ['teacherPassword']);
			$teacher->setTeacherSex($arr [0] ['teacherSex']);
			$teacher->setTeacherBirthday($arr [0] ['teacherBirthday']);
			$teacher->setTeacherArriveDate($arr [0] ['teacherArriveDate']);
			$teacher->setTeacherCardNumber($arr [0] ['teacherCardNumber']);
			$teacher->setTeacherPhone($arr [0] ['teacherPhone']);
			$teacher->setTeacherPhoto($arr [0] ['teacherPhoto']);
			$teacher->setTeacherAddress($arr [0] ['teacherAddress']);
			$teacher->setTeacherMemo($arr [0] ['teacherMemo']);
		}
		return $teacher;
	}

	/*用封装的方式完成分页查询*/
	function getFenyePage($fenyePage,$teacherNumber,$teacherName,$teacherBirthday,$teacherArriveDate) {
		// 创建一个SqlHelper对象实例
		$sqlHelper = new SqlHelper ();
		//根据查询条件构造sql语句
		$sql1 = "select * from t_Teacher where 1=1";
		$sql2 = "select count(teacherNumber) from t_Teacher where 1=1";
		$condition = "";
		if($teacherNumber != "") $condition = $condition." and teacherNumber like '%$teacherNumber%'";
		if($teacherName != "") $condition = $condition." and teacherName like '%$teacherName%'";
		if($teacherBirthday != "") $condition = $condition." and teacherBirthday='$teacherBirthday'";
		if($teacherArriveDate != "") $condition = $condition." and teacherArriveDate='$teacherArriveDate'";
		$sql1 = $sql1.$condition." limit ". ($fenyePage->pageNow - 1) * $fenyePage->pageSize . "," . $fenyePage->pageSize;
		$sql2 = $sql2.$condition;
		//调用数据层执行查询操作
		$sqlHelper->execute_dql_fenye ( $sql1, $sql2, $fenyePage );
		$sqlHelper->close_connect ();
	}

	// 根据教师编号删除某个教师信息
	function DeleteTeacher($teacherNumber) {
		$sql = "delete from t_Teacher where teacherNumber='$teacherNumber'";
		// 创建SqlHelper对象实例执行删除
		$sqlHelper = new SqlHelper ();
		return $sqlHelper->execute_dml ( $sql );
	}

	/*查询所有的教师信息*/
	function QueryAllTeacher() {
		$sql = "select teacherNumber,teacherName from t_Teacher";
		$sqlHelper = new SqlHelper ();
		$res = $sqlHelper->execute_dql2 ( $sql );
		// 关闭连接
		$sqlHelper->close_connect ();
		return $res;
	}
}

?>
