<?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.api.app.dao.butler.AppNewsDao">

    <select id="findAllCategory" resultType="com.api.vo.app.IdAndName">
        select id,name from sys_news_category
    </select>

    <select id="newsList" parameterType="com.api.model.app.SearchAppNews" resultType="com.api.vo.app.AppNewsVo">
        select id,title,create_date,IFNULL(views,0) as views from sys_news
        <where>
            <if test="newsCategoryId != null">
                news_category_id = #{newsCategoryId}
            </if>
        </where>
        order by create_date desc
    </select>
    <select id="findNewsByNewsId" parameterType="java.lang.Integer" resultType="com.api.vo.app.AppNewsVoFBI">
        select sn.id,sn.code,sn.title,sn.content,snc.name as news_category_name,
        (case sn.create_id = -1 when true then '外部发布' else su.actual_name end) as create_name,sn.create_date
        from sys_news_category as snc,sys_news as sn
		LEFT JOIN sys_user as su on sn.create_id = su.id
        where sn.news_category_id = snc.id and sn.id = #{newsId}
    </select>
    <select id="findNewsRotation" resultType="com.api.vo.app.AppNewsRotationVo">
        select id as news_id,title as title from sys_news where is_rotation = 1 order by rotation_weight desc
    </select>
    <select id="findHotNews" resultType="com.api.vo.app.AppNewsVo">
        select id,title,create_date,IFNULL(views,0) as views from sys_news
        order by views desc
    </select>
    <update id="addViews" parameterType="java.lang.Integer">
        update sys_news
        set views =  IFNULL(views,0)  + 1
        where id = #{newsId}
    </update>


</mapper>