<?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="cn.fateverse.log.mapper.OperationMapper">

    <resultMap type="cn.fateverse.log.entity.OperationLog" id="OperationLogResult">
        <id property="operId" column="oper_id"/>
        <result property="title" column="title"/>
        <result property="applicationName" column="application_name"/>
        <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="state" column="state"/>
        <result property="errorMsg" column="error_msg"/>
        <result property="operTime" column="oper_time"/>
        <result property="consumeTime" column="consume_time"/>
    </resultMap>

    <sql id="selectSql">
        select oper_id,
               title,
               application_name,
               business_type,
               method,
               request_method,
               operator_type,
               oper_name,
               dept_name,
               oper_url,
               oper_ip,
               oper_location,
               state,
               oper_time,
               consume_time
        from sys_operation_log
    </sql>


    <insert id="batchSave">
        insert into sys_operation_log
        (oper_id, title, application_name, business_type, method, request_method, operator_type, oper_name, dept_name,
        oper_url, oper_ip,
        oper_location, oper_param, json_result, state, error_msg, error_stack_trace, oper_time, consume_time)
        values
        <foreach collection="list" item="log" separator=",">
            (#{log.operId}, #{log.title}, #{log.applicationName}, #{log.businessType}, #{log.method},
            #{log.requestMethod}, #{log.operatorType},#{log.operName},
            #{log.deptName}, #{log.operUrl}, #{log.operIp}, #{log.operLocation}, #{log.operParam}, #{log.jsonResult},
            #{log.state}, #{log.errorMsg},
            #{log.errorStackTrace}, #{log.operTime}, #{log.consumeTime})
        </foreach>
    </insert>

    <select id="searchSubQuery" resultMap="OperationLogResult"
            parameterType="cn.fateverse.log.query.OperationLogQuery">
        <include refid="selectSql"/>
        <where>
            oper_id >= (select oper_id from sys_operation_log
            <where>
                <if test="operation.title !=null and operation.title !=''">
                    and title like concat('%',#{operation.title},'%')
                </if>
                <if test="operation.applicationName !=null and operation.applicationName !=''">
                    and application_name like concat('%',#{operation.applicationName},'%')
                </if>
                <if test="operation.operName !=null and operation.operName !=''">
                    and oper_name like concat('%',#{operation.operName},'%')
                </if>
                <if test="operation.businessType !=null ">
                    and business_type like concat('%',#{operation.businessType},'%')
                </if>
                <if test="operation.state !=null and operation.state !=''">
                    and state =#{operation.state}
                </if>
                <if test="operation.startTime !=null ">
                    and oper_time &gt;=#{operation.startTime}
                </if>
                <if test="operation.endTime !=null ">
                    and oper_time &lt;=#{operation.endTime}
                </if>
            </where>
            limit #{start},1
            )
            <if test="operation.title !=null and operation.title !=''">
                and title like concat('%',#{operation.title},'%')
            </if>
            <if test="operation.applicationName !=null and operation.applicationName !=''">
                and application_name like concat('%',#{operation.applicationName},'%')
            </if>
            <if test="operation.operName !=null and operation.operName !=''">
                and oper_name like concat('%',#{operation.operName},'%')
            </if>
            <if test="operation.businessType !=null ">
                and business_type like concat('%',#{operation.businessType},'%')
            </if>
            <if test="operation.state !=null and operation.state !=''">
                and state =#{operation.state}
            </if>
            <if test="operation.startTime !=null ">
                and oper_time &gt;=#{operation.startTime}
            </if>
            <if test="operation.endTime !=null ">
                and oper_time &lt;=#{operation.endTime}
            </if>
        </where>
        order by oper_time desc limit #{size}
    </select>


    <select id="search" resultMap="OperationLogResult"
            parameterType="cn.fateverse.log.query.OperationLogQuery">
        <include refid="selectSql"/>
        <where>
            <if test="operation.title !=null and operation.title !=''">
                and title like concat('%',#{operation.title},'%')
            </if>
            <if test="operation.applicationName !=null and operation.applicationName !=''">
                and application_name like concat('%',#{operation.applicationName},'%')
            </if>
            <if test="operation.operName !=null and operation.operName !=''">
                and oper_name like concat('%',#{operation.operName},'%')
            </if>
            <if test="operation.businessType !=null ">
                and business_type like concat('%',#{operation.businessType},'%')
            </if>
            <if test="operation.state !=null and operation.state !=''">
                and state =#{operation.state}
            </if>
            <if test="operation.startTime !=null ">
                and oper_time &gt;=#{operation.startTime}
            </if>
            <if test="operation.endTime !=null ">
                and oper_time &lt;=#{operation.endTime}
            </if>
        </where>
        order by oper_time desc
    </select>

    <select id="searchCount" resultType="java.lang.Long">
        select count(*) from sys_operation_log
        <where>
            <if test="operation.title !=null and operation.title !=''">
                and title like concat('%',#{operation.title},'%')
            </if>
            <if test="operation.applicationName !=null and operation.applicationName !=''">
                and application_name like concat('%',#{operation.applicationName},'%')
            </if>
            <if test="operation.operName !=null and operation.operName !=''">
                and oper_name like concat('%',#{operation.operName},'%')
            </if>
            <if test="operation.businessType !=null ">
                and business_type like concat('%',#{operation.businessType},'%')
            </if>
            <if test="operation.state !=null and operation.state !=''">
                and state =#{operation.state}
            </if>
            <if test="operation.startTime !=null ">
                and oper_time &gt;=#{operation.startTime}
            </if>
            <if test="operation.endTime !=null ">
                and oper_time &lt;=#{operation.endTime}
            </if>
        </where>
    </select>

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

    <select id="selectById" resultMap="OperationLogResult">
        select oper_id,
               title,
               application_name,
               business_type,
               method,
               request_method,
               operator_type,
               oper_name,
               dept_name,
               oper_url,
               oper_ip,
               oper_location,
               oper_param,
               json_result,
               state,
               error_msg,
               error_stack_trace,
               oper_time,
               consume_time
        from sys_operation_log
        where oper_id = #{operId}
    </select>

</mapper>
