<?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.dromara.member.mapper.AppOperLogMapper">

    <resultMap type="org.dromara.member.domain.AppOperLog" id="AppOperLogResult">
        <id property="operId" column="oper_id"/>
        <result property="title" column="title"/>
        <result property="businessType" column="business_type"/>
        <result property="method" column="method"/>
        <result property="requestMethod" column="request_method"/>
        <result property="operName" column="oper_name"/>
        <result property="operUrl" column="oper_url"/>
        <result property="operIp" column="oper_ip"/>
        <result property="operLocation" column="oper_location"/>
        <result property="operParam" column="oper_param"/>
        <result property="jsonResult" column="json_result"/>
        <result property="status" column="status"/>
        <result property="errorMsg" column="error_msg"/>
        <result property="operTime" column="oper_time"/>
        <result property="costTime" column="cost_time"/>
    </resultMap>

    <resultMap type="org.dromara.member.domain.vo.AppOperLogVo" id="AppOperLogVoResult">
        <id property="operId" column="oper_id"/>
        <result property="title" column="title"/>
        <result property="businessType" column="business_type"/>
        <result property="method" column="method"/>
        <result property="requestMethod" column="request_method"/>
        <result property="operName" column="oper_name"/>
        <result property="operUrl" column="oper_url"/>
        <result property="operIp" column="oper_ip"/>
        <result property="operLocation" column="oper_location"/>
        <result property="operParam" column="oper_param"/>
        <result property="jsonResult" column="json_result"/>
        <result property="status" column="status"/>
        <result property="errorMsg" column="error_msg"/>
        <result property="operTime" column="oper_time"/>
        <result property="costTime" column="cost_time"/>
    </resultMap>

    <sql id="selectAppOperLogVo">
        select oper_id,
               title,
               business_type,
               method,
               request_method,
               oper_name,
               oper_url,
               oper_ip,
               oper_location,
               oper_param,
               json_result,
               status,
               error_msg,
               oper_time,
               cost_time
        from app_oper_log
    </sql>

    <!-- 查询操作日志列表 -->
    <select id="selectAppOperLogList" parameterType="AppOperLog" resultMap="AppOperLogVoResult">
        <include refid="selectAppOperLogVo"/>
        <where>
            <if test="title != null and title != ''">and title like concat('%', #{title}, '%')</if>
            <if test="businessType != null">and business_type = #{businessType}</if>
            <if test="method != null and method != ''">and method like concat('%', #{method}, '%')</if>
            <if test="requestMethod != null and requestMethod != ''">and request_method = #{requestMethod}</if>
            <if test="operName != null and operName != ''">and oper_name like concat('%', #{operName}, '%')</if>
            <if test="operIp != null and operIp != ''">and oper_ip like concat('%', #{operIp}, '%')</if>
            <if test="status != null">and status = #{status}</if>
            <if test="params.beginTime != null and params.beginTime != ''">and oper_time &gt;= #{params.beginTime}</if>
            <if test="params.endTime != null and params.endTime != ''">and oper_time &lt;= #{params.endTime}</if>
        </where>
        order by oper_time desc
    </select>

    <!-- 根据ID查询操作日志 -->
    <select id="selectAppOperLogByOperId" parameterType="Long" resultMap="AppOperLogVoResult">
        <include refid="selectAppOperLogVo"/>
        where oper_id = #{operId}
    </select>

    <!-- 根据操作人查询操作日志 -->
    <select id="selectAppOperLogByOperName" parameterType="String" resultMap="AppOperLogVoResult">
        <include refid="selectAppOperLogVo"/>
        where oper_name = #{operName}
        order by oper_time desc
    </select>

    <!-- 新增操作日志 -->
    <insert id="insertAppOperLog" parameterType="org.dromara.member.domain.AppOperLog" useGeneratedKeys="true"
            keyProperty="operId">
        insert into app_oper_log
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="title != null and title != ''">title,</if>
            <if test="businessType != null">business_type,</if>
            <if test="method != null and method != ''">method,</if>
            <if test="requestMethod != null and requestMethod != ''">request_method,</if>
            <if test="operName != null and operName != ''">oper_name,</if>
            <if test="operUrl != null and operUrl != ''">oper_url,</if>
            <if test="operIp != null and operIp != ''">oper_ip,</if>
            <if test="operLocation != null and operLocation != ''">oper_location,</if>
            <if test="operParam != null and operParam != ''">oper_param,</if>
            <if test="jsonResult != null and jsonResult != ''">json_result,</if>
            <if test="status != null">status,</if>
            <if test="errorMsg != null and errorMsg != ''">error_msg,</if>
            <if test="operTime != null">oper_time,</if>
            <if test="costTime != null">cost_time,</if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="title != null and title != ''">#{title},</if>
            <if test="businessType != null">#{businessType},</if>
            <if test="method != null and method != ''">#{method},</if>
            <if test="requestMethod != null and requestMethod != ''">#{requestMethod},</if>
            <if test="operName != null and operName != ''">#{operName},</if>
            <if test="operUrl != null and operUrl != ''">#{operUrl},</if>
            <if test="operIp != null and operIp != ''">#{operIp},</if>
            <if test="operLocation != null and operLocation != ''">#{operLocation},</if>
            <if test="operParam != null and operParam != ''">#{operParam},</if>
            <if test="jsonResult != null and jsonResult != ''">#{jsonResult},</if>
            <if test="status != null">#{status},</if>
            <if test="errorMsg != null and errorMsg != ''">#{errorMsg},</if>
            <if test="operTime != null">#{operTime},</if>
            <if test="costTime != null">#{costTime},</if>
        </trim>
    </insert>

    <!-- 删除操作日志 -->
    <delete id="deleteAppOperLogByOperId" parameterType="Long">
        delete
        from app_oper_log
        where oper_id = #{operId}
    </delete>

    <!-- 批量删除操作日志 -->
    <delete id="deleteAppOperLogByOperIds" parameterType="String">
        delete from app_oper_log where oper_id in
        <foreach item="operId" collection="array" open="(" separator="," close=")">
            #{operId}
        </foreach>
    </delete>

    <!-- 清空操作日志 -->
    <delete id="cleanAppOperLog">
        truncate table app_oper_log
    </delete>

    <!-- 根据时间范围删除操作日志 -->
    <delete id="deleteAppOperLogByTime">
        delete from app_oper_log
        <where>
            <if test="beginTime != null and beginTime != ''">and oper_time &gt;= #{beginTime}</if>
            <if test="endTime != null and endTime != ''">and oper_time &lt;= #{endTime}</if>
        </where>
    </delete>

</mapper>