<?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.github.zuihou.authority.dao.common.LoginLogMapper">

    <select id="findTotalVisitCount" resultType="long">
        select count(1) from c_common_login_log
    </select>

    <select id="findTodayVisitCount" resultType="long" parameterType="map">
        select count(1) from c_common_login_log where login_date = #{today}
    </select>

    <select id="findTodayIp" resultType="long" parameterType="map">
        select count(distinct(request_ip)) from c_common_login_log where login_date = #{today}
    </select>

    <select id="findLastTenDaysVisitCount" resultType="map" parameterType="map">
        select login_date , count(1) `count` from
        ( select id, login_date from c_common_login_log where login_date >= #{tenDays}
        <if test="account != null and account != ''">
            and account = #{account}
        </if>
        ) as l group by login_date
    </select>

    <select id="findByBrowser" resultType="map">
     	select browser, count(id) `count` from c_common_login_log group by browser
    </select>
    <select id="findByOperatingSystem" resultType="map">
        select operating_system,count(id) `count` from c_common_login_log group by operating_system
    </select>

    <delete id="clearLog" parameterType="map">
        delete from c_common_login_log
        <trim prefix="WHERE" prefixOverrides="AND | OR">
            <if test="clearBeforeTime != null">
                AND create_time <![CDATA[ <= ]]> #{clearBeforeTime}
            </if>
            <if test="clearBeforeNum  != null">
                AND id NOT in(
                SELECT id FROM(
                SELECT id FROM c_common_login_log AS t
                ORDER BY t.create_time desc LIMIT 0, #{clearBeforeNum}
                ) t1
                )
            </if>
        </trim>
    </delete>
</mapper>
