<?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.ga.core.mapper.external.ExternalCallLogMapper">

    <resultMap id="RespMap" type="com.ga.core.vo.external.ExternalCallLogResp">
        <id     property="logId"           column="log_id"/>
        <result property="callType"        column="call_type"/>
        <result property="adapterClass"    column="adapter_class"/>
        <result property="methodName"      column="method_name"/>
        <result property="targetRefType"   column="target_ref_type"/>
        <result property="targetRefId"     column="target_ref_id"/>
        <result property="requestSummary"  column="request_summary"/>
        <result property="responseSummary" column="response_summary"/>
        <result property="resultCode"      column="result_code"/>
        <result property="resultMessage"   column="result_message"/>
        <result property="success"         column="success"/>
        <result property="durationMs"      column="duration_ms"/>
        <result property="errorClass"      column="error_class"/>
        <result property="errorMessage"    column="error_message"/>
        <result property="calledAt"        column="called_at"/>
        <result property="createdBy"       column="created_by"/>
    </resultMap>

    <sql id="cols">
        log_id, call_type, adapter_class, method_name,
        target_ref_type, target_ref_id,
        request_summary, response_summary,
        result_code, result_message, success, duration_ms,
        error_class, error_message, called_at, created_by
    </sql>

    <insert id="insertOne" parameterType="com.ga.core.vo.external.ExternalCallLogVO"
            useGeneratedKeys="true" keyProperty="logId" keyColumn="log_id">
        INSERT INTO external_call_log (
            call_type, adapter_class, method_name,
            target_ref_type, target_ref_id,
            request_summary, response_summary,
            result_code, result_message, success, duration_ms,
            error_class, error_message, called_at, created_by
        ) VALUES (
            #{callType},
            #{adapterClass},
            #{methodName},
            #{targetRefType,        jdbcType=VARCHAR},
            #{targetRefId,          jdbcType=BIGINT},
            #{requestSummary,       jdbcType=LONGVARCHAR},
            #{responseSummary,      jdbcType=LONGVARCHAR},
            #{resultCode,           jdbcType=VARCHAR},
            #{resultMessage,        jdbcType=LONGVARCHAR},
            #{success,              jdbcType=BOOLEAN},
            #{durationMs,           jdbcType=INTEGER},
            #{errorClass,           jdbcType=VARCHAR},
            #{errorMessage,         jdbcType=LONGVARCHAR},
            COALESCE(#{calledAt,jdbcType=TIMESTAMP}, CURRENT_TIMESTAMP),
            #{createdBy,            jdbcType=BIGINT}
        )
    </insert>

    <select id="selectById" resultMap="RespMap">
        SELECT <include refid="cols"/>
        FROM external_call_log
        WHERE log_id = #{logId}
    </select>

    <select id="selectList" resultMap="RespMap"
            parameterType="com.ga.core.vo.external.ExternalCallLogSearchParam">
        SELECT <include refid="cols"/>
        FROM external_call_log
        <where>
            <if test="callType != null and callType != ''">
                AND call_type = #{callType}
            </if>
            <if test="methodName != null and methodName != ''">
                AND method_name = #{methodName}
            </if>
            <if test="success != null">
                AND success = #{success}
            </if>
            <if test="targetRefType != null and targetRefType != ''">
                AND target_ref_type = #{targetRefType}
            </if>
            <if test="targetRefId != null">
                AND target_ref_id = #{targetRefId}
            </if>
            <if test="startDate != null and startDate != ''">
                AND called_at &gt;= (#{startDate}::date)
            </if>
            <if test="endDate != null and endDate != ''">
                AND called_at &lt; (#{endDate}::date + INTERVAL '1 day')
            </if>
        </where>
        ORDER BY called_at DESC, log_id DESC
    </select>

</mapper>
