<?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="org.xyzh.system.mapper.log.TbSysLogMapper">

    <!-- 结果映射 -->
    <resultMap id="BaseResultMap" type="org.xyzh.api.system.dto.TbSysLogDTO">
        <id column="log_id" property="logId" jdbcType="VARCHAR"/>
        <result column="optsn" property="optsn" jdbcType="VARCHAR"/>
        <result column="type" property="type" jdbcType="INTEGER"/>
        <result column="level" property="level" jdbcType="VARCHAR"/>
        <result column="module" property="module" jdbcType="VARCHAR"/>
        <result column="ip_address" property="ipAddress" jdbcType="VARCHAR"/>
        <result column="ip_source" property="ip_source" jdbcType="VARCHAR"/>
        <result column="browser" property="browser" jdbcType="VARCHAR"/>
        <result column="os" property="os" jdbcType="VARCHAR"/>
        <result column="message" property="message" jdbcType="VARCHAR"/>
        <result column="data" property="data" typeHandler="org.xyzh.common.jdbc.handler.FastJson2TypeHandler"/>
        <result column="creator" property="creator" jdbcType="VARCHAR"/>
        <result column="creator_name" property="creatorName" jdbcType="VARCHAR"/>
        <result column="service" property="servce" jdbcType="VARCHAR"/>
        <result column="dept_path" property="deptPath" jdbcType="VARCHAR"/>
        <result column="updater" property="updater" jdbcType="VARCHAR"/>
        <result column="create_time" property="createTime" jdbcType="TIMESTAMP"/>
        <result column="update_time" property="updateTime" jdbcType="TIMESTAMP"/>
        <result column="delete_time" property="deleteTime" jdbcType="TIMESTAMP"/>
        <result column="deleted" property="deleted" jdbcType="BOOLEAN"/>
    </resultMap>

    <!-- 基础列 -->
    <sql id="Base_Column_List">
        log_id, optsn, type, level, module, ip_address, ip_source, browser, os, message,
        data, creator, creator_name, service, dept_path, updater,
        create_time, update_time, delete_time, deleted
    </sql>

    <!-- 插入系统日志 -->
    <insert id="insertLog" parameterType="org.xyzh.api.system.dto.TbSysLogDTO">
        INSERT INTO sys.tb_sys_log
        <trim prefix="(" suffix=")" suffixOverrides=",">
            log_id,
            optsn,
            type,
            level,
            module,
            message,
            service,
            <if test="ipAddress != null and ipAddress != ''">ip_address,</if>
            <if test="ip_source != null and ip_source != ''">ip_source,</if>
            <if test="browser != null and browser != ''">browser,</if>
            <if test="os != null and os != ''">os,</if>
            <if test="data != null">data,</if>
            <if test="creator != null and creator != ''">creator,</if>
            <if test="creatorName != null and creatorName != ''">creator_name,</if>
            <if test="deptPath != null and deptPath != ''">dept_path,</if>
            <if test="updater != null and updater != ''">updater,</if>
            <if test="createTime != null">create_time,</if>
            <if test="updateTime != null">update_time,</if>
            <if test="deleteTime != null">delete_time,</if>
            <if test="deleted != null">deleted,</if>
        </trim>
        VALUES
        <trim prefix="(" suffix=")" suffixOverrides=",">
            #{logId},
            #{optsn},
            #{type},
            #{level},
            #{module},
            #{message},
            #{servce},
            <if test="ipAddress != null and ipAddress != ''">#{ipAddress},</if>
            <if test="ip_source != null and ip_source != ''">#{ip_source},</if>
            <if test="browser != null and browser != ''">#{browser},</if>
            <if test="os != null and os != ''">#{os},</if>
            <if test="data != null">#{data, typeHandler=org.xyzh.common.jdbc.handler.FastJson2TypeHandler},</if>
            <if test="creator != null and creator != ''">#{creator},</if>
            <if test="creatorName != null and creatorName != ''">#{creatorName},</if>
            <if test="deptPath != null and deptPath != ''">#{deptPath},</if>
            <if test="updater != null and updater != ''">#{updater},</if>
            <if test="createTime != null">#{createTime},</if>
            <if test="updateTime != null">#{updateTime},</if>
            <if test="deleteTime != null">#{deleteTime},</if>
            <if test="deleted != null">#{deleted},</if>
        </trim>
    </insert>

    <!-- 更新系统日志 -->
    <update id="updateLog" parameterType="org.xyzh.api.system.dto.TbSysLogDTO">
        UPDATE sys.tb_sys_log
        <set>
            <if test="type != null">type = #{type},</if>
            <if test="level != null and level != ''">level = #{level},</if>
            <if test="module != null and module != ''">module = #{module},</if>
            <if test="ipAddress != null and ipAddress != ''">ip_address = #{ipAddress},</if>
            <if test="ip_source != null and ip_source != ''">ip_source = #{ip_source},</if>
            <if test="browser != null and browser != ''">browser = #{browser},</if>
            <if test="os != null and os != ''">os = #{os},</if>
            <if test="message != null and message != ''">message = #{message},</if>
            <if test="data != null">data = #{data, typeHandler=org.xyzh.common.jdbc.handler.FastJson2TypeHandler},</if>
            <if test="servce != null and servce != ''">service = #{servce},</if>
            <if test="updater != null and updater != ''">updater = #{updater},</if>
            <if test="deptPath != null and deptPath != ''">dept_path = #{deptPath},</if>
            <if test="updateTime != null">update_time = #{updateTime},</if>
            <if test="deleteTime != null">delete_time = #{deleteTime},</if>
            <if test="deleted != null">deleted = #{deleted},</if>
        </set>
        WHERE log_id = #{logId}
    </update>

    <!-- 删除系统日志 -->
    <update id="deleteLog" parameterType="org.xyzh.api.system.dto.TbSysLogDTO">
        UPDATE sys.tb_sys_log
        SET deleted = true,
            delete_time = NOW()
        WHERE log_id = #{logId}
    </update>

    <!-- 根据日志ID查询系统日志 -->
    <select id="getLogById" resultMap="BaseResultMap">
        SELECT <include refid="Base_Column_List" />
        FROM sys.tb_sys_log
        WHERE log_id = #{logId}
          AND (deleted IS NULL OR deleted = false)
    </select>

    <!-- 根据条件查询系统日志列表 -->
    <select id="getLogByFilter" resultMap="BaseResultMap" parameterType="org.xyzh.api.system.dto.TbSysLogDTO">
        SELECT <include refid="Base_Column_List" />
        FROM sys.tb_sys_log
        <where>
            <if test="filter.logId != null and filter.logId != ''">
                AND log_id = #{filter.logId}
            </if>
            <if test="filter.type != null">
                AND type = #{filter.type}
            </if>
            <if test="filter.level != null and filter.level != ''">
                AND level = #{filter.level}
            </if>
            <if test="filter.module != null and filter.module != ''">
                AND module = #{filter.module}
            </if>
            <if test="filter.ipAddress != null and filter.ipAddress != ''">
                AND ip_address = #{filter.ipAddress}
            </if>
            <if test="filter.creator != null and filter.creator != ''">
                AND creator = #{filter.creator}
            </if>
            <if test="filter.servce != null and filter.servce != ''">
                AND service = #{filter.servce}
            </if>
            <if test="filter.deptPath != null and filter.deptPath != ''">
                AND dept_path LIKE CONCAT(#{filter.deptPath}, '%')
            </if>
            <if test="filter.message != null and filter.message != ''">
                AND message LIKE CONCAT('%', #{filter.message}, '%')
            </if>
            <if test="filter.startTime != null">
                AND create_time &gt;= #{filter.startTime}
            </if>
            <if test="filter.endTime != null">
                AND create_time &lt;= #{filter.endTime}
            </if>
            AND (deleted IS NULL OR deleted = false)
        </where>
        ORDER BY create_time DESC
    </select>

    <!-- 根据条件查询系统日志分页列表 -->
    <select id="getLogPageByFilter" resultMap="BaseResultMap">
        SELECT <include refid="Base_Column_List" />
        FROM sys.tb_sys_log
        <where>
            <if test="filter.logId != null and filter.logId != ''">
                AND log_id = #{filter.logId}
            </if>
            <if test="filter.type != null">
                AND type = #{filter.type}
            </if>
            <if test="filter.level != null and filter.level != ''">
                AND level = #{filter.level}
            </if>
            <if test="filter.module != null and filter.module != ''">
                AND module = #{filter.module}
            </if>
            <if test="filter.ipAddress != null and filter.ipAddress != ''">
                AND ip_address = #{filter.ipAddress}
            </if>
            <if test="filter.creator != null and filter.creator != ''">
                AND creator = #{filter.creator}
            </if>
            <if test="filter.servce != null and filter.servce != ''">
                AND service = #{filter.servce}
            </if>
            <if test="filter.deptPath != null and filter.deptPath != ''">
                AND dept_path LIKE CONCAT(#{filter.deptPath}, '%')
            </if>
            <if test="filter.message != null and filter.message != ''">
                AND message LIKE CONCAT('%', #{filter.message}, '%')
            </if>
            <if test="filter.startTime != null">
                AND create_time &gt;= #{filter.startTime}
            </if>
            <if test="filter.endTime != null">
                AND create_time &lt;= #{filter.endTime}
            </if>
            AND (deleted IS NULL OR deleted = false)
        </where>
        ORDER BY create_time DESC
        LIMIT #{pageParam.pageSize} OFFSET #{pageParam.offset}
    </select>

    <!-- 根据条件查询系统日志数量 -->
    <select id="getLogCount" resultType="java.lang.Integer" parameterType="org.xyzh.api.system.dto.TbSysLogDTO">
        SELECT COUNT(1)
        FROM sys.tb_sys_log
        <where>
            <if test="logId != null and logId != ''">
                AND log_id = #{logId}
            </if>
            <if test="type != null">
                AND type = #{type}
            </if>
            <if test="level != null and level != ''">
                AND level = #{level}
            </if>
            <if test="module != null and module != ''">
                AND module = #{module}
            </if>
            <if test="ipAddress != null and ipAddress != ''">
                AND ip_address = #{ipAddress}
            </if>
            <if test="creator != null and creator != ''">
                AND creator = #{creator}
            </if>
            <if test="servce != null and servce != ''">
                AND service = #{servce}
            </if>
            <if test="deptPath != null and deptPath != ''">
                AND dept_path LIKE CONCAT(#{deptPath}, '%')
            </if>
            <if test="message != null and message != ''">
                AND message LIKE CONCAT('%', #{message}, '%')
            </if>
            <if test="startTime != null">
                AND create_time &gt;= #{startTime}
            </if>
            <if test="endTime != null">
                AND create_time &lt;= #{endTime}
            </if>
            AND (deleted IS NULL OR deleted = false)
        </where>
    </select>

</mapper>
