<?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.vms.dao.mapper.RseStatMapper">

    <insert id="CRT_TB_RSE_OBU_CLCT_STAT_HH" parameterType="com.its.vms.dto.voStatisticsTime">
    <![CDATA[
        INSERT INTO TB_RSE_OBU_CLCT_STAT_HH (STAT_DT,
                                             RSE_CTLR_NMBR,
                                             DAY_TYPE_CD,
                                             CLCT_NUM
        )
        SELECT  #{prcs.STAT_DT}   AS STAT_DT,
                RSE_CTLR_NMBR     AS RSE_CTLR_NMBR,
                CASE TO_CHAR(TO_DATE(#{prcs.STAT_DT}, 'YYYYMMDDHH24MISS'), 'D') WHEN '1' THEN 'DTW7'
                                                                                ELSE 'DTW'||TO_CHAR(TO_NUMBER(TO_CHAR(TO_DATE(#{prcs.STAT_DT}, 'YYYYMMDDHH24MISS'), 'D')) - 1)
                    END               AS DAY_TYPE_CD,
                COUNT(1)          AS CLCT_NUM
        FROM TB_RSE_OBU_CLCT_HS
        WHERE CLCT_DT BETWEEN #{prcs.FROM_DT} AND #{prcs.TO_DT}
        GROUP BY RSE_CTLR_NMBR
        ]]>
    </insert>

    <update id="CRT_TB_RSE_OD_STAT_HH" parameterType="com.its.vms.dto.voStatisticsTime">
    <![CDATA[
        MERGE INTO TB_RSE_OD_STAT_HH L
        USING (SELECT #{prcs.STAT_DT}        AS STAT_DT,
                      F_RSE_CTLR_NMBR        AS F_RSE_CTLR_NMBR,
                      T_RSE_CTLR_NMBR        AS T_RSE_CTLR_NMBR,
                      #{prcs.DAY_TYPE_CD}    AS DAY_TYPE_CD,
                      COUNT(1)               AS TFVL,
                      ROUND(AVG(TRVL_HH), 0) AS TRVL_HH
               FROM (SELECT OBU_IDNT_NMBR, F_RSE_CTLR_NMBR, T_RSE_CTLR_NMBR, F_CLCT_DT, T_CLCT_DT,
                            (TO_DATE(T_CLCT_DT, 'YYYYMMDDHH24MISS') - TO_DATE(F_CLCT_DT, 'YYYYMMDDHH24MISS')) * 86400 AS TRVL_HH
                     FROM (SELECT OBU_IDNT_NMBR,
                                  LAG(RSE_CTLR_NMBR) OVER (PARTITION BY OBU_IDNT_NMBR ORDER BY CLCT_DT) AS F_RSE_CTLR_NMBR,
                                  RSE_CTLR_NMBR                                                         AS T_RSE_CTLR_NMBR,
                                  LAG(CLCT_DT) OVER (PARTITION BY OBU_IDNT_NMBR ORDER BY CLCT_DT)       AS F_CLCT_DT,
                                  CLCT_DT                                                               AS T_CLCT_DT
                           FROM (SELECT OBU_IDNT_NMBR, CLCT_DT, RSE_CTLR_NMBR AS RSE_CTLR_NMBR
                                 FROM TB_RSE_OBU_CLCT_HS
                                 WHERE CLCT_DT BETWEEN #{prcs.FROM_DT} AND #{prcs.TO_DT}
                                 UNION ALL
                                 SELECT OBU_IDNT_NMBR, CLCT_DT, RSE_CTLR_NMBR AS RSE_CTLR_NMBR
                                 FROM TB_RSE_OBU_CLCT_HS
                                 WHERE CLCT_DT BETWEEN #{prcs.B_FROM_DT} AND #{prcs.B_TO_DT}
                                   AND (OBU_IDNT_NMBR, CLCT_DT) IN (SELECT OBU_IDNT_NMBR, MAX(CLCT_DT) AS CLCT_DT
                                                                    FROM TB_RSE_OBU_CLCT_HS
                                                                    WHERE CLCT_DT BETWEEN #{prcs.B_FROM_DT} AND #{prcs.B_TO_DT}
                                                                    GROUP BY OBU_IDNT_NMBR)
                                )
                          )
                     WHERE F_RSE_CTLR_NMBR IS NOT NULL
                       AND F_RSE_CTLR_NMBR <> T_RSE_CTLR_NMBR
                    )
               GROUP BY F_RSE_CTLR_NMBR, T_RSE_CTLR_NMBR) M
        ON ( L.STAT_DT         = M.STAT_DT
         AND L.F_RSE_CTLR_NMBR = M.F_RSE_CTLR_NMBR
         AND L.T_RSE_CTLR_NMBR = M.T_RSE_CTLR_NMBR )
        WHEN MATCHED THEN
            UPDATE SET L.DAY_TYPE_CD = M.DAY_TYPE_CD,
                       L.TFVL        = M.TFVL
        WHEN NOT MATCHED THEN
            INSERT (
                    L.STAT_DT,
                    L.F_RSE_CTLR_NMBR,
                    L.T_RSE_CTLR_NMBR,
                    L.DAY_TYPE_CD,
                    L.TFVL
                ) VALUES (
                    M.STAT_DT,
                    M.F_RSE_CTLR_NMBR,
                    M.T_RSE_CTLR_NMBR,
                    M.DAY_TYPE_CD,
                    M.TFVL
                )
        ]]>
    </update>

    <insert id="CRT_TB_RSE_OBU_CLCT_STAT_DD" parameterType="com.its.vms.dto.voStatisticsTime">
    <![CDATA[
		INSERT INTO TB_RSE_OBU_CLCT_STAT_DD (STAT_DT,
                                             RSE_CTLR_NMBR,
		                                     DAY_TYPE_CD,
		                                     CLCT_NUM
		                                    )
		SELECT  #{prcs.STAT_DT}  AS STAT_DT,
                RSE_CTLR_NMBR    AS RSE_CTLR_NMBR,
		        MIN(DAY_TYPE_CD) AS DAY_TYPE_CD,
		        SUM(CLCT_NUM)    AS CLCT_NUM
		  FROM TB_RSE_OBU_CLCT_STAT_HH
		 WHERE STAT_DT BETWEEN #{prcs.FROM_DT} AND #{prcs.TO_DT}                               
		 GROUP BY RSE_CTLR_NMBR
    ]]>
    </insert>

    <update id="CRT_TB_RSE_OBU_CLCT_HH_PNST" parameterType="com.its.vms.dto.voStatisticsTime">
    <![CDATA[
        MERGE INTO TB_RSE_OBU_CLCT_PNST_HH L
            USING (
                SELECT  #{prcs.STAT_DT}  AS STAT_DT,
                        RSE_CTLR_NMBR    AS RSE_CTLR_NMBR,
                        MIN(DAY_TYPE_CD) AS DAY_TYPE_CD,
                        SUM(CLCT_NUM)    AS CLCT_NUM
                FROM TB_RSE_OBU_CLCT_STAT_HH
                WHERE STAT_DT = #{prcs.STAT_DT}
                GROUP BY RSE_CTLR_NMBR ) M
            ON (L.RSE_CTLR_NMBR = M.RSE_CTLR_NMBR)
            WHEN MATCHED THEN UPDATE SET
                L.STAT_DT     = M.STAT_DT,
                L.DAY_TYPE_CD = M.DAY_TYPE_CD,
                L.CLCT_NUM    = M.CLCT_NUM
            WHEN NOT MATCHED THEN
                INSERT(L.STAT_DT, L.RSE_CTLR_NMBR, L.DAY_TYPE_CD, L.CLCT_NUM )
                    VALUES(M.STAT_DT, M.RSE_CTLR_NMBR, M.DAY_TYPE_CD, M.CLCT_NUM )
        ]]>
    </update>

</mapper>
