<?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.judong.chuanyiserver.dao.LogDao">

    <insert id="insertLog">
        insert into sys_log(server_name, model_name, content, user_id, create_time, ip_address, operation_status,
                            method_name, remark)
            VALUE (#{serverName}, #{modelName}, #{content}, #{userId}, now(), #{ipAddress}, #{operationStatus},
                   #{methodName}, #{remark})
    </insert>

    <select id="getLogCount" resultType="java.lang.Long">
        select count(*)
        from sys_log
        where 1=1
        <if test="serverName != null and serverName !=''">
            AND server_name LIKE CONCAT('%', #{serverName}, '%')
        </if>
        <if test="modelName != null and modelName !=''">
            AND model_name LIKE CONCAT('%', #{modelName}, '%')
        </if>
        <if test="content != null and content !=''">
            AND content LIKE CONCAT('%', #{content}, '%')
        </if>
        <if test="userId != null and userId !='' ">
            AND user_id =#{userId}
        </if>
        <if test="ipAddress != null and ipAddress !='' ">
            AND ip_address LIKE CONCAT('%', #{ipAddress}, '%')
        </if>
        <if test="methodName != null and methodName !=''">
            AND method_name LIKE CONCAT('%', #{methodName}, '%')
        </if>
        <if test="startDate !=null and startDate != ''">
            AND create_time > #{startDate}
        </if>
        <if test="endDate !=null and endDate !=''">
            AND create_time &lt;#{endDate}
        </if>
    </select>

    <select id="getLogPage" resultType="com.judong.chuanyiserver.entity.Log">
        select id,
               server_name,
               model_name,
               content,
               user_id,
               create_time,
               ip_address,
               operation_status,
               method_name,
               remark
        from sys_log
        where 1=1
        <if test="serverName != null and serverName !=''">
            AND server_name LIKE CONCAT('%', #{serverName}, '%')
        </if>
        <if test="modelName != null and modelName !=''">
            AND model_name LIKE CONCAT('%', #{modelName}, '%')
        </if>
        <if test="content != null and content !=''">
            AND content LIKE CONCAT('%', #{content}, '%')
        </if>
        <if test="userId != null and userId !=''">
            AND user_id =#{userId}
        </if>
        <if test="ipAddress != null and ipAddress !=''">
            AND ip_address LIKE CONCAT('%', #{ipAddress}, '%')
        </if>
        <if test="methodName != null and methodName !=''">
            AND method_name LIKE CONCAT('%', #{methodName}, '%')
        </if>
        <if test="startDate !=null and startDate != ''">
            AND create_time > #{startDate}
        </if>
        <if test="endDate !=null and endDate !=''">
            AND create_time &lt; #{endDate}
        </if>
        order by create_time DESC
        limit #{startNum},#{limitNum}
    </select>

    <!--删除6个月之前的数据-->
    <delete id="logDeleteByTime">
        DELETE FROM sys_log WHERE create_time &lt;#{time}
    </delete>

</mapper>