<?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.usky.system.mapper.SysLogininforMapper">

	<resultMap type="com.usky.system.domain.SysLogininfor" id="SysLogininforResult">
		<id     property="infoId"        column="info_id"           />
		<result property="userName"      column="user_name"         />
		<result property="status"        column="status"            />
		<result property="ipaddr"        column="ipaddr"            />
		<result property="msg"           column="msg"               />
		<result property="accessTime"    column="access_time"       />
		<result property="deptId"        column="dept_id"           />
		<result property="deptName"      column="dept_name"         />
		<result property="tenantId"      column="tenant_id"         />
		<result property="createBy"      column="create_by"         />
		<result property="loginLocation"    column="login_location"       />
		<result property="browser"    column="browser"       />
		<result property="os"    column="os"       />
	</resultMap>

	<insert id="insertLogininfor" parameterType="com.usky.system.domain.SysLogininfor">
		insert into sys_logininfor
		<trim prefix="(" suffix=")" suffixOverrides=",">
			<if test="userName != null"> user_name, </if>
			<if test="status != null"> status, </if>
			<if test="ipaddr != null"> ipaddr, </if>
			<if test="msg != null"> msg, </if>
			<if test="accessTime != null"> access_time, </if>
			<if test="deptId != null"> dept_id, </if>
			<if test="tenantId != null"> tenant_id, </if>
			<if test="createBy != null"> create_by, </if>
			<if test="loginLocation != null"> login_location, </if>
			<if test="browser != null"> browser, </if>
			<if test="os != null"> os, </if>
		</trim>
		values
		<trim prefix="(" suffix=")" suffixOverrides=",">
			<if test="userName != null"> #{userName},</if>
			<if test="status != null"> #{status},</if>
			<if test="ipaddr != null"> #{ipaddr},</if>
			<if test="msg != null"> #{msg},</if>
			<if test="accessTime != null"> #{accessTime},</if>
			<if test="deptId != null"> #{deptId},</if>
			<if test="tenantId != null"> #{tenantId},</if>
			<if test="createBy != null"> #{createBy},</if>
			<if test="loginLocation != null"> #{loginLocation},</if>
			<if test="browser != null"> #{browser},</if>
			<if test="os != null"> #{os},</if>
		</trim>
	</insert>

	<select id="selectLogininforList" parameterType="com.usky.system.domain.SysLogininfor" resultMap="SysLogininforResult">
		select d.info_id, d.user_name, d.ipaddr, d.status, d.msg, d.access_time, d.login_location, d.browser, d.os, sd.dept_name
		from sys_logininfor d
		left join sys_dept sd on d.dept_id = sd.dept_id
		<where>
			<if test="ipaddr != null and ipaddr != ''">
				AND d.ipaddr like concat('%', #{ipaddr}, '%')
			</if>
			<if test="status != null and status != ''">
				AND d.status = #{status}
			</if>
			<if test="userName != null and userName != ''">
				AND d.user_name like concat('%', #{userName}, '%')
			</if>
			<if test="params.beginTime != null and params.beginTime != ''"><!-- 开始时间检索 -->
				and date_format(d.access_time,'%y%m%d') >= date_format(#{params.beginTime},'%y%m%d')
			</if>
			<if test="params.endTime != null and params.endTime != ''"><!-- 结束时间检索 -->
				and date_format(d.access_time,'%y%m%d') &lt;= date_format(#{params.endTime},'%y%m%d')
			</if>
			<if test="1 == 1">
				and d.tenant_id = #{tenantId}
			</if>
			<!-- 数据范围过滤 -->
			${params.dataScope}
		</where>
		order by d.info_id desc
	</select>

	<select id="selectLogininforListExport" parameterType="com.usky.system.domain.SysLogininfor" resultType="com.usky.system.service.vo.SysLoginExportVO">
		select d.info_id, d.ipaddr, d.user_name,
		(case when d.status = '0' then '成功' else '失败' end) as status,
		d.msg, d.access_time, d.login_location, d.browser, d.os,
		sd.dept_name
		from sys_logininfor d
		left join sys_dept sd on d.dept_id = sd.dept_id
		<where>
			<if test="ipaddr != null and ipaddr != ''">
				AND d.ipaddr like concat('%', #{ipaddr}, '%')
			</if>
			<if test="status != null and status != ''">
				AND d.status = #{status}
			</if>
			<if test="userName != null and userName != ''">
				AND d.user_name like concat('%', #{userName}, '%')
			</if>
			<if test="params.beginTime != null and params.beginTime != ''"><!-- 开始时间检索 -->
				and date_format(d.access_time,'%y%m%d') >= date_format(#{params.beginTime},'%y%m%d')
			</if>
			<if test="params.endTime != null and params.endTime != ''"><!-- 结束时间检索 -->
				and date_format(d.access_time,'%y%m%d') &lt;= date_format(#{params.endTime},'%y%m%d')
			</if>
			<if test="1 == 1">
				and d.tenant_id = #{tenantId}
			</if>
			${params.dataScope}
		</where>
		order by d.info_id desc
	</select>

	<delete id="deleteLogininforByIds" parameterType="Long">
		delete from sys_logininfor where info_id in
		<foreach collection="array" item="infoId" open="(" separator="," close=")">
			#{infoId}
		</foreach>
	</delete>

	<update id="cleanLogininfor">
	truncate table sys_logininfor
	</update>

</mapper>