<?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.common.system.SystemLogMapper">

    <insert id="insertLoginLog">
        INSERT INTO login_log (user_id, login_id, login_type, fail_reason, ip_address, user_agent, login_at)
        VALUES (#{userId}, #{loginId}, #{loginType}, #{failReason}, #{ipAddress}, #{userAgent}, NOW())
    </insert>

    <insert id="insertApiAccessLog">
        INSERT INTO api_access_log (user_id, method, url, request_param, response_code,
                                    response_time, ip_address, error_message, created_at)
        VALUES (#{userId}, #{method}, #{url}, #{requestParam}, #{responseCode},
                #{responseTime}, #{ipAddress}, #{errorMessage}, NOW())
    </insert>

    <insert id="insertDataChangeLog">
        INSERT INTO data_change_log (user_id, menu_code, table_name, record_id, action_type,
                                     before_data, after_data, changed_keys, created_at)
        VALUES (#{userId}, #{menuCode}, #{tableName}, #{recordId}, #{actionType},
                CAST(#{beforeJson} AS JSONB), CAST(#{afterJson} AS JSONB), #{changedKeys}, NOW())
    </insert>

    <select id="selectLoginLogs" resultType="map">
        SELECT log_id     AS "logId",
               user_id    AS "userId",
               login_id   AS "loginId",
               login_type AS "loginType",
               fail_reason AS "failReason",
               ip_address AS "ipAddress",
               login_at   AS "loginAt"
        FROM login_log
        <where>
            <if test="param.userId != null">AND user_id = #{param.userId}</if>
            <if test="param.startDate != null and param.startDate != ''">
                AND login_at &gt;= #{param.startDate}::timestamp
            </if>
            <if test="param.endDate != null and param.endDate != ''">
                AND login_at &lt;= #{param.endDate}::timestamp + interval '1 day'
            </if>
        </where>
        ORDER BY login_at DESC
    </select>

    <select id="selectApiAccessLogs" resultType="map">
        SELECT log_id        AS "logId",
               user_id       AS "userId",
               method        AS "method",
               url           AS "url",
               response_code AS "responseCode",
               response_time AS "responseTime",
               ip_address    AS "ipAddress",
               created_at    AS "createdAt"
        FROM api_access_log
        <where>
            <if test="param.userId != null">AND user_id = #{param.userId}</if>
            <if test="param.url != null and param.url != ''">AND url LIKE CONCAT('%', #{param.url}, '%')</if>
            <if test="param.startDate != null and param.startDate != ''">
                AND created_at &gt;= #{param.startDate}::timestamp
            </if>
            <if test="param.endDate != null and param.endDate != ''">
                AND created_at &lt;= #{param.endDate}::timestamp + interval '1 day'
            </if>
        </where>
        ORDER BY created_at DESC
    </select>

    <select id="selectDataChangeLogs" resultType="map">
        SELECT log_id       AS "logId",
               user_id      AS "userId",
               menu_code    AS "menuCode",
               table_name   AS "tableName",
               record_id    AS "recordId",
               action_type  AS "actionType",
               changed_keys AS "changedKeys",
               created_at   AS "createdAt"
        FROM data_change_log
        <where>
            <if test="param.userId != null">AND user_id = #{param.userId}</if>
            <if test="param.tableName != null and param.tableName != ''">AND table_name = #{param.tableName}</if>
            <if test="param.recordId != null and param.recordId != ''">AND record_id = #{param.recordId}</if>
            <if test="param.startDate != null and param.startDate != ''">
                AND created_at &gt;= #{param.startDate}::timestamp
            </if>
            <if test="param.endDate != null and param.endDate != ''">
                AND created_at &lt;= #{param.endDate}::timestamp + interval '1 day'
            </if>
        </where>
        ORDER BY created_at DESC
    </select>

</mapper>
