<?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.vds.dao.mapper.VdsStatMapper">

    <insert id="CRT_TB_VDS_DTCT_HS" parameterType="com.its.vds.entity.VdsStatTime">
    <![CDATA[
        INSERT INTO TB_VDS_DTCT_HS (PRCN_DT,
                                    VDS_DTCT_NMBR,
                                    TFVL,
                                    SPED,
                                    AVRG_OCPY_RATE,
                                    AVRG_LNGT,
                                    HDWY,
                                    SPCE_OCPY_RATE,
                                    SPCE_AVRG_SPED
                                    )
        SELECT  #{prcs.STAT_DT}               AS PRCN_DT,
                VDS_DTCT_NMBR                 AS VDS_DTCT_NMBR,
                SUM(TFVL)                     AS TFVL,
                ROUND(AVG(SPED), 0)           AS SPED,
                AVG(AVRG_OCPY_RATE)           AS AVRG_OCPY_RATE,
                AVG(AVRG_LNGT)                AS AVRG_LNGT,
                ROUND(AVG(HDWY), 0)           AS HDWY,
                AVG(SPCE_OCPY_RATE)           AS SPCE_OCPY_RATE,
                ROUND(AVG(SPCE_AVRG_SPED), 0) AS SPCE_AVRG_SPED
		  FROM TB_VDS_DTCT_CLCT
		 WHERE CLCT_DT BETWEEN #{prcs.FROM_DT} AND #{prcs.TO_DT}
         GROUP BY VDS_DTCT_NMBR
        ]]>
    </insert>

    <insert id="CRT_TB_VDS_DTCT_STAT_15M" parameterType="com.its.vds.entity.VdsStatTime">
    <![CDATA[
        INSERT INTO TB_VDS_DTCT_STAT_15M (STAT_DT,
                                          VDS_DTCT_NMBR,
                                          TFVL,
                                          AVRG_SPED,
                                          AVRG_OCPY_RATE,
                                          AVRG_LNGT,
                                          AVRG_HDWY,
                                          AVRG_SPCE_OCPY_RATE,
                                          AVRG_SPCE_AVRG_SPED
                                          )
        SELECT  #{prcs.STAT_DT}               AS STAT_DT,
                VDS_DTCT_NMBR                 AS VDS_DTCT_NMBR,
                SUM(TFVL)                     AS TFVL,
                ROUND(AVG(SPED), 0)           AS AVRG_SPED,
                AVG(AVRG_OCPY_RATE)           AS AVRG_OCPY_RATE,
                AVG(AVRG_LNGT)                AS AVRG_LNGT,
                ROUND(AVG(HDWY), 0)           AS AVRG_HDWY,
                AVG(SPCE_OCPY_RATE)           AS AVRG_SPCE_OCPY_RATE,
                ROUND(AVG(SPCE_AVRG_SPED), 0) AS AVRG_SPCE_AVRG_SPED
        FROM TB_VDS_DTCT_CLCT
        WHERE CLCT_DT BETWEEN #{prcs.FROM_DT} AND #{prcs.TO_DT}
        GROUP BY VDS_DTCT_NMBR
        ]]>
    </insert>

    <insert id="CRT_TB_VDS_DTCT_STAT_HH" parameterType="com.its.vds.entity.VdsStatTime">
    <![CDATA[
        INSERT INTO TB_VDS_DTCT_STAT_HH (STAT_DT,
                                         VDS_DTCT_NMBR,
                                         TFVL,
                                         AVRG_SPED,
                                         AVRG_OCPY_RATE,
                                         AVRG_LNGT,
                                         AVRG_HDWY,
                                         AVRG_SPCE_OCPY_RATE,
                                         AVRG_SPCE_AVRG_SPED
                                         )
        SELECT  #{prcs.STAT_DT}                    AS STAT_DT,
                VDS_DTCT_NMBR                      AS VDS_DTCT_NMBR,
                SUM(TFVL)                          AS TFVL,
                ROUND(AVG(AVRG_SPED), 0)           AS AVRG_SPED,
                AVG(AVRG_OCPY_RATE)                AS AVRG_OCPY_RATE,
                AVG(AVRG_LNGT)                     AS AVRG_LNGT,
                ROUND(AVG(AVRG_HDWY), 0)           AS AVRG_HDWY,
                AVG(AVRG_SPCE_OCPY_RATE)           AS AVRG_SPCE_OCPY_RATE,
                ROUND(AVG(AVRG_SPCE_AVRG_SPED), 0) AS AVRG_SPCE_AVRG_SPED
        FROM TB_VDS_DTCT_STAT_15M
        WHERE STAT_DT BETWEEN #{prcs.FROM_DT} AND #{prcs.TO_DT}
        GROUP BY VDS_DTCT_NMBR
        ]]>
    </insert>

    <insert id="CRT_TB_VDS_DTCT_STAT_DD" parameterType="com.its.vds.entity.VdsStatTime">
    <![CDATA[
        INSERT INTO TB_VDS_DTCT_STAT_DD (STAT_DT,
                                         VDS_DTCT_NMBR,
                                         TFVL,
                                         AVRG_SPED,
                                         AVRG_OCPY_RATE,
                                         AVRG_LNGT,
                                         AVRG_HDWY,
                                         AVRG_SPCE_OCPY_RATE,
                                         AVRG_SPCE_AVRG_SPED
                                         )
        SELECT  #{prcs.STAT_DT}                    AS STAT_DT,
                VDS_DTCT_NMBR                      AS VDS_DTCT_NMBR,
                MIN(SYST_KIND_DVSN)                AS SYST_KIND_DVSN,
                SUM(TFVL)                          AS TFVL,
                ROUND(AVG(AVRG_SPED), 0)           AS AVRG_SPED,
                AVG(AVRG_OCPY_RATE)                AS AVRG_OCPY_RATE,
                AVG(AVRG_LNGT)                     AS AVRG_LNGT,
                ROUND(AVG(AVRG_HDWY), 0)           AS AVRG_HDWY,
                AVG(AVRG_SPCE_OCPY_RATE)           AS AVRG_SPCE_OCPY_RATE,
                ROUND(AVG(AVRG_SPCE_AVRG_SPED), 0) AS AVRG_SPCE_AVRG_SPED
        FROM TB_VDS_DTCT_STAT_HH
        WHERE STAT_DT BETWEEN #{prcs.FROM_DT} AND #{prcs.TO_DT}
        GROUP BY VDS_DTCT_NMBR
        ]]>
    </insert>

    <update id="CRT_TB_VDS_DTCT_STAT_MN" parameterType="com.its.vds.entity.VdsStatTime">
    <![CDATA[
        MERGE INTO TB_VDS_DTCT_STAT_MN L
            USING (
                SELECT  #{prcs.STAT_DT}                    AS STAT_DT,
                        VDS_DTCT_NMBR                      AS VDS_DTCT_NMBR,
                        SUM(TFVL)                          AS TFVL,
                        ROUND(AVG(AVRG_SPED), 0)           AS AVRG_SPED,
                        AVG(AVRG_OCPY_RATE)                AS AVRG_OCPY_RATE,
                        AVG(AVRG_LNGT)                     AS AVRG_LNGT,
                        ROUND(AVG(AVRG_HDWY), 0)           AS AVRG_HDWY,
                        AVG(AVRG_SPCE_OCPY_RATE)           AS AVRG_SPCE_OCPY_RATE,
                        ROUND(AVG(AVRG_SPCE_AVRG_SPED), 0) AS AVRG_SPCE_AVRG_SPED
                FROM TB_VDS_DTCT_STAT_DD
                WHERE STAT_DT BETWEEN #{prcs.FROM_DT} AND #{prcs.TO_DT}
                GROUP BY VDS_DTCT_NMBR ) M
            ON (L.STAT_DT        = M.STAT_DT
                AND L.VDS_DTCT_NMBR  = M.VDS_DTCT_NMBR)
            WHEN MATCHED THEN UPDATE SET
                L.TFVL                = M.TFVL,
                L.AVRG_SPED           = M.AVRG_SPED,
                L.AVRG_OCPY_RATE      = M.AVRG_OCPY_RATE,
                L.AVRG_LNGT           = M.AVRG_LNGT,
                L.AVRG_HDWY           = M.AVRG_HDWY,
                L.AVRG_SPCE_OCPY_RATE = M.AVRG_SPCE_OCPY_RATE,
                L.AVRG_SPCE_AVRG_SPED = M.AVRG_SPCE_AVRG_SPED
            WHEN NOT MATCHED THEN
                INSERT(L.STAT_DT, L.VDS_DTCT_NMBR, L.TFVL, L.AVRG_SPED,
                       L.AVRG_OCPY_RATE, L.AVRG_LNGT, L.AVRG_HDWY, L.AVRG_SPCE_OCPY_RATE, L.AVRG_SPCE_AVRG_SPED )
                VALUES(M.STAT_DT, M.VDS_DTCT_NMBR, M.TFVL, M.AVRG_SPED,
                       M.AVRG_OCPY_RATE, M.AVRG_LNGT, M.AVRG_HDWY, M.AVRG_SPCE_OCPY_RATE, M.AVRG_SPCE_AVRG_SPED )
        ]]>
    </update>

    <update id="CRT_TB_VDS_DTCT_STAT_YY" parameterType="com.its.vds.entity.VdsStatTime">
    <![CDATA[
        MERGE INTO TB_VDS_DTCT_STAT_YY L
            USING (
                SELECT  #{prcs.STAT_DT}                    AS STAT_DT,
                        VDS_DTCT_NMBR                      AS VDS_DTCT_NMBR,
                        SUM(TFVL)                          AS TFVL,
                        ROUND(AVG(AVRG_SPED), 0)           AS AVRG_SPED,
                        AVG(AVRG_OCPY_RATE)                AS AVRG_OCPY_RATE,
                        AVG(AVRG_LNGT)                     AS AVRG_LNGT,
                        ROUND(AVG(AVRG_HDWY), 0)           AS AVRG_HDWY,
                        AVG(AVRG_SPCE_OCPY_RATE)           AS AVRG_SPCE_OCPY_RATE,
                        ROUND(AVG(AVRG_SPCE_AVRG_SPED), 0) AS AVRG_SPCE_AVRG_SPED
                FROM TB_VDS_DTCT_STAT_MN
                WHERE STAT_DT BETWEEN #{prcs.FROM_DT} AND #{prcs.TO_DT}
                GROUP BY VDS_DTCT_NMBR ) M
            ON (L.STAT_DT       = M.STAT_DT
                AND L.VDS_DTCT_NMBR = M.VDS_DTCT_NMBR)
            WHEN MATCHED THEN UPDATE SET
                L.TFVL                = M.TFVL,
                L.AVRG_SPED           = M.AVRG_SPED,
                L.AVRG_OCPY_RATE      = M.AVRG_OCPY_RATE,
                L.AVRG_LNGT           = M.AVRG_LNGT,
                L.AVRG_HDWY           = M.AVRG_HDWY,
                L.AVRG_SPCE_OCPY_RATE = M.AVRG_SPCE_OCPY_RATE,
                L.AVRG_SPCE_AVRG_SPED = M.AVRG_SPCE_AVRG_SPED
            WHEN NOT MATCHED THEN
                INSERT(L.STAT_DT, L.VDS_DTCT_NMBR, L.TFVL, L.AVRG_SPED,
                       L.AVRG_OCPY_RATE, L.AVRG_LNGT, L.AVRG_HDWY,
                       L.AVRG_SPCE_OCPY_RATE, L.AVRG_SPCE_AVRG_SPED )
                VALUES(M.STAT_DT, M.VDS_DTCT_NMBR, M.TFVL, M.AVRG_SPED,
                       M.AVRG_OCPY_RATE, M.AVRG_LNGT, M.AVRG_HDWY,
                       M.AVRG_SPCE_OCPY_RATE, M.AVRG_SPCE_AVRG_SPED )
        ]]>
    </update>

</mapper>
