<?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.munjaon.client.cubrid.mapper.CubridMapper">
    <select id="checkTime" resultType="String">
        /* CubridMapper.checkTime */
        SELECT DATE_FORMAT(NOW(), '%Y%m%d%H%i%s')
    </select>
    <select id="checkTableForMessage" resultType="int">
        /* CubridMapper.checkTableForMessage */
        SELECT COUNT(MSG_ID) FROM MUNJAON_MSG WHERE 1=1 LIMIT 1
    </select>
    <select id="checkTableForLog" resultType="int">
        /* CubridMapper.checkTableForLog */
        SELECT COUNT(MSG_ID) FROM MUNJAON_MSG_LOG WHERE 1=1 LIMIT 1
    </select>
    <select id="selectToDeliver" resultType="com.munjaon.client.model.MunjaonMsg">
        /* CubridMapper.selectToDeliver */
        SELECT
            MSG_ID
             , MSG_TYPE
             , to_char(REQUEST_DATE, 'YYYYMMDDHH24MISS') RESERVE_DATE
             , to_char(SYSDATETIME, 'YYYYMMDDHH24MISS') REQUEST_DATE
             , RECV_PHONE
             , SEND_PHONE
             , SUBJECT
             , MESSAGE
             , FILENAME01
             , FILENAME02
             , FILENAME03
             , KAKAO_SENDER_KEY
             , KAKAO_TEMPLATE_CODE
             , KAKAO_JSON_FILE
        FROM MUNJAON_MSG
        WHERE SEND_STATUS = '0'
          AND MSG_TYPE = #{msgType}
          AND REQUEST_DATE <![CDATA[<=]]> SYSDATETIME
        ORDER BY REQUEST_DATE ASC
            LIMIT 100
    </select>
    <update id="updateToDeliver">
        /* CubridMapper.updateToDeliver */
        UPDATE MUNJAON_MSG SET
            SEND_STATUS = '1'
            , DELIVER_DATE = SYSDATETIME
        WHERE MSG_ID = #{msgId}
    </update>
    <update id="updateDeliverForList">
        /* CubridMapper.updateDeliverForList */
        UPDATE MUNJAON_MSG SET
            SEND_STATUS = '1'
            , DELIVER_DATE = SYSDATETIME
        WHERE SEND_STATUS = '0'
            AND (
        <foreach separator="OR" item="item" collection="list">
            MSG_ID = #{item}
        </foreach>
        )
    </update>
    <update id="updateToReport">
        /* CubridMapper.updateToReport */
        UPDATE MUNJAON_MSG SET
            SEND_STATUS = #{sendStatus}
                             , SENT_DATE = FROM_UNIXTIME(UNIX_TIMESTAMP(#{sendDate}))
                             , REPORT_DATE = NOW()
                             , TELECOM = #{telecom}
        WHERE MSG_ID = #{msgId}
    </update>
    <select id="selectToMove" resultType="com.munjaon.client.model.MunjaonMsg">
        /* CubridMapper.selectToMove */
        SELECT MSG_ID, to_date(REQUEST_DATE, 'YYYYMM') REQUEST_DATE
        FROM MUNJAON_MSG
        WHERE SEND_STATUS NOT IN ('0', '1')
            LIMIT 100
    </select>
    <select id="selectToExpire" resultType="com.munjaon.client.model.MunjaonMsg">
        /* CubridMapper.selectToExpire */
        SELECT MSG_ID, to_date(REQUEST_DATE, 'YYYYMM') REQUEST_DATE
        FROM MUNJAON_MSG
        WHERE REQUEST_DATE <![CDATA[<]]> FROM_UNIXTIME(UNIX_TIMESTAMP(#{expireTime}))
          AND SEND_STATUS = '1'
            LIMIT 100
    </select>
    <insert id="insertToLog">
        /* CubridMapper.insertToLog */
        INSERT INTO MUNJAON_MSG_LOG
        (
            MSG_ID, MSG_TYPE, SEND_STATUS, REQUEST_DATE,
            RECV_PHONE, SEND_PHONE, SUBJECT, MESSAGE, TELECOM,
            FILENAME01, FILENAME02, FILENAME03, DELIVER_DATE, SENT_DATE, REPORT_DATE
        )
        SELECT
            MSG_ID, MSG_TYPE, SEND_STATUS, REQUEST_DATE,
            RECV_PHONE, SEND_PHONE, SUBJECT, MESSAGE, TELECOM,
            FILENAME01, FILENAME02, FILENAME03, DELIVER_DATE, SENT_DATE, REPORT_DATE
        FROM MUNJAON_MSG
        WHERE MSG_ID = #{msgId}
    </insert>
    <delete id="deleteFromDeliver">
        /* CubridMapper.deleteFromDeliver */
        DELETE FROM MUNJAON_MSG
        WHERE MSG_ID = #{msgId}
    </delete>
</mapper>