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

	<resultMap type="org.springblade.common.core.domain.entity.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="costTime"       column="cost_time"      />
	</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
        from da_ky_sys_oper_log
    </sql>

	<insert id="insertOperlog" parameterType="org.springblade.common.core.domain.entity.SysOperLog">
		insert into da_ky_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)
        values (#{title}, #{businessType}, #{method}, #{requestMethod}, #{operatorType}, #{operName}, #{deptName}, #{operUrl}, #{operIp}, #{operLocation}, #{operParam}, #{jsonResult}, #{status}, #{errorMsg}, #{costTime}, sysdate())
	</insert>
	<!--添加操作日志-->
    <insert id="addSysOperLog">
		INSERT INTO da_ky_sys_logs(
			log_id,opera_user_name,ip,user_id,
			model,oper_time,method,params,
			operate_detail,oper_type,oper_uri,
			log_type,result,times,
			failure_reason,grade,err_type,
			method_type,title,result_data
		)values (
			#{logId},#{operaUserName},#{ip},#{userId},
			#{model},#{operTime},#{method},#{params},
			#{operateDetail},#{operType},#{operUri},
			#{logType},#{result},#{times},
			#{failureReason},#{grade},#{errType},
			#{methodType},#{title},#{resultData}
			)
	</insert>

    <select id="selectOperLogList" parameterType="org.springblade.common.core.domain.entity.SysOperLog" resultMap="SysOperLogResult">
		<include refid="selectOperLogVo"/>
		<where>
			<if test="operIp != null and operIp != ''">
				AND INSTR(oper_ip, #{operIp}) > 0
			</if>
			<if test="title != null and title != ''">
				AND INSTR(title, #{title}) > 0
			</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="operName != null and operName != ''">
				AND INSTR(oper_name, #{operName}) > 0
			</if>
			<if test="beginTime != null and beginTime != '' and endTime != null and endTime != ''">
				AND oper_time BETWEEN CONCAT(#{beginTime}, ' 00:00:00') AND CONCAT(#{endTime}, ' 23:59:59')
			</if>
		</where>
		order by oper_id desc
	</select>

	<delete id="deleteOperLogByIds" parameterType="Long">
 		delete from da_ky_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>
	<!--查询业务模块-->
	<select id="getModule" resultType="org.springblade.common.core.domain.entity.SysLogsVo">
		select  CONCAT(IFNULL(sm.menu_name,''),'->',IFNULL(sm2.menu_name,''))  module ,sm3.menu_name operateDetail
		from  da_ky_sys_menu  sm
		left join  da_ky_sys_menu sm2 on sm2.parent_id=sm.menu_id  and sm2.menu_type=1
		left join  da_ky_sys_menu sm3 on sm3.parent_id=sm2.menu_id   and sm3.menu_type=2
		where sm3.component=#{module}
		limit 1
	</select>
	<!--查询 异常日志路径-->
    <select id="getLogsErrorModule" resultType="org.springblade.common.core.domain.entity.SysLogsMenuHead">
		select sm.menu_name menuName,sm.menu_type menuType,sm2.menu_name menuName2,
			   sm3.menu_name menuName3,sm4.menu_name menuName4
		FROM  da_ky_sys_menu sm
				  left join  da_ky_sys_menu sm2 on sm.parent_id=sm2.menu_id and sm2.visible=0
				  left join  da_ky_sys_menu sm3 on sm2.parent_id=sm3.menu_id and sm3.visible=0
				  left join  da_ky_sys_menu sm4 on sm3.parent_id=sm4.menu_id and sm4.visible=0
		where  sm.perms=#{params}
		limit 1
	</select>

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

</mapper>
