<?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.ruoyi.asset.mapper.TbLocationMapper">
    
    <resultMap type="TbLocation" id="TbLocationResult">
        <result property="id"    column="id"    />
        <result property="number"    column="number"    />
        <result property="name"    column="name"    />
        <result property="parentId"    column="parent_id"    />
        <result property="sequence"    column="sequence"    />
        <result property="label"    column="label"    />
        <result property="remark"    column="remark"    />
        <result property="createBy"    column="create_by"    />
        <result property="createTime"    column="create_time"    />
        <result property="updateBy"    column="update_by"    />
        <result property="updateTime"    column="update_time"    />
    </resultMap>

    <sql id="selectTbLocationVo">
        select id, number, name, parent_id, sequence, label, remark, create_by, create_time, update_by, update_time from tb_location
    </sql>

    <select id="selectTbLocationList" parameterType="TbLocation" resultMap="TbLocationResult">
        <include refid="selectTbLocationVo"/>
        <where>  
            <if test="number != null  and number != ''"> and number = #{number}</if>
            <if test="name != null  and name != ''"> and name like concat('%', #{name}, '%')</if>
            <if test="parentId != null"> and parent_id = #{parentId}</if>
            <if test="label != null  and label != ''"> and label = #{label}</if>
        </where>
    </select>
    
    <select id="selectTbLocationById" parameterType="Long" resultMap="TbLocationResult">
        <include refid="selectTbLocationVo"/>
        where id = #{id}
    </select>

    <select id="selectTbLocationByIds" parameterType="String" resultMap="TbLocationResult">
        <include refid="selectTbLocationVo"/>
        where id in
        <foreach item="id" collection="ids" open="(" separator="," close=")">
            #{id}
        </foreach>
    </select>

    <select id="selectTbLocationIdByNumbers" parameterType="String" resultType="Long">
        SELECT id FROM tb_location WHERE number in
        <foreach item="number" collection="numbers" open="(" separator="," close=")">
            #{number}
        </foreach>
    </select>

    <!--selectTbLocationByNumber-->
    <select id="selectTbLocationByNumber" parameterType="String" resultMap="TbLocationResult">
        <include refid="selectTbLocationVo"/>
        where number = #{number}
    </select>

    <select id="selectNumberByFather" parameterType="String" resultType="String">
        select a.number, a.id, a.name from tb_location a
        where a.number = #{number}
           or a.parent_id = (select b.id from tb_location b where b.number = #{number})
    </select>

    <select id="selectChildTbLocationById" parameterType="Long" resultType="String">
        SELECT number FROM `tb_location` where id = #{id} or sequence like concat('%', #{id}, '%');
    </select>

    <select id="selectChildTbLocationByIds" parameterType="Long" resultType="String">
        SELECT DISTINCT number FROM `tb_location`
        <where>
            id in
            <foreach item="id" collection="ids" open="(" separator="," close=")">
                #{id}
            </foreach>
            <foreach item="id" collection="ids">
               or sequence like concat('%', #{id}, '%')
            </foreach>
        </where>

    </select>
        
    <insert id="insertTbLocation" parameterType="TbLocation" useGeneratedKeys="true" keyProperty="id">
        insert into tb_location
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="number != null and number != ''">number,</if>
            <if test="name != null">name,</if>
            <if test="parentId != null">parent_id,</if>
            <if test="sequence != null and sequence != ''">sequence,</if>
            <if test="label != null and label != ''">label,</if>
            <if test="remark != null">remark,</if>
            <if test="createBy != null and createBy != ''">create_by,</if>
            <if test="createTime != null">create_time,</if>
            <if test="updateBy != null">update_by,</if>
            <if test="updateTime != null">update_time,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="number != null and number != ''">#{number},</if>
            <if test="name != null">#{name},</if>
            <if test="parentId != null">#{parentId},</if>
            <if test="sequence != null and sequence != ''">#{sequence},</if>
            <if test="label != null and label != ''">#{label},</if>
            <if test="remark != null">#{remark},</if>
            <if test="createBy != null and createBy != ''">#{createBy},</if>
            <if test="createTime != null">#{createTime},</if>
            <if test="updateBy != null">#{updateBy},</if>
            <if test="updateTime != null">#{updateTime},</if>
         </trim>
    </insert>

    <update id="updateTbLocation" parameterType="TbLocation">
        update tb_location
        <trim prefix="SET" suffixOverrides=",">
            <if test="number != null and number != ''">number = #{number},</if>
            <if test="name != null">name = #{name},</if>
            <if test="parentId != null">parent_id = #{parentId},</if>
            <if test="sequence != null and sequence != ''">sequence = #{sequence},</if>
            <if test="label != null and label != ''">label = #{label},</if>
            <if test="remark != null">remark = #{remark},</if>
            <if test="createBy != null and createBy != ''">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>
        </trim>
        where id = #{id}
    </update>

    <delete id="deleteTbLocationById" parameterType="Long">
        delete from tb_location where id = #{id}
    </delete>

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

    <select id="selectTbLocationListByNumbers" parameterType="String" resultMap="TbLocationResult">
        <include refid="selectTbLocationVo"/>
        <where>
            number in
            <foreach item="number" collection="numbers" open="(" separator="," close=")">
                #{number}
            </foreach>
        </where>
    </select>
</mapper>