<?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.sundot.airport.system.mapper.BaseCheckCategoryMapper">

    <resultMap type="BaseCheckCategory" id="BaseCheckCategoryResult">
        <result property="tenantId" column="tenant_id"/>
        <result property="revision" column="revision"/>
        <result property="createBy" column="create_by"/>
        <result property="createTime" column="create_time"/>
        <result property="updateBy" column="update_by"/>
        <result property="updateTime" column="update_time"/>
        <result property="id" column="id"/>
        <result property="name" column="name"/>
        <result property="parentId" column="parent_id"/>
        <result property="ancestors" column="ancestors"/>
        <result property="level" column="level"/>
        <result property="orderNum" column="order_num"/>
        <result property="code" column="code"/>
        <result property="remark" column="remark"/>
    </resultMap>

    <sql id="selectBaseCheckCategoryVo">
        select tenant_id,
               revision,
               create_by,
               create_time,
               update_by,
               update_time,
               id,
               name,
               parent_id,
               ancestors,
               level,
               order_num,
               code,
               remark
        from base_check_category
    </sql>

    <select id="selectBaseCheckCategoryList" parameterType="BaseCheckCategory" resultMap="BaseCheckCategoryResult">
        <include refid="selectBaseCheckCategoryVo"/>
        <where>
            <if test="tenantId != null  and tenantId != ''">and tenant_id = #{tenantId}</if>
            <if test="revision != null ">and revision = #{revision}</if>
            <if test="name != null  and name != ''">and name like concat('%', #{name}, '%')</if>
            <if test="parentId != null ">and parent_id = #{parentId}</if>
            <if test="ancestors != null  and ancestors != ''">and ancestors = #{ancestors}</if>
            <if test="level != null ">and level = #{level}</if>
            <if test="orderNum != null ">and order_num = #{orderNum}</if>
            <if test="code != null  and code != ''">and code = #{code}</if>
        </where>
        order by parent_id, order_num
    </select>

    <select id="selectBaseCheckCategoryById" parameterType="Long" resultMap="BaseCheckCategoryResult">
        <include refid="selectBaseCheckCategoryVo"/>
        where id = #{id}
    </select>

    <insert id="insertBaseCheckCategory" parameterType="BaseCheckCategory" useGeneratedKeys="true" keyProperty="id">
        insert into base_check_category
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="tenantId != null">tenant_id,</if>
            <if test="revision != null">revision,</if>
            <if test="createBy != null">create_by,</if>
            <if test="createTime != null">create_time,</if>
            <if test="updateBy != null">update_by,</if>
            <if test="updateTime != null">update_time,</if>
            <if test="name != null and name != ''">name,</if>
            <if test="parentId != null">parent_id,</if>
            <if test="ancestors != null and ancestors != ''">ancestors,</if>
            <if test="level != null">level,</if>
            <if test="orderNum != null">order_num,</if>
            <if test="code != null and code != ''">code,</if>
            <if test="remark != null">remark,</if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="tenantId != null">#{tenantId},</if>
            <if test="revision != null">#{revision},</if>
            <if test="createBy != null">#{createBy},</if>
            <if test="createTime != null">#{createTime},</if>
            <if test="updateBy != null">#{updateBy},</if>
            <if test="updateTime != null">#{updateTime},</if>
            <if test="name != null and name != ''">#{name},</if>
            <if test="parentId != null">#{parentId},</if>
            <if test="ancestors != null and ancestors != ''">#{ancestors},</if>
            <if test="level != null">#{level},</if>
            <if test="orderNum != null">#{orderNum},</if>
            <if test="code != null and code != ''">#{code},</if>
            <if test="remark != null">#{remark},</if>
        </trim>
    </insert>

    <update id="updateBaseCheckCategory" parameterType="BaseCheckCategory">
        update base_check_category
        <trim prefix="SET" suffixOverrides=",">
            <if test="tenantId != null">tenant_id = #{tenantId},</if>
            <if test="revision != null">revision = #{revision},</if>
            <if test="createBy != null">create_by = #{createBy},</if>
            <if test="createTime != null">create_time = #{createTime},</if>
            <if test="updateBy != null">update_by = #{updateBy},</if>
            <if test="updateTime != null">update_time = #{updateTime},</if>
            <if test="name != null and name != ''">name = #{name},</if>
            <if test="parentId != null">parent_id = #{parentId},</if>
            <if test="ancestors != null and ancestors != ''">ancestors = #{ancestors},</if>
            <if test="level != null">level = #{level},</if>
            <if test="orderNum != null">order_num = #{orderNum},</if>
            <if test="code != null and code != ''">code = #{code},</if>
            <if test="remark != null">remark = #{remark},</if>
        </trim>
        where id = #{id}
    </update>

    <delete id="deleteBaseCheckCategoryById" parameterType="Long">
        delete
        from base_check_category
        where id = #{id}
    </delete>

    <delete id="deleteBaseCheckCategoryByIds" parameterType="String">
        delete from base_check_category where id in
        <foreach item="id" collection="array" open="(" separator="," close=")">
            #{id}
        </foreach>
    </delete>

    <select id="selectChildrenById" parameterType="Long" resultMap="BaseCheckCategoryResult">
        select *
        from base_check_category
        where find_in_set(#{id}, ancestors)
    </select>

    <update id="updateChildren" parameterType="java.util.List">
        update base_check_category set ancestors =
        <foreach collection="list" item="item" index="index"
                 separator=" " open="case id" close="end">
            when #{item.id} then #{item.ancestors}
        </foreach>,
        level =
        <foreach collection="list" item="item" index="index"
                 separator=" " open="case id" close="end">
            when #{item.id} then #{item.level}
        </foreach>
        where id in
        <foreach collection="list" item="item" index="index"
                 separator="," open="(" close=")">
            #{item.id}
        </foreach>
    </update>

    <select id="selectLevel1ByLevel2Name" parameterType="String" resultMap="BaseCheckCategoryResult">
        SELECT parent.*
        FROM base_check_category child
                 INNER JOIN base_check_category parent ON child.parent_id = parent.id
        WHERE child.level = 2
          AND child.name = #{level2Name}
          AND parent.level = 1 LIMIT 1
    </select>

</mapper>