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

    <select id="selectDfxCryptoKeyList" resultType="com.bsmlab.dfx.dfxconsole.app.agent.service.DfxCryptoKeyDto">
        <![CDATA[
        SELECT A.KEY_VALUE, A.HASH_VALUE, A.ALGORITHM_NAME, A.MODE_NAME, A.TARGET_AGENT_HOST_ID
            , (EXTRACT(EPOCH FROM A.APPLY_TS) * 1000)::BIGINT AS APPLY_TS
            , A.DATA_ENCRYPTION_YN
        FROM (
            SELECT KEY_VALUE, HASH_VALUE, ALGORITHM_NAME, MODE_NAME, TARGET_AGENT_HOST_ID, APPLY_TS, DATA_ENCRYPTION_YN
            FROM TB_DFX_CRYPTO_KEY
            WHERE 1 = 1
            ORDER BY TARGET_AGENT_HOST_ID, KEY_VALUE
            LIMIT #{itemCountPerPage}
            OFFSET (#{page} - 1) * #{itemCountPerPage}
            ) A
        WHERE 1 = 1
        ]]>
    </select>

    <select id="selectDfxCryptoKeyListTotalCount" resultType="int">
        <![CDATA[
        SELECT COUNT(*)
        FROM (
            SELECT KEY_VALUE, HASH_VALUE, ALGORITHM_NAME, MODE_NAME, TARGET_AGENT_HOST_ID, APPLY_TS, DATA_ENCRYPTION_YN
            FROM TB_DFX_CRYPTO_KEY
            WHERE 1 = 1
            ) A
        WHERE 1 = 1
        ]]>
    </select>

    <select id="selectDfxCryptoKeyByKeyValue" parameterType="com.bsmlab.dfx.dfxconsole.app.agent.service.DfxCryptoKeyDto" resultType="com.bsmlab.dfx.dfxconsole.app.agent.service.DfxCryptoKeyDto">
        <![CDATA[
        SELECT KEY_VALUE, HASH_VALUE, ALGORITHM_NAME, MODE_NAME, TARGET_AGENT_HOST_ID, (EXTRACT(EPOCH FROM APPLY_TS) * 1000)::BIGINT AS APPLY_TS, DATA_ENCRYPTION_YN
        FROM TB_DFX_CRYPTO_KEY
        WHERE 1 = 1
            AND KEY_VALUE = #{keyValue}
        ]]>
    </select>

    <insert id="insertDfxCryptoKey" parameterType="com.bsmlab.dfx.dfxconsole.app.agent.service.DfxCryptoKeyDto">
        <![CDATA[
        INSERT INTO TB_DFX_AGENT_MESSAGE (
            KEY_VALUE, HASH_VALUE, ALGORITHM_NAME, MODE_NAME, TARGET_AGENT_HOST_ID
            , APPLY_TS
            , DATA_ENCRYPTION_YN
        )
        VALUES (
            #{keyValue}, #{hashValue}, #{algorithmName}, #{modeName}, #{targetAgentHostId}
            , #{applyTs, jdbcType=TIMESTAMP_WITH_TIMEZONE, javaType=Long}
            , #{dataEncryptionYn}
        ]]>
    </insert>

    <update id="updateDfxCryptoKeyToApply" parameterType="com.bsmlab.dfx.dfxconsole.app.agent.service.DfxCryptoKeyDto">
        <![CDATA[
        UPDATE TB_DFX_AGENT_MESSAGE
        SET TARGET_AGENT_HOST_ID = #{targetAgentHostId}
            , APPLY_TS = NOW()
            , DATA_ENCRYPTION_YN = #{dataEncryptionYn}
        WHERE 1 = 1
            KEY_VALUE = #{keyValue}
        ]]>
    </update>

</mapper>
