<?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="org.dromara.rfid.mapper.RfidLocationMapper">

    <resultMap type="org.dromara.rfid.domain.vo.RfidLocationVo" id="RfidLocationResult">
    </resultMap>

    <select id="selectCustomRfidLocationVoList" resultMap="RfidLocationResult">
        select t.id, t.location_code, t.location_alias, t.location_type, t.parent_id, t.is_marked, t.remark, t.created_by, t.created_at, t.updated_by, t.updated_at 
        from rfid_location t
        <if test="ew != null">
            ${ew.customSqlSegment}
        </if>
    </select>

    <!-- 根据ID查询详情 -->
    <select id="selectCustomRfidLocationVoById" resultMap="RfidLocationResult">
        select t.id, t.location_code, t.location_alias, t.location_type, t.parent_id, t.is_marked, t.remark, t.created_by, t.created_at, t.updated_by, t.updated_at
        from rfid_location t
        where t.id = #{id}
    </select>

    <!-- 批量查询 - 根据ID列表 -->
    <select id="selectCustomRfidLocationVoByIds" resultMap="RfidLocationResult">
        select t.id, t.location_code, t.location_alias, t.location_type, t.parent_id, t.is_marked, t.remark, t.created_by, t.created_at, t.updated_by, t.updated_at
        from rfid_location t
        where t.id in
        <foreach collection="ids" item="id" open="(" separator="," close=")">
            #{id}
        </foreach>
    </select>

    <!-- 统计查询 -->
    <select id="countCustomRfidLocation" resultType="java.lang.Long">
        select count(1) from rfid_location t
        <if test="ew != null">
            ${ew.customSqlSegment}
        </if>
    </select>

    <!-- 分页查询（带自定义条件） -->
    <select id="selectCustomRfidLocationVoPage" resultMap="RfidLocationResult">
        select t.id, t.location_code, t.location_alias, t.location_type, t.parent_id, t.is_marked, t.remark, t.created_by, t.created_at, t.updated_by, t.updated_at
        from rfid_location t
        <if test="ew != null">
            ${ew.customSqlSegment}
        </if>
    </select>

    <!-- 批量插入 -->
    <insert id="batchInsertRfidLocation">
        insert into rfid_location(
            location_code,

            location_alias,

            location_type,

            parent_id,

            is_marked,

            remark,

            created_by,

            created_at,

            updated_by,

            updated_at

        )
        values
        <foreach collection="list" item="item" separator=",">
            (
            #{item.locationCode},

            #{item.locationAlias},

            #{item.locationType},

            #{item.parentId},

            #{item.isMarked},

            #{item.remark},

            #{item.createdBy},

            #{item.createdAt},

            #{item.updatedBy},

            #{item.updatedAt}

            )
        </foreach>
    </insert>

    <!-- 批量更新 -->
    <update id="batchUpdateRfidLocation">
        <foreach collection="list" item="item" separator=";">
            update rfid_location
            <set>
                <if test="item.locationCode != null and item.locationCode != ''">
                    location_code = #{item.locationCode},
                </if>
                <if test="item.locationAlias != null and item.locationAlias != ''">
                    location_alias = #{item.locationAlias},
                </if>
                <if test="item.locationType != null and item.locationType != ''">
                    location_type = #{item.locationType},
                </if>
                <if test="item.parentId != null">
                    parent_id = #{item.parentId},
                </if>
                <if test="item.isMarked != null and item.isMarked != ''">
                    is_marked = #{item.isMarked},
                </if>
                <if test="item.remark != null and item.remark != ''">
                    remark = #{item.remark},
                </if>
                <if test="item.createdBy != null and item.createdBy != ''">
                    created_by = #{item.createdBy},
                </if>
                <if test="item.createdAt != null">
                    created_at = #{item.createdAt},
                </if>
                <if test="item.updatedBy != null and item.updatedBy != ''">
                    updated_by = #{item.updatedBy},
                </if>
                <if test="item.updatedAt != null">
                    updated_at = #{item.updatedAt}
                </if>
            </set>
            where id = #{item.id}
        </foreach>
    </update>

    <!-- 根据自定义条件删除 -->
    <delete id="deleteCustomRfidLocation">
        delete from rfid_location
        <if test="ew != null">
            ${ew.customSqlSegment}
        </if>
    </delete>

    <!-- 根据ID列表批量删除 -->
    <delete id="deleteCustomRfidLocationByIds">
        delete from rfid_location
        where id in
        <foreach collection="ids" item="id" open="(" separator="," close=")">
            #{id}
        </foreach>
    </delete>

    <!-- 检查是否存在 -->
    <select id="existsRfidLocation" resultType="java.lang.Boolean">
        select count(1) > 0 from rfid_location t
        <if test="ew != null">
            ${ew.customSqlSegment}
        </if>
    </select>

    <!-- 批量更新子节点的 ancestors（用于父节点变更时，一次性更新所有后代节点） -->
    <update id="updateChildrenAncestors">
        update rfid_location
        set ancestors = REPLACE(ancestors, #{oldAncestors}, #{newAncestors})
        where find_in_set(#{parentId}, ancestors)
    </update>

</mapper>
