<?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.bootdo.system.dao.UserDao">

    <select id="get" resultType="com.bootdo.system.domain.UserDO">
		select `user_id`,`username`,`name`,`password`,`dept_id`,`email`,`mobile`,`status`,`user_id_create`,`gmt_create`,`gmt_modified`,`sex`,`birth`,`pic_id`,`live_address`,`hobby`,`province`,`city`,`district`,`post`,`duty` from sys_user where user_id = #{value}
	</select>

    <select id="list" resultType="com.bootdo.system.domain.UserDO" >
        select
        `user_id`,`username`,`name`,`password`,`dept_id`,`email`,`mobile`,`status`,`user_id_create`,`gmt_create`,`gmt_modified`,`sex`,`birth`,`pic_id`,`live_address`,`hobby`,`province`,`city`,`district`,`post`,`duty`
        from sys_user as us
        <where>
            <if test="userId != null and userId != ''">and us.user_id = #{userId}</if>
            <if test="username != null and username != ''">and us.username = #{username}</if>
            <if test="name != null and name != ''">and us.name = #{name}</if>
            <if test="password != null and password != ''">and us.password = #{password}</if>
            <if test="deptId != null and deptId != ''">and us.dept_id = #{deptId}</if>
            <if test="deptIds != null and deptIds.size() > 0">and us.dept_id in
                <foreach collection="deptIds" item="item" index="index" separator="," open="(" close=")">
                    #{item}
                </foreach>
            </if>
            <if test="email != null and email != ''">and us.email = #{email}</if>
            <if test="mobile != null and mobile != ''">and us.mobile = #{mobile}</if>
            <if test="status != null and status != ''">and us.status = #{status}</if>
            <if test="userIdCreate != null and userIdCreate != ''">and us.user_id_create = #{userIdCreate}</if>
            <if test="gmtCreate != null and gmtCreate != ''">and us.gmt_create = #{gmtCreate}</if>
            <if test="gmtModified != null and gmtModified != ''">and us.gmt_modified = #{gmtModified}</if>
            <if test="sex != null and sex != ''">and us.sex = #{sex}</if>
            <if test="birth != null and birth != ''">and us.birth = #{birth}</if>
            <if test="picId != null and picId != ''">and us.pic_id = #{picId}</if>
            <if test="liveAddress != null and liveAddress != ''">and us.live_address = #{liveAddress}</if>
            <if test="hobby != null and hobby != ''">and us.hobby = #{hobby}</if>
            <if test="province != null and province != ''">and us.province = #{province}</if>
            <if test="city != null and city != ''">and us.city = #{city}</if>
            <if test="district != null and district != ''">and us.district = #{district}</if>
            <if test="roleType != null and roleType == 1">and NOT EXISTS ( SELECT user_id FROM sys_user_role WHERE user_id = us.user_id )</if>
            <if test="roleType != null and roleType == 2">and EXISTS ( SELECT user_id FROM sys_user_role WHERE user_id = us.user_id )</if>
        </where>
        <choose>
            <when test="sort != null and sort.trim() != ''">
                order by ${sort} ${order}
            </when>
            <otherwise>
                order by user_id desc
            </otherwise>
        </choose>
        <if test="offset != null and limit != null">
            limit #{offset}, #{limit}
        </if>
    </select>

    <select id="count" resultType="int">
        select count(*) from sys_user as us
        <where>
            <if test="userId != null and userId != ''">and us.user_id = #{userId}</if>
            <if test="username != null and username != ''">and us.username = #{username}</if>
            <if test="name != null and name != ''">and us.name = #{name}</if>
            <if test="password != null and password != ''">and us.password = #{password}</if>
            <if test="deptId != null and deptId != ''">and us.dept_id = #{deptId}</if>
            <if test="email != null and email != ''">and us.email = #{email}</if>
            <if test="mobile != null and mobile != ''">and us.mobile = #{mobile}</if>
            <if test="status != null and status != ''">and us.status = #{status}</if>
            <if test="userIdCreate != null and userIdCreate != ''">and us.user_id_create = #{userIdCreate}</if>
            <if test="gmtCreate != null and gmtCreate != ''">and us.gmt_create = #{gmtCreate}</if>
            <if test="gmtModified != null and gmtModified != ''">and us.gmt_modified = #{gmtModified}</if>
            <if test="sex != null and sex != ''">and us.sex = #{sex}</if>
            <if test="birth != null and birth != ''">and us.birth = #{birth}</if>
            <if test="picId != null and picId != ''">and us.pic_id = #{picId}</if>
            <if test="liveAddress != null and liveAddress != ''">and us.live_address = #{liveAddress}</if>
            <if test="hobby != null and hobby != ''">and us.hobby = #{hobby}</if>
            <if test="province != null and province != ''">and us.province = #{province}</if>
            <if test="city != null and city != ''">and us.city = #{city}</if>
            <if test="district != null and district != ''">and us.district = #{district}</if>
            <if test="roleType != null and roleType == 1">and NOT EXISTS ( SELECT user_id FROM sys_user_role WHERE user_id = us.user_id )</if>
            <if test="roleType != null and roleType == 2">and EXISTS ( SELECT user_id FROM sys_user_role WHERE user_id = us.user_id )</if>
        </where>
    </select>

    <insert id="save" parameterType="com.bootdo.system.domain.UserDO" useGeneratedKeys="true" keyProperty="userId">
		insert into sys_user
		(
			`username`, 
			`name`, 
			`password`, 
			`dept_id`, 
			`email`, 
			`mobile`, 
			`status`, 
			`user_id_create`, 
			`gmt_create`, 
			`gmt_modified`, 
			`sex`, 
			`birth`, 
			`pic_id`, 
			`live_address`, 
			`hobby`, 
			`province`, 
			`city`, 
			`district`,
            `post`,
            `duty`
		)
		values
		(
			#{username}, 
			#{name}, 
			#{password}, 
			#{deptId}, 
			#{email}, 
			#{mobile}, 
			#{status}, 
			#{userIdCreate}, 
			#{gmtCreate}, 
			#{gmtModified}, 
			#{sex}, 
			#{birth}, 
			#{picId}, 
			#{liveAddress}, 
			#{hobby}, 
			#{province}, 
			#{city}, 
			#{district},
            #{post},
            #{duty}
		)
	</insert>

    <update id="update" parameterType="com.bootdo.system.domain.UserDO">
        update sys_user
        <set>
            <if test="username != null">`username` = #{username},</if>
            <if test="name != null">`name` = #{name},</if>
            <if test="password != null">`password` = #{password},</if>
            <if test="deptId != null">`dept_id` = #{deptId},</if>
            <if test="email != null">`email` = #{email},</if>
            <if test="mobile != null">`mobile` = #{mobile},</if>
            <if test="status != null">`status` = #{status},</if>
            <if test="userIdCreate != null">`user_id_create` = #{userIdCreate},</if>
            <if test="gmtCreate != null">`gmt_create` = #{gmtCreate},</if>
            <if test="gmtModified != null">`gmt_modified` = #{gmtModified},</if>
            <if test="sex != null">`sex` = #{sex},</if>
            <if test="birth != null">`birth` = #{birth},</if>
            <if test="picId != null">`pic_id` = #{picId},</if>
            <if test="liveAddress != null">`live_address` = #{liveAddress},</if>
            <if test="hobby != null">`hobby` = #{hobby},</if>
            <if test="province != null">`province` = #{province},</if>
            <if test="city != null">`city` = #{city},</if>
            <if test="district != null">`district` = #{district},</if>
            <if test="post != null">`post` = #{post},</if>
            <if test="duty != null">`duty` = #{duty}</if>
        </set>
        where user_id = #{userId}
    </update>

    <delete id="remove">
		delete from sys_user where user_id = #{value}
	</delete>

    <delete id="batchRemove">
        delete from sys_user where user_id in
        <foreach item="userId" collection="array" open="(" separator="," close=")">
            #{userId}
        </foreach>
    </delete>

    <select id="listAllDept" resultType="long">
				select DISTINCT dept_id from sys_user
	</select>

	<!--根据第三同步标识符和用户名称查询-->
	<select id="getByHobbyAndUserName"  resultType="com.bootdo.system.domain.UserDO" >
	    select
        `user_id`,`username`,`name`,`password`,`dept_id`,`email`,`mobile`,`status`,`user_id_create`,`gmt_create`,`gmt_modified`,`sex`,`birth`,`pic_id`,`live_address`,`hobby`,`province`,`city`,`district`,`post`,`duty`
        from sys_user
        <where>
            <if test="username != null and username != ''">and username = #{username}</if>
            <if test="hobby != null and hobby != ''">and hobby = #{hobby}</if>
            <if test="status != null and status != ''">and status = #{status}</if>
        </where>

	 </select>
</mapper>