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

/*学生信息管理业务层类*/
class StudentService {
	/*添加学生信息*/
	function AddStudent(Student $student) {
		$student_studentNumber = $student->getStudentNumber();
		$student_studentName = $student->getStudentName();
		$student_studentPassword = $student->getStudentPassword();
		$student_studentSex = $student->getStudentSex();
		$student_studentClassNumber = $student->getStudentClassNumber();
		$student_studentBirthday = $student->getStudentBirthday();
		$student_studentState = $student->getStudentState();
		$student_studentPhoto = $student->getStudentPhoto();
		$student_studentTelephone = $student->getStudentTelephone();
		$student_studentEmail = $student->getStudentEmail();
		$student_studentQQ = $student->getStudentQQ();
		$student_studentAddress = $student->getStudentAddress();
		$student_studentMemo = $student->getStudentMemo();

		// 构建sql语句
		$sql = "insert into t_Student(studentNumber,studentName,studentPassword,studentSex,studentClassNumber,studentBirthday,studentState,studentPhoto,studentTelephone,studentEmail,studentQQ,studentAddress,studentMemo) values ('$student_studentNumber','$student_studentName','$student_studentPassword','$student_studentSex','$student_studentClassNumber','$student_studentBirthday','$student_studentState','$student_studentPhoto','$student_studentTelephone','$student_studentEmail','$student_studentQQ','$student_studentAddress','$student_studentMemo')";
		//调用数据层执行添加
		$sqlHelper = new SqlHelper ();
		$res = $sqlHelper->execute_dml ( $sql );
		$sqlHelper->close_connect ();
		return $res;
	}

	/*更新学生信息信息*/
	function UpdateStudent(Student $student) {
		$student_studentNumber = $student->getStudentNumber();
		$student_studentName = $student->getStudentName();
		$student_studentPassword = $student->getStudentPassword();
		$student_studentSex = $student->getStudentSex();
		$student_studentClassNumber = $student->getStudentClassNumber();
		$student_studentBirthday = $student->getStudentBirthday();
		$student_studentState = $student->getStudentState();
		$student_studentPhoto = $student->getStudentPhoto();
		$student_studentTelephone = $student->getStudentTelephone();
		$student_studentEmail = $student->getStudentEmail();
		$student_studentQQ = $student->getStudentQQ();
		$student_studentAddress = $student->getStudentAddress();
		$student_studentMemo = $student->getStudentMemo();
		$sql = "update t_Student set ";
		$sql = $sql."studentName='$student_studentName'";
		$sql = $sql.",studentPassword='$student_studentPassword'";
		$sql = $sql.",studentSex='$student_studentSex'";
		$sql = $sql.",studentClassNumber='$student_studentClassNumber'";
		$sql = $sql.",studentBirthday='$student_studentBirthday'";
		$sql = $sql.",studentState='$student_studentState'";
		$sql = $sql.",studentPhoto='$student_studentPhoto'";
		$sql = $sql.",studentTelephone='$student_studentTelephone'";
		$sql = $sql.",studentEmail='$student_studentEmail'";
		$sql = $sql.",studentQQ='$student_studentQQ'";
		$sql = $sql.",studentAddress='$student_studentAddress'";
		$sql = $sql.",studentMemo='$student_studentMemo'";
		$sql = $sql." where studentNumber='$student_studentNumber'";
		//调用数据层执行更新操作
		$sqlHelper = new SqlHelper (); 
		$res = $sqlHelper->execute_dml ( $sql );
		$sqlHelper->close_connect ();
		return $res;
	}

	// 根据学号获取学生信息
	function GetStudent($studentNumber) {
		$sql = "select * from t_Student where studentNumber='$studentNumber'";
		$sqlHelper = new SqlHelper ();
		$arr = $sqlHelper->execute_dql2 ( $sql );
		$sqlHelper->close_connect ();
		// 二次封装,$arr=>Student对象实例
		$student = new Student();
		if(count($arr)>0) {
			$student->setStudentNumber($arr [0] ['studentNumber']);
			$student->setStudentName($arr [0] ['studentName']);
			$student->setStudentPassword($arr [0] ['studentPassword']);
			$student->setStudentSex($arr [0] ['studentSex']);
			$student->setStudentClassNumber($arr [0] ['studentClassNumber']);
			$student->setStudentBirthday($arr [0] ['studentBirthday']);
			$student->setStudentState($arr [0] ['studentState']);
			$student->setStudentPhoto($arr [0] ['studentPhoto']);
			$student->setStudentTelephone($arr [0] ['studentTelephone']);
			$student->setStudentEmail($arr [0] ['studentEmail']);
			$student->setStudentQQ($arr [0] ['studentQQ']);
			$student->setStudentAddress($arr [0] ['studentAddress']);
			$student->setStudentMemo($arr [0] ['studentMemo']);
		}
		return $student;
	}

	/*用封装的方式完成分页查询*/
	function getFenyePage($fenyePage,$studentNumber,$studentName,$studentClassNumber,$studentBirthday) {
		// 创建一个SqlHelper对象实例
		$sqlHelper = new SqlHelper ();
		//根据查询条件构造sql语句
		$sql1 = "select * from t_Student where 1=1";
		$sql2 = "select count(studentNumber) from t_Student where 1=1";
		$condition = "";
		if($studentNumber != "") $condition = $condition." and studentNumber like '%$studentNumber%'";
		if($studentName != "") $condition = $condition." and studentName like '%$studentName%'";
		if($studentClassNumber != "") $condition = $condition." and studentClassNumber='$studentClassNumber'";
		if($studentBirthday != "") $condition = $condition." and studentBirthday='$studentBirthday'";
		$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 DeleteStudent($studentNumber) {
		$sql = "delete from t_Student where studentNumber='$studentNumber'";
		// 创建SqlHelper对象实例执行删除
		$sqlHelper = new SqlHelper ();
		return $sqlHelper->execute_dml ( $sql );
	}

	/*查询所有的学生信息*/
	function QueryAllStudent() {
		$sql = "select studentNumber,studentName from t_Student";
		$sqlHelper = new SqlHelper ();
		$res = $sqlHelper->execute_dql2 ( $sql );
		// 关闭连接
		$sqlHelper->close_connect ();
		return $res;
	}
}

?>
