<?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.boot.security.server.dao.UserDao">

	<sql id="where">
		<where>
			<if test="params.username != null and params.username != ''">
				and t.username like concat('%', #{params.username}, '%')
			</if>
			<if test="params.nickname != null and params.nickname != ''">
				and t.nickname like concat('%', #{params.nickname}, '%')
			</if>
			<if test="params.status != null and params.status != ''">
				and t.status = #{params.status}
			</if>
		</where>
	</sql>

	<select id="count" resultType="int">
		select count(1) from sys_user t
		<include refid="where" />
	</select>

	<select id="list" resultType="SysUser">
		select * from sys_user t
		<include refid="where" />
		${params.orderBy}
        limit #{offset}, #{limit}
	</select>

	<insert id="saveUserRoles">
		insert into sys_role_user(roleId, userId) values
		<foreach collection="roleIds" item="roleId" separator=",">
			(#{roleId}, #{userId})
		</foreach>
	</insert>

	<update id="update">
		update sys_user t
		<set>
			<if test="username != null">
				username = #{username},
			</if>
			<if test="nickname != null">
				nickname = #{nickname},
			</if>
			<if test="headImgUrl != null">
				headImgUrl = #{headImgUrl},
			</if>
			<if test="phone != null">
				phone = #{phone},
			</if>
			<if test="telephone != null">
				telephone = #{telephone},
			</if>
			<if test="email != null">
				email = #{email},
			</if>
			<if test="birthday != null">
				birthday = #{birthday},
			</if>
			<if test="sex != null">
				sex = #{sex},
			</if>
			<if test="status != null">
				status = #{status},
			</if>
			updateTime = #{updateTime}
		</set>
		where t.id = #{id}
	</update>
</mapper>