<?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.njcn.system.mapper.AuditMapper">

    <select id="getCount" resultType="int">
        SELECT count(id)
        FROM sys_user_log
        WHERE update_time >= #{auditParam.searchBeginTime}
        AND #{auditParam.searchEndTime} >= update_time
        <if test="auditParam.loginName!=null">
            and login_name = #{auditParam.loginName}
        </if>
        <if test="auditParam.type!=null">
            and type = #{auditParam.type}
        </if>
        <if test="auditParam.operateType!=null">
            and operate_type = #{auditParam.operateType}
        </if>
    </select>

    <select id="selectAuditLog" resultType="UserLog">
        SELECT
        id,
        user_name userName,
        login_name loginName,
        ip,
        operate,
        operate_type operateType,
        result,
        level,
        type,
        service_name serviceName,
        state,
        create_by createBy,
        create_time createTime,
        update_by updateBy,
        update_time updateTime,
        fail_reason failReason
        FROM
        sys_user_log
        WHERE update_time >= #{auditParam.searchBeginTime}
        AND #{auditParam.searchEndTime} >= update_time
        <if test="auditParam.loginName!=null">
            and login_name = #{auditParam.loginName}
        </if>
        <if test="auditParam.type!=null">
            and type = #{auditParam.type}
        </if>
        <if test="auditParam.operateType!=null">
            and operate_type = #{auditParam.operateType}
        </if>
        AND id >= (select id from sys_user_log order by id limit #{auditParam.pageNum}, 1) limit #{auditParam.pageSize}
    </select>

    <select id="queryExportUser" resultType="UserLogExcel">
        SELECT
           id,
           login_name loginName,
           user_name userName,
           ip,
           operate,
           operate_type operateType,
           result,
           fail_reason failReason,
           level,
           type,
           service_name serviceName,
           state,
           create_by createBy,
           create_time createTime,
           update_by updateBy,
           update_time updateTime
        FROM sys_user_log
        Where ${ew.sqlSegment}
    </select>

    <delete id="deleteByTime">
        DELETE FROM sys_user_log
        WHERE ${ew.sqlSegment}
    </delete>

    <select id="selectCensusAuditLog" resultType="com.njcn.system.pojo.vo.AuditLogCusVO">
        SELECT
        login_name LoginName,
        operate_type operateType,
        count(id) AS count
        FROM sys_user_log
        WHERE user_name !=""
        AND user_name !="unknown user"
		AND operate != "unknown operate"
		AND update_time >= #{auditParam.searchBeginTime}
        AND #{auditParam.searchEndTime} >= update_time
        <if test="auditParam.loginName!=null and auditParam.loginName!=''">
            and login_name = #{auditParam.loginName}
        </if>
        <if test="auditParam.type!=null and auditParam.type!=''">
            and type = #{auditParam.type}
        </if>
        <if test="auditParam.operateType!=null and auditParam.operateType!=''">
            and operate_type = #{auditParam.operateType}
        </if>
        GROUP BY login_name, operate_type
    </select>

    <select id="selectUserLog" resultType="com.njcn.system.pojo.po.UserLog">
        SELECT
        *
        FROM sys_user_log
        WHERE login_name !=""
		AND login_name !="unknown user"

    </select>

<!--    <select id="selectOperateType" resultType="String">-->
<!--        SELECT-->
<!--        operate_type-->
<!--        FROM sys_user_log-->
<!--        GROUP BY operate_type-->
<!--    </select>-->
    <select id="queryExportLimit" resultType="com.njcn.system.excel.UserLogExcel">

        SELECT
            id,
            login_name loginName,
            user_name userName,
            ip,
            operate,
            operate_type operateType,
            result,
            fail_reason failReason,
            level,
            type,
            service_name serviceName,
            state,
            create_by createBy,
            create_time createTime,
            update_by updateBy,
            update_time updateTime
        FROM sys_user_log
        <where>
            <if test="startTime != null and startTime != ''">
                and create_time >= #{startTime}
            </if>
            <if test="endTime != null and endTime != ''">
                and create_time &lt;= #{endTime}
            </if>
        </where>

    </select>
    <select id="getListIDs" resultType="java.lang.String">
        SELECT
        id
        FROM sys_user_log
        <where>
            <if test="startTime != null and startTime != ''">
                and create_time >= #{startTime}
            </if>
            <if test="endTime != null and endTime != ''">
                and create_time &lt;= #{endTime}
            </if>
        </where>
    </select>

</mapper>