<?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="net.chenlin.dp.modules.sys.dao.SysUserMapper">
	
	<select id="listForPage" resultType="net.chenlin.dp.modules.sys.entity.SysUserEntity">
		SELECT
			u.user_id,
			u.org_id,
			(
				SELECT 
					name 
				FROM 
					sys_org o 
				WHERE 
					o.org_id = u.org_id
			) AS orgName,
			u.username,
			u.email,
			u.mobile,
			u.status,
			u.remark,
			u.gmt_create
		FROM
			sys_user u
		<where>
			<if test="userIdCreate != null">
				AND u.user_id_create = #{userIdCreate} 
			</if>
			<if test="username != null and username.trim() != ''">
				AND u.username LIKE concat('%',#{username},'%') 
				OR u.email LIKE concat('%',#{username},'%') 
				OR u.mobile LIKE concat('%',#{username},'%') 
			</if>
		</where>
		ORDER BY 
			u.user_id DESC
	</select>
	
	<select id="getByUserName" resultType="net.chenlin.dp.modules.sys.entity.SysUserEntity">
		SELECT
			user_id,
			username,
			password,
			email,
			mobile,
			status
		FROM
			sys_user
		WHERE
			username = #{username}
	</select>
	
	<select id="listAllMenuId" resultType="long">
		SELECT DISTINCT
			rm.menu_id
		FROM
			sys_user_role ur
		LEFT JOIN sys_role_menu rm ON ur.role_id = rm.role_id
		WHERE
			ur.user_id = #{userId}
	</select>
	
	<select id="listAllOrgId" resultType="long">
		SELECT DISTINCT
			ro.org_id
		FROM
			sys_role_org ro
		LEFT JOIN sys_user_role ur ON ro.role_id = ur.role_id
		WHERE
			ur.user_id = #{userId}
	</select>
	
	<insert id="save" parameterType="net.chenlin.dp.modules.sys.entity.SysUserEntity" useGeneratedKeys="true" keyProperty="userId">
		INSERT INTO sys_user (
			username,
			password,
			email,
			mobile,
			status,
			user_id_create,
			org_id,
			gmt_create
		)
		VALUES (
			#{username},
			#{password},
			#{email},
			#{mobile},
			#{status},
			#{userIdCreate},
			#{orgId},
			NOW()
		)
	</insert>
	
	<select id="getObjectById" resultType="net.chenlin.dp.modules.sys.entity.SysUserEntity">
		SELECT
			u.user_id,
			u.org_id,
			(
				SELECT 
					name 
				FROM 
					sys_org o 
				WHERE 
					o.org_id = u.org_id
			) AS orgName,
			u.username,
			u.password,
			u.email,
			u.mobile,
			u.remark,
			u.status
		FROM 
			sys_user u
		WHERE
			u.user_id = #{value}
	</select>
	
	<update id="update">
		UPDATE sys_user
		<set> 
			<if test="username != null">username = #{username}, </if>
			<if test="email != null">email = #{email}, </if>
			<if test="mobile != null">mobile = #{mobile}, </if>
			<if test="status != null">status = #{status}, </if>
			<if test="orgId != null">org_id = #{orgId}, </if>
			<if test="remark != null">remark = #{remark}, </if>
			gmt_modified = NOW()
		</set>
		where user_id = #{userId}
	</update>
	
	<delete id="batchRemove">
		DELETE
		FROM
			sys_user
		WHERE
			user_id IN
		<foreach item="id" collection="array" open="(" separator="," close=")">
			#{id}
		</foreach>
	</delete>
	
	<update id="updatePswdByUser">
		UPDATE sys_user
		SET
			password = #{newPswd}
		WHERE
			user_id = #{userId} AND password = #{pswd}
	</update>
	
	<update id="updateUserStatus">
		UPDATE sys_user
		SET
			status = #{status}
		WHERE
			user_id IN
		<foreach item="userId" collection="id" open="(" separator="," close=")">
			#{userId}
		</foreach> 
	</update>
	
	<update id="updatePswd">
		UPDATE sys_user
		SET
			password = #{password}
		WHERE
			user_id = #{userId}
	</update>

</mapper>