<?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.web.mapper.its.traffic.TrafficMapper">
    <select id="findAllCctv" resultType="com.its.web.dto.traffic.TbCctvCtlrDto">
        SELECT
            cctv_mngm_nmbr
            , cctv_ctlr_id
            , cctv_ctlr_ip
            , cctv_ctlr_port
            , cctv_capt_ip
            , cctv_capt_port
            , cctv_fibr_ip
            , cctv_encd_ip
            , strm_svr_ip
            , strm_svr_port
            , strm_sesn_nm
            , x_crdn
            , y_crdn
            , istl_lctn_nm
            , istl_lctn_addr
            , cctv_chnl
            , del_yn
            , frst_regr_nmbr
            , frst_rgst_dt
            , last_crpr_nmbr
            , last_crct_dt
            , link_id
            , node_id
            , cctv_id
            , rely_port
            , strm_rtsp_addr
            , strm_rtmp_addr
            , strm_http_addr
            , strm_stor_addr
            , area_cd
            , cctv_type
            , cctv_sbst_imgn
            , cctv_sbst_dspl_yn
            , cctv_lc
            , cmra_id
            , cmra_pswd
            , cmra_chnl
            , cmra_modl
            , full_strm_sesn_nm
            , cmra_port
            , hmpg_dspl_en
            , mac_addr
            , serial_no
            , tta_cnfn_yn
        FROM
            TB_CCTV_CTLR
        WHERE
            DEL_YN = 'N'
        ORDER BY ISTL_LCTN_NM
    </select>

    <select id="findAllVms" resultType="com.its.web.dto.traffic.TbVmsCtlrDto">
        SELECT
           vms_ctlr_nmbr
            ,vms_id
            ,vms_ctlr_id
            ,vms_nm
            ,vms_ctlr_ip
            ,vms_usag_type_cd
            ,vms_frm_type_cd
            ,vms_type_cd
            ,vms_modl_type_cd
            ,vms_cmnc_port
            ,oper_mode
            ,vms_cmnc_err_bass_val
            ,vms_nght_brgh_step
            ,vms_week_brgh_step
            ,vms_phse_chng_cycl
            ,vms_modl_err_rate
            ,cmncfail_slot_nmbr
            ,pwer_fail_slot_nmbr
            ,del_yn
            ,cmtrinfr_cnct_yn
            ,wthrinfr_cnct_yn
            ,envrinfr_cnct_yn
            ,vms_max_phse_num
            ,frst_regr_nmbr
            ,frst_rgst_dt
            ,last_crpr_nmbr
            ,last_crct_dt
            ,panl_on_time
            ,panl_off_time
            ,panl_pwer_mode
            ,brgh_mode
            ,web_cmra_ip
            ,x_crdn
            ,y_crdn
            ,istl_lctn_nm
            ,trfc_strg_use_yn
            ,vms_loc_ifsc_id
            ,fan_mtns_tmpr
            ,hetr_mtns_tmpr
            ,fan_mode
            ,hetr_mode
            ,brgh_curr_step
            ,web_cmra_port
            ,web_cmra_id
            ,web_cmra_pwd
            ,strm_sesn_nm
            ,web_cmra_cmpy
            ,web_cmra_mdl
            ,istl_lctn_addr
            ,full_strm_sesn_nm
            ,protocol_ver
            ,web_cmra_protocol
            ,area_code
        FROM
            TB_VMS_CTLR
        WHERE
            del_yn = 'N'
        ORDER BY istl_lctn_nm ASC
    </select>

    <select id="findVmsDsplPhaseInfo" parameterType="java.lang.String" resultType="com.its.web.dto.traffic.VmsDsplPhaseDto">
        SELECT A.VMS_CTLR_NMBR,
               A.PHASE,
               A.VMS_DSPL_MSG_TXT
        FROM TB_VMS_DSPL_PRST A,
             (SELECT VMS_CTLR_NMBR, MAX(DSPL_DT) AS DSPL_DT
              FROM TB_VMS_DSPL_PRST
              --WHERE DSPL_DT >= TO_CHAR((SYSDATE - 8/1440),'YYYYMMDDHH24MISS')
              GROUP by VMS_CTLR_NMBR) B
        WHERE A.VMS_CTLR_NMBR = B.VMS_CTLR_NMBR
          AND A.DSPL_DT       = B.DSPL_DT
          AND A.VMS_CTLR_NMBR = #{vmsCtlrNmbr}
        ORDER BY A.VMS_CTLR_NMBR, A.PHASE
    </select>

    <select id="findVmsDsplImage" parameterType="java.util.HashMap" resultType="java.util.HashMap">
        SELECT VMS_DSPL_MSG_IMAG
          FROM TB_VMS_DSPL_PRST
         WHERE VMS_CTLR_NMBR = #{vmsCtlrNmbr}
           AND PHASE         = #{phase}
    </select>

    <select id="findAllIncident" resultType="com.its.web.dto.traffic.TbIncdOcrrDto">
        SELECT
            incd_ocrr_id,
            link_id,
