<?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.BlogTypeDao">
    <resultMap id="BlogTypeResult" type="BlogType">
        <result property="id" column="id"/>
        <result property="typeName" column="typeName"/>
        <result property="orderNo" column="orderNo"/>
        <result property="blogCount" column="blogCount"/>
    </resultMap>
    <select id="countList" parameterType="String" resultMap="BlogTypeResult">
        select t2.id, t2.typeName, count(t1.id) as blogCount
        from t_blog t1
                 right join t_blogtype t2 on t1.typeId = t2.id
        group by t2.typeName
        order by t2.orderNo
    </select>

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

    <select id="list" parameterType="Map" resultMap="BlogTypeResult">
        select * from t_blogtype
        order by orderNo
        <if test="typeName!=null and typeName!=''">
            where typeName = #{typeName}
        </if>
        <if test="start!=null and size!=null">
            limit #{start},${size}
        </if>
    </select>

    <select id="getTotal" parameterType="Integer" resultType="long">
        select count(*)
        from t_blogType
    </select>

    <insert id="add" parameterType="BlogType">
        insert into t_blogtype(typeName, orderNo)
        values (#{typeName}, #{orderNo})
    </insert>

    <update id="update" parameterType="BlogType">
        update t_blogtype
        <set>
            <if test="typeName!=null and typeName!=''">
                typeName=#{typeName},
            </if>
            <if test="orderNo!=null and orderNo!=''">
                orderNo=#{orderNo},
            </if>
        </set>
        where id=#{id}
    </update>

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

</mapper>