<?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_user.SysUserMapper">

	<!-- 增 -->
	<insert id="add">
		insert into 
		sys_user (id, username, avatar, intro, age, sex, phone, email, create_time) 
		values (#{id}, #{username}, #{avatar}, #{intro}, #{age}, #{sex}, #{phone}, #{email}, now()) 
	</insert>

	<!-- 改 -->
	<update id="update">
		update sys_user set
		username = #{username}, 
		avatar = #{avatar}, 
		intro = #{intro}, 
		age = #{age}, 
		sex = #{sex}, 
		phone = #{phone}, 
		email = #{email}, 
		status = #{status}
		where id = #{id}
	</update>

	<!-- ================================== 查询相关 ================================== -->
	
	<!-- 通用映射：手动模式 -->
	<resultMap id="model" type="com.pj.project.sys_user.SysUser">
		<result property="id" column="id" />
		<result property="username" column="username" />
		<result property="password" column="password" />
		<result property="avatar" column="avatar" />
		<result property="intro" column="intro" />
		<result property="age" column="age" />
		<result property="sex" column="sex" />
		<result property="phone" column="phone" />
		<result property="email" column="email" />
		<result property="status" column="status" />
		<result property="createTime" column="create_time" />
		<result property="loginTime" column="login_time" />
		<result property="loginIp" column="login_ip" />
		<result property="loginCount" column="login_count" />
		<result property="isDel" column="is_del" />
	</resultMap>
	
	<!-- 公共查询sql片段 -->
	<sql id="select_sql">
		select * 
		from sys_user 
	</sql>
	
	<!-- 查 - 根据id -->
	<select id="getById" resultMap="model">
		<include refid="select_sql"></include>
		where id = #{id}
	</select>
	
	<!-- 查询，根据名称 -->
	<select id="getByUsername" resultMap="model">
		<include refid="select_sql"></include>
		where username = #{username}
		and is_del = 1
	</select>

	<!-- 查询，根据phone -->
	<select id="getByPhone" resultMap="model">
		<include refid="select_sql"></include>
		where phone = #{phone}
		and is_del = 1
	</select>
	
	<!-- 查询，根据email -->
	<select id="getByEmail" resultMap="model">
		<include refid="select_sql"></include>
		where email = #{email}
		and is_del = 1
	</select>
	
	<!-- 查 - 指定用户的真实密码  -->
	<select id="getPwById" resultType="string">
		select pw from sys_user 
		where id = #{id}
	</select>
	
	<!-- 查集合 - 根据条件（参数为空时代表忽略指定条件） -->
	<select id="getList" resultMap="model">
		<include refid="select_sql"></include>
		<where>
			<if test=' this.has("id") '> and id = #{id} </if>
			<if test=' this.has("username") '> and username like concat('%', #{username}, '%') </if>
			<if test=' this.has("sex") '> and sex = #{sex} </if>
			<if test=' this.has("phone") '> and phone = #{phone} </if>
			<if test=' this.has("email") '> and email = #{email} </if>
			<if test=' this.has("status") '> and status = #{status} </if>
			<if test=' this.has("isDel") '> and is_del = #{isDel} </if>
		</where>
		order by
		<choose>
			<when test='sortType == 1'> id desc </when>
			<when test='sortType == 11'> login_time desc </when>
			<when test='sortType == 13'> login_count desc </when>
			<otherwise> id desc </otherwise>
		</choose>
	</select>
	
	
	
	
	
	
	
	
	

</mapper>
