<?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="im.zhaojun.system.mapper.LoginLogMapper">
  <resultMap id="BaseResultMap" type="im.zhaojun.system.model.LoginLog">
    <!--
      WARNING - @mbg.generated
      This element is automatically generated by MyBatis Generator, do not modify.
    -->
    <id column="id" jdbcType="INTEGER" property="id" />
    <result column="login_time" jdbcType="VARCHAR" property="loginTime" />
    <result column="username" jdbcType="VARCHAR" property="username" />
    <result column="login_status" jdbcType="CHAR" property="loginStatus" />
    <result column="ip" jdbcType="VARCHAR" property="ip" />
  </resultMap>
  <sql id="Base_Column_List">
    <!--
      WARNING - @mbg.generated
      This element is automatically generated by MyBatis Generator, do not modify.
    -->
    id, login_time, username, login_status, ip
  </sql>
  <select id="selectByPrimaryKey" parameterType="java.lang.Integer" resultMap="BaseResultMap">
    <!--
      WARNING - @mbg.generated
      This element is automatically generated by MyBatis Generator, do not modify.
    -->
    select 
    <include refid="Base_Column_List" />
    from login_log
    where id = #{id,jdbcType=INTEGER}
  </select>
  <delete id="deleteByPrimaryKey" parameterType="java.lang.Integer">
    <!--
      WARNING - @mbg.generated
      This element is automatically generated by MyBatis Generator, do not modify.
    -->
    delete from login_log
    where id = #{id,jdbcType=INTEGER}
  </delete>
  <insert id="insert" parameterType="im.zhaojun.system.model.LoginLog">
    <!--
      WARNING - @mbg.generated
      This element is automatically generated by MyBatis Generator, do not modify.
    -->
    insert into login_log (login_time, username, login_status, 
      ip)
    values (#{loginTime,jdbcType=VARCHAR}, #{username,jdbcType=VARCHAR}, #{loginStatus,jdbcType=CHAR},
      #{ip,jdbcType=VARCHAR})
  </insert>
  <update id="updateByPrimaryKey" parameterType="im.zhaojun.system.model.LoginLog">
    <!--
      WARNING - @mbg.generated
      This element is automatically generated by MyBatis Generator, do not modify.
    -->
    update login_log
    set login_time = #{loginTime,jdbcType=VARCHAR},
      username = #{username,jdbcType=VARCHAR},
      login_status = #{loginStatus,jdbcType=CHAR},
      ip = #{ip,jdbcType=VARCHAR}
    where id = #{id,jdbcType=INTEGER}
  </update>


  <select id="selectAll" resultMap="BaseResultMap">
    select
    <include refid="Base_Column_List"/>
    from login_log order by login_time desc
  </select>

  <select id="recentlyWeekLoginCount" parameterType="java.lang.String" resultType="int">
    select
      #   a.login_time,
      decode(b.count,null, 0) as count
    from (
           SELECT curdate() as login_time
           union all
           SELECT date_sub(curdate(), interval 1 day) as click_date
           union all
           SELECT date_sub(curdate(), interval 2 day) as click_date
           union all
           SELECT date_sub(curdate(), interval 3 day) as click_date
           union all
           SELECT date_sub(curdate(), interval 4 day) as click_date
           union all
           SELECT date_sub(curdate(), interval 5 day) as click_date
           union all
           SELECT date_sub(curdate(), interval 6 day) as click_date
         ) a left join (
      select
        date(login_time) as datetime,
        count(*)         as count
      from login_log where username = #{username}
      group by date(login_time)
    ) b on a.login_time = b.datetime order by login_time;
  </select>

  <select id="count" resultType="int">
    select count(*) from login_log
  </select>
</mapper>