<?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.aiotagro.cattletrade.business.mapper.JbqClientLogMapper">

    <!-- 通用查询映射结果 -->
    <resultMap id="BaseResultMap" type="com.aiotagro.cattletrade.business.entity.JbqClientLog">
        <id column="id" property="id" />
        <result column="device_id" property="deviceId" />
        <result column="device_voltage" property="deviceVoltage" />
        <result column="device_temp" property="deviceTemp" />
        <result column="server_device_id" property="serverDeviceId" />
        <result column="latitude" property="latitude" />
        <result column="longitude" property="longitude" />
        <result column="walk_steps" property="walkSteps" />
        <result column="y_walk_steps" property="yWalkSteps" />
        <result column="create_time" property="createTime" />
        <result column="create_by" property="createBy" />
        <result column="update_time" property="updateTime" />
        <result column="update_by" property="updateBy" />
    </resultMap>

    <!-- 通用查询结果列 -->
    <sql id="Base_Column_List">
        id, device_id, device_voltage, device_temp, server_device_id, latitude, longitude, walk_steps, y_walk_steps, create_time, create_by, update_time, update_by
    </sql>
    <select id="jbqLogList" resultType="com.aiotagro.cattletrade.business.entity.JbqClientLog">
        SELECT
            log.*
        FROM
            jbq_client_log log
        WHERE
            log.device_id IN (
                SELECT dd.device_id
                FROM delivery_device dd
                WHERE dd.delivery_id = #{deliveryId}
                  AND dd.device_type = 2
            )
          AND log.update_time BETWEEN #{createTime} AND #{checkTime}
        order by log.update_time desc
    </select>

    <!-- 批量插入耳标日志数据 -->
    <insert id="batchInsert" parameterType="java.util.List">
        INSERT INTO jbq_client_log (
            device_id, device_voltage, device_temp, server_device_id,
            latitude, longitude, walk_steps, y_walk_steps,
            create_time, create_by, update_time, update_by
        ) VALUES
        <foreach collection="list" item="item" separator=",">
            (
                #{item.deviceId}, #{item.deviceVoltage}, #{item.deviceTemp}, #{item.serverDeviceId},
                #{item.latitude}, #{item.longitude}, #{item.walkSteps}, #{item.yWalkSteps},
                #{item.createTime}, #{item.createBy}, #{item.updateTime}, #{item.updateBy}
            )
        </foreach>
    </insert>

    <select id="listLogsForYingyan" resultType="com.aiotagro.cattletrade.business.entity.JbqClientLog">
        SELECT
            <include refid="Base_Column_List"/>
        FROM
            jbq_client_log
        <where>
            <if test="deviceIds != null and deviceIds.size > 0">
                device_id IN
                <foreach collection="deviceIds" item="item" open="(" separator="," close=")">
                    #{item}
                </foreach>
            </if>
            <if test="startTime != null">
                AND update_time <![CDATA[>=]]> #{startTime}
            </if>
            <if test="endTime != null">
                AND update_time <![CDATA[<=]]> #{endTime}
            </if>
            AND latitude IS NOT NULL
            AND latitude != ''
            AND longitude IS NOT NULL
            AND longitude != ''
        </where>
        ORDER BY update_time ASC
        <if test="limit != null">
            LIMIT #{limit}
        </if>
    </select>

</mapper>
