<?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.DfxAgentInfoMapper">

    <select id="selectDfxAgentInfoList" resultType="com.bsmlab.dfx.dfxconsole.app.agent.service.DfxAgentInfoDto">
        <![CDATA[
        SELECT AGENT_ID, HOST_NAME, LISTEN_PORT, DESCRIPTION, POSTMAN_COUNT, DROPBOX_COUNT, STATUS_CODE
            , (EXTRACT(EPOCH FROM LAST_STATUS_TS) * 1000)::BIGINT AS LAST_STATUS_TS
            , SETTINGS_DATA
            , TO_CHAR(LAST_STATUS_TS, 'YYYY-MM-DD HH24:MI:SS') AS LAST_STATUS_TIME_STRING
        FROM TB_DFX_AGENT_INFO
        ORDER BY HOST_NAME, AGENT_ID
        ]]>
    </select>

    <select id="selectDfxAgentInfoByAgentId" parameterType="com.bsmlab.dfx.dfxconsole.app.agent.service.DfxAgentInfoDto" resultType="com.bsmlab.dfx.dfxconsole.app.agent.service.DfxAgentInfoDto">
        <![CDATA[
        SELECT AGENT_ID, HOST_NAME, LISTEN_PORT, DESCRIPTION, POSTMAN_COUNT, DROPBOX_COUNT, STATUS_CODE
            , (EXTRACT(EPOCH FROM LAST_STATUS_TS) * 1000)::BIGINT AS LAST_STATUS_TS
            , SETTINGS_DATA
            , TO_CHAR(LAST_STATUS_TS, 'YYYY-MM-DD HH24:MI:SS') AS LAST_STATUS_TIME_STRING
        FROM TB_DFX_AGENT_INFO
        WHERE 1 = 1
            AND AGENT_ID = #{agentId}
        ]]>
    </select>

    <insert id="insertDfxAgentInfo" parameterType="com.bsmlab.dfx.dfxconsole.app.agent.service.DfxAgentInfoDto">
        <![CDATA[
        INSERT INTO TB_DFX_AGENT_INFO (
            AGENT_ID, HOST_NAME, LISTEN_PORT, DESCRIPTION, POSTMAN_COUNT, DROPBOX_COUNT, STATUS_CODE
            , LAST_STATUS_TS
            , SETTINGS_DATA
        )
        VALUES (
            #{agentId}, #{hostName}, #{listenPort}, #{description}, #{postmanCount}, #{dropboxCount}, #{statusCode}
            , #{lastStatusTs, jdbcType=TIMESTAMP_WITH_TIMEZONE, javaType=long}
            , #{settingsData}
        )
        ]]>
    </insert>

    <update id="updateDfxAgentInfo" parameterType="com.bsmlab.dfx.dfxconsole.app.agent.service.DfxAgentInfoDto">
        <![CDATA[
        UPDATE TB_DFX_AGENT_INFO
        SET HOST_NAME = #{hostName}
            , LISTEN_PORT = #{listenPort}
            , DESCRIPTION = #{description}
            , POSTMAN_COUNT = #{postmanCount}
            , DROPBOX_COUNT = #{dropboxCount}
            , STATUS_CODE = #{statusCode}
            , LAST_STATUS_TS = #{lastStatusTs, jdbcType=TIMESTAMP_WITH_TIMEZONE, javaType=long}
            , SETTINGS_DATA = #{settingsData}
        WHERE 1 = 1
            AND AGENT_ID = #{agentId}
        ]]>
    </update>

    <select id="selectDfxDropboxList" resultType="com.bsmlab.dfx.dfxconsole.app.agent.service.DfxDropboxDto">
        <![CDATA[
        SELECT AGENT_ID, DROPBOX_ID, TASK_TYPE, DATA_SOURCE_ID, SQL_ID, DESCRIPTION
        FROM TB_DFX_DROPBOX
        ORDER BY HOST_NAME, AGENT_ID, DROPBOX_ID
        ]]>
    </select>

    <select id="selectDfxDropboxByAgentIdAndDropBoxId" resultType="com.bsmlab.dfx.dfxconsole.app.agent.service.DfxDropboxDto">
        <![CDATA[
        SELECT AGENT_ID, DROPBOX_ID, TASK_TYPE, DATA_SOURCE_ID, SQL_ID, DESCRIPTION
        FROM TB_DFX_DROPBOX
        WHERE 1 = 1
            AND AGENT_ID = #{agentId}
            AND DROPBOX_ID = #{dropboxId}
        ]]>
    </select>

    <insert id="insertDfxDropbox" parameterType="com.bsmlab.dfx.dfxconsole.app.agent.service.DfxDropboxDto">
        <![CDATA[
        INSERT INTO TB_DFX_DROPBOX (
            AGENT_ID, DROPBOX_ID, TASK_TYPE, DATA_SOURCE_ID, SQL_ID, DESCRIPTION
        )
        VALUES (
            #{agentId}, #{dropboxId}, #{taskType}, #{dataSourceId}, #{sqlId}, #{description}
        )
        ]]>
    </insert>

    <delete id="deleteDfxDropboxByAgentIdAndDropboxId" parameterType="com.bsmlab.dfx.dfxconsole.app.agent.service.DfxDropboxDto">
        <![CDATA[
        DELETE FROM TB_DFX_DROPBOX
        WHERE 1 = 1
            AND AGENT_ID = #{agentId} AND DROPBOX_ID = #{dropboxId}
        ]]>
    </delete>

    <delete id="deleteDfxDropboxByAgentId" parameterType="com.bsmlab.dfx.dfxconsole.app.agent.service.DfxAgentInfoDto">
        <![CDATA[
        DELETE FROM TB_DFX_DROPBOX
        WHERE 1 = 1
            AND AGENT_ID = #{agentId}
        ]]>
    </delete>

    <insert id="insertDfxPostman" parameterType="com.bsmlab.dfx.dfxconsole.app.agent.service.DfxPostmanDto">
        <![CDATA[
        INSERT INTO TB_DFX_POSTMAN (
            AGENT_ID, POSTMAN_ID, POSTMAN_TASK_TYPE_CODE, POSTMAN_ACTION_TYPE_CODE, POSTMAN_ACTION_CRON, DATA_SOURCE_ID, SQL_ID, POST_PROCESSING_SQL_ID, RECIPIENT_AGENT_ID, RECIPIENT_DROPBOX_ID, DESCRIPTION
        )
        VALUES (
            #{agentId}, #{postmanId}, #{postmanTaskTypeCode}, #{postmanActionTypeCode}, #{postmanActionCron}, #{dataSourceId}, #{sqlId}, #{postProcessingSqlId}, #{recipientAgentId}, #{recipientDropboxId}, #{description}
        )
        ]]>
    </insert>

    <update id="updateDfxPostman" parameterType="com.bsmlab.dfx.dfxconsole.app.agent.service.DfxPostmanDto">
        <![CDATA[
        UPDATE TB_DFX_POSTMAN
        SET POSTMAN_TASK_TYPE_CODE = #{postmanTaksTypeCode}
            , POSTMAN_ACTION_TYPE_CODE = #{postmanActionTypeCode}
            , POSTMAN_ACTION_CRON = #{postmanActionCron}
            , DATA_SOURCE_ID = #{dataSourceId}
            , SQL_ID = #{sqlId}
            , POST_PROCESSING_SQL_ID = #{postProcessingSqlId}
            , RECIPIENT_AGENT_ID = #{recipientAgentId}
            , RECIPIENT_DROPBOX_ID = #{recipientDropboxId}
            , DESCRIPTION = #{description}
        WHERE 1 = 1
            AND AGENT_ID = #{agentId}
            AND POSTMAN_ID = #{postmanId}
        ]]>
    </update>

    <delete id="deleteDfxPostmanByAgentId" parameterType="com.bsmlab.dfx.dfxconsole.app.agent.service.DfxAgentInfoDto">
        <![CDATA[
        DELETE FROM TB_DFX_POSTMAN
        WHERE 1 = 1
            AND AGENT_ID = #{agentId}
        ]]>
    </delete>

</mapper>
