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

    <insert id="CRT_TB_RSE_OBU_CLCT_STAT_HH" parameterType="com.its.dsrc.vo.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>

    <insert id="CRT_TB_RSE_OBU_CLCT_STAT_DD" parameterType="com.its.dsrc.vo.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.dsrc.vo.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>
