<?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.pj.project.sys_login_log.SysLoginLogMapper">

	<!-- 增 -->
	<insert id="add">
		insert into 
		sys_login_log (id, user_id, acc_token, login_ip, login_address, login_device, login_system, client_domain, client_id, create_time) 
		values (0, #{userId}, #{accToken}, #{loginIp}, #{loginAddress}, #{loginDevice}, #{loginSystem}, #{clientDomain}, #{clientId}, now()) 
	</insert>

	<!-- 删 -->
	<delete id="delete">
		delete from sys_login_log 
		where id = #{id}
	</delete>


	<!-- ================================== 查询相关 ================================== -->
	
	<!-- 通用映射：手动模式 -->
	<resultMap id="model" type="com.pj.project.sys_login_log.SysLoginLog">
		<result property="id" column="id" />
		<result property="userId" column="user_id" />
		<result property="accToken" column="acc_token" />
		<result property="loginIp" column="login_ip" />
		<result property="loginAddress" column="login_address" />
		<result property="loginDevice" column="login_device" />
		<result property="loginSystem" column="login_system" />
		<result property="clientId" column="client_id" />
		<result property="clientDomain" column="client_domain" />
		<result property="createTime" column="create_time" />
		<result property="sysUserUsername" column="sys_user_username" />
		<result property="sysUserAvatar" column="sys_user_avatar" />
		<result property="sysClientName" column="sys_client_name" />
		<result property="sysClientLogo" column="sys_client_logo" />
	</resultMap>
	
	<!-- 公共查询sql片段 -->
	<sql id="select_sql">
		select 
			*, 
			(select username from sys_user where id = sys_login_log.user_id) as sys_user_username, 
			(select avatar from sys_user where id = sys_login_log.user_id) as sys_user_avatar, 
			(select name from sys_client where id = sys_login_log.client_id) as sys_client_name, 
			(select logo from sys_client where id = sys_login_log.client_id) as sys_client_logo
		from sys_login_log 
	</sql>
	
	<!-- 查 - 根据id -->
	<select id="getById" resultMap="model">
		<include refid="select_sql"></include>
		where id = #{id}
	</select>
	
	<!-- 查集合 - 根据条件（参数为空时代表忽略指定条件） [G] -->
	<select id="getList" resultMap="model">
		<include refid="select_sql"></include>
		<where>
			<if test=' this.has("id") '> and id = #{id} </if>
			<if test=' this.has("userId") '> and user_id = #{userId} </if>
			<if test=' this.has("accToken") '> and acc_token = #{accToken} </if>
			<if test=' this.has("loginIp") '> and login_ip = #{loginIp} </if>
			<if test=' this.has("clientId") '> and client_id = #{clientId} </if>
			<if test=' this.has("clientDomain") '> and client_domain = #{clientDomain} </if>
		</where>
		order by id desc
	</select>
	
	
	
	
	
	
	
	
	

</mapper>
