<?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.aip.gateway.api.dao.mapper.BaseDbMapper">

    <select id="selectLinkedApiKeys" resultType="com.aip.gateway.api.entity.TbLinkedApiKey" fetchSize="100">
    <![CDATA[
        SELECT A.Id as apiKeyId,
               A.ApiKey as apiKey,
               A.policyLookupYn as policyLookupYn,
               A.fileInfoLookupYn as fileInfoLookupYn,
               A.applyLabelYn as applyLabelYn,
               A.releaseLabelYn as releaseLabelYn,
               A.encryptionFileYn as encryptionFileYn,
               A.decryptionFileYn as decryptionFileYn,
               A.ExpiredAt as expiredAt,
               B.ServerId as serverId,
               B.ServerIpAddr as serverIpAddr,
               B.ServerDesc as serverDesc,
               C.SystemId as systemId,
               C.SystemName as systemName
          FROM TB_LINKED_API_KEY A
         INNER JOIN TB_LINKED_SERVER B
            ON A.ServerId = B.ServerId
           AND A.UseYn = 1
           AND B.UseYn = 1
         INNER JOIN TB_LINKED_SYSTEM C
            ON B.SystemId  = C.SystemId
           AND C.UseYn = 1
        ]]>
    </select>

    <select id="selectLinkedDecryptKeys" resultType="com.aip.gateway.api.entity.TbLinkedDecryptKey" fetchSize="100">
    <![CDATA[
        SELECT A.Id as decryptKeyId,
               A.DecryptKey as decryptKey,
               A.ExpiredAt as expiredAt,
               B.ServerId as serverId,
               B.ServerIpAddr as serverIpAddr,
               B.ServerDesc as serverDesc,
               C.SystemId as systemId,
               C.SystemName as systemName
        FROM TB_LINKED_DECRYPT_KEY A
                 INNER JOIN TB_LINKED_SERVER B
                            ON A.ServerId = B.ServerId
                                AND A.UseYn = 1
                                AND B.UseYn = 1
                 INNER JOIN TB_LINKED_SYSTEM C
                            ON B.SystemId  = C.SystemId
                                AND C.UseYn = 1
        ]]>
    </select>

    <select id="selectLinkedSystems" resultType="com.aip.gateway.api.entity.TbLinkedSystem" fetchSize="100">
    <![CDATA[
        SELECT A.SystemId as systemId,
               A.SystemName as systemName,
               A.SystemDesc as systemDesc,
               A.CreatedAt as createdAt,
               A.DeletedAt as deletedAt,
               A.UseYn as UseYn
        FROM TB_LINKED_SYSTEM A
        ]]>
    </select>

    <select id="selectLinkedServers" resultType="com.aip.gateway.api.entity.TbLinkedServer" fetchSize="100">
    <![CDATA[
        SELECT A.ServerId as serverId,
               A.SystemId as systemId,
               A.ServerIpAddr as serverIpAddr,
               A.ServerDesc as serverDesc,
               A.CreatedAt as createdAt,
               A.DeletedAt as deletedAt,
               A.UseYn as useYn
        FROM TB_LINKED_SERVER A
        ]]>
    </select>

    <select id="selectAipConfig" parameterType="java.util.HashMap"  resultType="com.aip.gateway.api.entity.TbAipConfig" fetchSize="100">
    <![CDATA[
        SELECT A.Id as id,
               A.AipServerId as aipServerId,
               A.ConfigKey as configKey,
               A.ConfigValue as configValue,
               A.CreatedAt as createdAt
        FROM TB_AIP_CONFIG A
       WHERE A.AipServerId = convert(int, #{AIP_SERVER_ID})
        ]]>
    </select>

    <insert id="insertAipCallLog" parameterType="com.aip.gateway.api.entity.TbAipCallLog">
    <![CDATA[
        INSERT INTO TB_AIP_API_CALL_LOG (
            ApiEndPoint,
            ApiGuid,
            IPAddress,
            RequestAt,
            ResponseAt,
            ResponseStatusCode,
            ResponseTime,
            ErrorCode,
            ErrorMessage
        )
        VALUES (
                   #{obj.apiEndPoint},
                   #{obj.apiGuid},
                   #{obj.ipAddress},
                   #{obj.requestAt},
                   #{obj.responseAt},
                   #{obj.responseStatusCode},
                   #{obj.responseTime},
                   #{obj.errorCode},
                   #{obj.errorMessage}
               )
        ]]>
    </insert>

    <insert id="insertAipFileJobLog" parameterType="com.aip.gateway.api.entity.TbAipFileJobLog">
    <![CDATA[
        INSERT INTO TB_AIP_FILE_JOB_LOG (
            ApiId,
            FileId,
            FileName,
            FileExt,
            FileOwner,
            FileLabelGuid,
            FileProtectionGuid,
            FileSize,
            NewFileName,
            NewFileExt,
            NewFileOwner,
            NewFileLabelGuid,
            NewFileProtectionGuid,
            NewFileSize,
            ApiGuid,
            JobOwner,
            ServerIpAddr,
            ApiKey,
            DecryptKey,
            JobResult,
            JobTime,
            JobMessage
        )
        VALUES (
                   #{obj.apiId},
                   #{obj.fileId},
                   #{obj.fileName},
                   #{obj.fileExt},
                   #{obj.fileOwner},
                   #{obj.fileLabelGuid},
                   #{obj.fileProtectionGuid},
                   #{obj.fileSize},
                   #{obj.newFileName},
                   #{obj.newFileExt},
                   #{obj.newFileOwner},
                   #{obj.newFileLabelGuid},
                   #{obj.newFileProtectionGuid},
                   #{obj.newFileSize},
                   #{obj.apiGuid},
                   #{obj.jobOwner},
                   #{obj.serverIpAddr},
                   #{obj.apiKey},
                   #{obj.decryptKey},
                   #{obj.jobResult},
                   #{obj.jobTime},
                   #{obj.jobMessage}
               )
        ]]>
    </insert>

</mapper>
