<?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.yonge.cooleshow.bbs.dao.BbsArticleDao">
	<resultMap id="BaseResultMap" type="com.yonge.cooleshow.bbs.entity.BbsArticle">
            <result column="id_" property="id" />
	        <result column="user_id_" property="userId" />
	        <result column="title_" property="title" />
	        <result column="content_" property="content" />
	        <result column="label_id_" property="labelId" />
	        <result column="label_name_" property="labelName" />
	        <result column="views_count_" property="viewsCount" />
	        <result column="likes_count_" property="likesCount" />
	        <result column="comments_count_" property="commentsCount" />
	        <result column="collects_count_" property="collectsCount" />
	        <result column="status_" property="status" />
	        <result column="top_flag_" property="topFlag" />
	        <result column="reply_flag_" property="replyFlag" />
	        <result column="examine_time_" property="examineTime" />
	        <result column="created_time_" property="createdTime" />
	        <result column="updated_time_" property="updatedTime" />
	        <result column="del_flag_" property="delFlag" />
		</resultMap>

    <!-- 表字段 -->
    <sql id="baseColumns">
         t.id_ as id
        , t.user_id_ as userId
        , t.title_ as title
        , t.content_ as content
        , t.label_id_ as labelId
        , t.label_name_ as labelName
        , t.views_count_ as viewsCount
        , t.likes_count_ as likesCount
        , t.comments_count_ as commentsCount
        , t.collects_count_ as collectsCount
        , t.status_ as status
        , t.top_flag_ as topFlag
        , t.reply_flag_ as replyFlag
        , t.examine_time_ as examineTime
        , t.created_time_ as createdTime
        , t.updated_time_ as updatedTime
        , t.del_flag_ as delFlag
        </sql>

    <insert id="addArticle" useGeneratedKeys="true" keyColumn="id" keyProperty="id">
        INSERT INTO bbs_article
        (user_id_,title_,content_,label_id_,label_name_,status_,reply_flag_)
        VALUES(#{userId},#{title},#{content},#{labelId},#{labelName},#{status},#{replyFlag})
    </insert>
    <update id="deleteArticle" parameterType="java.util.List">
        UPDATE bbs_article SET del_flag_=1 WHERE id_ IN
        <foreach collection="list" item="item" open="(" separator="," close=")">
            #{item}
        </foreach>
    </update>
    <select id="selectPage" resultType="com.yonge.cooleshow.bbs.vo.BbsArticleVo">
		SELECT         
        	<include refid="baseColumns" />
		FROM bbs_article t
        <where>
            <if test="param.status != null ">
                and #{param.status} = t.status_
            </if>
            <if test="param.search != null and param.search != ''">
                and t.title_ like  concat('%',#{param.search},'%')
            </if>
            <if test="param.label != null">
                and t.label_id_ = #{param.label}
            </if>
            <if test="param.userId != null" >
                and t.user_id_ = #{param.userId}
            </if>
            <if test="param.delFlag != null" >
                and t.del_flag_ = #{param.delFlag}
            </if>
        </where>
        order by find_in_set(t.top_flag_,1),
            <if test="param.sort != null">
                <choose>
                    <when test="param.sort == 'HOT'">
                        t.comments_count_ desc,
                    </when>
                </choose>
            </if>

        t.created_time_ desc
	</select>
    <select id="articleInfo" resultType="com.yonge.cooleshow.bbs.vo.BbsArticleVo">
        SELECT
            t.id_ as id
             , t.user_id_ as userId
             , u.username_ as userName
             , u.real_name_ as realName
             , u.avatar_ as avatar
             , t.title_ as title
             , t.content_ as content
             , t.label_id_ as labelId
             , b.label_name_ as labelName
             , t.views_count_ as viewsCount
             , t.likes_count_ as likesCount
             , t.comments_count_ as commentsCount
             , t.collects_count_ as collectsCount
             , t.status_ as status
             , t.top_flag_ as topFlag
             , t.reply_flag_ as replyFlag
             , t.examine_time_ as examineTime
             , t.created_time_ as createdTime
             , t.updated_time_ as updatedTime
             , t.del_flag_ as delFlag
        FROM bbs_article t
        LEFT JOIN bbs_config_label b ON t.label_id_ = b.id_
        LEFT JOIN sys_user u ON t.user_id_ = u.id_
    </select>
    <select id="articleReply" resultType="com.yonge.cooleshow.bbs.vo.BbsReplyVo">
        SELECT
            r.*,
            u.username_ AS userName,
            u.real_name_ AS realName,
            u.avatar_ AS avatar
        FROM bbs_reply r
        LEFT JOIN sys_user u ON r.user_id_=u.id_
        <where>
            <if test="param.articleId !=null">
               AND r.article_id=#{param.articleId}
            </if>
            <if test="param.status !=null">
                AND r.status_=#{param.status}
            </if>
            <if test="param.parentId !=null">
                AND r.parent_id_=#{param.parentId}
            </if>
        </where>
    </select>
</mapper>