<?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.evobms.project.vehicledata.mapper.VehicleLocationMapper">

    <resultMap type="VehicleLocation" id="VehicleLocationResult">
        <result property="id"    column="id"    />
        <result property="deviceId"    column="device_id"    />
        <result property="timestamp"    column="timestamp"    />
        <result property="positioningStatus"    column="positioning_status"    />
        <result property="longitude"    column="longitude"    />
        <result property="latitude"    column="latitude"    />
        <result property="createTime"    column="create_time"    />
        <result property="updateTime"    column="update_time"    />
        <result property="createBy"    column="create_by"    />
        <result property="updateBy"    column="update_by"    />
    </resultMap>

    <sql id="selectVehicleLocationVo">
        select id, device_id, timestamp, positioning_status, longitude, latitude, create_time, update_time, create_by, update_by from vehicle_location
    </sql>

    <select id="selectVehicleLocationList" parameterType="VehicleLocation" resultMap="VehicleLocationResult">
        <include refid="selectVehicleLocationVo"/>
        <where>
            <if test="deviceId != null  and deviceId != ''"> and device_id = #{deviceId}</if>
            <if test="timestamp != null "> and timestamp = #{timestamp}</if>
            <if test="positioningStatus != null "> and positioning_status = #{positioningStatus}</if>
            <if test="longitude != null "> and longitude = #{longitude}</if>
            <if test="latitude != null "> and latitude = #{latitude}</if>
        </where>
    </select>

    <select id="selectVehicleLocationById" parameterType="Long" resultMap="VehicleLocationResult">
        <include refid="selectVehicleLocationVo"/>
        where id = #{id}
    </select>

    <insert id="insertVehicleLocation" parameterType="VehicleLocation" useGeneratedKeys="true" keyProperty="id">
        insert into vehicle_location
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="deviceId != null and deviceId != ''">device_id,</if>
            <if test="timestamp != null">timestamp,</if>
            <if test="positioningStatus != null">positioning_status,</if>
            <if test="longitude != null">longitude,</if>
            <if test="latitude != null">latitude,</if>
            <if test="createTime != null">create_time,</if>
            <if test="updateTime != null">update_time,</if>
            <if test="createBy != null">create_by,</if>
            <if test="updateBy != null">update_by,</if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="deviceId != null and deviceId != ''">#{deviceId},</if>
            <if test="timestamp != null">#{timestamp},</if>
            <if test="positioningStatus != null">#{positioningStatus},</if>
            <if test="longitude != null">#{longitude},</if>
            <if test="latitude != null">#{latitude},</if>
            <if test="createTime != null">#{createTime},</if>
            <if test="updateTime != null">#{updateTime},</if>
            <if test="createBy != null">#{createBy},</if>
            <if test="updateBy != null">#{updateBy},</if>
        </trim>
    </insert>

    <update id="updateVehicleLocation" parameterType="VehicleLocation">
        update vehicle_location
        <trim prefix="SET" suffixOverrides=",">
            <if test="deviceId != null and deviceId != ''">device_id = #{deviceId},</if>
            <if test="timestamp != null">timestamp = #{timestamp},</if>
            <if test="positioningStatus != null">positioning_status = #{positioningStatus},</if>
            <if test="longitude != null">longitude = #{longitude},</if>
            <if test="latitude != null">latitude = #{latitude},</if>
            <if test="createTime != null">create_time = #{createTime},</if>
            <if test="updateTime != null">update_time = #{updateTime},</if>
            <if test="createBy != null">create_by = #{createBy},</if>
            <if test="updateBy != null">update_by = #{updateBy},</if>
        </trim>
        where id = #{id}
    </update>

    <delete id="deleteVehicleLocationById" parameterType="Long">
        delete from vehicle_location where id = #{id}
    </delete>

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

    <select id="selectLatestByDeviceId" resultMap="VehicleLocationResult">
        <include refid="selectVehicleLocationVo"/>
        where device_id = #{deviceId}
        order by timestamp desc
        limit 1
    </select>

    <select id="selectByDeviceAndRange" resultMap="VehicleLocationResult">
        <include refid="selectVehicleLocationVo"/>
        where device_id = #{deviceId}
          and timestamp &gt;= #{start}
          and timestamp &lt;= #{end}
        order by timestamp asc
        <if test="limit != null">limit #{limit}</if>
    </select>
</mapper>
