<?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.RfidDeviceMapper">

    <resultMap type="org.dromara.rfid.domain.vo.RfidDeviceVo" id="RfidDeviceResult">
    </resultMap>

    <select id="selectCustomRfidDeviceVoList" resultMap="RfidDeviceResult">
        select t.id,
               t.device_code,
               t.device_name,
               t.location_id,
               l.location_alias as locationAlias,
               t.device_address,
               t.device_port,
               t.read_frequency,
               t.online_status,
               t.alarm_status,
               t.is_marked,
               t.remark,
               t.created_by,
               t.created_at,
               t.updated_by,
               t.updated_at
        from rfid_device t
        left join rfid_location l on t.location_id = l.id
        <if test="ew != null">
            ${ew.customSqlSegment}
        </if>
    </select>

    <!-- 根据ID查询详情 -->
    <select id="selectCustomRfidDeviceVoById" resultMap="RfidDeviceResult">
        select t.id,
               t.device_code,
               t.device_name,
               t.location_id,
               l.location_alias as locationAlias,
               t.device_address,
               t.device_port,
               t.read_frequency,
               t.online_status,
               t.alarm_status,
               t.is_marked,
               t.remark,
               t.created_by,
               t.created_at,
               t.updated_by,
               t.updated_at
        from rfid_device t
        left join rfid_location l on t.location_id = l.id
        where t.id = #{id}
    </select>

    <!-- 批量查询 - 根据ID列表 -->
    <select id="selectCustomRfidDeviceVoByIds" resultMap="RfidDeviceResult">
        select t.id,
               t.device_code,
               t.device_name,
               t.location_id,
               l.location_alias as locationAlias,
               t.device_address,
               t.device_port,
               t.read_frequency,
               t.online_status,
               t.alarm_status,
               t.is_marked,
               t.remark,
               t.created_by,
               t.created_at,
               t.updated_by,
               t.updated_at
        from rfid_device t
        left join rfid_location l on t.location_id = l.id
        where t.id in
        <foreach collection="ids" item="id" open="(" separator="," close=")">
            #{id}
        </foreach>
    </select>

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

    <!-- 分页查询（带自定义条件） -->
    <select id="selectCustomRfidDeviceVoPage" resultMap="RfidDeviceResult">
        select t.id,
               t.device_code,
               t.device_name,
               t.location_id,
               l.location_alias as locationAlias,
               t.device_address,
               t.device_port,
               t.read_frequency,
               t.online_status,
               t.alarm_status,
               t.is_marked,
               t.remark,
               t.created_by,
               t.created_at,
               t.updated_by,
               t.updated_at
        from rfid_device t
        left join rfid_location l on t.location_id = l.id
        <if test="ew != null">
            ${ew.customSqlSegment}
        </if>
    </select>

    <!-- 批量插入 -->
    <insert id="batchInsertRfidDevice">
        insert into rfid_device(
            device_code,

            device_name,

            location_id,

            device_address,

            device_port,

            read_frequency,

            online_status,

            alarm_status,

            is_marked,

            remark,

            created_by,

            created_at,

            updated_by,

            updated_at

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

            #{item.deviceName},

            #{item.locationId},

            #{item.deviceAddress},

            #{item.devicePort},

            #{item.readFrequency},

            #{item.onlineStatus},

            #{item.alarmStatus},

            #{item.isMarked},

            #{item.remark},

            #{item.createdBy},

            #{item.createdAt},

            #{item.updatedBy},

            #{item.updatedAt}

            )
        </foreach>
    </insert>

    <!-- 批量更新 -->
    <update id="batchUpdateRfidDevice">
        <foreach collection="list" item="item" separator=";">
            update rfid_device
            <set>
                <if test="item.deviceCode != null and item.deviceCode != ''">
                    device_code = #{item.deviceCode},
                </if>
                <if test="item.deviceName != null and item.deviceName != ''">
                    device_name = #{item.deviceName},
                </if>
                <if test="item.locationId != null">
                    location_id = #{item.locationId},
                </if>
                <if test="item.deviceAddress != null and item.deviceAddress != ''">
                    device_address = #{item.deviceAddress},
                </if>
                <if test="item.devicePort != null">
                    device_port = #{item.devicePort},
                </if>
                <if test="item.readFrequency != null">
                    read_frequency = #{item.readFrequency},
                </if>
                <if test="item.onlineStatus != null and item.onlineStatus != ''">
                    online_status = #{item.onlineStatus},
                </if>
                <if test="item.alarmStatus != null and item.alarmStatus != ''">
                    alarm_status = #{item.alarmStatus},
                </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="deleteCustomRfidDevice">
        delete from rfid_device
        <if test="ew != null">
            ${ew.customSqlSegment}
        </if>
    </delete>

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

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


</mapper>
