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

/*新闻信息管理业务层类*/
class NewsService {
	/*添加新闻信息*/
	function AddNews(News $news) {
		$news_newsTitle = $news->getNewsTitle();
		$news_newsContent = $news->getNewsContent();
		$news_newsDate = $news->getNewsDate();
		$news_newsPhoto = $news->getNewsPhoto();

		// 构建sql语句
		$sql = "insert into t_News(newsTitle,newsContent,newsDate,newsPhoto) values ('$news_newsTitle','$news_newsContent','$news_newsDate','$news_newsPhoto')";
		//调用数据层执行添加
		$sqlHelper = new SqlHelper ();
		$res = $sqlHelper->execute_dml ( $sql );
		$sqlHelper->close_connect ();
		return $res;
	}

	/*更新新闻信息信息*/
	function UpdateNews(News $news) {
		$news_newsId = $news->getNewsId();
		$news_newsTitle = $news->getNewsTitle();
		$news_newsContent = $news->getNewsContent();
		$news_newsDate = $news->getNewsDate();
		$news_newsPhoto = $news->getNewsPhoto();
		$sql = "update t_News set ";
		$sql = $sql."newsTitle='$news_newsTitle'";
		$sql = $sql.",newsContent='$news_newsContent'";
		$sql = $sql.",newsDate='$news_newsDate'";
		$sql = $sql.",newsPhoto='$news_newsPhoto'";
		$sql = $sql." where newsId=$news_newsId";
		//调用数据层执行更新操作
		$sqlHelper = new SqlHelper (); 
		$res = $sqlHelper->execute_dml ( $sql );
		$sqlHelper->close_connect ();
		return $res;
	}

	// 根据记录编号获取新闻信息
	function GetNews($newsId) {
		$sql = "select * from t_News where newsId=$newsId";
		$sqlHelper = new SqlHelper ();
		$arr = $sqlHelper->execute_dql2 ( $sql );
		$sqlHelper->close_connect ();
		// 二次封装,$arr=>News对象实例
		$news = new News();
		if(count($arr)>0) {
			$news->setNewsId($arr [0] ['newsId']);
			$news->setNewsTitle($arr [0] ['newsTitle']);
			$news->setNewsContent($arr [0] ['newsContent']);
			$news->setNewsDate($arr [0] ['newsDate']);
			$news->setNewsPhoto($arr [0] ['newsPhoto']);
		}
		return $news;
	}

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

	/*查询所有的新闻信息*/
	function QueryAllNews() {
		$sql = "select newsId,newsTitle from t_News";
		$sqlHelper = new SqlHelper ();
		$res = $sqlHelper->execute_dql2 ( $sql );
		// 关闭连接
		$sqlHelper->close_connect ();
		return $res;
	}
}

?>
