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

/*课程信息管理业务层类*/
class CourseInfoService {
	/*添加课程信息*/
	function AddCourseInfo(CourseInfo $courseInfo) {
		$courseInfo_courseNumber = $courseInfo->getCourseNumber();
		$courseInfo_courseName = $courseInfo->getCourseName();
		$courseInfo_courseTeacher = $courseInfo->getCourseTeacher();
		$courseInfo_courseTime = $courseInfo->getCourseTime();
		$courseInfo_coursePlace = $courseInfo->getCoursePlace();
		$courseInfo_courseScore = $courseInfo->getCourseScore();
		$courseInfo_courseMemo = $courseInfo->getCourseMemo();

		// 构建sql语句
		$sql = "insert into t_CourseInfo(courseNumber,courseName,courseTeacher,courseTime,coursePlace,courseScore,courseMemo) values ('$courseInfo_courseNumber','$courseInfo_courseName','$courseInfo_courseTeacher','$courseInfo_courseTime','$courseInfo_coursePlace',$courseInfo_courseScore,'$courseInfo_courseMemo')";
		//调用数据层执行添加
		$sqlHelper = new SqlHelper ();
		$res = $sqlHelper->execute_dml ( $sql );
		$sqlHelper->close_connect ();
		return $res;
	}

	/*更新课程信息信息*/
	function UpdateCourseInfo(CourseInfo $courseInfo) {
		$courseInfo_courseNumber = $courseInfo->getCourseNumber();
		$courseInfo_courseName = $courseInfo->getCourseName();
		$courseInfo_courseTeacher = $courseInfo->getCourseTeacher();
		$courseInfo_courseTime = $courseInfo->getCourseTime();
		$courseInfo_coursePlace = $courseInfo->getCoursePlace();
		$courseInfo_courseScore = $courseInfo->getCourseScore();
		$courseInfo_courseMemo = $courseInfo->getCourseMemo();
		$sql = "update t_CourseInfo set ";
		$sql = $sql."courseName='$courseInfo_courseName'";
		$sql = $sql.",courseTeacher='$courseInfo_courseTeacher'";
		$sql = $sql.",courseTime='$courseInfo_courseTime'";
		$sql = $sql.",coursePlace='$courseInfo_coursePlace'";
		$sql = $sql.",courseScore=$courseInfo_courseScore";
		$sql = $sql.",courseMemo='$courseInfo_courseMemo'";
		$sql = $sql." where courseNumber='$courseInfo_courseNumber'";
		//调用数据层执行更新操作
		$sqlHelper = new SqlHelper (); 
		$res = $sqlHelper->execute_dml ( $sql );
		$sqlHelper->close_connect ();
		return $res;
	}

	// 根据课程编号获取课程信息
	function GetCourseInfo($courseNumber) {
		$sql = "select * from t_CourseInfo where courseNumber='$courseNumber'";
		$sqlHelper = new SqlHelper ();
		$arr = $sqlHelper->execute_dql2 ( $sql );
		$sqlHelper->close_connect ();
		// 二次封装,$arr=>CourseInfo对象实例
		$courseInfo = new CourseInfo();
		if(count($arr)>0) {
			$courseInfo->setCourseNumber($arr [0] ['courseNumber']);
			$courseInfo->setCourseName($arr [0] ['courseName']);
			$courseInfo->setCourseTeacher($arr [0] ['courseTeacher']);
			$courseInfo->setCourseTime($arr [0] ['courseTime']);
			$courseInfo->setCoursePlace($arr [0] ['coursePlace']);
			$courseInfo->setCourseScore($arr [0] ['courseScore']);
			$courseInfo->setCourseMemo($arr [0] ['courseMemo']);
		}
		return $courseInfo;
	}

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

	/*查询所有的课程信息*/
	function QueryAllCourseInfo() {
		$sql = "select courseNumber,courseName from t_CourseInfo";
		$sqlHelper = new SqlHelper ();
		$res = $sqlHelper->execute_dql2 ( $sql );
		// 关闭连接
		$sqlHelper->close_connect ();
		return $res;
	}
}

?>
