<?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.BlogDao">
    <resultMap id="BlogResult" type="Blog">
        <result property="id" column="id"/>
        <result property="title" column="title"/>
        <result property="summary" column="summary"/>
        <result property="releaseDate" column="releaseDate"/>
        <result property="clickHit" column="clickHit"/>
        <result property="replyHit" column="replyHit"/>
        <result property="content" column="content"/>
        <result property="keyWord" column="keyWord"/>
        <association property="blogType" column="typeId" select="cn.ftebox.dao.BlogTypeDao.findById"/>
    </resultMap>

    <select id="countList" resultMap="BlogResult">
        select date_format(releaseDate, '%Y年%m月') as releaseDataStr, count(*) as blogCount
        from t_blog
        group by date_format(releaseDate, '%Y年%m月')
        order by date_format(releaseDate, '%Y年%m月') desc
    </select>

    <select id="list" parameterType="Map" resultMap="BlogResult">
        select * from t_blog
        <where>
            <if test="title!=null and title!=''">
                and title like #{title}
            </if>
            <if test="typeId!=null and typeId!=''">
                and typeId = #{typeId}
            </if>
            <if test="releaseDateStr!=null and releaseDateStr!=''">
                and date_fromat(releaseDate,'%Y年%m月')=#{releaseDateStr}
            </if>
        </where>
        order by releaseDate desc
        <if test="start!=null and size!=null">
            limit #{start},#{size}
        </if>
    </select>

    <select id="getTotal" parameterType="Map" resultType="long">
        select count(*)
        from t_blog
        <where>
            <if test="title!=null and title!=''">
                and title like #{title}
            </if>
            <if test="typeId!=null and typeId!=''">
                and typeId = #{typeId}
            </if>
            <if test="releaseDateStr!=null and releaseDateStr!=''">
                and date_fromat(releaseDate,'%Y年%m月')=#{releaseDateStr}
            </if>
        </where>
    </select>

    <select id="findById" parameterType="Integer" resultMap="BlogResult">
        select *
        from t_blog
        where id = #{id}
    </select>

    <insert id="add" useGeneratedKeys="true" keyProperty="id" parameterType="Blog">
        insert into t_blog
        values (null, #{title}, #{summary}, now(), 0, 0, #{content}, #{blogType.id}, #{keyWord})
    </insert>

    <update id="update" parameterType="Blog">
        update t_blog
        <set>
            <if test="title!=null and title!=''">
                title=#{title},
            </if>
            <if test="summary!=null and summary!=''">
                summary=#{summary},
            </if>
            <if test="content!=null and content!=''">
                content=#{content},
            </if>
            <if test="blogType.id!=null">
                typeId=#{blogType.id},
            </if>
            <if test="clickHit!=null">
                clickHit=#{clickHit},
            </if>
            <if test="replyHit!=null">
                replyHit=#{replyHit},
            </if>
            <if test="keyWord!=null and keyWord!=''">
                keyWord=#{keyWord},
            </if>
        </set>
        where id=#{id}
    </update>

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

    <select id="getBlogByTypeId" parameterType="Integer" resultType="Integer">
        select count(*)
        from t_blog
        where typeId = #{typeId}
    </select>

    <select id="getLastBlog" parameterType="Integer" resultMap="BlogResult">
        select *
        from t_blog
        where id &lt; #{id}
        order by id desc
        limit 1
    </select>

    <select id="getNextBlog" parameterType="Integer" resultMap="BlogResult">
        select *
        from t_blog
        where id &gt; #{id}
        order by id
        limit 1
    </select>
</mapper>