<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper
        PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">

<mapper namespace="cn.ftebox.dao.CommentDao">
    <resultMap id="CommentResult" type="Comment">
        <result property="id" column="id"/>
        <result property="userIp" column="userIp"/>
        <result property="content" column="content"/>
        <result property="commentDate" column="commentDate"/>
        <result property="state" column="state"/>
        <association property="blog" column="blogId" select="cn.ftebox.dao.BlogDao.findById"/>
    </resultMap>

    <insert id="add" parameterType="Comment">
        insert into t_comment
        values (null, #{userIp}, #{blog.id}, #{content}, now(), 0)
    </insert>

    <update id="update" parameterType="Comment">
        update t_comment
        <set>
            <if test="state!=null">
                state=#{state},
            </if>
        </set>
        where id=#{id}
    </update>

    <select id="list" parameterType="Map" resultMap="CommentResult">
        select * from t_comment
        <where>
            <if test="blogId!=null">
                and blogId=#{blogId}
            </if>
            <if test="state!=null">
                and state=#{state}
            </if>
        </where>
        order by commentDate
        <if test="start!=null and size!=null">
            limit #{start},#{size}
        </if>
    </select>

    <select id="getTotal" parameterType="Map" resultType="Long">
        select count(*) from t_comment
        <where>
            <if test="state!=null">
                and state=#{state}
            </if>
        </where>
    </select>

    <delete id="delete" parameterType="Integer">
        delete
        from t_comment
        where id = #{id}
    </delete>

    <delete id="deleteByBlogId" parameterType="Integer">
        delete
        from t_comment
        where blogId = #{blogId}
    </delete>
</mapper>