<?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.xyzh.system.mapper.log.TbSysLoginLogMapper">

    <!-- 结果映射 -->
    <resultMap id="BaseResultMap" type="org.xyzh.api.system.dto.TbSysLoginLogDTO">
        <id column="optsn" property="optsn" jdbcType="VARCHAR"/>
        <result column="user_id" property="userId" jdbcType="VARCHAR"/>
        <result column="username" property="username" jdbcType="VARCHAR"/>
        <result column="ip_address" property="ipAddress" jdbcType="VARCHAR"/>
        <result column="ip_source" property="ipSource" jdbcType="VARCHAR"/>
        <result column="browser" property="browser" jdbcType="VARCHAR"/>
        <result column="os" property="os" jdbcType="VARCHAR"/>
        <result column="password" property="password" jdbcType="VARCHAR"/>
        <result column="login_time" property="loginTime" jdbcType="TIMESTAMP"/>
        <result column="status" property="status" jdbcType="INTEGER"/>
        <result column="error_count" property="errorCount" jdbcType="INTEGER"/>
        <result column="message" property="message" jdbcType="VARCHAR"/>
        <result column="create_time" property="createTime" jdbcType="TIMESTAMP"/>
    </resultMap>

    <!-- 基础列 -->
    <sql id="Base_Column_List">
        optsn, user_id, username, ip_address, ip_source, browser, os, password,
        login_time, status, error_count, message, create_time
    </sql>

    <!-- 插入系统登录日志 -->
    <insert id="insertLoginLog" parameterType="org.xyzh.api.system.dto.TbSysLoginLogDTO">
        INSERT INTO sys.tb_sys_login_log
        <trim prefix="(" suffix=")" suffixOverrides=",">
            optsn,
            user_id,
            username,
            <if test="ipAddress != null and ipAddress != ''">ip_address,</if>
            <if test="ipSource != null and ipSource != ''">ip_source,</if>
            <if test="browser != null and browser != ''">browser,</if>
            <if test="os != null and os != ''">os,</if>
            <if test="password != null and password != ''">password,</if>
            <if test="loginTime != null">login_time,</if>
            <if test="status != null">status,</if>
            <if test="errorCount != null">error_count,</if>
            <if test="message != null and message != ''">message,</if>
            <if test="createTime != null">create_time,</if>
        </trim>
        VALUES
        <trim prefix="(" suffix=")" suffixOverrides=",">
            #{optsn},
            #{userId},
            #{username},
            <if test="ipAddress != null and ipAddress != ''">#{ipAddress},</if>
            <if test="ipSource != null and ipSource != ''">#{ipSource},</if>
            <if test="browser != null and browser != ''">#{browser},</if>
            <if test="os != null and os != ''">#{os},</if>
            <if test="password != null and password != ''">#{password},</if>
            <if test="loginTime != null">#{loginTime},</if>
            <if test="status != null">#{status},</if>
            <if test="errorCount != null">#{errorCount},</if>
            <if test="message != null and message != ''">#{message},</if>
            <if test="createTime != null">#{createTime},</if>
        </trim>
    </insert>

    <!-- 更新系统登录日志 -->
    <update id="updateLoginLog" parameterType="org.xyzh.api.system.dto.TbSysLoginLogDTO">
        UPDATE sys.tb_sys_login_log
        <set>
            <if test="userId != null and userId != ''">user_id = #{userId},</if>
            <if test="username != null and username != ''">username = #{username},</if>
            <if test="ipAddress != null and ipAddress != ''">ip_address = #{ipAddress},</if>
            <if test="ipSource != null and ipSource != ''">ip_source = #{ipSource},</if>
            <if test="browser != null and browser != ''">browser = #{browser},</if>
            <if test="os != null and os != ''">os = #{os},</if>
            <if test="password != null and password != ''">password = #{password},</if>
            <if test="loginTime != null">login_time = #{loginTime},</if>
            <if test="status != null">status = #{status},</if>
            <if test="errorCount != null">error_count = #{errorCount},</if>
            <if test="message != null and message != ''">message = #{message},</if>
        </set>
        WHERE optsn = #{optsn}
    </update>

    <!-- 删除系统登录日志 -->
    <delete id="deleteLoginLog" parameterType="org.xyzh.api.system.dto.TbSysLoginLogDTO">
        DELETE FROM sys.tb_sys_login_log
        WHERE optsn = #{optsn}
    </delete>

    <!-- 根据登录日志ID查询系统登录日志 -->
    <select id="getLoginLogById" resultMap="BaseResultMap">
        SELECT <include refid="Base_Column_List" />
        FROM sys.tb_sys_login_log
        WHERE optsn = #{loginLogId}
    </select>

    <!-- 根据条件查询系统登录日志列表 -->
    <select id="getLoginLogByFilter" resultMap="BaseResultMap" parameterType="org.xyzh.api.system.dto.TbSysLoginLogDTO">
        SELECT <include refid="Base_Column_List" />
        FROM sys.tb_sys_login_log
        <where>
            <if test="filter.optsn != null and filter.optsn != ''">
                AND optsn = #{filter.optsn}
            </if>
            <if test="filter.userId != null and filter.userId != ''">
                AND user_id = #{filter.userId}
            </if>
            <if test="filter.username != null and filter.username != ''">
                AND username LIKE CONCAT('%', #{filter.username}, '%')
            </if>
            <if test="filter.ipAddress != null and filter.ipAddress != ''">
                AND ip_address = #{filter.ipAddress}
            </if>
            <if test="filter.status != null">
                AND status = #{filter.status}
            </if>
            <if test="filter.startTime != null">
                AND login_time &gt;= #{filter.startTime}
            </if>
            <if test="filter.endTime != null">
                AND login_time &lt;= #{filter.endTime}
            </if>
        </where>
        ORDER BY login_time DESC
    </select>

    <!-- 根据条件查询系统登录日志分页列表 -->
    <select id="getLoginLogPageByFilter" resultMap="BaseResultMap">
        SELECT <include refid="Base_Column_List" />
        FROM sys.tb_sys_login_log
        <where>
            <if test="filter.optsn != null and filter.optsn != ''">
                AND optsn = #{filter.optsn}
            </if>
            <if test="filter.userId != null and filter.userId != ''">
                AND user_id = #{filter.userId}
            </if>
            <if test="filter.username != null and filter.username != ''">
                AND username LIKE CONCAT('%', #{filter.username}, '%')
            </if>
            <if test="filter.ipAddress != null and filter.ipAddress != ''">
                AND ip_address = #{filter.ipAddress}
            </if>
            <if test="filter.status != null">
                AND status = #{filter.status}
            </if>
            <if test="filter.startTime != null">
                AND login_time &gt;= #{filter.startTime}
            </if>
            <if test="filter.endTime != null">
                AND login_time &lt;= #{filter.endTime}
            </if>
        </where>
        ORDER BY login_time DESC
        LIMIT #{pageParam.pageSize} OFFSET #{pageParam.offset}
    </select>

    <!-- 根据条件查询系统登录日志数量 -->
    <select id="getLoginLogCount" resultType="java.lang.Integer" parameterType="org.xyzh.api.system.dto.TbSysLoginLogDTO">
        SELECT COUNT(1)
        FROM sys.tb_sys_login_log
        <where>
            <if test="optsn != null and optsn != ''">
                AND optsn = #{optsn}
            </if>
            <if test="userId != null and userId != ''">
                AND user_id = #{userId}
            </if>
            <if test="username != null and username != ''">
                AND username LIKE CONCAT('%', #{username}, '%')
            </if>
            <if test="ipAddress != null and ipAddress != ''">
                AND ip_address = #{ipAddress}
            </if>
            <if test="status != null">
                AND status = #{status}
            </if>
            <if test="startTime != null">
                AND login_time &gt;= #{startTime}
            </if>
            <if test="endTime != null">
                AND login_time &lt;= #{endTime}
            </if>
        </where>
    </select>

</mapper>
