<?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">
<mapper namespace="com.caimei.www.mapper.ArticleDao">
    <select id="getArticleTypes" resultType="com.caimei.www.pojo.page.BaseLink">
		select  a.id,
				a.name,
				a.sort
		from info_type a
		where a.enabledStatus = 1
		order by a.sort desc
	</select>
    <select id="getTopClickRate" resultType="java.lang.Integer">
		select IFNULL(clickRate, 1)
		from info_label
		where hotLabelStatus = 1
		order by clickRate desc
		limit 1
    </select>
    <select id="getArticleLabels" resultType="com.caimei.www.pojo.page.BaseLink">
		select id,name,clickRate as sort,link,jumpStatus as typeId
		from info_label
		where hotLabelStatus = 1
		order by recommendLabelStatus desc, hotLabelStatus desc, createDate desc
		limit 30
    </select>
    <select id="getArticleRecommended" resultType="com.caimei.www.pojo.page.ImageLink">
		select  a.id,
				a.title,
				a.guidanceImage as image
		from info a
		where a.recommendStatus = 1
		and a.enabledStatus = 1
		and NOW() >= a.pubdate
		<if test="typeId != null and typeId != ''">
			and a.typeId = #{typeId}
		</if>
		order by a.pubdate desc
	</select>
    <select id="getArticleRelated" resultType="com.caimei.www.pojo.page.Article">
        select
			a.id as id,
			a.title as title,
			a.guidanceImage as image,
			a.publisher as publisher,
			a.pubdate as publish_date,
			a.recommendContent as intro,
			a.infoContent as content,
			(IFNULL(c.pv, 0) + IFNULL(a.basePv, 0) + IFNULL((c.num + a.basePraise), 0)) as pv,
			a.label as label,
			a.typeId as typeId
        from info as a
		left join info_praise c on a.id = c.infoId
		<where>
			<if test="labels != null and labels != ''">
				<foreach collection="labels" item="label" open="(" close=")" index="index" separator="OR">
					a.label like CONCAT(CONCAT('%', #{label}), '%')
				</foreach>
			</if>
			<if test="id != null and id != ''">
				and a.id != #{id}
			</if>
			and NOW() >= a.pubdate
			and a.enabledStatus = 1
		</where>
		order by a.pubdate desc
	</select>
	<select id="getLastestInfoADs" resultType="com.caimei.www.pojo.page.ImageLink">
		select a.id, a.serviceObject as title, a.guidanceImage as image, a.link
		from info_ad a
		where a.location = 2
			and a.enabledStatus = 1
		order by a.createDate desc
		limit 3
	</select>
    <update id="clickArticleAd">
		update info_ad
		set clickRate = IFNULL(clickRate, 0) + 1,
			updateDate = NOW()
		where id = #{id}
	</update>
	<update id="clickArticleLabel">
		update info_label
		set clickRate = IFNULL(clickRate, 0) + 1,
			updateDate = NOW()
		where id = #{id}
	</update>

	<select id="getArticleInfo" resultType="com.caimei.www.pojo.page.Article">
		SELECT
		  a.id AS id,
		  a.title AS title,
		  a.guidanceImage AS image,
		  a.publisher AS publisher,
		  a.pubdate AS publish_date,
		  a.recommendContent AS intro,
		  a.infoContent AS content,
		  (
			IFNULL(c.pv, 0) + IFNULL(a.basePv, 0) + IFNULL(
			  (c.num + a.basePraise),
			  0
			)
		  ) AS pv,
		  a.label AS label,
		  a.typeId AS typeId,
		  a.keyword AS keyword,
		  a.recommendContent AS recommendContent,
		  a.source AS source,
		  IFNULL((c.num + a.basePraise), 0) AS likes
		FROM
		  info AS a
		  LEFT JOIN info_praise c ON a.id = c.infoId
		WHERE
		  a.id = #{id}
		  AND a.enabledStatus = 1
	</select>


</mapper>
