<?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.cm.common.plugin.dao.systemlogger.ISystemLoggerDao">

    <resultMap id="systemLoggerDTO" type="com.cm.common.plugin.pojo.dtos.systemlogger.SystemLoggerDTO">
        <id property="systemLoggerId" column="systemlogger_id"/>
        <result property="host" column="host"/>
        <result property="port" column="port"/>
        <result property="requestIp" column="request_ip"/>
        <result property="contextPath" column="context_path"/>
        <result property="uri" column="uri"/>
        <result property="classNameFull" column="class_name_full"/>
        <result property="className" column="class_name"/>
        <result property="methodName" column="method_name"/>
        <result property="parameterValues" column="parameter_values"/>
        <result property="startTime" column="start_time"/>
        <result property="endTime" column="end_time"/>
        <result property="usedTime" column="used_time"/>
        <result property="responseResult" column="response_result"/>
        <result property="creatorName" column="creator_name"/>
    </resultMap>

    <!-- 新增系统日志 -->
    <insert id="saveSystemLogger" parameterType="map">
        INSERT INTO log_systemlogger(
            systemlogger_id,
            host,
            port,
            request_ip,
            context_path,
            uri,
            class_name_full,
            class_name,
            method_name,
            parameter_values,
            start_time,
            end_time,
            used_time,
            response_result,
            creator_name,
            creator,
            gmt_create,
            modifier,
            gmt_modified,
            is_delete
        ) VALUES(
            #{systemLoggerId},
            #{host},
            #{port},
            #{requestIp},
            #{contextPath},
            #{uri},
            #{classNameFull},
            #{className},
            #{methodName},
            #{parameterValues},
            #{startTime},
            #{endTime},
            #{usedTime},
            #{responseResult},
            #{creatorName},
            #{creator},
            #{gmtCreate},
            #{modifier},
            #{gmtModified},
            #{isDelete}
        )
    </insert>

    <!-- 删除系统日志 -->
    <update id="removeSystemLogger" parameterType="map">
        UPDATE
            log_systemlogger
        SET
            is_delete = 1,
            modifier = #{modifier},
            gmt_modified = #{gmtModified}
        WHERE
            systemLogger_id IN
        <foreach collection="systemLoggerIds" index="index" open="(" separator="," close=")">
            #{systemLoggerIds[${index}]}
        </foreach>
    </update>

    <!-- 修改系统日志 -->
    <update id="updateSystemLogger" parameterType="map">
        UPDATE
            log_systemlogger
        SET
        <if test="host != null and host != ''">
            host = #{host},
        </if>
        <if test="port != null">
            port = #{port},
        </if>
        <if test="requestIp != null and requestIp != ''">
            request_ip = #{requestIp},
        </if>
        <if test="contextPath != null and contextPath != ''">
            context_path = #{contextPath},
        </if>
        <if test="uri != null and uri != ''">
            uri = #{uri},
        </if>
        <if test="classNameFull != null and classNameFull != ''">
            class_name_full = #{classNameFull},
        </if>
        <if test="className != null and className != ''">
            class_name = #{className},
        </if>
        <if test="methodName != null and methodName != ''">
            method_name = #{methodName},
        </if>
        <if test="parameterValues != null and parameterValues != ''">
            parameter_values = #{parameterValues},
        </if>
        <if test="startTime != null">
            start_time = #{startTime},
        </if>
        <if test="endTime != null">
            end_time = #{endTime},
        </if>
        <if test="usedTime != null">
            used_time = #{usedTime},
        </if>
        <if test="responseResult != null and responseResult != ''">
            response_result = #{responseResult},
        </if>
        <if test="creatorName != null and creator_name != ''">
            creator_name = #{creatorName},
        </if>
            modifier = #{modifier},
            gmt_modified = #{gmtModified}
        WHERE
            systemLogger_id = #{systemLoggerId}
    </update>

    <!-- 系统日志列表 -->
    <select id="listSystemLogger" parameterType="map" resultMap="systemLoggerDTO">
        SELECT
            *
        FROM
            log_systemlogger
        WHERE
            is_delete = 0
        <if test="keywords != null and keywords != ''">
        </if>
        <if test="startTime != null and startTime != ''">
        AND
            gmt_create <![CDATA[ >= ]]> #{startTime}
        </if>
        <if test="endTime != null and endTime != ''">
        AND
            gmt_create <![CDATA[ <= ]]> #{endTime}
        </if>
        <if test="systemLoggerIds != null and systemLoggerIds.size > 0">
        AND
            systemLogger_id IN
            <foreach collection="systemLoggerIds" index="index" open="(" separator="," close=")">
                #{systemLoggerIds[${index}]}
            </foreach>
        </if>
    </select>

    <!-- 系统日志详情 -->
    <select id="getSystemLogger" parameterType="map" resultMap="systemLoggerDTO">
        SELECT
            t1.*
        FROM
            log_systemlogger t1
        WHERE
            t1.is_delete = 0
        <if test="systemLoggerId != null and systemLoggerId != ''">
        AND
            t1.systemLogger_id = #{systemLoggerId}
        </if>
    </select>

    <!-- 统计日志数量 -->
    <select id="countSystemLogger" parameterType="map" resultType="Integer">
        SELECT
            COUNT(*)
        FROM
            log_systemlogger
        WHERE
            is_delete = 0
        <if test="currentYear != null and currentYear != ''">
        AND
            LEFT(gmt_create, 4) = #{currentYear}
        </if>
    </select>

</mapper>