<?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.utic.center.utic.ptis.server.dao.mapper.UticPtisServerMapper">

<!--    select 'IF_POL_MPSS_001', trunc(sysdate), '도로교통공단', '돌발정보', '5분', count(1) as count_snd from OCCURID_POLICE_LOG t where t.reg_date between to_char(sysdate-1, 'YYYYMMDD') || '000000' and to_char(sysdate-1, 'YYYYMMDD') || '235959';-->
<!--    select 'IF_POL_MPSS_002', trunc(sysdate), '도로교통공단', '통제정보', '5분', count(1) as count_snd from EVENTID_POLICE_LOG t where t.reg_date between to_char(sysdate-1, 'YYYYMMDD') || '000000' and to_char(sysdate-1, 'YYYYMMDD') || '235959';-->
<!--    select 'IF_POL_MPSS_003', trunc(sysdate), '도로교통공단', '소통정보', '5분', count(1) as count_snd from CURLINKST_POLICE_LOG t where t.reg_date between to_char(sysdate-1, 'YYYYMMDD') || '000000' and to_char(sysdate-1, 'YYYYMMDD') || '235959';-->

    <select id="findLastRegDateCurrLinkSt" resultType="java.lang.String">
        <![CDATA[
        SELECT NVL(MAX(REG_DATE), TO_CHAR(SYSDATE, 'YYYYMMDDHH24MISS')) AS lastRegDate
        FROM CURLINKST_POLICE_LOG
        ]]>
    </select>

    <select id="findLastRegDateEventId" resultType="java.lang.String" timeout="60">
        <![CDATA[
        SELECT NVL(MAX(REG_DATE), TO_CHAR(SYSDATE, 'YYYYMMDDHH24MISS')) AS lastRegDate
        FROM EVENTID_POLICE_LOG
        ]]>
    </select>

    <select id="findLastRegDateOccurId" resultType="java.lang.String" timeout="60">
        <![CDATA[
        SELECT NVL(MAX(REG_DATE), TO_CHAR(SYSDATE, 'YYYYMMDDHH24MISS')) AS lastRegDate
        FROM OCCURID_POLICE_LOG
        ]]>
    </select>

    <select id="findAllIndigoStats" resultType="com.utic.center.utic.ptis.server.dto.IndigoStatsDataDto" timeout="60">
        <![CDATA[
        SELECT IF_ID AS ifId, COUNT_SND AS countSnd
        FROM INDIGO_STATS
        WHERE INIT_DATE = TRUNC(SYSDATE+1)
        ]]>
    </select>

