<?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="com.toesbieya.my.mapper.SysCategoryMapper">
    <sql id="searchCondition">
        <if test="id!=null">and id=#{id}</if>
        <if test="ids!=null and ids!=''">and id in (${ids})</if>
        <if test="pid!=null">and pid=#{pid}</if>
        <if test="pids!=null and pids!=''">and pid in (${pids})</if>
        <if test="name!=null and name!=''">and name like "%"#{name}"%"</if>
        <if test="type!=null">and type=#{type}</if>
        <if test="startTime!=null">and ctime <![CDATA[ >= ]]> #{startTime}</if>
        <if test="endTime!=null">and ctime <![CDATA[ <= ]]> #{endTime}</if>
    </sql>
    <insert id="add" parameterType="com.toesbieya.my.model.entity.SysCategory">
        insert into sys_category (pid, name, type, ctime)
        values (#{pid}, #{name}, #{type}, #{ctime})
    </insert>
    <update id="update" parameterType="com.toesbieya.my.model.entity.SysCategory">
        update sys_category
        set pid=#{pid},
        name=#{name},
        type=#{type}
        where id = #{id}
    </update>
    <delete id="del">
        delete
        from sys_category
        where id = #{id}
    </delete>
    <select id="getAll" resultType="com.toesbieya.my.model.entity.SysCategory">
        select *
        from sys_category
    </select>
    <select id="search" resultType="com.toesbieya.my.model.entity.SysCategory"
            parameterType="com.toesbieya.my.model.vo.search.CategorySearch">
        select *
        from sys_category
        where 1
        <include refid="searchCondition"/>
        order by ctime desc
    </select>
    <select id="getById" resultType="com.toesbieya.my.model.entity.SysCategory">
        select *
        from sys_category
        where id = #{id}
    </select>
    <select id="isNameExist" resultType="java.lang.Boolean">
        select if(count(0) > 0, 1, 0)
        from sys_category
        where name = #{name}
        <if test="id!=null">
            and id!=#{id}
        </if>
    </select>
    <select id="hasChildren" resultType="java.lang.Boolean">
        select if(count(0) > 0, 1, 0)
        from sys_category
        where pid = #{id}
    </select>
    <select id="checkIsUse" resultType="java.lang.Boolean">
        select check_category_use(#{cid}) from dual
    </select>
</mapper>