<?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.usky.system.mapper.SysOperLogMapper">

	<resultMap type="com.usky.system.domain.SysOperLog" id="SysOperLogResult">
		<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="operatorType"   column="operator_type"  />
		<result property="operName"       column="oper_name"      />
		<result property="deptName"       column="dept_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="deptId"         column="dept_id"        />
		<result property="tenantId"       column="tenant_id"      />
		<result property="createBy"       column="create_by"      />
		<result property="startTime"     column="start_time"    />
		<result property="endTime"       column="end_time"      />
		<result property="consumingTime"   column="consuming_time" />
		<result property="consumingTimeWithUnit"     column="consuming_time_with_unit"    />
	</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, start_time, end_time, consuming_time, consuming_time_with_unit
		from sys_oper_log d
    </sql>
    
	<insert id="insertOperlog" parameterType="com.usky.system.domain.SysOperLog">
		insert into sys_oper_log
		<trim prefix="(" suffix=")" suffixOverrides=",">
			<if test="title != null"> title, </if>
			<if test="businessType != null"> business_type, </if>
			<if test="method != null"> method, </if>
			<if test="requestMethod != null"> request_method, </if>
			<if test="operatorType != null"> operator_type, </if>
			<if test="operName != null"> oper_name, </if>
			<if test="deptName != null"> dept_name, </if>
			<if test="operUrl != null"> oper_url, </if>
			<if test="operIp != null"> oper_ip, </if>
			<if test="operLocation != null"> oper_location, </if>
			<if test="operParam != null"> oper_param, </if>
			<if test="jsonResult != null"> json_result, </if>
			<if test="status != null"> status, </if>
			<if test="errorMsg != null"> error_msg, </if>
			<if test="operTime != null"> oper_time, </if>
			<if test="deptId != null"> dept_id, </if>
			<if test="tenantId != null"> tenant_id, </if>
			<if test="createBy != null"> create_by, </if>
			<if test="startTime != null"> start_time, </if>
			<if test="endTime != null"> end_time, </if>
			<if test="consumingTime != null"> consuming_time, </if>
			<if test="consumingTimeWithUnit !=null"> consuming_time_with_unit, </if>
		</trim>
		values
		<trim prefix="(" suffix=")" suffixOverrides=",">
			<if test="title != null"> #{title},</if>
			<if test="businessType != null"> #{businessType},</if>
			<if test="method != null"> #{method},</if>
			<if test="requestMethod != null"> #{requestMethod},</if>
			<if test="operatorType != null"> #{operatorType},</if>
			<if test="operName != null"> #{operName},</if>
			<if test="deptName != null"> #{deptName},</if>
			<if test="operUrl != null"> #{operUrl},</if>
			<if test="operIp != null"> #{operIp},</if>
			<if test="operLocation != null"> #{operLocation},</if>
			<if test="operParam != null"> #{operParam},</if>
			<if test="jsonResult != null"> #{jsonResult},</if>
			<if test="status != null"> #{status},</if>
			<if test="errorMsg != null"> #{errorMsg},</if>
			<if test="operTime != null"> #{operTime},</if>
			<if test="deptId != null"> #{deptId},</if>
			<if test="tenantId != null"> #{tenantId},</if>
			<if test="createBy != null"> #{createBy},</if>
			<if test="startTime != null"> #{startTime},</if>
		    <if test="endTime != null"> #{endTime},</if>
			<if test="consumingTime != null"> #{consumingTime},</if>
		    <if test="consumingTimeWithUnit !=null"> #{consumingTimeWithUnit},</if>
		</trim>
	</insert>
	
	<select id="selectOperLogList" parameterType="com.usky.system.domain.SysOperLog" resultMap="SysOperLogResult">
		<include refid="selectOperLogVo"/>
		<where>
			<if test="title != null and title != ''">
				AND title like concat('%', #{title}, '%')
			</if>
			<if test="businessType != null and businessType != ''">
				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="operName != null and operName != ''">
				AND oper_name like concat('%', #{operName}, '%')
			</if>
			<if test="params.beginTime != null and params.beginTime != ''"><!-- 开始时间检索 -->
				and date_format(oper_time,'%y%m%d') &gt;= date_format(#{params.beginTime},'%y%m%d')
			</if>
			<if test="params.endTime != null and params.endTime != ''"><!-- 结束时间检索 -->
				and date_format(oper_time,'%y%m%d') &lt;= date_format(#{params.endTime},'%y%m%d')
			</if>
			<if test="1 == 1">
				and tenant_id = #{tenantId}
			</if>
			<!-- 数据范围过滤 -->
			${params.dataScope}
		</where>
		order by oper_id desc
	</select>

	<select id="selectOperLogListExport" parameterType="com.usky.system.domain.SysOperLog" resultType="com.usky.system.service.vo.SysOperLogExportVO">
		SELECT oper_id, oper_ip, oper_name, title, (CASE business_type WHEN '0' THEN '其它' WHEN '1' THEN '新增' WHEN '2' THEN '修改' ELSE '删除' END) AS businessType, request_method, (CASE STATUS WHEN '0' THEN '成功' ELSE '失败' END) AS STATUS, oper_time, start_time, end_time, consuming_time, consuming_time_with_unit
		FROM sys_oper_log d
		<where>
			<if test="title != null and title != ''">
				AND title like concat('%', #{title}, '%')
			</if>
			<if test="businessType != null and businessType != ''">
				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="operName != null and operName != ''">
				AND oper_name like concat('%', #{operName}, '%')
			</if>
			<if test="params.beginTime != null and params.beginTime != ''"><!-- 开始时间检索 -->
				and date_format(oper_time,'%y%m%d') &gt;= date_format(#{params.beginTime},'%y%m%d')
			</if>
			<if test="params.endTime != null and params.endTime != ''"><!-- 结束时间检索 -->
				and date_format(oper_time,'%y%m%d') &lt;= date_format(#{params.endTime},'%y%m%d')
			</if>
			<if test="1 == 1">
				and tenant_id = #{tenantId}
			</if>
			<!-- 数据范围过滤 -->
			${params.dataScope}
		</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 d.oper_id = #{operId}
	</select>
	
	<update id="cleanOperLog">
        truncate table sys_oper_log
    </update>

</mapper> 