<?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.CategoryMapper">
    <resultMap id="categoryResultMap" type="cn.celess.common.entity.Category">
        <id column="t_id" property="id"/>
        <result column="t_name" property="name"/>
        <result column="is_category" property="category"/>
        <result column="is_delete" property="deleted"/>
    </resultMap>

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

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

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

    <select id="findCategoryByName" resultMap="categoryResultMap">
        select *
        from tag_category
        where t_name = #{name}
          and is_category = true
    </select>

    <select id="findCategoryById" resultMap="categoryResultMap">
        select *
        from tag_category
        where t_id = #{id}
          and is_category = true
    </select>

    <select id="findAll" resultMap="categoryResultMap">
        select *
        from tag_category
        where is_category = true
    </select>

    <select id="getAllName" resultType="java.lang.String">
        select t_name
        from tag_category
        where is_category = true
    </select>

    <select id="getNameById" resultType="java.lang.String">
        select t_name
        from tag_category
        where is_category = true
          and t_id = #{id}
    </select>

    <select id="getIdByName" resultType="java.lang.Long">
        select t_id
        from tag_category
        where is_category = true
          and t_name = #{name}
    </select>

    <select id="existsByName" resultType="java.lang.Boolean">
        SELECT EXISTS(SELECT * FROM tag_category WHERE t_name = #{name} and is_category = true)
    </select>

    <select id="existsById" resultType="java.lang.Boolean">
        SELECT EXISTS(SELECT * FROM tag_category WHERE t_id = #{id})
    </select>

    <select id="getLastestCategory" resultMap="categoryResultMap">
        select *
        from tag_category
        where is_category = true
        order by t_id desc
        limit 1;
    </select>

    <select id="count" resultType="java.lang.Long">
        select count(*)
        from tag_category
        where is_category = true
          and is_delete = false;
    </select>

</mapper>