<?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.UticStatPatternMapper">

    <select id="findHoliday" parameterType="java.lang.String" resultType="com.utic.center.utic.stat.server.dto.HolidayDto" timeout="10">
        <![CDATA[
        SELECT SUM(WDAY) AS wDay, SUM(HCNT) AS hCnt
        FROM (
                 SELECT TO_NUMBER(TO_CHAR(TO_DATE(#{patternTime}, 'YYYYMMDDHH24MISS'), 'D')) AS WDAY,
                        0 AS HCNT
                 FROM DUAL
                 UNION ALL
                 SELECT 0 AS WDAY,
                        COUNT(1) AS HCNT
                 FROM HOLIDAY
                 WHERE HOLIDAY = SUBSTR(#{patternTime}, 1, 8)
             )
        ]]>
    </select>

    <insert id="insertPatternHist" parameterType="java.lang.String" timeout="120">
        <![CDATA[
        INSERT INTO ${targetTable}(PATN_DATE, PATN_TIME, LINKID, SPEED, LOG_DATE, LINKLEVEL)
        SELECT /*+ INDEX(T PK_${fromTable}) */
               SUBSTR(#{patternTime}, 1, 8), T.PATN_TIME, T.LINKID, T.SPEED, T.LOG_DATE, T.LINKLEVEL
        FROM ${fromTable} T
        WHERE T.PATN_TIME = SUBSTR(#{patternTime}, 9, 4)
        ]]>
   </insert>

    <insert id="updatePattern" parameterType="java.lang.String" timeout="120">
        <![CDATA[
        MERGE INTO ${targetTable} NP
        USING (SELECT /*+ INDEX(A PK_${statTable}) */
                      A.LINKID, A.SPEED, SYSDATE AS LOG_DATE, A.LINKLEVEL
                   FROM ${statTable} A, LINK B
                   WHERE A.STAT_DATE = SUBSTR(#{patternTime}, 1, 12)
                     AND A.LINKLEVEL IN ('1', '4')
                     AND A.SPEED > 0 AND A.SPEED < 150
                     AND A.LINKID = B.LINKID
                     AND A.LINKLEVEL = B.LINKLEVEL
              ) MG
            ON (NP.PATN_TIME =  SUBSTR(#{patternTime}, 9, 4) AND NP.LINKID = MG.LINKID)
            WHEN MATCHED THEN
                UPDATE SET
                    NP.SPEED = DECODE(NP.SPEED, NULL, MG.SPEED, ((NP.SPEED * TO_NUMBER(#{minValue}) + MG.SPEED * TO_NUMBER(#{maxValue}) )/( TO_NUMBER(#{minValue}) + TO_NUMBER(#{maxValue}) ))),
                    NP.LOG_DATE = MG.LOG_DATE
            WHEN NOT MATCHED THEN
                INSERT (NP.PATN_TIME, NP.LINKID, NP.SPEED, NP.LOG_DATE, NP.LINKLEVEL)
                VALUES (SUBSTR(#{patternTime}, 9, 4), MG.LINKID, MG.SPEED, MG.LOG_DATE, MG.LINKLEVEL)
        ]]>
    </insert>

    <insert id="mergePatternWeekJob" parameterType="java.lang.String" timeout="120">
        <![CDATA[
        MERGE INTO ${targetTable} NP
            USING (SELECT /*+ INDEX(A PK_${statTable}) */
                          B.LINKID,
                          DECODE(#{weekType}, '3', NVL(A.SPEED, 0), 0) AS SPEED3,
                          DECODE(#{weekType}, '4', NVL(A.SPEED, 0), 0) AS SPEED4,
                          DECODE(#{weekType}, '5', NVL(A.SPEED, 0), 0) AS SPEED5,
                          SYSDATE AS LOG_DATE,
                          A.LINKLEVEL
                   FROM ${statTable} A, LINK B
                   WHERE A.STAT_DATE = SUBSTR(#{patternTime}, 1, 12)
                     AND B.LINKLEVEL IN ('1', '4')
                     AND A.SPEED > 0 AND A.SPEED < 150
                     AND A.LINKID(+) = B.LINKID
                     AND A.LINKLEVEL = B.LINKLEVEL) MG
            ON (NP.PATN_TIME = SUBSTR(#{patternTime}, 9, 4) AND NP.LINKID = MG.LINKID)
            WHEN MATCHED THEN
                UPDATE SET NP.${columnName} = MG.${columnName}
            WHEN NOT MATCHED THEN
                INSERT (NP.PATN_TIME, NP.LINKID, NP.SPEED3, NP.SPEED4, NP.SPEED5, NP.LINKLEVEL)
                VALUES (SUBSTR(#{patternTime}, 9, 4), MG.LINKID, MG.SPEED3, MG.SPEED4, MG.SPEED5, MG.LINKLEVEL)
        ]]>
    </insert>

    <insert id="mergePatternWeek" parameterType="java.lang.String" timeout="120">
        <![CDATA[
        MERGE INTO ${targetTable} NP
        USING (SELECT LINKID, LINKLEVEL,
                      (SPEED3+SPEED4+SPEED5)/(DECODE(SPEED3,0,0,1)+DECODE(SPEED4,0,0,1)+DECODE(SPEED4,0,0,1)) AS AVGSPEED
                   FROM ${jobTable}
                   WHERE PATN_TIME = SUBSTR(#{patternTime}, 9, 4)
                     AND (DECODE(SPEED3,0,0,1)+DECODE(SPEED4,0,0,1)+DECODE(SPEED4,0,0,1)) > 0
                     AND (SPEED3+SPEED4+SPEED5) > 0
                     AND (SPEED3+SPEED4+SPEED5) < 450) MG
            ON (NP.PATN_TIME = SUBSTR(#{patternTime}, 9, 4) AND NP.LINKID = MG.LINKID)
            WHEN MATCHED THEN
                UPDATE SET
                    NP.SPEED = DECODE(NP.SPEED, NULL, MG.AVGSPEED,
                                      ((NP.SPEED*TO_NUMBER(#{minValue})+MG.AVGSPEED*TO_NUMBER(#{maxValue}))/(TO_NUMBER(#{minValue})+TO_NUMBER(#{maxValue})))),
                    NP.LOG_DATE = SYSDATE
            WHEN NOT MATCHED THEN
                INSERT (NP.PATN_TIME, NP.LINKID, NP.SPEED,    NP.LOG_DATE, NP.LINKLEVEL)
                VALUES (SUBSTR(#{patternTime}, 9, 4), MG.LINKID, MG.AVGSPEED, SYSDATE, MG.LINKLEVEL)
        ]]>
    </insert>

    <select id="findPatternDay" parameterType="java.lang.String" resultType="com.utic.center.utic.stat.server.dto.PatternDto">
        <![CDATA[
        SELECT TO_CHAR(SYSDATE, 'YYYYMMDD') AS exeDay,
               TO_NUMBER(TO_CHAR(SYSDATE, 'D')) AS weekDay
        FROM DUAL
        ]]>
   </select>

    <select id="findPatternJobLog" parameterType="java.lang.String" resultType="com.utic.center.utic.stat.server.dto.PatternJobDto" timeout="30">
        <![CDATA[
        SELECT COUNT(1) AS jobCount
        FROM PATN_JOB_LOG
        WHERE PATN_TYPE = #{patternType}
          AND EXE_DAY   = #{exeDay}
        ]]>
    </select>

    <insert id="insertPatternJobLog" parameterType="java.lang.String" timeout="120">
        <![CDATA[
        INSERT INTO PATN_JOB_LOG (PATN_TYPE, EXE_DAY, START_DATE, END_DATE)
        VALUES (#{patternType}, #{exeDay}, SYSDATE, NULL)
        ]]>
   </insert>

    <update id="updatePatternJobLog" parameterType="java.lang.String" timeout="120">
        <![CDATA[
        UPDATE PATN_JOB_LOG SET END_DATE = SYSDATE
        WHERE PATN_TYPE = #{patternType}
          AND EXE_DAY   = #{exeDay}
        ]]>
  </update>

    <update id="deletePatternWeekJob" timeout="60">
        TRUNCATE TABLE ${tableName}
    </update>

    <update id="updatePatternWeekJob" parameterType="java.lang.String" timeout="120">
        <![CDATA[
        UPDATE ${tableName}
        SET SPEED3 = 0,
            SPEED4 = 0,
            SPEED5 = 0
        WHERE PATN_TIME = #{patternTime}
        ]]>
    </update>

</mapper>
