<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper 
PUBLIC 
	"-//mybatis.org//DTD Mapper 3.0//EN" 
	"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<!-- 命名，每一个映射对象不一样
	namespace:必须与对应的接口全类名一致
 -->
<mapper namespace="com.iamberry.wechat.service.mapper.NewsMapper">
	<sql id="allNewsField">
		NEWS_ID newsId, 
		NEWS_TITLE newsTitle, 
		NEWS_CONTEXTS newsContexts, 
		NEWS_DESC newsDesc,
		NEWS_AUTHOR newsAuthor, 
		NEWS_IMAGE newsImage,
		NEWS_SOURCE newsSource,
		NEWS_ACCESS_NUM newsAccessNum,
		NEWS_SHARE_NUM newsShareNum,
		NEWS_SOURCE_URL newsSourceUrl,
		NEWS_CREATE_DATE newsCreateDate
	</sql>

	<!-- 获取所有的新闻内容 -->
	<select id="listAll" resultType="News">
		SELECT
			<include refid="allNewsField"/>
		FROM
			TB_IAMBERRY_WWW_NEWS
		ORDER BY
			NEWS_ID DESC
	</select>
	
	<!-- 获取单个的新闻内容 -->
	<select id="getOne" parameterType="Integer" resultType="News">
		SELECT
			<include refid="allNewsField"/>
		FROM
			TB_IAMBERRY_WWW_NEWS
		WHERE
			NEWS_ID = #{id}
	</select>
	
	<!-- 根据 id 修改信息 -->
	<update id="updateOne" parameterType="News">
		UPDATE 
			TB_IAMBERRY_WWW_NEWS
		SET 
			NEWS_TITLE = #{newsTitle},  
			NEWS_CONTEXTS = #{newsContexts}, 
			NEWS_DESC = #{newsDesc},
			NEWS_AUTHOR = #{newsAuthor},  
			NEWS_IMAGE = #{newsImage},
			NEWS_SOURCE = #{newsSource},
			NEWS_SOURCE_URL = #{newsSourceUrl}
		WHERE 
			NEWS_ID = #{newsId}
	</update>
	
	<!-- 递增访问量 -->
	<update id="updateAccess" parameterType="Integer">
		UPDATE 
			TB_IAMBERRY_WWW_NEWS
		SET 
			NEWS_ACCESS_NUM = (NEWS_ACCESS_NUM + 1)
		WHERE 
			NEWS_ID = #{newsId}
	</update>
	
	<!-- 递增分享量 -->
	<update id="updateShare" parameterType="Integer">
		UPDATE 
			TB_IAMBERRY_WWW_NEWS
		SET 
			NEWS_SHARE_NUM = (NEWS_SHARE_NUM + 1)
		WHERE 
			NEWS_ID = #{newsId}
	</update>
	
	<!-- 新增新闻 -->
	<insert id="addNews" parameterType="News">
		INSERT INTO 
			TB_IAMBERRY_WWW_NEWS(
				NEWS_TITLE, NEWS_CONTEXTS, NEWS_DESC, NEWS_AUTHOR, 
				NEWS_IMAGE, NEWS_SOURCE, NEWS_SOURCE_URL, NEWS_CREATE_DATE) 
		VALUES 
			(#{newsTitle}, #{newsContexts}, #{newsDesc}, #{newsAuthor}, #{newsImage}, #{newsSource}, #{newsSourceUrl}, NOW()) 
	</insert>

	<!-- 查询所有的产品并分页 -->
	<select id="listNews" resultType="News" parameterType="News">
		SELECT
			NEWS_ID newsId,
			NEWS_TITLE newsTitle,
			NEWS_DESC newsDesc,
			NEWS_AUTHOR newsAuthor,
			NEWS_IMAGE newsImage,
			NEWS_SOURCE newsSource,
			NEWS_ACCESS_NUM newsAccessNum,
			NEWS_SHARE_NUM newsShareNum,
			NEWS_SOURCE_URL newsSourceUrl,
			NEWS_CREATE_DATE newsCreateDate
		FROM TB_IAMBERRY_WWW_NEWS
		<where>
			<if test="newsTitle != null and newsTitle != ''">
				NEWS_TITLE LIKE CONCAT('%',#{newsTitle},'%')
			</if>
		</where>
		ORDER BY NEWS_CREATE_DATE desc
		<if test="page!=null and page.recordBegin>0 and page.pageSize>0 ">
			limit ${page.recordBegin},${page.pageSize}
		</if>
		<if test="page!=null and page.recordBegin==0 and page.pageSize>0 ">
			limit ${page.pageSize}
		</if>
	</select>


	<!-- 查询总数 -->
	<select id="getCountnews" resultType="Integer" parameterType="News">
		SELECT
			COUNT(NEWS_ID)
		FROM TB_IAMBERRY_WWW_NEWS
		<where>
			<if test="newsTitle != null and newsTitle != ''">
				NEWS_TITLE LIKE CONCAT('%',#{newsTitle},'%')
			</if>
		</where>
		ORDER BY NEWS_CREATE_DATE desc
	</select>

</mapper>
