<?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.bsmlab.dfx.dfxconsole.app.agent.service.DfxAgentMessageHistoryMapper">

    <select id="selectDfxAgentMessageList" resultType="com.bsmlab.dfx.dfxconsole.app.agent.service.DfxAgentMessageDto">
        <![CDATA[
        SELECT A.MESSAGE_UUID, A.SENDER_AGENT_ID, A.SENDER_POSTMAN_ID
            , (EXTRACT(EPOCH FROM A.SENDER_TS) * 1000)::BIGINT AS SENDER_TS, TO_CHAR(A.SENDER_TS, 'YYYY-MM-DD HH24:MI:SS') AS SENDER_TIME_STRING
            , A.RECIPIENT_AGENT_ID, A.RECIPIENT_DROPBOX_ID
            , (EXTRACT(EPOCH FROM A.RECIPIENT_TS) * 1000)::BIGINT AS RECIPIENT_TS, TO_CHAR(A.RECIPIENT_TS, 'YYYY-MM-DD HH24:MI:SS') AS RECIPIENT_TIME_STRING
            , A.MESSAGE_TYPE_CODE
            , (EXTRACT(EPOCH FROM A.CONSOLE_RECEIVE_TS) * 1000)::BIGINT AS CONSOLE_RECEIVE_TS, TO_CHAR(A.CONSOLE_RECEIVE_TS, 'YYYY-MM-DD HH24:MI:SS') AS CONSOLE_RECEIVE_TIME_STRING
            , A.PROCESS_STATUS_CODE, A.MESSAGE_DATA, A.MESSAGE_DATA_COUNT
            , (EXTRACT(EPOCH FROM A.PROCESS_ACK_TS) * 1000)::BIGINT AS PROCESS_ACK_TS, TO_CHAR(A.PROCESS_ACK_TS, 'YYYY-MM-DD HH24:MI:SS') AS PROCESS_ACK_TIME_STRING
            , B.HOST_NAME AS SENDER_HOST_NAME, C.HOST_NAME AS RECIPIENT_HOST_NAME
        FROM (
            SELECT SENDER_AGENT_ID, SENDER_POSTMAN_ID, SENDER_TS, RECIPIENT_AGENT_ID, RECIPIENT_DROPBOX_ID, RECIPIENT_TS, MESSAGE_UUID, MESSAGE_TYPE_CODE, CONSOLE_RECEIVE_TS, PROCESS_STATUS_CODE, MESSAGE_DATA, MESSAGE_DATA_COUNT, PROCESS_ACK_TS
            FROM TB_DFX_AGENT_MESSAGE
            WHERE 1 = 1
            ORDER BY CONSOLE_RECEIVE_TS
            LIMIT #{itemCountPerPage}
            OFFSET (#{page} - 1) * #{itemCountPerPage}
            ) A
            JOIN TB_DFX_AGENT_INFO B ON (A.SENDER_AGENT_ID = B.AGENT_ID)
            JOIN TB_DFX_AGENT_INFO C ON (A.RECIPIENT_AGENT_ID = C.AGENT_ID)
        WHERE 1 = 1
        ]]>
    </select>

    <select id="selectDfxAgentMessageListTotalCount" resultType="int">
        <![CDATA[
        SELECT COUNT(*)
        FROM (
            SELECT SENDER_AGENT_ID, SENDER_POSTMAN_ID, SENDER_TS, RECIPIENT_AGENT_ID, RECIPIENT_DROPBOX_ID, RECIPIENT_TS, MESSAGE_UUID, MESSAGE_TYPE_CODE, CONSOLE_RECEIVE_TS, PROCESS_STATUS_CODE, MESSAGE_DATA, MESSAGE_DATA_COUNT, PROCESS_ACK_TS
            FROM TB_DFX_AGENT_MESSAGE
            WHERE 1 = 1
            ) A
            JOIN TB_DFX_AGENT_INFO B ON (A.SENDER_AGENT_ID = B.AGENT_ID)
            JOIN TB_DFX_AGENT_INFO C ON (A.RECIPIENT_AGENT_ID = C.AGENT_ID)
        WHERE 1 = 1
        ]]>
    </select>

    <select id="selectDfxAgentMessageBySenderAgentIdAndMessageUuid" parameterType="com.bsmlab.dfx.dfxconsole.app.agent.service.DfxAgentMessageDto" resultType="com.bsmlab.dfx.dfxconsole.app.agent.service.DfxAgentMessageDto">
        <![CDATA[
        SELECT A.MESSAGE_UUID, A.SENDER_AGENT_ID, A.SENDER_POSTMAN_ID
            , (EXTRACT(EPOCH FROM A.SENDER_TS) * 1000)::BIGINT AS SENDER_TS, TO_CHAR(A.SENDER_TS, 'YYYY-MM-DD HH24:MI:SS') AS SENDER_TIME_STRING
            , A.RECIPIENT_AGENT_ID, A.RECIPIENT_DROPBOX_ID
            , (EXTRACT(EPOCH FROM A.RECIPIENT_TS) * 1000)::BIGINT AS RECIPIENT_TS, TO_CHAR(A.RECIPIENT_TS, 'YYYY-MM-DD HH24:MI:SS') AS RECIPIENT_TIME_STRING
            , A.MESSAGE_TYPE_CODE
            , (EXTRACT(EPOCH FROM A.CONSOLE_RECEIVE_TS) * 1000)::BIGINT AS CONSOLE_RECEIVE_TS, TO_CHAR(A.CONSOLE_RECEIVE_TS, 'YYYY-MM-DD HH24:MI:SS') AS CONSOLE_RECEIVE_TIME_STRING
            , A.PROCESS_STATUS_CODE, A.MESSAGE_DATA, A.MESSAGE_DATA_COUNT
            , (EXTRACT(EPOCH FROM A.PROCESS_ACK_TS) * 1000)::BIGINT AS PROCESS_ACK_TS, TO_CHAR(A.PROCESS_ACK_TS, 'YYYY-MM-DD HH24:MI:SS') AS PROCESS_ACK_TIME_STRING
            , B.HOST_NAME AS SENDER_HOST_NAME, C.HOST_NAME AS RECIPIENT_HOST_NAME
        FROM TB_DFX_AGENT_MESSAGE A
            JOIN TB_DFX_AGENT_INFO B ON (A.SENDER_AGENT_ID = B.AGENT_ID)
            JOIN TB_DFX_AGENT_INFO C ON (A.RECIPIENT_AGENT_ID = C.AGENT_ID)
        WHERE 1 = 1
            AND A.SENDER_AGENT_ID = #{senderAgentId}
            AND A.MESSAGE_UUID = #{messageUuid}
        ]]>
    </select>

    <insert id="insertDfxAgentMessage" parameterType="com.bsmlab.dfx.dfxconsole.app.agent.service.DfxAgentMessageDto">
        <![CDATA[
        INSERT INTO TB_DFX_AGENT_MESSAGE (
            MESSAGE_UUID, SENDER_AGENT_ID, SENDER_POSTMAN_ID
            , SENDER_TS
            , RECIPIENT_AGENT_ID, RECIPIENT_DROPBOX_ID
            , RECIPIENT_TS
            , MESSAGE_TYPE_CODE
            , CONSOLE_RECEIVE_TS
            , PROCESS_STATUS_CODE, MESSAGE_DATA, MESSAGE_DATA_COUNT
            , PROCESS_ACK_TS
        )
        VALUES (
            #{messageUuid}, #{senderAgentId}, #{senderPostmanId}
            , #{senderTs, jdbcType=TIMESTAMP_WITH_TIMEZONE, javaType=Long}
            , #{recipientAgentId}, #{recipientDropboxId}
            , #{recipientTs, jdbcType=TIMESTAMP_WITH_TIMEZONE, javaType=Long}
            , #{messageTypeCode}
            , #{consoleReceiveTs, jdbcType=TIMESTAMP_WITH_TIMEZONE, javaType=Long}
            , #{processStatusCode}, #{messageData}, #{messageDataCount}
            , #{processAckTs, jdbcType=TIMESTAMP_WITH_TIMEZONE, javaType=Long}
        )
        ]]>
    </insert>

    <select id="selectDfxAgentMessageHistoryList" resultType="com.bsmlab.dfx.dfxconsole.app.agent.service.DfxAgentMessageHistoryDto">
        <![CDATA[
        SELECT A.AGENT_MESSAGE_HISTORY_SEQ, A.MESSAGE_UUID, A.SENDER_AGENT_ID, A.SENDER_POSTMAN_ID
            , (EXTRACT(EPOCH FROM A.SENDER_TS) * 1000)::BIGINT AS SENDER_TS, TO_CHAR(A.SENDER_TS, 'YYYY-MM-DD HH24:MI:SS') AS SENDER_TIME_STRING
            , A.RECIPIENT_AGENT_ID, A.RECIPIENT_DROPBOX_ID
            , (EXTRACT(EPOCH FROM A.RECIPIENT_TS) * 1000)::BIGINT AS RECIPIENT_TS, TO_CHAR(A.RECIPIENT_TS, 'YYYY-MM-DD HH24:MI:SS') AS RECIPIENT_TIME_STRING
            , A.MESSAGE_TYPE_CODE
            , (EXTRACT(EPOCH FROM A.CONSOLE_RECEIVE_TS) * 1000)::BIGINT AS CONSOLE_RECEIVE_TS, TO_CHAR(A.CONSOLE_RECEIVE_TS, 'YYYY-MM-DD HH24:MI:SS') AS CONSOLE_RECEIVE_TIME_STRING
            , A.PROCESS_STATUS_CODE, A.MESSAGE_DATA, A.MESSAGE_DATA_COUNT
            , (EXTRACT(EPOCH FROM A.PROCESS_ACK_TS) * 1000)::BIGINT AS PROCESS_ACK_TS, TO_CHAR(A.PROCESS_ACK_TS, 'YYYY-MM-DD HH24:MI:SS') AS PROCESS_ACK_TIME_STRING
            , B.HOST_NAME AS SENDER_HOST_NAME, C.HOST_NAME AS RECIPIENT_HOST_NAME
        FROM (
            SELECT AGENT_MESSAGE_HISTORY_SEQ, SENDER_AGENT_ID, SENDER_TS, RECIPIENT_AGENT_ID, RECIPIENT_TS, MESSAGE_UUID, MESSAGE_TYPE_CODE, CONSOLE_RECEIVE_TS, PROCESS_STATUS_CODE, MESSAGE_DATA, MESSAGE_DATA_COUNT
            FROM TB_DFX_AGENT_MESSAGE_HISTORY
            WHERE 1 = 1
            ORDER BY AGENT_MESSAGE_HISTORY_SEQ DESC
            LIMIT 20
            OFFSET (1 - 1) * 20
            ) A
            JOIN TB_DFX_AGENT_INFO B ON (A.SENDER_AGENT_ID = B.AGENT_ID)
            JOIN TB_DFX_AGENT_INFO C ON (A.RECIPIENT_AGENT_ID = C.AGENT_ID)
        WHERE 1 = 1
            AND A.SENDER_AGENT_ID = #{senderAgentId}
            AND A.MESSAGE_UUID = #{messageUuid}
        ]]>
    </select>

    <select id="selectDfxAgentMessageHistoryBySenderAgentIdAndMessageUuid" parameterType="com.bsmlab.dfx.dfxconsole.app.agent.service.DfxAgentMessageHistoryDto" resultType="com.bsmlab.dfx.dfxconsole.app.agent.service.DfxAgentMessageHistoryDto">
        <![CDATA[
        SELECT A.AGENT_MESSAGE_HISTORY_SEQ, A.MESSAGE_UUID, A.SENDER_AGENT_ID, A.SENDER_POSTMAN_ID
            , (EXTRACT(EPOCH FROM A.SENDER_TS) * 1000)::BIGINT AS SENDER_TS, TO_CHAR(A.SENDER_TS, 'YYYY-MM-DD HH24:MI:SS') AS SENDER_TIME_STRING
            , A.RECIPIENT_AGENT_ID, A.RECIPIENT_DROPBOX_ID
            , (EXTRACT(EPOCH FROM A.RECIPIENT_TS) * 1000)::BIGINT AS RECIPIENT_TS, TO_CHAR(A.RECIPIENT_TS, 'YYYY-MM-DD HH24:MI:SS') AS RECIPIENT_TIME_STRING
            , A.MESSAGE_TYPE_CODE
            , (EXTRACT(EPOCH FROM A.CONSOLE_RECEIVE_TS) * 1000)::BIGINT AS CONSOLE_RECEIVE_TS, TO_CHAR(A.CONSOLE_RECEIVE_TS, 'YYYY-MM-DD HH24:MI:SS') AS CONSOLE_RECEIVE_TIME_STRING
            , A.PROCESS_STATUS_CODE, A.MESSAGE_DATA, A.MESSAGE_DATA_COUNT
            , (EXTRACT(EPOCH FROM A.PROCESS_ACK_TS) * 1000)::BIGINT AS PROCESS_ACK_TS, TO_CHAR(A.PROCESS_ACK_TS, 'YYYY-MM-DD HH24:MI:SS') AS PROCESS_ACK_TIME_STRING
            , B.HOST_NAME AS SENDER_HOST_NAME, C.HOST_NAME AS RECIPIENT_HOST_NAME
        FROM TB_DFX_AGENT_MESSAGE_HISTORY A
            JOIN TB_DFX_AGENT_INFO B ON (A.SENDER_AGENT_ID = B.AGENT_ID)
            JOIN TB_DFX_AGENT_INFO C ON (A.RECIPIENT_AGENT_ID = C.AGENT_ID)
        WHERE 1 = 1
            AND A.SENDER_AGENT_ID = #{senderAgentId}
            AND A.MESSAGE_UUID = #{messageUuid}
        ]]>
    </select>

    <insert id="insertDfxAgentMessageHistory" parameterType="com.bsmlab.dfx.dfxconsole.app.agent.service.DfxAgentMessageHistoryDto">
        <selectKey resultType="long" keyColumn="AGENT_MESSAGE_HISTORY_SEQ" keyProperty="agentMessageHistorySeq" order="BEFORE">
            SELECT NEXTVAL('SQ_DFX_AGENT_MESSAGE_HISTORY') AS AGENT_MESSAGE_HISTORY_SEQ
        </selectKey>
        <![CDATA[
        INSERT INTO TB_DFX_AGENT_MESSAGE_HISTORY (
            AGENT_MESSAGE_HISTORY_SEQ
            , MESSAGE_UUID, SENDER_AGENT_ID, SENDER_POSTMAN_ID
            , SENDER_TS
            , RECIPIENT_AGENT_ID, RECIPIENT_DROPBOX_ID
            , RECIPIENT_TS
            , MESSAGE_TYPE_CODE
            , CONSOLE_RECEIVE_TS
            , PROCESS_STATUS_CODE, MESSAGE_DATA, MESSAGE_DATA_COUNT
            , PROCESS_ACK_TS
        )
        VALUES (
            #{agentMessageHistorySeq}
            , #{messageUuid}, #{senderAgentId}, #{senderPostmanId}
            , #{senderTs, jdbcType=TIMESTAMP_WITH_TIMEZONE, javaType=Long}
            , #{recipientAgentId}, #{recipientDropboxId}
            , #{recipientTs, jdbcType=TIMESTAMP_WITH_TIMEZONE, javaType=Long}
            , #{messageTypeCode}
            , #{consoleReceiveTs, jdbcType=TIMESTAMP_WITH_TIMEZONE, javaType=Long}
            , #{processStatusCode}, #{messageData}, #{messageDataCount}
            , #{processAckTs, jdbcType=TIMESTAMP_WITH_TIMEZONE, javaType=Long}
        )
        ]]>
    </insert>

    <update id="updateDfxAgentMessageHistoryForProcessAck" parameterType="com.bsmlab.dfx.dfxconsole.app.agent.service.DfxAgentMessageHistoryDto">
        <![CDATA[
        UPDATE TB_DFX_AGENT_MESSAGE_HISTORY
        SET PROCESS_STATUS_CODE = #{processStatusCode}
            , PROCESS_ACK_TS = NOW()
        WHERE 1 = 1
            AND SENDER_AGENT_ID = #{senderAgentId}
            AND MESSAGE_UUID = #{messageUuid}
        ]]>
    </update>

    <update id="mergeDfxAgentMessageByDfxAgentMessageHistory">
        <![CDATA[
        INSERT INTO TB_DFX_AGENT_MESSAGE (
            MESSAGE_UUID, SENDER_AGENT_ID, SENDER_POSTMAN_ID, SENDER_TS, RECIPIENT_AGENT_ID, RECIPIENT_DROPBOX_ID, RECIPIENT_TS, MESSAGE_TYPE_CODE, CONSOLE_RECEIVE_TS, PROCESS_STATUS_CODE, MESSAGE_DATA, MESSAGE_DATA_COUNT, PROCESS_ACK_TS
        )
        SELECT MESSAGE_UUID, SENDER_AGENT_ID, MAX(SENDER_POSTMAN_ID) AS SENDER_POSTMAN_ID, MIN(SENDER_TS) AS SENDER_TS
            , MAX(RECIPIENT_AGENT_ID) AS RECIPIENT_AGENT_ID, MAX(RECIPIENT_DROPBOX_ID) AS RECIPIENT_DROPBOX_ID, MIN(RECIPIENT_TS) AS RECIPIENT_TS
            , MAX(MESSAGE_TYPE_CODE) AS MESSAGE_TYPE_CODE, MAX(CONSOLE_RECEIVE_TS) AS CONSOLE_RECEIVE_TS
            , CASE WHEN MAX(PROCESS_STATUS_ORDER) = 3 THEN 'PROCESS_FAIL'
                WHEN MAX(PROCESS_STATUS_ORDER) = 2 THEN 'PROCESS_DONE'
                WHEN MAX(PROCESS_STATUS_ORDER) = 1 THEN 'PROCESS_RECEIVE'
                ELSE 'PROCESS_RECEIVE' END AS PROCESS_STATUS_CODE
            , MAX(MESSAGE_DATA) AS MESSAGE_DATA, MAX(MESSAGE_DATA_COUNT) AS MESSAGE_DATA_COUNT, MIN(PROCESS_ACK_TS) AS PROCESS_ACK_TS
        FROM (
            SELECT MESSAGE_UUID, SENDER_AGENT_ID, SENDER_POSTMAN_ID, SENDER_TS, RECIPIENT_AGENT_ID, RECIPIENT_DROPBOX_ID, RECIPIENT_TS, MESSAGE_TYPE_CODE, CONSOLE_RECEIVE_TS, PROCESS_STATUS_CODE, MESSAGE_DATA, MESSAGE_DATA_COUNT, PROCESS_ACK_TS
                , CASE WHEN PROCESS_STATUS_CODE = 'PROCESS_RECEIVED' THEN 1 WHEN PROCESS_STATUS_CODE = 'PROCESS_DONE' THEN 2 WHEN PROCESS_STATUS_CODE = 'PROCESS_FAIL' THEN 3 ELSE 9 END AS PROCESS_STATUS_ORDER
            FROM TB_DFX_AGENT_MESSAGE_HISTORY
            WHERE 1 = 1
                AND CONSOLE_RECEIVE_TS BETWEEN CURRENT_TIMESTAMP - INTERVAL '14 days' AND CURRENT_TIMESTAMP - INTERVAL '1 hour'
            ) A
        GROUP BY MESSAGE_UUID, SENDER_AGENT_ID
        ON CONFLICT (MESSAGE_UUID, SENDER_AGENT_ID) DO
            UPDATE SET
                SENDER_POSTMAN_ID = EXCLUDED.SENDER_POSTMAN_ID
                , SENDER_TS = EXCLUDED.SENDER_TS
                , RECIPIENT_AGENT_ID = EXCLUDED.RECIPIENT_AGENT_ID
                , RECIPIENT_DROPBOX_ID = EXCLUDED.RECIPIENT_DROPBOX_ID
                , RECIPIENT_TS = EXCLUDED.RECIPIENT_TS
                , MESSAGE_TYPE_CODE = EXCLUDED.MESSAGE_TYPE_CODE
                , CONSOLE_RECEIVE_TS = EXCLUDED.CONSOLE_RECEIVE_TS
                , PROCESS_STATUS_CODE = EXCLUDED.PROCESS_STATUS_CODE
                , MESSAGE_DATA = EXCLUDED.MESSAGE_DATA
                , MESSAGE_DATA_COUNT = EXCLUDED.MESSAGE_DATA_COUNT
                , PROCESS_ACK_TS = EXCLUDED.PROCESS_ACK_TS
        ]]>
    </update>
</mapper>
