<?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="cn.celess.common.mapper.TagMapper">

    <resultMap id="tagResultMap" type="cn.celess.common.entity.Tag"
               extends="cn.celess.common.mapper.CategoryMapper.categoryResultMap">
    </resultMap>

    <insert id="insert" useGeneratedKeys="true" keyProperty="id">
        insert into tag_category (t_name, is_category)
        VALUES (#{name}, false);
    </insert>

    <update id="update">
        update tag_category
        set t_name=#{name}
        where t_id = #{id}
          and is_category = false;
    </update>

    <update id="delete">
        update tag_category
        set is_delete = true
        where t_id = #{id}
          and is_category = false;
    </update>

    <select id="findTagById" resultMap="tagResultMap">
        select *
        from tag_category
        where t_id = #{id}
          and is_category = false;
    </select>

    <select id="findTagByName" resultMap="tagResultMap">
        select *
        from tag_category
        where t_name = #{name}
          and is_category = false;
    </select>

    <select id="existsByName" resultType="boolean">
        SELECT EXISTS(SELECT * FROM tag_category WHERE t_name = #{name} and is_category = false)
    </select>

    <select id="getLastestTag" resultMap="tagResultMap">
        select *
        from tag_category
        where is_category = false
        order by t_id desc
        limit 1
    </select>

    <select id="findAll" resultMap="tagResultMap">
        select *
        from tag_category
        where is_category = false
    </select>

    <select id="count" resultType="long">
        select count(*)
        from tag_category
        where is_category = false
          and is_delete = false;
    </select>
</mapper>