<?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.its.op.dao.mapper.its.common.CommonMapper">

    <select id="findAllCongestTraffic" resultType="com.its.op.dto.its.common.CongestTrafficDto">
<![CDATA[
        SELECT * FROM
            (SELECT
                 A.IFSC_ID,
                 A.SPED,
                 A.CMTR_GRAD_CD,
                 A.STRT_NM,
                 A.END_NM,
                 A.CMMN_CD_KOR_NM,
                 A.SECT_LNGT AS SECT_LNGT,
                 B.ATRD_NM
             FROM
                 (
                     SELECT
                         A.IFSC_ID,
                         A.SPED,
                         A.CMTR_GRAD_CD,
                         B.STRT_NM,
                         B.END_NM,
                         B.SECT_LNGT,
                         C.CMMN_CD_KOR_NM
                     FROM
                         TB_IFSC_TRAF A,
                         TB_IFSC B,
                         TB_CMMN_CD C
                     WHERE A.IFSC_ID = B.IFSC_ID
                       AND C.CMMN_CLSF_CD = 'LTC'
                       AND A.CMTR_GRAD_CD = C.CMMN_CD
                       AND (A.CMTR_GRAD_CD = 'LTC2' OR A.CMTR_GRAD_CD = 'LTC3')
                 ) A LEFT OUTER JOIN
                 (
                     SELECT
                         A.IFSC_ID,
                         D.ATRD_NM
                     FROM
                         TB_IFSC A,
                         TB_ROAD_RLTN_IFSC B,
                         TB_ATRD_RLTN_ROAD C,
                         TB_ATRD D
                     WHERE
                         A.IFSC_ID = B.IFSC_ID
                       AND B.ROAD_ID = C.ROAD_ID
                       AND C.ATRD_ID = D.ATRD_ID
                 ) B
                 ON A.IFSC_ID = B.IFSC_ID
            )
        WHERE ATRD_NM IS NOT NULL
        ]]>
    </select>

    <select id="findAllDetlTrafLink" parameterType="java.util.HashMap" resultType="com.its.op.dto.its.common.DetlTrafLinkDto$DetlTrafLinkInfo">
        <![CDATA[
        SELECT *
        FROM (SELECT A.PRCN_DT,
                     A.CMTR_GRAD_CD,
                     A.SPED,
                     A.TRVL_HH,
                     A.MISS_YN,
                     A.PRCN_SPED,
                     A.SMTH_SPED,
                     A.ADJS_SPED,
                     A.PTRN_SPED,
                     A.DATA_NUM,
                     DECODE(C.CMMN_CD, NULL, A.CMTR_GRAD_CD, C.CMMN_CD_KOR_NM) AS CMTR_GRAD_DESC,
                     DECODE(B.CMMN_CD, NULL, A.PRCN_SORC, B.CMMN_CD_KOR_NM)    AS PRCN_SORC_DESC,
                     DECODE(D.CMMN_CD, NULL, A.ADJS_SORC, D.CMMN_CD_KOR_NM)    AS ADJS_SORC_DESC,
                     0                                                         AS RANK
              FROM TB_LINK_TRAF A,
                   (SELECT *
                    FROM TB_CMMN_CD
                    WHERE CMMN_CLSF_CD = 'CST'
                       OR CMMN_CLSF_CD = 'MCS') B,
                   (SELECT *
                    FROM TB_CMMN_CD
                    WHERE CMMN_CLSF_CD = 'CST'
                       OR CMMN_CLSF_CD = 'MCS') D,
                   (SELECT *
                    FROM TB_CMMN_CD
                    WHERE CMMN_CLSF_CD = 'LTC') C
              WHERE LINK_ID = TO_NUMBER(#{ID})
                AND PRCN_DT >= TO_CHAR(SYSDATE - 10 / 1440, 'YYYYMMDDHH24MISS')
                AND A.PRCN_SORC = B.CMMN_CD(+)
                AND A.ADJS_SORC = D.CMMN_CD(+)
                AND A.CMTR_GRAD_CD = C.CMMN_CD(+))
        ]]>
    </select>

    <select id="findAllDetlTrafIfsc" parameterType="java.util.HashMap" resultType="com.its.op.dto.its.common.DetlTrafIfscDto$DetlTrafIfscInfo">
        <![CDATA[
        SELECT *
        FROM (SELECT A.PRCN_DT,
                     A.CMTR_GRAD_CD,
                     A.SPED,
                     A.TRVL_HH,
                     A.PTRN_SPED,
                     A.DATA_NUM,
                     C.CMMN_CD_KOR_NM AS CMTR_GRAD_DESC,
                     0                AS RANK
              FROM TB_IFSC_TRAF A,
                   (SELECT *
                    FROM TB_CMMN_CD
                    WHERE CMMN_CLSF_CD = 'LTC') C
              WHERE IFSC_ID = TO_NUMBER(#{ID})
                AND PRCN_DT >= TO_CHAR(SYSDATE - 10 / 1440, 'YYYYMMDDHH24MISS')
                AND A.CMTR_GRAD_CD = C.CMMN_CD(+))
        ]]>
    </select>

    <select id="findAllDetlTrafRoad" parameterType="java.util.HashMap" resultType="com.its.op.dto.its.common.DetlTrafRoadDto$DetlTrafRoadInfo">
        <![CDATA[
        SELECT *
        FROM (SELECT A.PRCN_DT,
                     A.CMTR_GRAD_CD,
                     A.SPED,
                     A.TRVL_HH,
                     A.PTRN_SPED,
                     A.DATA_NUM,
                     C.CMMN_CD_KOR_NM AS CMTR_GRAD_DESC,
                     0                AS RANK
              FROM TB_ROAD_TRAF A,
                   (SELECT *
                    FROM TB_CMMN_CD
                    WHERE CMMN_CLSF_CD = 'LTC') C
              WHERE ROAD_ID = TO_NUMBER(#{ID})
                AND PRCN_DT > TO_CHAR(SYSDATE - 10 / 1440, 'YYYYMMDDHH24MISS')
                AND A.CMTR_GRAD_CD = C.CMMN_CD(+))
        ]]>
    </select>

    <select id="findAllDetlTrafLinkHs" parameterType="java.util.HashMap" resultType="com.its.op.dto.its.common.DetlTrafLinkDto$DetlTrafLinkInfo">
        <![CDATA[
        SELECT *
        FROM (SELECT A.PRCN_DT,
                     A.CMTR_GRAD_CD,
                     A.SPED,
                     A.TRVL_HH,
                     A.MISS_YN,
                     A.PRCN_SPED,
                     A.SMTH_SPED,
                     A.ADJS_SPED,
                     A.PTRN_SPED,
                     A.DATA_NUM,
                     DECODE(C.CMMN_CD, NULL, A.CMTR_GRAD_CD, C.CMMN_CD_KOR_NM) AS CMTR_GRAD_DESC,
                     DECODE(B.CMMN_CD, NULL, A.PRCN_SORC, B.CMMN_CD_KOR_NM)    AS PRCN_SORC_DESC,
                     DECODE(D.CMMN_CD, NULL, A.ADJS_SORC, D.CMMN_CD_KOR_NM)    AS ADJS_SORC_DESC,
                     A.RANK
              FROM (SELECT RANK() OVER ( PARTITION BY LINK_ID ORDER BY LINK_ID, PRCN_DT DESC) AS RANK,
                           PRCN_DT,
                           MISS_YN,
                           PRCN_SORC,
                           ADJS_SORC,
                           SPED,
                           TRVL_HH,
                           CMTR_GRAD_CD,
                           PRCN_SPED,
                           SMTH_SPED,
                           ADJS_SPED,
                           PTRN_SPED,
                           DATA_NUM
                    FROM TB_LINK_TRAF_HS
                    WHERE LINK_ID = TO_NUMBER(#{ID})
                      AND PRCN_DT > TO_CHAR(SYSDATE - 1 / 24, 'YYYYMMDDHH24MISS')) A,
                   (SELECT *
                    FROM TB_CMMN_CD
                    WHERE CMMN_CLSF_CD = 'CST'
                       OR CMMN_CLSF_CD = 'MCS') B,
                   (SELECT *
                    FROM TB_CMMN_CD
                    WHERE CMMN_CLSF_CD = 'CST'
                       OR CMMN_CLSF_CD = 'MCS') D,
                   (SELECT *
                    FROM TB_CMMN_CD
                    WHERE CMMN_CLSF_CD = 'LTC') C
              WHERE RANK <= 12
                AND A.PRCN_SORC = B.CMMN_CD(+)
                AND A.ADJS_SORC = D.CMMN_CD(+)
                AND A.CMTR_GRAD_CD = C.CMMN_CD(+)
              ORDER BY RANK)
        ]]>
    </select>
    <select id="findAllDetlTrafIfscHs" parameterType="java.util.HashMap" resultType="com.its.op.dto.its.common.DetlTrafIfscDto$DetlTrafIfscInfo">
        <![CDATA[
        SELECT *
        FROM (SELECT A.PRCN_DT,
                     A.CMTR_GRAD_CD,
                     A.SPED,
                     A.TRVL_HH,
                     A.PTRN_SPED,
                     A.DATA_NUM,
                     C.CMMN_CD_KOR_NM AS CMTR_GRAD_DESC,
                     A.RANK
              FROM (SELECT RANK() OVER ( PARTITION BY IFSC_ID ORDER BY IFSC_ID, PRCN_DT DESC) AS RANK,
                           PRCN_DT,
                           SPED,
                           TRVL_HH,
                           CMTR_GRAD_CD,
                           PTRN_SPED,
                           DATA_NUM
                    FROM TB_IFSC_TRAF_HS
                    WHERE IFSC_ID = TO_NUMBER(#{ID})
                      AND PRCN_DT > TO_CHAR(SYSDATE - 1 / 24, 'YYYYMMDDHH24MISS')) A,
                   (SELECT *
                    FROM TB_CMMN_CD
                    WHERE CMMN_CLSF_CD = 'LTC') C
              WHERE RANK <= 12
                AND A.CMTR_GRAD_CD = C.CMMN_CD(+)
              ORDER BY RANK)
        ]]>
    </select>
    <select id="findAllDetlTrafRoadHs" parameterType="java.util.HashMap" resultType="com.its.op.dto.its.common.DetlTrafRoadDto$DetlTrafRoadInfo">
        <![CDATA[
        SELECT *
        FROM (SELECT A.PRCN_DT,
                     A.CMTR_GRAD_CD,
                     A.SPED,
                     A.TRVL_HH,
                     A.PTRN_SPED,
                     A.DATA_NUM,
                     C.CMMN_CD_KOR_NM AS CMTR_GRAD_DESC,
                     A.RANK
              FROM (SELECT RANK() OVER ( PARTITION BY ROAD_ID ORDER BY ROAD_ID, PRCN_DT DESC) RANK,
                           PRCN_DT,
                           SPED,
                           TRVL_HH,
                           CMTR_GRAD_CD,
                           PTRN_SPED,
                           DATA_NUM
                    FROM TB_ROAD_TRAF_HS
                    WHERE ROAD_ID = TO_NUMBER(#{ID})
                      AND PRCN_DT > TO_CHAR(SYSDATE - 1 / 24, 'YYYYMMDDHH24MISS')) A,
                   (SELECT *
                    FROM TB_CMMN_CD
                    WHERE CMMN_CLSF_CD = 'LTC') C
              WHERE RANK <= 12
                AND A.CMTR_GRAD_CD = C.CMMN_CD(+)
              ORDER BY RANK)
        ]]>
    </select>

    <!--    <select id="findAllWeatherInfo" resultType="com.its.op.dto.its.common.WeatherInfoDto">-->
<!--        SELECT * FROM-->
<!--            (SELECT-->
<!--                 ANNOUNCEDATE,-->
<!--                 ANNOUNCETIMECODE,-->
<!--                 NVL(TEMPERATURE,'-') TEMPERATURE,-->
<!--                 WEATHERCODE,-->
<!--                 MINTEMPERATURE,-->
<!--                 MAXTEMPERATURE-->
<!--             FROM KMA_OVERLAND-->
<!--             WHERE ANNOUNCEDATE > SYSDATE - 1-->
<!--               AND ANNOUNCETIMECODE = 0-->
<!--             ORDER BY ANNOUNCEDATE DESC)-->
<!--        WHERE ROWNUM = 1-->
<!--    </select>-->

</mapper>
