<?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.dromara.member.mapper.AppLoginLogMapper">

    <resultMap type="org.dromara.member.domain.AppLoginLog" id="AppLoginLogResult">
        <id property="infoId" column="info_id"/>
        <result property="userName" column="user_name"/>
        <result property="status" column="status"/>
        <result property="ipaddr" column="ipaddr"/>
        <result property="loginLocation" column="login_location"/>
        <result property="browser" column="browser"/>
        <result property="os" column="os"/>
        <result property="msg" column="msg"/>
        <result property="loginTime" column="login_time"/>
    </resultMap>

    <resultMap type="org.dromara.member.domain.vo.AppLoginLogVo" id="AppLoginLogVoResult">
        <id property="infoId" column="info_id"/>
        <result property="userName" column="user_name"/>
        <result property="status" column="status"/>
        <result property="ipaddr" column="ipaddr"/>
        <result property="loginLocation" column="login_location"/>
        <result property="browser" column="browser"/>
        <result property="os" column="os"/>
        <result property="msg" column="msg"/>
        <result property="loginTime" column="login_time"/>
    </resultMap>

    <sql id="selectAppLoginLogVo">
        select info_id,
               user_name,
               status,
               ipaddr,
               login_location,
               browser,
               os,
               msg,
               login_time
        from app_login_log
    </sql>

    <!-- 查询登录日志列表 -->
    <select id="selectAppLoginLogList" parameterType="AppLoginLog" resultMap="AppLoginLogVoResult">
        <include refid="selectAppLoginLogVo"/>
        <where>
            <if test="userName != null and userName != ''">and user_name like concat('%', #{userName}, '%')</if>
            <if test="status != null and status != ''">and status = #{status}</if>
            <if test="ipaddr != null and ipaddr != ''">and ipaddr like concat('%', #{ipaddr}, '%')</if>
            <if test="loginLocation != null and loginLocation != ''">and login_location like concat('%',
                #{loginLocation}, '%')
            </if>
            <if test="params.beginTime != null and params.beginTime != ''">and login_time &gt;= #{params.beginTime}</if>
            <if test="params.endTime != null and params.endTime != ''">and login_time &lt;= #{params.endTime}</if>
        </where>
        order by login_time desc
    </select>

    <!-- 根据ID查询登录日志 -->
    <select id="selectAppLoginLogByInfoId" parameterType="Long" resultMap="AppLoginLogVoResult">
        <include refid="selectAppLoginLogVo"/>
        where info_id = #{infoId}
    </select>

    <!-- 根据用户名查询登录日志 -->
    <select id="selectAppLoginLogByUserName" parameterType="String" resultMap="AppLoginLogVoResult">
        <include refid="selectAppLoginLogVo"/>
        where user_name = #{userName}
        order by login_time desc
    </select>

    <!-- 新增登录日志 -->
    <insert id="insertAppLoginLog" parameterType="org.dromara.member.domain.AppLoginLog" useGeneratedKeys="true"
            keyProperty="infoId">
        insert into app_login_log
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="userName != null and userName != ''">user_name,</if>
            <if test="status != null and status != ''">status,</if>
            <if test="ipaddr != null and ipaddr != ''">ipaddr,</if>
            <if test="loginLocation != null and loginLocation != ''">login_location,</if>
            <if test="browser != null and browser != ''">browser,</if>
            <if test="os != null and os != ''">os,</if>
            <if test="msg != null and msg != ''">msg,</if>
            <if test="loginTime != null">login_time,</if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="userName != null and userName != ''">#{userName},</if>
            <if test="status != null and status != ''">#{status},</if>
            <if test="ipaddr != null and ipaddr != ''">#{ipaddr},</if>
            <if test="loginLocation != null and loginLocation != ''">#{loginLocation},</if>
            <if test="browser != null and browser != ''">#{browser},</if>
            <if test="os != null and os != ''">#{os},</if>
            <if test="msg != null and msg != ''">#{msg},</if>
            <if test="loginTime != null">#{loginTime},</if>
        </trim>
    </insert>

    <!-- 删除登录日志 -->
    <delete id="deleteAppLoginLogByInfoId" parameterType="Long">
        delete
        from app_login_log
        where info_id = #{infoId}
    </delete>

    <!-- 批量删除登录日志 -->
    <delete id="deleteAppLoginLogByInfoIds" parameterType="String">
        delete from app_login_log where info_id in
        <foreach item="infoId" collection="array" open="(" separator="," close=")">
            #{infoId}
        </foreach>
    </delete>

    <!-- 清空登录日志 -->
    <delete id="cleanAppLoginLog">
        truncate table app_login_log
    </delete>

    <!-- 根据时间范围删除登录日志 -->
    <delete id="deleteAppLoginLogByTime">
        delete from app_login_log
        <where>
            <if test="beginTime != null and beginTime != ''">and login_time &gt;= #{beginTime}</if>
            <if test="endTime != null and endTime != ''">and login_time &lt;= #{endTime}</if>
        </where>
    </delete>

</mapper>