--             SUBSTR(INCD_STRT_DT, 0,4) || '-' ||
--             SUBSTR(INCD_STRT_DT, 5,2) || '-' ||
--             SUBSTR(INCD_STRT_DT, 7,2) || ' ' ||
--             SUBSTR(INCD_STRT_DT, 9,2) || '시' AS incd_strt_dt,
--             SUBSTR(INCD_END_PRAR_DT, 0,4) || '-' ||
--             SUBSTR(INCD_END_PRAR_DT, 5,2) || '-' ||
--             SUBSTR(INCD_END_PRAR_DT, 7,2) || ' ' ||
--             SUBSTR(INCD_END_PRAR_DT, 9,2) || '시' AS incd_end_prar_dt,
            DECODE(INCD_STRT_DT, null, '-', TO_CHAR(TO_DATE(INCD_STRT_DT, 'YYYY-MM-DD HH24:MI:SS'), 'YYYY-MM-DD HH24:MI')) AS incd_strt_dt,
            DECODE(INCD_END_PRAR_DT, null, '-', TO_CHAR(TO_DATE(INCD_END_PRAR_DT, 'YYYY-MM-DD HH24:MI:SS'), 'YYYY-MM-DD HH24:MI')) AS incd_end_prar_dt,
            DECODE(OCRR_LCTN_NM, NULL, '-', ' ', '-', OCRR_LCTN_NM) AS road_nm,
            DECODE(INCD_TITL, NULL, '-', ' ', '-', INCD_TITL) AS incd_titl,
            DECODE(INCD_EXPL, NULL, '-', ' ', '-', INCD_EXPL) AS incd_expl,
            incd_type_clsf_cd,
            cmtr_grad_cd,
            x_crdn,
            y_crdn
        FROM
            VW_INCD_OCRR A
        WHERE incd_prgr_step_cd = 'ISS2'
          and TO_DATE(incd_end_prar_dt, 'YYYY-MM-DD HH24:MI:SS') > sysdate
        ORDER BY rgst_dt desc
    </select>

    <select id="findMainIncident" resultType="com.its.web.dto.traffic.TbIncdOcrrDto">
        select
            B.incd_ocrr_id,
            B.ago_minutes,
            B.incd_titl
        from (select
                  A.*,
                  ROWNUM
              from (select
                        incd_ocrr_id,
                        ROUND(
                                (
                                    sysdate - TO_DATE(incd_strt_dt, 'YYYY-MM-DD HH24:MI:SS')
                                ) * 24 * 60
                        , 0) as ago_minutes,
                        incd_titl
                     from VW_INCD_OCRR
                    where incd_prgr_step_cd = 'ISS2'
                      and TO_DATE(incd_end_prar_dt, 'YYYY-MM-DD HH24:MI:SS') > sysdate
                    order by rgst_dt desc) A
             ) B
        where ROWNUM BETWEEN 1 AND 3
    </select>

    <select id="findAllIntersectionCameraArrow" resultType="com.its.web.dto.traffic.TbScCmraArrowDto">
        SELECT
            ixr_id,
            cmra_id,
            drct_dvsn_cd,
            start_x,
            end_x,
            start_y,
            end_y
          FROM
            TB_SC_CMRA_ARROW
    </select>

    <select id="findLinkVertexByRange" resultType="com.its.web.dto.traffic.VertexDto" parameterType="java.util.HashMap">
    <![CDATA[
        SELECT
            AA.ROADWAY_ID,
            AA.ROADWAY_NM,
            AA.STRT_NM_NODE,
            AA.END_NM_NODE,
            AA.X_CRDN,
            AA.Y_CRDN,
            AA.X_CRDN_MIN,
            AA.Y_CRDN_MIN,
            AA.X_CRDN_MAX,
            AA.Y_CRDN_MAX,
            BB.CMTR_GRAD_CD,
            BB.SPED,
            ROUND(BB.TRVL_HH / 60) + 1 TRVL_HH,
            CC.CMMN_CD_KOR_NM AS GRAD_NM
        FROM
            (
                SELECT
                    A.LINK_ID ROADWAY_ID,
                    A.ROAD_NAME ROADWAY_NM,
                    NVL((SELECT NODE_NAME FROM TB_NODE WHERE A.F_NODE_ID = NODE_ID),'-') STRT_NM_NODE,
                    NVL((SELECT NODE_NAME FROM TB_NODE WHERE A.T_NODE_ID = NODE_ID),'-') END_NM_NODE,
                    B.X_CRDN,
                    B.Y_CRDN,
                    B.X_CRDN_MIN,
                    B.Y_CRDN_MIN,
                    B.X_CRDN_MAX,
                    B.Y_CRDN_MAX
                FROM
                    TB_LINK A,
                    (
                        SELECT
                            LINK_ID,
                            X_CRDN_ARR AS X_CRDN,
                            Y_CRDN_ARR AS Y_CRDN,
                            X_CRDN_MIN,
                            Y_CRDN_MIN,
                            X_CRDN_MAX,
                            Y_CRDN_MAX
                        FROM TB_LINK_VRTX_ARR
                        WHERE LEVL = #{levl}
                          AND #{swLat} <= Y_CRDN_MAX
                          AND #{neLat} >= Y_CRDN_MIN
                          AND #{swLng} <= X_CRDN_MAX
                          AND #{neLng} >= X_CRDN_MIN
                    ) B
                WHERE A.LINK_ID = B.LINK_ID
            ) AA LEFT OUTER JOIN TB_LINK_TRAF BB ON AA.ROADWAY_ID = BB.LINK_ID
                 INNER JOIN TB_CMMN_CD CC ON BB.CMTR_GRAD_CD = CC.CMMN_CD
    ]]>
    </select>

    <select id="findIfscVertexByRange" resultType="com.its.web.dto.traffic.VertexDto" parameterType="java.util.HashMap">
        <![CDATA[
        SELECT
            AA.ROADWAY_ID,
            AA.ROADWAY_NM,
            AA.STRT_NM_NODE,
            AA.END_NM_NODE,
            AA.X_CRDN,
            AA.Y_CRDN,
            AA.X_CRDN_MIN,
            AA.Y_CRDN_MIN,
            AA.X_CRDN_MAX,
            AA.Y_CRDN_MAX,
            BB.CMTR_GRAD_CD,
            BB.SPED,
            ROUND(BB.TRVL_HH / 60) + 1 TRVL_HH,
            CC.CMMN_CD_KOR_NM AS GRAD_NM
        FROM
            (
                SELECT
                    A.IFSC_ID ROADWAY_ID,
                    A.IFSC_NM ROADWAY_NM,
                    NVL((SELECT NODE_NAME FROM TB_NODE WHERE A.F_NODE_ID = NODE_ID),'-') STRT_NM_NODE,
                    NVL((SELECT NODE_NAME FROM TB_NODE WHERE A.T_NODE_ID = NODE_ID),'-') END_NM_NODE,
                    B.X_CRDN,
                    B.Y_CRDN,
                    B.X_CRDN_MIN,
                    B.Y_CRDN_MIN,
                    B.X_CRDN_MAX,
                    B.Y_CRDN_MAX
                FROM
                    TB_IFSC A,
                    (
                        SELECT
                            IFSC_ID,
                            X_CRDN_ARR AS X_CRDN,
                            Y_CRDN_ARR AS Y_CRDN,
                            X_CRDN_MIN,
                            Y_CRDN_MIN,
                            X_CRDN_MAX,
                            Y_CRDN_MAX
                        FROM TB_IFSC_VRTX_ARR
                        WHERE LEVL = #{levl}
                          AND #{swLat} <= Y_CRDN_MAX
                          AND #{neLat} >= Y_CRDN_MIN
                          AND #{swLng} <= X_CRDN_MAX
                          AND #{neLng} >= X_CRDN_MIN
                    ) B
                WHERE A.IFSC_ID = B.IFSC_ID
            ) AA LEFT OUTER JOIN TB_IFSC_TRAF BB ON AA.ROADWAY_ID = BB.IFSC_ID
                 INNER JOIN TB_CMMN_CD CC ON BB.CMTR_GRAD_CD = CC.CMMN_CD;
        ]]>
    </select>

    <select id="findRoadVertexByRange" resultType="com.its.web.dto.traffic.VertexDto" parameterType="java.util.HashMap">
        <![CDATA[
        SELECT
            AA.ROADWAY_ID,
            AA.ROADWAY_NM,
            AA.STRT_NM_NODE,
            AA.END_NM_NODE,
            AA.X_CRDN,
            AA.Y_CRDN,
            AA.X_CRDN_MIN,
            AA.Y_CRDN_MIN,
            AA.X_CRDN_MAX,
            AA.Y_CRDN_MAX,
            BB.CMTR_GRAD_CD,
            BB.SPED,
            ROUND(BB.TRVL_HH / 60) + 1 TRVL_HH,
            CC.CMMN_CD_KOR_NM AS GRAD_NM
        FROM
            (
                SELECT
                    A.ROAD_ID ROADWAY_ID,
                    A.ROAD_NAME ROADWAY_NM,
                    NVL((SELECT NODE_NAME FROM TB_NODE WHERE A.F_NODE_ID = NODE_ID),'-') STRT_NM_NODE,
                    NVL((SELECT NODE_NAME FROM TB_NODE WHERE A.T_NODE_ID = NODE_ID),'-') END_NM_NODE,
                    B.X_CRDN,
                    B.Y_CRDN,
                    B.X_CRDN_MIN,
                    B.Y_CRDN_MIN,
                    B.X_CRDN_MAX,
                    B.Y_CRDN_MAX
                FROM
                    TB_ROAD A,
                    (
                        SELECT
                            AA.ROAD_ID,
                            AA.X_CRDN_ARR AS X_CRDN,
                            AA.Y_CRDN_ARR AS Y_CRDN,
                            AA.X_CRDN_MIN,
                            AA.Y_CRDN_MIN,
                            AA.X_CRDN_MAX,
                            AA.Y_CRDN_MAX
                        FROM TB_ROAD_VRTX_ARR AA
                        WHERE LEVL = #{levl}
                        AND #{swLat} <= Y_CRDN_MAX
                        AND #{neLat} >= Y_CRDN_MIN
                        AND #{swLng} <= X_CRDN_MAX
                        AND #{neLng} >= X_CRDN_MIN
                    ) B
                WHERE A.ROAD_ID = B.ROAD_ID
            ) AA LEFT OUTER JOIN TB_ROAD_TRAF BB ON AA.ROADWAY_ID = BB.ROAD_ID
                 INNER JOIN TB_CMMN_CD CC ON BB.CMTR_GRAD_CD = CC.CMMN_CD
        ]]>
    </select>

    <select id="findAllAtrd" resultType="com.its.web.dto.traffic.TbAtrdDto">
        SELECT
            A.atrd_id,
            A.atrd_nm,
            A.drct_cd,
            B.cmmn_cd_kor_nm AS drct_nm
        FROM TB_ATRD A
        JOIN (SELECT * FROM TB_CMMN_CD WHERE cmmn_clsf_cd = 'IFD') B
          ON A.drct_cd = B.cmmn_cd
        WHERE A.del_yn = 'N'
    </select>

    <select id="findAtrdNameList" resultType="com.its.web.dto.traffic.TbAtrdDto">
        SELECT
            DISTINCT(atrd_nm)
        FROM TB_ATRD A
        WHERE A.del_yn = 'N'
        ORDER BY atrd_nm
    </select>

    <select id="findAtrdRoadVertexByIdAndLevel" parameterType="java.util.HashMap" resultType="com.its.web.dto.traffic.TbAtrdDto">
        SELECT
            A.atrd_id,
            A.atrd_nm,
            A.drct_cd,
            B.cmmn_cd_kor_nm AS drct_nm,
            C.road_id,
            C.ord,
            D.crdn_cnt,
            D.x_crdn_arr,
            D.y_crdn_arr,
            D.x_crdn_min,
            D.y_crdn_min,
            D.x_crdn_max,
            D.y_crdn_max,
            E.road_name,
            E.strt_nm,
            E.end_nm,
            E.t_node_id,
            E.f_node_id,
            CASE
                WHEN F.PRCN_DT >= TO_CHAR(SYSDATE - 10/24/60,'YYYYMMDDHH24MISS')
                    THEN F.CMTR_GRAD_CD
                ELSE 'LTC0'
                END CMTR_GRAD_CD,
            CASE
                WHEN F.PRCN_DT >= TO_CHAR(SYSDATE - 10/24/60,'YYYYMMDDHH24MISS')
                    THEN NVL(F.SPED,0)
                ELSE 0
                END SPED,
            CASE
                WHEN F.PRCN_DT >= TO_CHAR(SYSDATE - 10/24/60,'YYYYMMDDHH24MISS')
                    THEN NVL(ROUND(F.TRVL_HH / 60) + 1,0)
                ELSE 0
                END TRVL_HH
        FROM TB_ATRD A,
             TB_CMMN_CD B,
             TB_ATRD_ROAD_RLTN C,
             TB_ROAD_VRTX_ARR D,
             TB_ROAD E
        LEFT OUTER JOIN TB_ROAD_TRAF F
                     ON F.ROAD_ID = E.ROAD_ID
        WHERE 1=1
          AND A.del_yn = 'N'
          AND A.drct_cd = B.cmmn_cd
          AND C.ATRD_ID = A.ATRD_ID
          AND C.ROAD_ID = D.ROAD_ID
          AND B.cmmn_clsf_cd = 'IFD'
          AND E.ROAD_ID = D.ROAD_ID
          AND E.del_yn = 'N'
          AND D.levl = #{levl}
        <if test="upHillId != null and downHillId != null ">
            AND A.atrd_id in (#{upHillId}, #{downHillId})
        </if>
        ORDER BY A.atrd_id, C.ord;
    </select>

    <select id="findAtrdIfscVertexByIdAndLevel" parameterType="java.util.HashMap" resultType="com.its.web.dto.traffic.TbAtrdDto">
        SELECT
            A.atrd_id,
            A.atrd_nm,
            A.drct_cd,
            B.cmmn_cd_kor_nm AS drct_nm,
            D.ifsc_id AS road_id,
            D.ord,
            E.crdn_cnt,
            E.x_crdn_arr,
            E.y_crdn_arr,
            E.x_crdn_min,
            E.y_crdn_min,
            E.x_crdn_max,
            E.y_crdn_max,
            F.ifsc_nm AS road_name,
            F.strt_nm,
            F.end_nm,
            F.t_node_id,
            F.f_node_id,
            CASE
                WHEN G.PRCN_DT >= TO_CHAR(SYSDATE - 10/24/60,'YYYYMMDDHH24MISS')
                    THEN G.CMTR_GRAD_CD
                ELSE 'LTC0'
                END CMTR_GRAD_CD,
            CASE
                WHEN G.PRCN_DT >= TO_CHAR(SYSDATE - 10/24/60,'YYYYMMDDHH24MISS')
                    THEN NVL(G.SPED,0)
                ELSE 0
                END SPED,
            CASE
                WHEN G.PRCN_DT >= TO_CHAR(SYSDATE - 10/24/60,'YYYYMMDDHH24MISS')
                    THEN NVL(ROUND(G.TRVL_HH / 60) + 1,0)
                ELSE 0
                END TRVL_HH
        FROM TB_ATRD A,
             TB_CMMN_CD B,
             TB_ATRD_ROAD_RLTN C,
             TB_ROAD_IFSC_RLTN D,
             TB_IFSC_VRTX_ARR E,
             TB_IFSC F
        LEFT OUTER JOIN TB_IFSC_TRAF G
                     ON F.IFSC_ID = G.IFSC_ID
        WHERE 1=1
          AND A.del_yn = 'N'
          AND A.drct_cd = B.cmmn_cd
          AND C.ATRD_ID = A.ATRD_ID
          AND C.ROAD_ID = D.ROAD_ID
          AND B.cmmn_clsf_cd = 'IFD'
          AND E.IFSC_ID = D.IFSC_ID
          AND F.IFSC_ID = E.IFSC_ID
          AND F.del_yn = 'N'
          AND E.levl = #{levl}
        <if test="upHillId != null and downHillId != null ">
            AND A.atrd_id in (#{upHillId}, #{downHillId})
        </if>
        ORDER BY A.atrd_id, C.ord, D.ord;
    </select>

    <select id="findAtrdLinkVertexByIdAndLevel" parameterType="java.util.HashMap" resultType="com.its.web.dto.traffic.TbAtrdDto">
        SELECT
            A.atrd_id,
            A.atrd_nm,
            A.drct_cd,
            B.cmmn_cd_kor_nm AS drct_nm,
            G.link_id AS road_id,
            E.ord,
            F.crdn_cnt,
            F.x_crdn_arr,
            F.y_crdn_arr,
            F.x_crdn_min,
            F.y_crdn_min,
            F.x_crdn_max,
            F.y_crdn_max,
            G.road_name,
            I.node_name as strt_nm,
            J.node_name as end_nm,
            G.t_node_id,
            G.f_node_id,
            CASE
                WHEN H.PRCN_DT >= TO_CHAR(SYSDATE - 10/24/60,'YYYYMMDDHH24MISS')
                    THEN H.CMTR_GRAD_CD
                ELSE 'LTC0'
                END CMTR_GRAD_CD,
            CASE
                WHEN H.PRCN_DT >= TO_CHAR(SYSDATE - 10/24/60,'YYYYMMDDHH24MISS')
                    THEN NVL(H.SPED,0)
                ELSE 0
                END SPED,
            CASE
                WHEN H.PRCN_DT >= TO_CHAR(SYSDATE - 10/24/60,'YYYYMMDDHH24MISS')
                    THEN NVL(ROUND(H.TRVL_HH / 60) + 1,0)
                ELSE 0
                END TRVL_HH
        FROM TB_ATRD A,
             TB_CMMN_CD B,
             TB_ATRD_ROAD_RLTN C,
             TB_ROAD_IFSC_RLTN D,
             TB_IFSC_LINK_RLTN E,
             TB_LINK_VRTX_ARR F,
             TB_LINK G,
             TB_NODE I,
             TB_NODE J,
             TB_LINK_TRAF H
        WHERE 1=1
          AND A.del_yn = 'N'
          AND A.drct_cd = B.cmmn_cd
          AND C.ATRD_ID = A.ATRD_ID
          AND C.ROAD_ID = D.ROAD_ID
          AND B.cmmn_clsf_cd = 'IFD'
          AND E.IFSC_ID = D.IFSC_ID
          AND F.link_ID = E.link_ID
          AND E.link_id = G.link_id
          AND G.link_id = H.link_id
          AND G.del_yn = 'N'
          AND F.levl = #{levl}
          AND G.F_NODE_ID = I.NODE_ID
          AND G.T_NODE_ID = J.NODE_ID
          <if test="upHillId != null and downHillId != null ">
            AND A.atrd_id in (#{upHillId}, #{downHillId})
          </if>
        ORDER BY A.atrd_id, C.ord, D.ord, E.ord;
    </select>

    <update id="updateCctvHmpgUseYn" parameterType="java.util.HashMap">
        UPDATE TB_CCTV_CTLR SET hmpg_dspl_en = #{useYn} WHERE cctv_mngm_nmbr = #{cctvMngmNmbr}
    </update>
</mapper>