<?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.fuyuanshen.equipment.mapper.DeviceFenceAccessRecordMapper">

    <!-- 修改后的分页查询围栏进出记录列表 -->
    <select id="selectVoPageWithFenceAndDeviceName"
            resultType="com.fuyuanshen.equipment.domain.vo.DeviceFenceAccessRecordVo">
        SELECT r.id,
        r.fence_id,
        f.name AS fence_name,
        r.device_id,
        d.device_name,
        r.user_id,
        r.event_type,
        r.latitude,
        r.longitude,
        r.accuracy,
        r.event_time,
        r.create_time
        FROM device_fence_access_record r
        LEFT JOIN device_geo_fence f ON r.fence_id = f.id
        LEFT JOIN device d ON r.device_id = d.id
        ${ew.customSqlSegment}
        <where>
            <if test="fenceName != null and fenceName != ''">
                AND f.name LIKE CONCAT('%', #{fenceName}, '%')
            </if>
        </where>
        ORDER BY r.id ASC
    </select>



    <!-- 分页查询围栏进出记录列表（纯XML形式） -->
    <select id="selectVoPageByXml" resultType="com.fuyuanshen.equipment.domain.vo.DeviceFenceAccessRecordVo">
        SELECT r.id,
        r.fence_id,
        f.name AS fence_name,
        r.device_id,
        d.device_name,
        r.user_id,
        r.event_type,
        r.latitude,
        r.longitude,
        r.accuracy,
        r.event_time, r.event_address,
        r.create_time
        FROM device_fence_access_record r
        INNER JOIN device_geo_fence f ON r.fence_id = f.id
        INNER JOIN device d ON r.device_id = d.id
        <where>
            <if test="bo.fenceId != null">
                AND r.fence_id = #{bo.fenceId}
            </if>
            <if test="bo.deviceId != null and bo.deviceId != ''">
                AND r.device_id = #{bo.deviceId}
            </if>
            <if test="bo.userId != null">
                AND r.user_id = #{bo.userId}
            </if>
            <if test="bo.eventType != null">
                AND r.event_type = #{bo.eventType}
            </if>
            <if test="bo.latitude != null">
                AND r.latitude = #{bo.latitude}
            </if>
            <if test="bo.longitude != null">
                AND r.longitude = #{bo.longitude}
            </if>
            <if test="bo.accuracy != null">
                AND r.accuracy = #{bo.accuracy}
            </if>
            <if test="bo.eventTime != null">
                AND r.event_time = #{bo.eventTime}
            </if>
            <if test="bo.createTime != null">
                AND r.create_time = #{bo.createTime}
            </if>
            <if test="bo.fenceName != null and bo.fenceName != ''">
                AND f.name LIKE CONCAT('%', #{bo.fenceName}, '%')
            </if>
            <!-- 添加时间范围筛选条件 -->
            <if test="bo.beginTime != null and bo.beginTime != ''">
                AND r.event_time >= #{bo.beginTime}
            </if>
            <if test="bo.endTime != null and bo.endTime != ''">
                AND r.event_time <![CDATA[ <= ]]> #{bo.endTime}
            </if>
        </where>
        ORDER BY r.event_time DESC
    </select>


    <!-- 查询设备最新的围栏记录 -->
    <select id="selectLatestRecordByDeviceId" resultType="com.fuyuanshen.equipment.domain.vo.DeviceFenceAccessRecordVo">
        SELECT *
        FROM device_fence_access_record
        WHERE device_id = #{deviceId}
        ORDER BY event_time DESC
            LIMIT 1
    </select>


</mapper>