<?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.dwdb.UticStat05MinFltrMapper">

    <update id="truncateStatLocal" timeout="60">
        TRUNCATE TABLE STAT_LOCAL
    </update>

    <insert id="insertStatLocalRcvLinkTraffic" parameterType="java.lang.String" timeout="120">
        <![CDATA[
        INSERT INTO STAT_LOCAL(LOCAL_DATE, CENTER_ID, LINKID, REV_TYPE, SPEED)
        SELECT /*+ INDEX(T PK_RCV_LINK_TRAFFIC_${yyMm}) */
               TO_CHAR(T.REGDATE, 'YYYYMMDDHH24MISS'), T.FromCenterID, T.LinkNumber, 'U' AS REV_TYPE, T.SpeedRate
        FROM RCV_LINK_TRAFFIC_${yyMm} T
        WHERE REGDATE  > TO_DATE(#{past30Min}, 'YYYYMMDDHH24MISS')
          AND REGDATE <= TO_DATE(#{past25Min}, 'YYYYMMDDHH24MISS')
        ]]>
    </insert>

    <update id="truncateLinkFilter" timeout="60">
        TRUNCATE TABLE LINK_FILTER
    </update>

    <insert id="insertLinkFilterStdDev" parameterType="java.lang.String" timeout="120">
        <![CDATA[
        INSERT INTO LINK_FILTER(LINKID, MAK_DATE, AVG_SPEED, DATA_CNT,
                                STDV, STDV_HSPEED, STDV_LSPEED)
        SELECT LINKID, SUBSTR(#{past25Min}, 1, 14), AVGSPEED, DATACNT, STDSPEED,
               DECODE(#{codeValue0}, 'Y', AVGSPEED + STDSPEED * TO_NUMBER(#{codeValue5}), 200),
               DECODE(#{codeValue0}, 'Y', AVGSPEED - STDSPEED * TO_NUMBER(#{codeValue5}), 0)
        FROM (
                 SELECT LINKID        AS LINKID,
                        COUNT(1)      AS DATACNT,
                        AVG(SPEED)    AS AVGSPEED,
                        STDDEV(SPEED) AS STDSPEED
                 FROM STAT_LOCAL
                 WHERE LOCAL_DATE IS NOT NULL
                 GROUP BY LINKID
             ) a
        ]]>
    </insert>

    <update id="updateLinkFilterPattern" parameterType="java.lang.String" timeout="120">
        <![CDATA[
        UPDATE LINK_FILTER T
        SET (STAT_HSPEED, STAT_LSPEED) =
                (
                    SELECT /*+ INDEX(P PK_${fromTable}) */
                           P.SPEED + #{maxValue, jdbcType=NUMERIC}, P.SPEED - #{minValue, jdbcType=NUMERIC}
                    FROM ${fromTable} P
                    WHERE P.PATN_TIME = SUBSTR(#{past25Min}, 9, 4)
                      AND P.LINKID = T.LINKID
                      AND ROWNUM = 1
                )
        ]]>
    </update>

    <update id="updateLinkFilterSpeedNull" timeout="120">
        <![CDATA[
        UPDATE LINK_FILTER T
        SET STAT_HSPEED = 999,
            STAT_LSPEED = 0
        WHERE STAT_HSPEED IS NULL
        ]]>
   </update>

    <update id="updateLinkFilterRoadRank" timeout="120">
        <![CDATA[
        UPDATE LINK_FILTER T
        SET (ROADRANK, LINKLEVEL) =
                (
                    SELECT ROADRANK, LINKLEVEL
                    FROM LINK
                    WHERE LINKID = T.LINKID
                )
        ]]>
    </update>

    <update id="updateLinkFilterMinMax" parameterType="java.lang.Integer" timeout="120">
        <![CDATA[
        UPDATE LINK_FILTER A
        SET GRAD_HSPEED = DECODE(ROADRANK, '101', #{maxValue6, jdbcType=NUMERIC},
                                           '102', #{maxValue7, jdbcType=NUMERIC},
                                           '103', #{maxValue8, jdbcType=NUMERIC},
                                                  #{maxValue9, jdbcType=NUMERIC}
                          ),
            GRAD_LSPEED = DECODE(ROADRANK, '101', #{minValue6, jdbcType=NUMERIC},
                                           '102', #{minValue7, jdbcType=NUMERIC},
                                           '103', #{minValue8, jdbcType=NUMERIC},
                                                  #{minValue9, jdbcType=NUMERIC}
                          )
        WHERE ROADRANK IS NOT NULL
        ]]>
    </update>

    <insert id="insertStatLocalLog" timeout="120">
        <![CDATA[
        INSERT INTO STAT_LOCAL_LOG_${yyMm}(LOCAL_DATE, CENTER_ID, LINKID, REV_TYPE, SPEED)
        SELECT LOCAL_DATE, CENTER_ID, LINKID, REV_TYPE, SPEED
        FROM STAT_LOCAL
        ]]>
    </insert>

</mapper>
