<?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.tsi.sig.server.mapper.MainMapper">

	<select id="getNodeList" resultType="com.tsi.sig.server.vo.CvibNodeVO">
		SELECT nodeid as nodeid, name as name, FORMAT(latitude, 10) as lat, FORMAT(longitude, 9) as lng, addr1, addr2, addr3, addnode, ipaddr, nodetype, region_id as regionid
          FROM tb_tsc_node
         WHERE useyn  = 'Y'
           AND nodeyn = 'Y'
	</select>

	<select id="getCvibNodeInfo" parameterType="java.util.HashMap" resultType="com.tsi.sig.server.vo.CvibNodeVO">
		SELECT nodeid, name, FORMAT(latitude, 10) as LAT, FORMAT(longitude, 9) as LNG, addr1, addr2, addr3, addnode, ipaddr, nodetype
		  FROM tb_tsc_node
		 WHERE useyn  = 'Y'
           AND nodeyn = 'Y'
        <if test='nodeId != null'>
		   AND nodeid = #{nodeId}
		</if>
	</select>

	<select id="getCvibMapNodeList" parameterType="java.util.HashMap" resultType="com.tsi.sig.server.vo.CvibNodeVO">
		SELECT nodeid, name, latitude AS lat, longitude AS lng , addr1, addr2, addr3, addnode, ipaddr
		  FROM tb_tsc_node
		WHERE 1=1
		  AND longitude BETWEEN #{minX} AND  #{maxX}
		  AND latitude BETWEEN #{minY}  AND #{maxY}
		  AND useyn  = 'Y'
          AND nodeyn = 'Y'
	</select>
	<!-- 서비스 상태 코드(1:진행중-서비스 진행중,2:정상종료-모든 교차로 제어 및 해제 완료,3:취소-아직 통과하지 않은 교차로 존재,
						4:센터강제종료-운영자가 서비스를 강제로 종료,5:비정상종료-서비스가 존재하지 않음,6:서비스시작실패-제어대상교차로가 없음,
						8:비정상종료-앱서버에 에러 발생,9:비정상종료-일정시간 앱에서 위치 및 속도 정보가 오지 않는 경우,
						10:자동종료-경로이탈,11:자동종료-경로진입 가능시간 초과,12:자동종료-정차가능시간 초과,13:취소-모든 교차로 제어및 해제 완료,
						14:실패-서비스 제어 요청 실패,15:실패-서비스 가능 교차로가 존재하지 않음,16:자동종료-위치정보 수신 가능 시간 초과)	-->
	<select id="getEvpServiceList" resultType="com.tsi.sig.server.vo.EvpServiceVo">
		SELECT SERVICE_ID,
			   DATE_FORMAT(clct_dt, '%Y-%m-%d %H:%i:%s') CLCT_DT,
			   EV_NO,
			   CUR_LAT,
			   CUR_LNG,
			   SERVICE_NM,
			   ARR_LAT,
			   ARR_LNG,
			   ARR_TM,
			   VEH_LEN,
			   OCR_NO,
			   OCR_TYPE,
			   SERVICE_DIST,
			   STATUS_CD
		FROM tb_evp_service
		WHERE STATUS_CD = 1
		ORDER BY CLCT_DT
	</select>

	<select id="getEvpServiceDto" resultType="com.tsi.sig.server.dto.EvpsServiceDto">
		SELECT SERVICE_ID AS serviceId,
			   DATE_FORMAT(clct_dt, '%Y-%m-%d %H:%i:%s') clctDt,
			   EV_NO AS evNo,
			   CUR_LAT AS curLat,
			   CUR_LNG AS curLng,
			   SERVICE_NM AS serviceNm,
			   ARR_LAT AS arrLat,
			   ARR_LNG AS arrLng,
			   ARR_TM AS arrTm,
			   VEH_LEN AS vehLen,
			   OCR_NO AS ocrNo,
			   OCR_TYPE AS ocrType,
			   SERVICE_DIST AS serviceDist,
			   STATUS_CD AS statusCd
		FROM tb_evp_service
		WHERE STATUS_CD = 1
		  AND SERVICE_ID = #{serviceId}
	</select>

	<select id="getEvpHistoryList" parameterType="java.util.HashMap" resultType="com.tsi.sig.server.vo.EvpServiceVo">
		SELECT SERVICE_ID,
			   DATE_FORMAT(clct_dt, '%Y-%m-%d %H:%i:%s') CLCT_DT,
			   EV_NO,
			   CUR_LAT,
			   CUR_LNG,
			   SERVICE_NM,
			   ARR_LAT,
			   ARR_LNG,
			   ARR_TM,
			   VEH_LEN,
			   OCR_NO,
			   OCR_TYPE,
			   SERVICE_DIST,
			   STATUS_CD
		FROM tb_evp_service
			WHERE DATE_FORMAT(clct_dt, '%Y-%m-%d %H:%i:%s') BETWEEN #{start} AND #{end}
			<if test="!'ALL'.equals(region)">
				AND LEFT(service_id, 3) = #{region}
			</if>
			<if test="serviceId != null and !serviceId.equals('')">
				AND SERVICE_ID = #{serviceId}
			</if>
		ORDER BY CLCT_DT, SERVICE_ID
	</select>

	<select id="getEvpRouteList" parameterType="java.util.HashMap" resultType="com.tsi.sig.server.vo.EvpRouteVo">
		SELECT SERVICE_ID,
		       SEQ_NO,
		       LAT,
		       LNG
		  FROM tb_evp_route
		WHERE SERVICE_ID = #{serviceId}
		  AND LAT BETWEEN 33.10000000 AND 38.45000000
		  AND LNG BETWEEN 125.06666667 AND 131.87222222
	</select>

	<select id="getEvpRouteDtoList" parameterType="java.util.HashMap" resultType="com.tsi.sig.server.dto.RouteInfo">
		SELECT SERVICE_ID,
			   SEQ_NO,
			   LAT,
			   LNG
		FROM tb_evp_route
		WHERE SERVICE_ID = #{serviceId}
		  AND LAT BETWEEN 33.10000000 AND 38.45000000
		  AND LNG BETWEEN 125.06666667 AND 131.87222222
	</select>

	<select id="getEvpNodeList" parameterType="java.util.HashMap" resultType="com.tsi.sig.server.vo.EvpNodeVo">
		SELECT SERVICE_ID,
			   SEQ_NO,
		       NODE_ID,
		       NODE_NM,
			   LAT,
			   LNG
		FROM tb_evp_node
		WHERE SERVICE_ID = #{serviceId}
		  AND LAT BETWEEN 33.10000000 AND 38.45000000
		  AND LNG BETWEEN 125.06666667 AND 131.87222222
	</select>

	<select id="getEvpNodeDtoList" parameterType="java.util.HashMap" resultType="com.tsi.sig.server.dto.NodeInfo">
		SELECT SEQ_NO AS seqNo,
			   NODE_ID AS nodeId,
			   NODE_NM AS nodeNm,
			   LAT AS lat,
			   LNG AS lng
		FROM tb_evp_node
		WHERE SERVICE_ID = #{serviceId}
		  AND LAT BETWEEN 33.10000000 AND 38.45000000
		  AND LNG BETWEEN 125.06666667 AND 131.87222222
	</select>

	<select id="getEvpPhaseList" parameterType="java.util.HashMap" resultType="com.tsi.sig.server.vo.EvpPhaseVo">
		SELECT SERVICE_ID,
			   SEQ_NO,
			   NODE_ID,
			   RING,
		       PHASE_NO,
		       PLAN_CLASS,
		       FLOW_NO,
		       HEAD_LAT,
		       HEAD_LNG,
		       MID_LAT,
		       MID_LNG,
		       END_LAT,
		       END_LNG,
		       HEAD_ANGLE,
		       END_ANGLE
		FROM TB_EVP_PHASE
	</select>

	<select id="getEvpPhaseDtoList" parameterType="java.util.HashMap" resultType="com.tsi.sig.server.dto.PhaseInfo">
		SELECT RING AS ring,
			   PLAN_CLASS AS planClass,
			   PHASE_NO AS phaseNo,
			   FLOW_NO AS flowNo,
			   HEAD_LAT AS headLat,
			   HEAD_LNG AS headLng,
			   MID_LAT AS midLat,
			   MID_LNG AS midLng,
			   END_LAT AS endLat,
			   END_LNG AS endLng,
			   HEAD_ANGLE AS headAngle,
			   END_ANGLE AS endAngle
		FROM TB_EVP_PHASE
		WHERE SERVICE_ID = #{serviceId}
		  AND NODE_ID = #{nodeId}
		  AND SEQ_NO = #{seqNo}
	</select>

	<select id="getEvpSignalList" parameterType="java.util.HashMap" resultType="com.tsi.sig.server.vo.EvpSignalVo">
		SELECT
			A.seq_no,
			DATE_FORMAT(A.clct_dt, '%Y-%m-%d %H:%i:%s') clct_dt,
			A.service_id,
			A.node_id,
			D.node_nm,
			A.rem_dist,
			A.state,
			A.plan_class,
			A.a_ring_phase,
			A.b_ring_phase,
			A.hold_phase,
			D.lat,
			D.lng,
			B.flow_no a_flow_no,
			B.head_lat a_head_lat,
			B.head_lng a_head_lng,
			B.head_angle a_head_angle,
			B.mid_lat a_mid_lat,
			B.mid_lng a_mid_lng,
			B.end_lat a_end_lat,
			B.end_lng a_end_lng,
			B.end_angle a_end_angle,
			C.flow_no b_flow_no,
			C.head_lat b_head_lat,
			C.head_lng b_head_lng,
			C.head_angle b_head_angle,
			C.mid_lat b_mid_lat,
			C.mid_lng b_mid_lng,
			C.end_lat b_end_lat,
			C.end_lng b_end_lng,
			C.end_angle b_end_angle
		FROM (
		       select
					  seq_no,
					  clct_dt,
					  service_id,
					  node_id,
					  rem_dist,
					  state,
					  plan_class,
					  if (state = 5, hold_phase, a_ring_phase) a_ring_phase,
					  if (state = 5, hold_phase, b_ring_phase) b_ring_phase,
					  hold_phase
			     from tb_evp_signal
		     ) A,
			 (select
				  *
			  from tb_evp_phase
			  where ring = 1
				and head_lat BETWEEN 33.10000000 AND 38.45000000
				and head_lng BETWEEN 125.06666667 AND 131.87222222
				and mid_lat BETWEEN 33.10000000 AND 38.45000000
				and mid_lng BETWEEN 125.06666667 AND 131.87222222
				and end_lat BETWEEN 33.10000000 AND 38.45000000
				and end_lng BETWEEN 125.06666667 AND 131.87222222
			 ) B,
			 (
				 select
					 *
				 from tb_evp_phase
				 where ring = 2
				   and head_lat BETWEEN 33.10000000 AND 38.45000000
				   and head_lng BETWEEN 125.06666667 AND 131.87222222
				   and mid_lat BETWEEN 33.10000000 AND 38.45000000
				   and mid_lng BETWEEN 125.06666667 AND 131.87222222
				   and end_lat BETWEEN 33.10000000 AND 38.45000000
				   and end_lng BETWEEN 125.06666667 AND 131.87222222
			 ) C,
			 tb_evp_node D
		where (1=1)
		  and A.service_id   = #{serviceId}
		  and A.seq_no 		 = B.seq_no
		  and A.node_id 	 = B.node_id
		  and A.service_id 	 = B.service_id
		  and A.a_ring_phase = B.phase_no
		  and A.plan_class 	 = B.plan_class
		  and A.node_id 	 = C.node_id
		  and A.service_id 	 = C.service_id
		  and A.b_ring_phase = C.phase_no
		  and A.seq_no 		 = C.seq_no
		  and A.plan_class 	 = C.plan_class
		  and A.service_id 	 = D.service_id
		  and A.node_id 	 = D.node_id
		  and A.seq_no 		 = D.seq_no
		ORDER BY clct_dt, A.seq_no;
	</select>

	<select id="getEvpSignalCurrList" parameterType="java.util.HashMap" resultType="com.tsi.sig.server.vo.EvpSignalVo">
		SELECT
			A.seq_no,
			DATE_FORMAT(A.clct_dt, '%Y-%m-%d %H:%i:%s') clct_dt,
			A.service_id,
			A.node_id,
			D.node_nm,
			A.rem_dist,
			A.state,
			A.plan_class,
			A.a_ring_phase,
			A.b_ring_phase,
			A.hold_phase,
			D.lat,
			D.lng,
			B.flow_no a_flow_no,
			B.head_lat a_head_lat,
			B.head_lng a_head_lng,
			B.head_angle a_head_angle,
			B.mid_lat a_mid_lat,
			B.mid_lng a_mid_lng,
			B.end_lat a_end_lat,
			B.end_lng a_end_lng,
			B.end_angle a_end_angle,
			C.flow_no b_flow_no,
			C.head_lat b_head_lat,
			C.head_lng b_head_lng,
			C.head_angle b_head_angle,
			C.mid_lat b_mid_lat,
			C.mid_lng b_mid_lng,
			C.end_lat b_end_lat,
			C.end_lng b_end_lng,
			C.end_angle b_end_angle
		FROM (
				 select
					 seq_no,
					 clct_dt,
					 service_id,
					 node_id,
					 rem_dist,
					 state,
					 plan_class,
					 if (state = 5, hold_phase, a_ring_phase) a_ring_phase,
					 if (state = 5, hold_phase, b_ring_phase) b_ring_phase,
					 hold_phase
				 from tb_evp_signal_curr
			 ) A,
			 (select
				  *
			  from tb_evp_phase
			  where ring = 1
				and head_lat BETWEEN 33.10000000 AND 38.45000000
				and head_lng BETWEEN 125.06666667 AND 131.87222222
				and mid_lat BETWEEN 33.10000000 AND 38.45000000
				and mid_lng BETWEEN 125.06666667 AND 131.87222222
				and end_lat BETWEEN 33.10000000 AND 38.45000000
				and end_lng BETWEEN 125.06666667 AND 131.87222222
			 ) B,
			 (
				 select
					 *
				 from tb_evp_phase
				 where ring = 2
				   and head_lat BETWEEN 33.10000000 AND 38.45000000
				   and head_lng BETWEEN 125.06666667 AND 131.87222222
				   and mid_lat BETWEEN 33.10000000 AND 38.45000000
				   and mid_lng BETWEEN 125.06666667 AND 131.87222222
				   and end_lat BETWEEN 33.10000000 AND 38.45000000
				   and end_lng BETWEEN 125.06666667 AND 131.87222222
			 ) C,
			 tb_evp_node D
		where (1=1)
		  and A.service_id   = #{serviceId}
		  and A.seq_no 		 = B.seq_no
		  and A.node_id 	 = B.node_id
		  and A.service_id 	 = B.service_id
		  and A.a_ring_phase = B.phase_no
		  and A.plan_class 	 = B.plan_class
		  and A.node_id 	 = C.node_id
		  and A.service_id 	 = C.service_id
		  and A.b_ring_phase = C.phase_no
		  and A.seq_no 		 = C.seq_no
		  and A.plan_class 	 = C.plan_class
		  and A.service_id 	 = D.service_id
		  and A.node_id 	 = D.node_id
		  and A.seq_no 		 = D.seq_no
		ORDER BY clct_dt, A.seq_no;
	</select>

	<select id="getEvpEventList" parameterType="java.util.HashMap" resultType="com.tsi.sig.server.vo.EvpEventVo">
		SELECT
			DATE_FORMAT(clct_dt, '%Y-%m-%d %H:%i:%s') clct_dt,
			service_id,
			event_cd,
			IF(event_cd = 0,  '서비스시작',
		    IF(event_cd = 1,  '차량위치',
			IF(event_cd = 2,  '서비스종료', '정보없음'
			))) AS event_desc,
			cur_lat,
			cur_lng,
			cur_spd,
			rem_dist
		FROM
			tb_evp_event
		WHERE service_id = #{serviceId}
		  AND cur_lat BETWEEN 33.10000000 AND 38.45000000
		  AND cur_lng BETWEEN 125.06666667 AND 131.87222222
		ORDER BY clct_dt
	</select>


	<select id="getEvpEventCurrList" parameterType="java.util.HashMap" resultType="com.tsi.sig.server.vo.EvpEventVo">
		SELECT
			DATE_FORMAT(clct_dt, '%Y-%m-%d %H:%i:%s') clct_dt,
			service_id,
			event_cd,
			IF(event_cd = 0,  '서비스시작',
		      IF(event_cd = 1,  '차량위치',
			   IF(event_cd = 2,  '서비스종료', '정보없음'
				   ))) AS event_desc,
			cur_lat,
			cur_lng,
			cur_spd,
			rem_dist
		FROM
			tb_evp_event_curr
		WHERE service_id = #{serviceId}
		  AND cur_lat BETWEEN 33.10000000 AND 38.45000000
		  AND cur_lng BETWEEN 125.06666667 AND 131.87222222
		ORDER BY clct_dt
	</select>
	<select id="getEvpCenterCode" resultType="com.tsi.sig.server.vo.EvpCenterVo">
		SELECT
		    center_id,
			center_nm
		  FROM tb_evp_center
		 WHERE use_yn = 'Y'
	</select>
</mapper>