<?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.itcs.IntersectionMapper">
    <select id="findAllIntersection" resultType="com.its.web.dto.traffic.IntersectionDto">
        SELECT
            A.ixr_id
            , A.ixr_nm
            , A.phas_cnt
            , A.spot_fetr
            , A.y_crdn AS x_crdn
            , A.x_crdn AS y_crdn
            , A.ixr_type
            , A.seq
            , NVL(A.dvsn, 1) dvsn
            , A.use_en
            , B.clct_dt
            , B.ixr_los
            , B.dely_hh
         FROM IXR_MNGM A
         LEFT OUTER JOIN IXR_LOS_STTS B
           ON A.ixr_id = B.ixr_id
        WHERE A.use_en = 1
        ORDER BY A.ixr_nm
    </select>

    <select id="getIxrComboList" resultType="com.its.web.dto.traffic.IntersectionDvsnDto">
        select distinct(dvsn) dvsn from ixr_mngm
    </select>

    <select id="getIxrCameraCount" parameterType="java.util.HashMap" resultType="com.its.web.dto.traffic.IntersectionCameraCountDto">
        select count(1) as total,
               sum(decode(b.hmpg_dspl_en, 1, 1, 0)) as disabled
          from ixr_mngm a, cmra_mngm b
         where a.dvsn = #{dvsn}
           and a.ixr_id = b.ixr_id
    </select>

    <select id="findEditCameraList" parameterType="java.util.HashMap" resultType="com.its.web.dto.traffic.IntersectionCameraDto">
        SELECT
            im.ixr_id,
            im.ixr_nm,
            cmra.cmra_id,
            cmra.drct_dvsn_cd,
            cmra.drct_lctn,
            cmra.bus_dvrs_lane_en,
            cmra.srvc_anly_en,
            cmra.cmra_ip,
            cmra.cmra_port,
            cmra.cmra_http_port,
            cmra.cmra_url,
            cmra.cmra_type,
            cmra.cmra_x_crdn,
            cmra.cmra_y_crdn,
            cmra.cmra_angn,
            cmra.cmra_use_yn,
            cmra.hmpg_cmra_url,
            cmra.istl_lctn
        FROM (select *
                from itcs.ixr_mngm
               where use_en = 1
                 and dvsn = #{dvsn}) im
        left outer join (
                        SELECT
                            CM.ixr_id,
                            CM.cmra_id,
                            CDM.drct_dvsn_cd,
                            CDM.drct_lctn,
                            CDM.bus_dvrs_lane_en,
                            CDM.srvc_anly_en,
                            CM.cmra_ip,
                            CM.cmra_port,
                            CM.cmra_http_port,
                            CM.cmra_url,
                            CM.cmra_type,
                            CM.cmra_y_crdn AS cmra_x_crdn,
                            CM.cmra_x_crdn AS cmra_y_crdn,
                            CM.cmra_angn,
                            CM.hmpg_dspl_en AS cmra_use_yn,
                            CM.hmpg_cmra_url,
                            CM.istl_lctn AS istl_lctn
                        FROM itcs.CMRA_MNGM CM
                        JOIN itcs.CMRA_DRCT_MNGM CDM
                          ON CM.ixr_id = CDM.ixr_id
                         AND CM.cmra_id = CDM.cmra_id
            ) cmra
         on im.ixr_id = cmra.ixr_id
        order by ixr_nm, drct_lctn
    </select>


    <select id="findAllIntersectionDetail" resultType="com.its.web.dto.traffic.IntersectionCameraDto">
        SELECT
            CM.ixr_id,
            CM.cmra_id,
            CDM.drct_dvsn_cd,
            CDM.drct_lctn,
            CDM.bus_dvrs_lane_en,
            CDM.srvc_anly_en,
            CM.cmra_ip,
            CM.cmra_port,
            CM.cmra_http_port,
            CM.cmra_url,
            CM.cmra_type,
            CM.cmra_y_crdn AS cmra_x_crdn,
            CM.cmra_x_crdn AS cmra_y_crdn,
            CM.cmra_angn,
            ALS.acrd_los,
            ALS.dely_hh,
            CM.hmpg_dspl_en AS cmra_use_yn,
            CM.hmpg_cmra_url,
            CM.istl_lctn AS istl_lctn
        FROM CMRA_MNGM CM
        JOIN CMRA_DRCT_MNGM CDM
          ON CM.ixr_id = CDM.ixr_id
         AND CM.cmra_id = CDM.cmra_id
        LEFT OUTER JOIN ACRD_LOS_STTS ALS
          ON CM.ixr_id = ALS.ixr_id
         AND CDM.drct_dvsn_cd = ALS.drct_dvsn_cd
       WHERE CM.hmpg_dspl_en = 1
    </select>

    <select id="findAllIntersectionCamera" resultType="com.its.web.dto.traffic.IntersectionCameraDto">
        SELECT
            CM.ixr_id,
            CM.cmra_id,
            CDM.drct_dvsn_cd,
            CDM.drct_lctn,
            CDM.bus_dvrs_lane_en,
            CDM.srvc_anly_en,
            CM.cmra_ip,
            CM.cmra_port,
            CM.cmra_http_port,
            CM.cmra_url,
            CM.cmra_type,
            CM.cmra_y_crdn AS cmra_x_crdn,
            CM.cmra_x_crdn AS cmra_y_crdn,
            CM.cmra_angn,
            ALS.acrd_los,
            ALS.dely_hh,
            CM.hmpg_dspl_en AS cmra_use_yn,
            CM.hmpg_cmra_url,
            CM.istl_lctn AS istl_lctn
        FROM CMRA_MNGM CM
                 JOIN CMRA_DRCT_MNGM CDM
                   ON CM.ixr_id = CDM.ixr_id
                  AND CM.cmra_id = CDM.cmra_id
                 JOIN ACRD_LOS_STTS ALS
                   ON CM.ixr_id = ALS.ixr_id
                  AND CDM.drct_dvsn_cd = ALS.drct_dvsn_cd
        WHERE CDM.drct_dvsn_cd=ALS.drct_dvsn_cd
          AND CM.hmpg_dspl_en = 1
    </select>


    <select id="findIntersectionAtrdName" resultType="com.its.web.dto.statistics.DaeroMngmDto">
        SELECT
            nmbr,
            daero_nm
        FROM
            DAERO_MNGM
    </select>

    <select id="findStatisticsTrafficAmountByMonth" parameterType="java.util.HashMap" resultType="com.its.web.dto.statistics.TrafficStatisticsDto">
        SELECT
           L.STRT_IXR AS strt_name,
           L.END_IXR AS end_name,
           L.IXR_ID AS ixr_id,
           L.IXR_NM AS ixr_nm,
           T.T01 AS t01,
           T.T02 AS t02,
           T.T03 AS t03,
           T.T04 AS t04,
           T.T05 AS t05,
           T.T06 AS t06,
           T.T07 AS t07,
           T.T08 AS t08,
           T.T09 AS t09,
           T.T10 AS t10,
           T.T11 AS t11,
           T.T12 AS t12,
           T.T13 AS t13,
           T.T14 AS t14,
           T.T15 AS t15,
           T.T16 AS t16,
           T.T17 AS t17,
           T.T18 AS t18,
           T.T19 AS t19,
           T.T20 AS t20,
           T.T21 AS t21,
           T.T22 AS t22,
           T.T23 AS t23,
           T.T24 AS t24,
           T.T25 AS t25,
           T.T26 AS t26,
           T.T27 AS t27,
           T.T28 AS t28,
           T.T29 AS t29,
           T.T30 AS t30,
           T.T31 AS t31
          FROM LINK_MNGM L
              INNER JOIN (
                  SELECT
                     LINK_ID,
                     NVL(SUM(DECODE(SUBSTR(CLCT_HH, 9, 2),'01',TFVL)), 0) AS T01,
                     NVL(SUM(DECODE(SUBSTR(CLCT_HH, 9, 2),'02',TFVL)), 0) AS T02,
                     NVL(SUM(DECODE(SUBSTR(CLCT_HH, 9, 2),'03',TFVL)), 0) AS T03,
                     NVL(SUM(DECODE(SUBSTR(CLCT_HH, 9, 2),'04',TFVL)), 0) AS T04,
                     NVL(SUM(DECODE(SUBSTR(CLCT_HH, 9, 2),'05',TFVL)), 0) AS T05,
                     NVL(SUM(DECODE(SUBSTR(CLCT_HH, 9, 2),'06',TFVL)), 0) AS T06,
                     NVL(SUM(DECODE(SUBSTR(CLCT_HH, 9, 2),'07',TFVL)), 0) AS T07,
                     NVL(SUM(DECODE(SUBSTR(CLCT_HH, 9, 2),'08',TFVL)), 0) AS T08,
                     NVL(SUM(DECODE(SUBSTR(CLCT_HH, 9, 2),'09',TFVL)), 0) AS T09,
                     NVL(SUM(DECODE(SUBSTR(CLCT_HH, 9, 2),'10',TFVL)), 0) AS T10,
                     NVL(SUM(DECODE(SUBSTR(CLCT_HH, 9, 2),'11',TFVL)), 0) AS T11,
                     NVL(SUM(DECODE(SUBSTR(CLCT_HH, 9, 2),'12',TFVL)), 0) AS T12,
                     NVL(SUM(DECODE(SUBSTR(CLCT_HH, 9, 2),'13',TFVL)), 0) AS T13,
                     NVL(SUM(DECODE(SUBSTR(CLCT_HH, 9, 2),'14',TFVL)), 0) AS T14,
                     NVL(SUM(DECODE(SUBSTR(CLCT_HH, 9, 2),'15',TFVL)), 0) AS T15,
                     NVL(SUM(DECODE(SUBSTR(CLCT_HH, 9, 2),'16',TFVL)), 0) AS T16,
                     NVL(SUM(DECODE(SUBSTR(CLCT_HH, 9, 2),'17',TFVL)), 0) AS T17,
                     NVL(SUM(DECODE(SUBSTR(CLCT_HH, 9, 2),'18',TFVL)), 0) AS T18,
                     NVL(SUM(DECODE(SUBSTR(CLCT_HH, 9, 2),'19',TFVL)), 0) AS T19,
                     NVL(SUM(DECODE(SUBSTR(CLCT_HH, 9, 2),'20',TFVL)), 0) AS T20,
                     NVL(SUM(DECODE(SUBSTR(CLCT_HH, 9, 2),'21',TFVL)), 0) AS T21,
                     NVL(SUM(DECODE(SUBSTR(CLCT_HH, 9, 2),'22',TFVL)), 0) AS T22,
                     NVL(SUM(DECODE(SUBSTR(CLCT_HH, 9, 2),'23',TFVL)), 0) AS T23,
                     NVL(SUM(DECODE(SUBSTR(CLCT_HH, 9, 2),'24',TFVL)), 0) AS T24,
                     NVL(SUM(DECODE(SUBSTR(CLCT_HH, 9, 2),'25',TFVL)), 0) AS T25,
                     NVL(SUM(DECODE(SUBSTR(CLCT_HH, 9, 2),'26',TFVL)), 0) AS T26,
                     NVL(SUM(DECODE(SUBSTR(CLCT_HH, 9, 2),'27',TFVL)), 0) AS T27,
                     NVL(SUM(DECODE(SUBSTR(CLCT_HH, 9, 2),'28',TFVL)), 0) AS T28,
                     NVL(SUM(DECODE(SUBSTR(CLCT_HH, 9, 2),'29',TFVL)), 0) AS T29,
                     NVL(SUM(DECODE(SUBSTR(CLCT_HH, 9, 2),'30',TFVL)), 0) AS T30,
                     NVL(SUM(DECODE(SUBSTR(CLCT_HH, 9, 2),'31',TFVL)), 0) AS T31
                    FROM IXR_LINK_TFVL
                   WHERE TO_DATE(CLCT_HH, 'YYYY-MM-DD HH24:MI:SS')
                  BETWEEN TO_DATE(#{FROM_DT}, 'YYYY-MM-DD HH24:MI:SS')
                      AND TO_DATE(#{TO_DT}, 'YYYY-MM-DD HH24:MI:SS')
                  GROUP BY LINK_ID
              ) T ON T.LINK_ID=L.LINK_ID
          <if test="IXR_ID != null and !IXR_ID.equals('') and !IXR_ID.equals('ALL')">
              AND L.IXR_ID = #{IXR_ID}
          </if>
          INNER JOIN DAERO_MNGM D ON L.DAERO_DVSN=D.NMBR
          <if test="ROAD_NMBR != null and !ROAD_NMBR.equals('') and !ROAD_NMBR.equals('ALL')">
              WHERE D.NMBR=#{ROAD_NMBR}
          </if>
          GROUP BY T.LINK_ID, L.IXR_ID, L.IXR_NM, L.STRT_IXR, L.END_IXR, T01, T02, T03, T04, T05, T06, T07, T08,
                   T09, T10, T11, T12, T13, T14, T15, T16, T17, T18, T19, T20, T21, T22, T23,
                   T24, T25, T26, T27, T28, T29, T30, T31
    </select>


    <select id="findStatisticsTrafficAmountByDay" parameterType="java.util.HashMap" resultType="com.its.web.dto.statistics.TrafficStatisticsDto">
        SELECT
            L.STRT_IXR AS strt_name,
            L.END_IXR AS end_name,
            L.IXR_ID AS ixr_id,
            L.IXR_NM AS ixr_nm,
            T.T00 AS t00,
            T.T01 AS t01,
            T.T02 AS t02,
            T.T03 AS t03,
            T.T04 AS t04,
            T.T05 AS t05,
            T.T06 AS t06,
            T.T07 AS t07,
            T.T08 AS t08,
            T.T09 AS t09,
            T.T10 AS t10,
            T.T11 AS t11,
            T.T12 AS t12,
            T.T13 AS t13,
            T.T14 AS t14,
            T.T15 AS t15,
            T.T16 AS t16,
            T.T17 AS t17,
            T.T18 AS t18,
            T.T19 AS t19,
            T.T20 AS t20,
            T.T21 AS t21,
            T.T22 AS t22,
            T.T23 AS t23
        FROM LINK_MNGM L
                 INNER JOIN (
            SELECT
                LINK_ID,
                NVL(SUM(DECODE(TO_CHAR(CLCT_HH, 'HH24'),'00',TFVL)), 0) AS T00,
                NVL(SUM(DECODE(TO_CHAR(CLCT_HH, 'HH24'),'01',TFVL)), 0) AS T01,
                NVL(SUM(DECODE(TO_CHAR(CLCT_HH, 'HH24'),'02',TFVL)), 0) AS T02,
                NVL(SUM(DECODE(TO_CHAR(CLCT_HH, 'HH24'),'03',TFVL)), 0) AS T03,
                NVL(SUM(DECODE(TO_CHAR(CLCT_HH, 'HH24'),'04',TFVL)), 0) AS T04,
                NVL(SUM(DECODE(TO_CHAR(CLCT_HH, 'HH24'),'05',TFVL)), 0) AS T05,
                NVL(SUM(DECODE(TO_CHAR(CLCT_HH, 'HH24'),'06',TFVL)), 0) AS T06,
                NVL(SUM(DECODE(TO_CHAR(CLCT_HH, 'HH24'),'07',TFVL)), 0) AS T07,
                NVL(SUM(DECODE(TO_CHAR(CLCT_HH, 'HH24'),'08',TFVL)), 0) AS T08,
                NVL(SUM(DECODE(TO_CHAR(CLCT_HH, 'HH24'),'09',TFVL)), 0) AS T09,
                NVL(SUM(DECODE(TO_CHAR(CLCT_HH, 'HH24'),'10',TFVL)), 0) AS T10,
                NVL(SUM(DECODE(TO_CHAR(CLCT_HH, 'HH24'),'11',TFVL)), 0) AS T11,
                NVL(SUM(DECODE(TO_CHAR(CLCT_HH, 'HH24'),'12',TFVL)), 0) AS T12,
                NVL(SUM(DECODE(TO_CHAR(CLCT_HH, 'HH24'),'13',TFVL)), 0) AS T13,
                NVL(SUM(DECODE(TO_CHAR(CLCT_HH, 'HH24'),'14',TFVL)), 0) AS T14,
                NVL(SUM(DECODE(TO_CHAR(CLCT_HH, 'HH24'),'15',TFVL)), 0) AS T15,
                NVL(SUM(DECODE(TO_CHAR(CLCT_HH, 'HH24'),'16',TFVL)), 0) AS T16,
                NVL(SUM(DECODE(TO_CHAR(CLCT_HH, 'HH24'),'17',TFVL)), 0) AS T17,
                NVL(SUM(DECODE(TO_CHAR(CLCT_HH, 'HH24'),'18',TFVL)), 0) AS T18,
                NVL(SUM(DECODE(TO_CHAR(CLCT_HH, 'HH24'),'19',TFVL)), 0) AS T19,
                NVL(SUM(DECODE(TO_CHAR(CLCT_HH, 'HH24'),'20',TFVL)), 0) AS T20,
                NVL(SUM(DECODE(TO_CHAR(CLCT_HH, 'HH24'),'21',TFVL)), 0) AS T21,
                NVL(SUM(DECODE(TO_CHAR(CLCT_HH, 'HH24'),'22',TFVL)), 0) AS T22,
                NVL(SUM(DECODE(TO_CHAR(CLCT_HH, 'HH24'),'23',TFVL)), 0) AS T23
            FROM IXR_LINK_TFVL
            WHERE TO_DATE(CLCT_HH, 'YYYY-MM-DD HH24:MI:SS')
            BETWEEN TO_DATE(#{FROM_DT}, 'YYYY-MM-DD HH24:MI:SS') AND TO_DATE(#{TO_DT}, 'YYYY-MM-DD HH24:MI:SS')
            GROUP BY LINK_ID
        ) T ON T.LINK_ID=L.LINK_ID
        <if test="IXR_ID != null and !IXR_ID.equals('') and !IXR_ID.equals('ALL')">
            AND L.IXR_ID = #{IXR_ID}
        </if>
        INNER JOIN DAERO_MNGM D ON L.DAERO_DVSN=D.NMBR
        <if test="ROAD_NMBR != null and !ROAD_NMBR.equals('') and !ROAD_NMBR.equals('ALL')">
            WHERE D.NMBR=#{ROAD_NMBR}
        </if>
        GROUP BY T.LINK_ID, L.IXR_ID, L.IXR_NM, L.STRT_IXR, L.END_IXR, T00, T01, T02, T03, T04, T05, T06, T07, T08,
                 T09, T10, T11, T12, T13, T14, T15, T16, T17, T18, T19, T20, T21, T22, T23
    </select>

    <update id="updateCctvHmpgUseYn" parameterType="com.its.web.dto.admin.HmpgUseYnDto">
        UPDATE CMRA_MNGM
           SET HMPG_DSPL_EN = #{useYn}
         WHERE IXR_ID = #{ixrId}
        <if test='cctvId != null and !cctvId.equals("")'>
            AND CMRA_ID = #{cctvId}
        </if>
    </update>

    <update id="updateTotalCctvHmpgUseYn" parameterType="com.its.web.dto.admin.HmpgUseYnDto">
        UPDATE CMRA_MNGM
        SET HMPG_DSPL_EN = #{useYn,jdbcType=INTEGER}
        WHERE IXR_ID in (select ixr_id from ixr_mngm where dvsn = #{dvsn})
    </update>

<!--    <select id="findIxrName" resultType="com.its.web.dto.statistics.IxrMngmDto">-->
<!--        SELECT-->
<!--            IXR_ID,-->
<!--            IXR_NM-->
<!--        FROM-->
<!--            IXR_MNGM-->
<!--        ORDER BY IXR_NM-->
<!--    </select>-->
    <select id="findIxrName" resultType="com.its.web.dto.statistics.IxrMngmDto">
        SELECT
            IXR_ID,
            IXR_NM
        FROM
            IXR_MNGM A
        WHERE IXR_ID IN (SELECT DISTINCT(IXR_ID) FROM LINK_MNGM)
        ORDER BY IXR_NM
    </select>
</mapper>