<?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="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 !=''">
				concat(l.railway_name,a.camera_code)
				like concat("%",#{railwayCode},"%")
			</if>
			<if test="lineDir != null and lineDir != ''">
				line_dir = #{lineDir}
			</if>
			<if test="mils != null and mils != ''">
				concat(a.install_mile, a.begin_mile, a.end_mile) = #{mils}
			</if>
			<if test="!dsFlay">
				and
				<trim prefix="(" prefixOverrides="or" suffix=")">
					<if test="dsUserId != null and dsUserId != ''">
						or a.create_by=#{dsUserId}
					</if>
					<if test="dsDeptId != null and dsDeptId != 0">
						or  a.dept_id=#{dsDeptId}
					</if>
					<if test="dsDeptIds != null">
						or a.dept_id in
						<foreach item="item" collection="dsDeptIds" separator="," open="(" close=")" index="">
							#{item}
						</foreach>
					</if>
				</trim>
			</if>
		</where>
	</select>

	<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.current_time
		FROM
		base_vehicle_terminal AS a ,
		(
		select vp.*
		from vehicle_position vp
		where not exists (
		select 1
		from vehicle_position df
		where df.current_time IS NOT NULL and df.vehicle_code = vp.vehicle_code
		and df.current_time > vp.current_time )
		GROUP BY vp.vehicle_code ) AS l
		WHERE a.terminal_code=l.terminal_code
		ORDER BY l.current_time DESC
		<where>
			<if test="railwayCode != null and railwayCode !=''">
				concat(l.railway_code,a.terminal_code)
				like concat("%",#{railwayCode},"%")
			</if>
		</where>
	</select>
</mapper>
