<?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.ozs.mapper.BaseDeviceDynamicManagementMapper">

    <select id="vehicleTreelist" resultType="com.ozs.entity.vo.BaseVehicleTerminalDto"
            parameterType="com.ozs.entity.vo.VehicleTreeVo">
        SELECT
        vp.railway_code AS railwayCode,
        vp.vehicle_code AS vehicleCode,
        vp.line_dir AS lineDir,
        vp.travel_mile AS travelMile,
        vp.terminal_longitude AS terminalLongitude,
        vp.terminal_latitude AS terminalLatitude,
        vp.curren_time,
        vp.terminal_code AS terminalCode,
        vp.id
        FROM
        vehicle_position vp
        WHERE
        vp.id IN (
        SELECT
        df.id
        FROM
        (
        SELECT
        id,
        curren_time,
        ROW_NUMBER() OVER ( PARTITION BY vehicle_code ORDER BY curren_time DESC ) AS rn
        FROM
        vehicle_position
        <where>
            <if test="railwayCode != null and railwayCode !=''">
                and l.railway_code like concat("%",#{railwayCode},"%")
            </if>
        </where>
        ) df
        WHERE
        df.rn = 1
        )
        ORDER BY
        vp.curren_time DESC;
    </select>

    <select id="list" resultType="com.ozs.entity.vo.BaseCameraManagementDto"
            parameterType="com.ozs.entity.vo.CameraTreeVo">
        SELECT
        l.railway_code AS railwayCode,
        l.railway_name AS railwayName,
        a.camera_code AS cameraCode,
        a.line_dir AS lineDir,
        a.install_mile AS installMile,
        a.id AS id
        FROM
        base_camera_management AS a
        inner join
        base_railway_management AS l
        on a.railway_code=l.railway_code
        <where>
            <if test="railwayCode != null and railwayCode !=''">
                l.railway_name || a.camera_code || a.railway_code
                like CONCAT('%', #{railwayCode}, '%')
            </if>
            <if test="lineDir != null and lineDir != ''">
                and a.line_dir = #{lineDir}
            </if>
            <if test="mils != null and mils != ''">
                a.install_mile || a.begin_mile || a.end_mile = #{mils}
            </if>
            and a.enable_or_not=1
        </where>
    </select>
</mapper>
