<?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.ruoyi.system.operLog.mapper.OperLogMapper">

	<resultMap type="OperLogVO" id="SysOperLogResult" autoMapping="true">
		<result column="biz_ids" property="bizIds" typeHandler="com.ruoyi.common.mybatis.typehandler.StringSplitListTypeHandler"/>
	</resultMap>

	<sql id="selectOperLogVo">
		select
			oper_id,
			title,
			business_type,
			method,
			request_method,
			operator_type,
			oper_name,
			dept_name,
			oper_url,
			oper_ip,
			oper_location,
			oper_param,
			json_result,
			status,
			error_msg,
			oper_time,
			cost_time,
			biz_ids,
			biz_type,
			oper_user_id
		from sys_oper_log
	</sql>

	<insert id="insertOperlog" parameterType="OperLog">
		insert into sys_oper_log(
			title,
			business_type,
			method,
			request_method,
			operator_type,
			oper_name,
			dept_name,
			oper_url,
			oper_ip,
			oper_location,
			oper_param,
			json_result,
			status,
			error_msg,
			cost_time,
			oper_time,
			biz_ids,
			biz_type,
			oper_user_id
		)
		values (
			#{title},
			#{businessType},
			#{method},
			#{requestMethod},
			#{operatorType},
			#{operName},
			#{deptName},
			#{operUrl},
			#{operIp},
			#{operLocation},
			#{operParam},
			#{jsonResult},
			#{status},
			#{errorMsg},
			#{costTime},
			sysdate(),
			#{bizIds,typeHandler=com.ruoyi.common.mybatis.typehandler.StringSplitListTypeHandler},
			#{bizType},
			#{operUserId}
		)
	</insert>

	<select id="selectOperLogList" parameterType="OperLogQuery" resultMap="SysOperLogResult">
		<include refid="selectOperLogVo"/>
		<where>
			<if test="operIp != null and operIp != ''">
				AND oper_ip like concat('%', #{operIp}, '%')
			</if>
			<if test="title != null and title != ''">
				AND title like concat('%', #{title}, '%')
			</if>
			<if test="businessType != null">
				AND business_type = #{businessType}
			</if>
			<if test="businessTypes != null and businessTypes.length > 0">
			    AND business_type in
			    <foreach collection="businessTypes" item="businessType" open="(" separator="," close=")">
		 			#{businessType}
		        </foreach>
			</if>
			<if test="status != null">
				AND status = #{status}
			</if>
			<if test="operUrl != null and operUrl != ''">
				AND oper_url like concat('%', #{operUrl}, '%')
			</if>
			<if test="operName != null and operName != ''">
				AND oper_name like concat('%', #{operName}, '%')
			</if>
			<if test="bizId != null">
				and find_in_set(#{bizId}, biz_ids)
			</if>
			<if test="bizType != null and bizType != ''">
				and biz_type = #{bizType}
			</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>
			<if test="operUserId != null">
				and oper_user_id = #{operUserId}
			</if>
			<if test="operTimeRange != null and operTimeRange.size() > 1">
				AND oper_time &gt;= #{operTimeRange[0]}
				AND oper_time &lt;= #{operTimeRange[1]}
			</if>
		</where>
		order by oper_id desc
	</select>

	<delete id="deleteOperLogByIds" parameterType="Long">
 		delete from sys_oper_log where oper_id in
 		<foreach collection="array" item="operId" open="(" separator="," close=")">
 			#{operId}
        </foreach>
 	</delete>

 	<select id="selectOperLogById" parameterType="Long" resultMap="SysOperLogResult">
		<include refid="selectOperLogVo"/>
		where oper_id = #{operId}
	</select>

	<update id="cleanOperLog">
        truncate table sys_oper_log
    </update>

</mapper>
