<?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
        a.id AS id,
        l.railway_code AS railwayCode,
        a.terminal_code AS terminalCode,
        l.vehicle_code AS vehicleCode,
        l.line_dir AS lineDir,
        l.travel_mile AS travelMile,
        l.terminal_longitude as terminalLongitude,
        l.terminal_latitude as terminalLatitude,
        l.curren_time
        FROM
        base_vehicle_terminal AS a ,
        (
        select * from vehicle_position AS gb_vp where gb_vp.terminal_code in(
        select vp.terminal_code
        from vehicle_position vp
        where not exists (
        select 1
        from vehicle_position df
        <where>
            df.curren_time IS NOT NULL
            <if test="terminalCodeList != null and terminalCodeList.size != 0 ">
                and df.vehicle_code in
                <foreach collection="terminalCodeList" item="item" index="index"
                         separator="," open="(" close=")">
                    #{item}
                </foreach>
            </if>
            and df.vehicle_code = vp.vehicle_code
            and df.curren_time > vp.curren_time
        </where>
        )
        GROUP BY vp.terminal_code ) ) AS l
        <where>
            a.terminal_code=l.terminal_code
            <if test="railwayCode != null and railwayCode !=''">
                and l.railway_code like concat("%",#{railwayCode},"%")
            </if>
        </where>
        ORDER BY l.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>
