<?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="cn.com.taiji.beidou.ship.track.mapper.ShipTrackMapper">

    <resultMap type="shipTrackResult" id="shipTrackResult">
        <result property="serial" column="serial"/>
        <result property="deviceId" column="device_id"/>
        <result property="sendTime" column="sendTime"/>
        <result property="longitude" column="longitude"/>
        <result property="latitude" column="latitude"/>
        <result property="direction" column="direction"/>
        <result property="speed" column="speed"/>
    </resultMap>

    <resultMap type="ShipContainsResult" id="ShipContainsResult">
        <result property="deviceId" column="device_id"/>
        <result property="longitude" column="longitude"/>
        <result property="latitude" column="latitude"/>
        <result property="shipName" column="shipName"/>
    </resultMap>

    <select id="selectShipTrackDetail" parameterType="shipTrackReq" resultMap="shipTrackResult">
        select ROW_NUMBER() over(ORDER BY track.location_time) as serial,device_id,track.location_time as sendTime,longitude,latitude,direction,speed from ax_beidou_track track
        where track.device_id = #{deviceId}
        <if test="endTime != null and endTime != ''">and track.location_time &lt; #{endTime}</if>
        <if test="startTime != null and startTime != ''">and track.location_time &gt; #{startTime}</if>
        order by track.location_time DESC
    </select>
    <select id="selectShipContainsResults" parameterType="ShipContainsReq" resultMap="ShipContainsResult">
        SELECT a.device_id,
               a.longitude,
               a.latitude,
               b.ship_id as shipName FROM (
        SELECT  device_id,longitude,latitude,
                ROW_NUMBER() OVER(PARTITION BY device_id ORDER BY location_time desc) AS SCORE_ROWNUMBER
        from ax_beidou_track where location_time BETWEEN #{startTime} and #{endTime}
        and ST_Contains(st_geometryfromtext(#{wkt}),st_point(longitude,latitude))=1
        ) a
        INNER JOIN ax_beidou_ship_archives b ON a.device_id = b.devide_no
        WHERE a.SCORE_ROWNUMBER = 1
    </select>
</mapper>