<?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.stat.server.dao.mapper.UticStat05MinStatMapper">

    <insert id="insertCenterLog" parameterType="java.lang.String" timeout="120">
        <![CDATA[
        INSERT INTO CENTER_LOG(EXE_DATE, CENTER_ID, LOG_DATE, DATA_CNT)
        SELECT SUBSTR(#{regDate}, 1, 14), CENTER_ID, SYSDATE, COUNT(1)
        FROM (SELECT A.LINKID, C.CENTER_ID
              FROM (SELECT DISTINCT LINKID AS LINKID
                    FROM STAT_LOCAL) A,
                   LINK B,
                   LOCAL_AREA C
              WHERE B.LINKLEVEL = '1'
                AND A.LINKID = B.LINKID
                AND C.CENTER_ID IS NOT NULL
                AND SUBSTR(A.LINKID, 1, 3) = C.SLOCAL_ID
             )
        GROUP BY CENTER_ID
        ]]>
    </insert>

    <insert id="insertStatTrafficLog" parameterType="java.lang.String" timeout="120">
        <![CDATA[
        INSERT INTO STAT_TRAFFIC_LOG(EXE_DATE, CENTER_ID, MISSVALUEYN, LANES, DATA_CNT)
        SELECT SUBSTR(#{regDate}, 1, 12), C.CENTER_ID, A.MISSVALUEYN, B.LANES, COUNT(1)
        FROM (SELECT /*+ INDEX(T PK_TRAFFIC_CENTER_${yyMm}) */
                  T.LINKID      AS LINKID,
                  T.MISSVALUEYN AS MISSVALUEYN
              FROM TRAFFIC_CENTER_${yyMm} T
              WHERE T.REGDATE = TO_DATE(SUBSTR(#{regDate}, 1, 14), 'YYYYMMDDHH24MISS')
             ) A,
             (SELECT L.LINKID AS LINKID,
                     DECODE(L.LANES, 1, 1, 2) AS LANES
              FROM LINK L
              WHERE L.LINKLEVEL = '1'
             ) B,
             (SELECT M.SLOCAL_ID AS SLOCAL_ID,
                     M.CENTER_ID AS CENTER_ID
              FROM LOCAL_AREA M
              WHERE M.CENTER_ID IS NOT NULL
             ) C
        WHERE A.LINKID = B.LINKID
          AND SUBSTR(A.LINKID, 1, 3) = C.SLOCAL_ID
        GROUP BY C.CENTER_ID, A.MISSVALUEYN, B.LANES
        ]]>
    </insert>
    <insert id="insertStatTrafficLog_backup" parameterType="java.lang.String" timeout="120">
        <![CDATA[
        INSERT INTO STAT_TRAFFIC_LOG(EXE_DATE, CENTER_ID, MISSVALUEYN, LANES, DATA_CNT)
        SELECT /*+ INDEX(T PK_TRAFFIC_CENTER_${yyMm}) */
              SUBSTR(#{regDate}, 1, 12), C.CENTER_ID, A.MISSVALUEYN, DECODE(B.LANES, 1, 1, 2), COUNT(1)
        FROM TRAFFIC_CENTER_${yyMm} A, LINK B, LOCAL_AREA C
        WHERE B.LINKLEVEL = '1'
          AND A.REGDATE = TO_DATE(SUBSTR(#{regDate}, 1, 14), 'YYYYMMDDHH24MISS')
          AND A.LINKID = B.LINKID
          AND SUBSTR(A.LINKID, 1, 3) = C.SLOCAL_ID
          AND C.CENTER_ID IS NOT NULL
        GROUP BY C.CENTER_ID, A.MISSVALUEYN, DECODE(B.LANES, 1, 1, 2)
        ]]>
    </insert>

    <insert id="insertFilterLog" parameterType="java.lang.String" timeout="120">
        <![CDATA[
        INSERT INTO FILTER_LOG(EXE_DATE, CENTER_ID, LOG_DATE, LINK_FILTER_CNT, LINKLEVEL_FILTER_CNT, ZERO_CNT,
                               STD_YN, STD_CNT, STA_YN, STA_CNT, RGF_YN, RGF_CNT, RGF_USE_YN)
        SELECT SUBSTR(#{regDate}, 1, 14), CENTER_ID, SYSDATE,
               SUM(CNT_LINK), SUM(LEVEL_CNT), SUM(ZERO_CNT),
               #{codeValue0}, DECODE(#{codeValue0}, 'Y', SUM(CNT_STD), 0),
               #{codeValue1}, DECODE(#{codeValue1}, 'Y', SUM(CNT_STAT),0),
               #{codeValue2}, DECODE(#{codeValue2}, 'Y', SUM(CNT_MAX), 0),
               #{codeValue3}
        FROM (
                 SELECT A.CENTER_ID, COUNT(1) CNT_LINK, 0 CNT_STD, 0 CNT_STAT, 0 CNT_MAX, 0 LEVEL_CNT, 0 ZERO_CNT
                 FROM STAT_LOCAL A, LINK_FILTER B
                 WHERE A.LINKID = B.LINKID
                   AND B.ROADRANK IS NULL
                 GROUP BY A.CENTER_ID
                 UNION ALL
                 SELECT A.CENTER_ID, 0 CNT_LINK, COUNT(1) CNT_STD, 0 CNT_STAT, 0 CNT_MAX, 0 LEVEL_CNT, 0 ZERO_CN
                 FROM STAT_LOCAL A, LINK_FILTER B
                 WHERE A.LINKID = B.LINKID
                   AND (A.SPEED > B.STDV_HSPEED OR A.SPEED < B.STDV_LSPEED)
                   AND B.DATA_CNT >= TO_NUMBER(#{codeValue4})
                 GROUP BY A.CENTER_ID
                 UNION ALL
                 SELECT CENTER_ID, 0 CNT_LINK, 0 CNT_STD, 0 CNT_STAT, 0 CNT_MAX, 0 LEVEL_CNT, COUNT(1) ZERO_CNT
                 FROM STAT_LOCAL
                 WHERE SPEED = 0
                 GROUP BY CENTER_ID
                 UNION ALL
                 SELECT A.CENTER_ID, 0 CNT_LINK, 0 CNT_STD, COUNT(1) CNT_STAT, 0 CNT_MAX, 0 LEVEL_CNT, 0 ZERO_CN
                 FROM STAT_LOCAL A, LINK_FILTER B
                 WHERE A.LINKID = B.LINKID
                   AND (A.SPEED > B.STAT_HSPEED OR A.SPEED < B.STAT_LSPEED)
                 GROUP BY A.CENTER_ID
                 UNION ALL
                 SELECT A.CENTER_ID, 0 CNT_LINK, 0 CNT_STD, 0 CNT_STAT, COUNT(1) CNT_MAX, 0 LEVEL_CNT, 0 ZERO_CN
                 FROM STAT_LOCAL A, LINK_FILTER B
                 WHERE A.LINKID = B.LINKID
                   AND (A.SPEED > B.GRAD_HSPEED OR A.SPEED < B.GRAD_LSPEED)
                 GROUP BY A.CENTER_ID
                 UNION ALL
                 SELECT A.CENTER_ID, 0 CNT_LINK, 0 CNT_STD, 0 CNT_STAT, 0 CNT_MAX, COUNT(1) LEVEL_CNT, 0 ZERO_CN
                 FROM STAT_LOCAL A, LINK_FILTER B
                 WHERE A.LINKID = B.LINKID
                   AND B.LINKLEVEL IS NOT NULL
                   AND B.LINKLEVEL != '1'
                 GROUP BY A.CENTER_ID
             )
        GROUP BY CENTER_ID
        ]]>
    </insert>

    <insert id="insertLinkFilterLog" parameterType="java.lang.String" timeout="120">
        <![CDATA[
        INSERT INTO LINK_FILTER_LOG_${yyMm}(LOCAL_DATE, CENTER_ID, LINKID, REV_TYPE, LINK_FILTER_YN, LINKLEVEL_FILTER_YN, ZERO_YN, STD_YN, STA_YN, RGF_YN)
        SELECT LOCAL_DATE, CENTER_ID, LINKID, REV_TYPE,
               DECODE(LINK_FILTER, 0, 'N', 'Y'), DECODE(LINKLEVEL_FILTER, 0, 'N', 'Y'), DECODE(ZERO, 0, 'N', 'Y'),
               DECODE(STD, 0, 'N', 'Y'), DECODE(STA, 0, 'N', 'Y'), DECODE(RGF, 0, 'N', 'Y')
        FROM (
                 SELECT CENTER_ID, LOCAL_DATE, LINKID, REV_TYPE,
                        SUM(LINK_FILTER) LINK_FILTER, SUM(LINKLEVEL_FILTER) LINKLEVEL_FILTER, SUM(ZERO) ZERO,
                        SUM(STD) STD, SUM(STA) STA, SUM(RGF) RGF
                 FROM (
                          SELECT A.CENTER_ID, A.LOCAL_DATE, A.LINKID, A.REV_TYPE, 1 LINK_FILTER, 0 LINKLEVEL_FILTER, 0 ZERO, 0 STD, 0 STA, 0 RGF
                          FROM STAT_LOCAL A, LINK_FILTER B
                          WHERE A.LINKID = B.LINKID
                            AND B.ROADRANK IS NULL
                          UNION ALL
                          SELECT A.CENTER_ID, A.LOCAL_DATE, A.LINKID, A.REV_TYPE, 0 LINK_FILTER, 0 LINKLEVEL_FILTER, 0 ZERO, 1 STD, 0 STA, 0 RGF
                          FROM STAT_LOCAL A, LINK_FILTER B
                          WHERE A.LINKID = B.LINKID
                            AND (A.SPEED > B.STDV_HSPEED OR A.SPEED < B.STDV_LSPEED)
                            AND B.DATA_CNT >= TO_NUMBER(#{codeValue4})
                          UNION ALL
                          SELECT A.CENTER_ID, A.LOCAL_DATE, A.LINKID, A.REV_TYPE, 0 LINK_FILTER, 0 LINKLEVEL_FILTER, 1 ZERO, 0 STD, 0 STA, 0 RGF
                          FROM STAT_LOCAL A
                          WHERE SPEED = 0
                          UNION ALL
                          SELECT A.CENTER_ID, A.LOCAL_DATE, A.LINKID, A.REV_TYPE, 0 LINK_FILTER, 0 LINKLEVEL_FILTER, 0 ZERO, 0 STD, 1 STA, 0 RGF
                          FROM STAT_LOCAL A, LINK_FILTER B
                          WHERE A.LINKID = B.LINKID
                            AND (A.SPEED > B.STAT_HSPEED OR A.SPEED < B.STAT_LSPEED)
                          UNION ALL
                          SELECT A.CENTER_ID, A.LOCAL_DATE, A.LINKID, A.REV_TYPE, 0 LINK_FILTER, 0 LINKLEVEL_FILTER, 0 ZERO, 0 STD, 0 STA, 1 RGF
                          FROM STAT_LOCAL A, LINK_FILTER B
                          WHERE A.LINKID = B.LINKID
                            AND (A.SPEED > B.GRAD_HSPEED OR A.SPEED < B.GRAD_LSPEED)
                          UNION ALL
                          SELECT A.CENTER_ID, A.LOCAL_DATE, A.LINKID, A.REV_TYPE, 0 LINK_FILTER, 1 LINKLEVEL_FILTER, 0 ZERO, 0 STD, 0 STA, 0 RGF
                          FROM STAT_LOCAL A, LINK_FILTER B
                          WHERE A.LINKID = B.LINKID
                            AND B.LINKLEVEL IS NOT NULL
                            AND B.LINKLEVEL != '1'
                      )
                 GROUP BY CENTER_ID, LOCAL_DATE, LINKID, REV_TYPE
             )
            ]]>
    </insert>

    <insert id="insertStat5MinCenter" parameterType="java.lang.String" timeout="120">
        <![CDATA[
        INSERT INTO STAT_5MIN_CENTER_${yyMm}(STAT_DATE, LINKID, LINKLEVEL, DAY_TYPE, SPEED, DATA_CNT, TRAVEL_TIME)
        SELECT a1.S_DATE, a1.LINKID, '1' AS LinkLEVEL,
               a1.DAY_TYPE, a1.Speed, DataCnt,
               ROUND(b1.LENGTH / ((a1.Speed * 1000)/3600)) AS TRAVEL_TIME
        FROM (
                 SELECT SUBSTR(#{regDate}, 1, 12) AS S_DATE,
                        A.LINKID,
                        TO_CHAR(TO_DATE(#{regDate}, 'YYYYMMDDHH24MISS'), 'D') AS DAY_TYPE,
                        AVG(SPEED) AS Speed,
                        COUNT(1) AS DataCnt
                 FROM STAT_LOCAL a, LINK_FILTER b
                 WHERE A.LINKID = B.LINKID
                   AND B.ROADRANK IS NOT NULL
                   AND B.LINKLEVEL = '1'
                   AND B.DATA_CNT >= TO_NUMBER(#{codeValue4})
                   AND A.SPEED <=  B.STDV_HSPEED
                   AND A.SPEED >=  B.STDV_LSPEED
                   AND A.SPEED <=  B.STAT_HSPEED
                   AND A.SPEED >=  B.STAT_LSPEED
                   AND A.SPEED <=  B.GRAD_HSPEED
                   AND A.SPEED >=  B.GRAD_LSPEED
                   AND A.SPEED  > 0
                 GROUP BY A.LINKID
             ) a1, LINK b1
        WHERE a1.LINKID = b1.LINKID
        UNION ALL
        SELECT a1.S_DATE, a1.LINKID, '1' LinkLEVEL,
               a1.DAY_TYPE, a1.Speed, DataCnt,
               ROUND(b1.LENGTH / ((a1.Speed * 1000)/3600)) AS TRAVEL_TIME
        FROM (
                 SELECT SUBSTR(#{regDate}, 1, 12) AS S_DATE,
                        A.LINKID,
                        TO_CHAR(TO_DATE(#{regDate}, 'YYYYMMDDHH24MISS'), 'D') AS DAY_TYPE,
                        AVG(SPEED) AS Speed,
                        COUNT(1) AS DataCnt
                 FROM STAT_LOCAL a, LINK_FILTER b
                 WHERE A.LINKID = B.LINKID
                   AND B.ROADRANK IS NOT NULL
                   AND B.LINKLEVEL = '1'
                   AND B.DATA_CNT < TO_NUMBER(#{codeValue4})
                   AND A.SPEED <= B.STAT_HSPEED
                   AND A.SPEED >= B.STAT_LSPEED
                   AND A.SPEED <= B.GRAD_HSPEED
                   AND A.SPEED >= B.GRAD_LSPEED
                   AND A.SPEED  > 0
                 GROUP BY A.LINKID
             ) a1, LINK b1
        WHERE a1.LINKID = b1.LINKID
        ]]>
    </insert>

    <insert id="insertStatFilterLog" parameterType="java.lang.String" timeout="120">
        <![CDATA[
        INSERT INTO STAT_FILTER_LOG(EXE_DATE, CENTER_ID, COLLECT_CNT, FILTER_CNT)
        SELECT SUBSTR(#{regDate}, 1, 12) AS EXE_DATE, CENTER_ID, SUM(COLLECT_CNT) AS COLLECT_CNT, SUM(FILTER_CNT) AS FILTER_CNT
        FROM (
                 SELECT CENTER_ID, COUNT(1) AS COLLECT_CNT, 0 AS FILTER_CNT
                 FROM (SELECT A.LINKID, C.CENTER_ID
                       FROM (SELECT DISTINCT LINKID AS LINKID FROM STAT_LOCAL) A, LINK B, LOCAL_AREA C
                       WHERE B.LINKLEVEL = '1'
                         AND A.LINKID = B.LINKID
                         AND C.CENTER_ID IS NOT NULL
                         AND SUBSTR(A.LINKID, 1, 3) = C.SLOCAL_ID
                      )
                 GROUP BY CENTER_ID
                 UNION ALL
                 SELECT /*+ INDEX(A PK_STAT_5MIN_CENTER_${yyMm}) */
                        C.CENTER_ID, 0 AS COLLECT_CNT, COUNT(1) AS FILTER_CNT
                 FROM STAT_5MIN_CENTER_${yyMm} A, LINK B, LOCAL_AREA C
                 WHERE A.STAT_DATE = SUBSTR(#{regDate}, 1, 12)
                   AND A.LINKLEVEL = '1'
                   AND A.LINKID = B.LINKID
                   AND C.CENTER_ID IS NOT NULL
                   AND SUBSTR(A.LINKID, 1, 3) = C.SLOCAL_ID
                 GROUP BY C.CENTER_ID
             )
        GROUP BY CENTER_ID
        ]]>
    </insert>

    <insert id="insertStat5MinCenterLevel" parameterType="java.lang.String" timeout="120">
        <![CDATA[
        INSERT INTO STAT_5MIN_CENTER_${yyMm}(STAT_DATE, LINKID, LINKLEVEL, DAY_TYPE, SPEED, DATA_CNT)
        SELECT SUBSTR(#{regDate}, 1, 12), b1.LOGICALLINKID, #{linkLevel} AS LINKLEVEL,
               TO_CHAR(TO_DATE(#{regDate}, 'YYYYMMDDHH24MISS'), 'D') AS DAY_TYPE,
               ROUND(SUM(a1.LENGTH * a1.SPEED)/SUM(a1.LENGTH)) AS SPEED,
               COUNT(1) AS DATA_CNT
        FROM
            (
                SELECT /*+ INDEX(B PK_STAT_5MIN_CENTER_${yyMm}) */
                       A.LINKID, A.LENGTH, B.SPEED
                FROM LINK A, STAT_5MIN_CENTER_${yyMm} B
                WHERE A.LINKID = B.LINKID
                  AND A.LINKLEVEL ='1'
                  AND B.STAT_DATE = SUBSTR(#{regDate}, 1, 12)
            ) a1,
            (
                SELECT A.LOGICALLINKID, A.LINKID
                FROM LOGICALLINK a,
                     (
                         SELECT LINKID
                         FROM LINK
                         WHERE LINKLEVEL = #{linkLevel}
                     ) B
                WHERE A.LOGICALLINKID = B.LINKID
                  AND A.LINKLEVEL = '1'
            ) b1
        WHERE a1.LINKID = b1.LINKID
        GROUP BY b1.LOGICALLINKID
        ]]>
    </insert>

    <update id="updateStat5MinCenterTravelTime" parameterType="java.lang.String" timeout="120">
        <![CDATA[
        UPDATE STAT_5MIN_CENTER_${yyMm} a
        SET TRAVEL_TIME =
                (
                    SELECT (B.LENGTH * 3.6)/A.SPEED
                    FROM LINK b
                    WHERE A.LinkID = B.LinkID
                )
        WHERE LINKLEVEL IN('2', '3', '4')
          AND STAT_DATE = SUBSTR(#{regDate}, 1, 12)
        ]]>
    </update>

    <insert id="insertStat5MinLoc" parameterType="java.lang.String" timeout="120">
        <![CDATA[
        INSERT INTO STAT_5MIN_LOC_${yyMm}(STAT_DATE,
                                  LINKID, LINKLEVEL, DAY_TYPE, SPEED, DATA_CNT, TRAVEL_TIME)
        SELECT a1.S_DATE, a1.LINKID, '1' AS LinkLEVEL,
               a1.DAY_TYPE, a1.Speed, DataCnt,
               ROUND(b1.LENGTH / ((a1.Speed * 1000)/3600)) AS TRAVEL_TIME
        FROM (
                 SELECT SUBSTR(#{regDate}, 1, 12) AS S_DATE,
                        A.LINKID,
                        TO_CHAR(TO_DATE(#{regDate}, 'YYYYMMDDHH24MISS'), 'D') AS DAY_TYPE,
                        AVG(SPEED) AS Speed,
                        COUNT(1) AS DataCnt
                 FROM STAT_LOCAL a, LINK_FILTER b
                 WHERE A.LINKID = B.LINKID
                   AND B.ROADRANK IS NOT NULL
                   AND B.LINKLEVEL = '1'
                   AND B.DATA_CNT >= TO_NUMBER(#{codeValue4})
                   AND A.SPEED <= B.STDV_HSPEED
                   AND A.SPEED >= B.STDV_LSPEED
                   AND A.SPEED <= B.STAT_HSPEED
                   AND A.SPEED >= B.STAT_LSPEED
                   AND A.SPEED <= B.GRAD_HSPEED
                   AND A.SPEED >= B.GRAD_LSPEED
                   AND A.REV_TYPE = 'U'
                   AND A.SPEED > 0
                 GROUP BY A.LINKID
             ) a1, LINK b1
        WHERE a1.LINKID = b1.LINKID
        UNION ALL
        SELECT a1.S_DATE, a1.LINKID, '1' AS LinkLEVEL,
               a1.DAY_TYPE, a1.Speed, DataCnt,
               ROUND(b1.LENGTH / ((a1.Speed * 1000)/3600)) AS TRAVEL_TIME
        FROM (
                 SELECT SUBSTR(#{regDate}, 1, 12) AS S_DATE,
                        A.LINKID,
                        TO_CHAR(TO_DATE(#{regDate}, 'YYYYMMDDHH24MISS'), 'D') AS DAY_TYPE,
                        AVG(SPEED) AS Speed,
                        COUNT(1) AS DataCnt
                 FROM STAT_LOCAL a, LINK_FILTER b
                 WHERE A.LINKID = B.LINKID
                   AND B.ROADRANK IS NOT NULL
                   AND B.LINKLEVEL = '1'
                   AND B.DATA_CNT < TO_NUMBER(#{codeValue4})
                   AND A.SPEED <= B.STAT_HSPEED
                   AND A.SPEED >= B.STAT_LSPEED
                   AND A.SPEED <= B.GRAD_HSPEED
                   AND A.SPEED >= B.GRAD_LSPEED
                   AND A.REV_TYPE = 'U'
                   AND A.SPEED > 0
                 GROUP BY A.LINKID
             ) a1, LINK b1
        WHERE a1.LINKID = b1.LINKID
        ]]>
    </insert>


    <insert id="insertStat5MinLocLevel" parameterType="java.lang.String" timeout="120">
        <![CDATA[
        INSERT INTO STAT_5MIN_LOC_${yyMm}(STAT_DATE, LINKID, LINKLEVEL, DAY_TYPE, SPEED, DATA_CNT)
        SELECT SUBSTR(#{regDate}, 1, 12),
               b1.LOGICALLINKID, #{linkLevel} AS LINKLEVEL,
               TO_CHAR(TO_DATE(#{regDate}, 'YYYYMMDDHH24MISS'), 'D') AS DAY_TYPE,
               ROUND(SUM(a1.LENGTH * a1.SPEED)/SUM(a1.LENGTH)) AS SPEED,
               COUNT(1) AS DATA_CNT
        FROM
            (
                SELECT /*+ INDEX(B PK_STAT_5MIN_LOC_${yyMm}) */
                       A.LINKID, A.LENGTH, B.SPEED
                FROM LINK A, STAT_5MIN_LOC_${yyMm} B
                WHERE A.LINKID = B.LINKID
                  AND A.LINKLEVEL = '1'
                  AND B.STAT_DATE = SUBSTR(#{regDate}, 1, 12)
            ) a1,
            (
                SELECT A.LOGICALLINKID, A.LINKID
                FROM LOGICALLINK a,
                     (
                         SELECT LINKID
                         FROM LINK
                         WHERE LINKLEVEL = #{linkLevel}
                     ) B
                WHERE A.LOGICALLINKID = B.LINKID
                  AND A.LINKLEVEL = '1'
            ) b1
        WHERE a1.LINKID = b1.LINKID
        GROUP BY b1.LOGICALLINKID
        ]]>
     </insert>

    <update id="updateStat5MinLocTravelTime" parameterType="java.lang.String" timeout="120">
        <![CDATA[
        UPDATE STAT_5MIN_LOC_${yyMm} a
        SET TRAVEL_TIME =
                (
                    SELECT (B.LENGTH * 3.6)/A.SPEED
                    FROM LINK b
                    WHERE A.LinkID = B.LinkID
                )
        WHERE LINKLEVEL IN('2', '3', '4')
          AND STAT_DATE = SUBSTR(#{regDate}, 1, 12)
        ]]>
    </update>

</mapper>