<!--    <insert id="insertIndigoStats" parameterType="java.lang.String">-->
<!--        <![CDATA[-->
<!--        INSERT INTO INDIGO_STATS (IF_ID, INIT_DATE, AGENCY, INFO, PERIOD, COUNT_SND)-->
<!--        VALUES (#{ifId}, SYSDATE, #{agency}, #{info}, #{period}, #{countSnd})-->
<!--        ]]>-->
<!--    </insert>-->

    <insert id="insertIndigoStats" parameterType="java.lang.String" timeout="60">
        <![CDATA[
        MERGE INTO INDIGO_STATS X
            USING (SELECT #{ifId}           AS IF_ID,
                          TRUNC(SYSDATE+1)  AS INIT_DATE,
                          #{agency}         AS AGENCY,
                          #{info}           AS INFO,
                          #{period}         AS PERIOD,
                          #{countSnd}       AS COUNT_SND
                   FROM DUAL ) Y
            ON (X.IF_ID = Y.IF_ID AND X.INIT_DATE = Y.INIT_DATE)
            WHEN MATCHED THEN
                UPDATE SET
                    X.COUNT_SND = Y.COUNT_SND
            WHEN NOT MATCHED THEN
                INSERT (X.IF_ID, X.INIT_DATE, X.AGENCY, X.INFO, X.PERIOD, X.COUNT_SND)
                VALUES (Y.IF_ID, Y.INIT_DATE, Y.AGENCY, Y.INFO, Y.PERIOD, Y.COUNT_SND)
        ]]>
    </insert>

    <insert id="mergeIndigoStats" parameterType="java.lang.String" timeout="60">
        <![CDATA[
        MERGE INTO INDIGO_STATS X
            USING (SELECT #{ifId}           AS IF_ID,
                          TRUNC(SYSDATE+1)  AS INIT_DATE,
                          #{agency}         AS AGENCY,
                          #{info}           AS INFO,
                          #{period}         AS PERIOD,
                          #{countSnd}       AS COUNT_SND
                   FROM DUAL ) Y
            ON (X.IF_ID = Y.IF_ID AND X.INIT_DATE = Y.INIT_DATE)
            WHEN MATCHED THEN
                UPDATE SET
                    X.COUNT_SND = Y.COUNT_SND
            WHEN NOT MATCHED THEN
                INSERT (X.IF_ID, X.INIT_DATE, X.AGENCY, X.INFO, X.PERIOD, X.COUNT_SND)
                VALUES (Y.IF_ID, Y.INIT_DATE, Y.AGENCY, Y.INFO, Y.PERIOD, Y.COUNT_SND)
        ]]>
    </insert>

    <select id="findOneSysNow" resultType="java.lang.String">
        <![CDATA[
        SELECT TO_CHAR(SYSDATE, 'YYYYMMDDHH24MISS') AS sysNow FROM DUAL
        ]]>
    </select>

    <insert id="insertCurlinkstPoliceLog" timeout="60">
        <![CDATA[
        INSERT /*+ APPEND */ INTO CURLINKST_POLICE_LOG (REG_DATE, STD_LINK_ID, SPEED, TRAVELTIME)
        SELECT TO_CHAR(SYSDATE, 'YYYYMMDDHH24MISS') AS REGDATE,
               ST.LINKID,
               ST.SPEED,
               ST.TRAVELTIME
        FROM TRAFFIC_CENTER@UTIS ST,
             LINK@UTIS           LK
        WHERE ST.LINKLEVEL = '1'
          AND ST.LINKID = LK.LINKID
          AND ST.MISSVALUEYN IN ('R', 'K', 'H', 'L', 'Y', 'O')
          AND (
            (LK.ROADRANK IN ('102', '104', '105' , '106' , '107'))
                OR
            (
                (LK.ROADRANK = '103')
                    AND (SUBSTR(LK.LINKID, 1, 3) in ('100','101','102','103','104','105','106','107','108','109','110','111','112','113','114','115','116','117','118','119','120','121','122','123','124','161','162','163','164','165','166','167','168','169','170','200','201','202','203','204','205','206','207','208','209','210','211','212','213','216','217','218','219','220','221','222','224','225','226','228','229','232','234','235'))
                )
            )
          AND (LK.LINKID NOT BETWEEN '1830000000' AND '1879999999')
        ]]>
    </insert>

    <update id="deleteRcvLinkTrafficTmp" timeout="60">
        TRUNCATE TABLE RCV_LINK_TRAFFIC_TMP
    </update>

    <insert id="insertRcvLinkTrafficTmp" timeout="60">
        <![CDATA[
        INSERT INTO RCV_LINK_TRAFFIC_TMP
        SELECT  *
        FROM RCV_LINK_TRAFFIC
        ]]>
    </insert>

    <delete id="deleteRcvLinkTraffic" timeout="60">
        <![CDATA[
        DELETE FROM RCV_LINK_TRAFFIC WHERE LOGDATE < SYSDATE - 5/1440
        ]]>
    </delete>

    <select id="findOneMaxEventRegDate" resultType="java.lang.String">
        <![CDATA[
        SELECT MAX( TO_CHAR(REPORT_DATE,'YYYYMMDDHH24MISS') ) AS maxEventRegDate
        FROM UTISINCI.TB_IMS_DATA@UTISDB
        WHERE REPORT_DATE > SYSDATE - 30/1440
          AND INCIDENT_TYPE_CD <> '1'
        ]]>
    </select>

    <insert id="insertEventidPoliceLog" parameterType="java.lang.String" timeout="60">
        <![CDATA[
        INSERT /*+ APPEND */ INTO EVENTID_POLICE_LOG (REG_DATE, STD_LINK_ID, EVT_ID, EVT_TYPE, EVT_SPECIAL, START_TIME, END_TIME, EVT_DESC, EVT_INFO)
        SELECT MIN(TO_CHAR(ST.REPORT_DATE, 'YYYYMMDDHH24MISS')) AS REG_DATE,
               MIN(ST.LINK_ID)                                  AS STD_LINK_ID,
               TO_NUMBER(SUBSTR(ST.INCIDENT_ID,4,9))            AS EVT_ID,
               '4'                                              AS EVT_TYPE,
               '1'                                              AS EVT_SPECIAL,
               MIN(TO_CHAR(ST.START_DATE ,'YYYYMMDDHH24MISS'))  AS START_TIME,
               MAX(TO_CHAR(ST.END_DATE,   'YYYYMMDDHH24MISS'))  AS END_TIME,
               TRIM(ST.INCIDENT_TITLE)                          AS EVT_DESC,
               TRIM(ST.INCIDENT_DESC)                           AS EVT_INFO
        FROM UTISINCI.TB_IMS_DATA@UTISDB ST, UTIADMIN.LINK@UTISDB L
        WHERE ST.INCIDENT_TYPE_CD <> '1'
          AND ST.LINK_ID      =  L.LINKID
          AND L.LINKLEVEL     =  1
          AND ST.REPORT_DATE  > TO_DATE(#{fromDate}, 'YYYYMMDDHH24MISS')
          AND ST.REPORT_DATE <= TO_DATE(#{toDate},   'YYYYMMDDHH24MISS')
        GROUP BY ST.INCIDENT_ID, ST.INCIDENT_TITLE, ST.INCIDENT_DESC
        ]]>
    </insert>

    <insert id="insertEventidPoliceLogTmp" parameterType="java.lang.String" timeout="60">
        <![CDATA[
        INSERT /*+ APPEND */ INTO EVENTID_POLICE_LOG_TMP (REG_DATE, STD_LINK_ID, EVT_ID, EVT_TYPE, EVT_SPECIAL, START_TIME, END_TIME, EVT_DESC, EVT_INFO)
        SELECT MIN(TO_CHAR(ST.REPORT_DATE, 'YYYYMMDDHH24MISS')) AS REG_DATE,
               MIN(ST.LINK_ID)                                  AS STD_LINK_ID,
               TO_NUMBER(SUBSTR(ST.INCIDENT_ID,4,9))            AS EVT_ID,
               '4'                                              AS EVT_TYPE,
               '1'                                              AS EVT_SPECIAL,
               MIN(TO_CHAR(ST.START_DATE ,'YYYYMMDDHH24MISS'))  AS START_TIME,
               MAX(TO_CHAR(ST.END_DATE,   'YYYYMMDDHH24MISS'))  AS END_TIME,
               TRIM(ST.INCIDENT_TITLE)                          AS EVT_DESC,
               TRIM(ST.INCIDENT_DESC)                           AS EVT_INFO
        FROM UTISINCI.TB_IMS_DATA@UTISDB ST, UTIADMIN.LINK@UTISDB L
        WHERE ST.INCIDENT_TYPE_CD <> '1'
          AND ST.LINK_ID      =  L.LINKID
          AND L.LINKLEVEL     =  1
          AND ST.REPORT_DATE  > TO_DATE(#{fromDate}, 'YYYYMMDDHH24MISS')
          AND ST.REPORT_DATE <= TO_DATE(#{toDate},   'YYYYMMDDHH24MISS')
        GROUP BY ST.INCIDENT_ID, ST.INCIDENT_TITLE, ST.INCIDENT_DESC
        ]]>
    </insert>

    <select id="findOneMaxIncdRegDate" resultType="java.lang.String" timeout="60">
        <![CDATA[
        SELECT MAX( TO_CHAR(REPORT_DATE,'YYYYMMDDHH24MISS') ) AS maxIncdRegDate
        FROM UTISINCI.TB_IMS_DATA@UTISDB
        WHERE REPORT_DATE > SYSDATE - 30/1440
          AND INCIDENT_TYPE_CD = '1'
        ]]>
    </select>

    <insert id="insertOccuridPoliceLog" parameterType="java.lang.String" timeout="60">
        <![CDATA[
        INSERT /*+ APPEND */ INTO OCCURID_POLICE_LOG (REG_DATE, STD_LINK_ID, UPDATE_TIME, TYPE_DESC, TYPE_OTHER, STATUS, UPDATE_TYPE, STATUS_DESC, UPDATE_DESC)
        SELECT TO_CHAR(ST.REPORT_DATE,'YYYYMMDDHH24MISS')   AS REG_DATE,
               ST.LINK_ID                                   AS STD_LINK_ID,
               TO_CHAR(SYSDATE ,'YYYYMMDDHH24MISS')         AS UPDATE_TIME,
               INCIDENT_TYPE_CD                             AS TYPE_DESC,
               ST.INCIDENT_TITLE || ' ' || ST.INCIDENT_DESC AS TYPE_OTHER,
               '2'                                          AS STATUS,
               '2'                                          AS UPDATE_TYPE,
               TO_CHAR(ST.LOCATION_DATA_X, '000.000000000000')||TO_CHAR(ST.LOCATION_DATA_Y, '00.0000000000000') AS STATUS_DESC,
               TO_CHAR(ST.START_DATE, 'YYYYMMDDHH24MISS')||TO_CHAR(ST.END_DATE, 'YYYYMMDDHH24MISS')             AS UPDATE_DESC
        FROM UTISINCI.TB_IMS_DATA@UTISDB ST, UTIADMIN.LINK@UTISDB L
        WHERE INCIDENT_TYPE_CD IN ('1', '2', '3', '4')
          AND ST.LINK_ID      = L.LINKID
          AND L.LINKLEVEL     = 1
          AND ST.REPORT_DATE  > TO_DATE(#{fromDate}, 'YYYYMMDDHH24MISS')
          AND ST.REPORT_DATE <= TO_DATE(#{toDate},   'YYYYMMDDHH24MISS')
        ]]>
    </insert>

    <insert id="insertOccuridPoliceLogTmp" parameterType="java.lang.String" timeout="60">
        <![CDATA[
        INSERT /*+ APPEND */ INTO OCCURID_POLICE_LOG_TMP (REG_DATE, STD_LINK_ID, UPDATE_TIME, TYPE_DESC, TYPE_OTHER, STATUS, UPDATE_TYPE, STATUS_DESC, UPDATE_DESC)
        SELECT TO_CHAR(ST.REPORT_DATE,'YYYYMMDDHH24MISS')   AS REG_DATE,
               ST.LINK_ID                                   AS STD_LINK_ID,
               TO_CHAR(SYSDATE ,'YYYYMMDDHH24MISS')         AS UPDATE_TIME,
               INCIDENT_TYPE_CD                             AS TYPE_DESC,
               ST.INCIDENT_TITLE || ' ' || ST.INCIDENT_DESC AS TYPE_OTHER,
               '2'                                          AS STATUS,
               '2'                                          AS UPDATE_TYPE,
               TO_CHAR(ST.LOCATION_DATA_X, '000.000000000000')||TO_CHAR(ST.LOCATION_DATA_Y, '00.0000000000000') AS STATUS_DESC,
               TO_CHAR(ST.START_DATE, 'YYYYMMDDHH24MISS')||TO_CHAR(ST.END_DATE, 'YYYYMMDDHH24MISS')             AS UPDATE_DESC
        FROM UTISINCI.TB_IMS_DATA@UTISDB ST, UTIADMIN.LINK@UTISDB L
        WHERE INCIDENT_TYPE_CD IN ('1', '2', '3', '4')
          AND ST.LINK_ID      = L.LINKID
          AND L.LINKLEVEL     = 1
          AND ST.REPORT_DATE  > TO_DATE(#{fromDate}, 'YYYYMMDDHH24MISS')
          AND ST.REPORT_DATE <= TO_DATE(#{toDate},   'YYYYMMDDHH24MISS')
        ]]>
    </insert>

    <delete id="deleteEventidPoliceLog" timeout="60">
        <![CDATA[
        DELETE FROM EVENTID_POLICE_LOG WHERE REG_DATE < TO_CHAR(SYSDATE - 30, 'YYYYMMDDHH24MISS')
        ]]>
    </delete>

    <delete id="deleteEventidPoliceLogTmp" timeout="60">
        <![CDATA[
        DELETE FROM EVENTID_POLICE_LOG_TMP WHERE REG_DATE < TO_CHAR(SYSDATE - 30, 'YYYYMMDDHH24MISS')
        ]]>
    </delete>

    <delete id="deleteOccuridPoliceLog" timeout="60">
        <![CDATA[
        DELETE FROM OCCURID_POLICE_LOG WHERE REG_DATE < TO_CHAR(SYSDATE - 30, 'YYYYMMDDHH24MISS')
        ]]>
    </delete>

    <delete id="deleteOccuridPoliceLogTmp" timeout="60">
        <![CDATA[
        DELETE FROM OCCURID_POLICE_LOG_TMP WHERE REG_DATE < TO_CHAR(SYSDATE - 30, 'YYYYMMDDHH24MISS')
        ]]>
    </delete>

</mapper>
