<?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.ruoyi.system.mapper.ApproveUserMapper">

    <resultMap type="ApproveUser" id="ApproveUserResult">
		<id     property="id"       column="id"      />
        <result property="userId"       column="user_id"      />
		<result property="loginId"       column="login_id"      />
        <result property="deptId"       column="dept_id"      />
        <result property="deptName"     column="dept_name"    />
    </resultMap>
	<sql id="selectApproveUserVo">
        select u.user_id,u.login_id u.dept_id, u.dept_name,
        d.dept_id, d.parent_id, d.ancestors, d.dept_name, d.order_num, d.leader, d.status as dept_status,
        r.role_id, r.role_name, r.role_key, r.role_sort, r.data_scope, r.status as role_status
        from approve_user u
		    left join sys_dept d on u.dept_id = d.dept_id
		    left join sys_user_role ur on u.user_id = ur.user_id
		    left join sys_role r on r.role_id = ur.role_id
    </sql>
    
    <select id="selectApproveUserList" parameterType="ApproveUser" resultMap="ApproveUserResult">
		select u.user_id,u.login_id u.dept_id, u.dept_name,
		from approve_user u
		left join sys_dept d on u.dept_id = d.dept_id
		where u.del_flag = '0'
		<if test="userId != null and userId != 0">
			AND u.user_id = #{userId}
		</if>

		<if test="deptId != null and deptId != 0">
			AND (u.dept_id = #{deptId} OR u.dept_id IN ( SELECT t.dept_id FROM sys_dept t WHERE find_in_set(#{deptId}, ancestors) ))
		</if>
	</select>
	<insert id="insertApproveUsers" parameterType="java.util.List">
		insert into approve_user(
		user_id,
		login_id,
		dept_id,
		dept_name
		)
		values
		<foreach collection="aus" item="au" index="index" separator="," >
			(
			#{au.userId},
			#{au.loginId},
			#{au.deptId},
			#{au.deptName}
			)
		</foreach>
	</insert>


	<update id="updateApproveUsers">
		<foreach collection="aus" item="aus" index="index">
			update  approve_user
			<trim prefix="SET" suffixOverrides=",">
				<if test="aus.userId != null and  aus.userId != ''">
					uesr_id = #{aus.userId},
				</if>
				<if test="aus.user != null and  aus.loginId != ''">
					login_id = #{aus.loginId},
				</if>
				<if test="aus.loginId != null and  aus.loginId != ''">
					dept_id = #{aus.deptId},
				</if>
				<if test="aus.loginId != null and  aus.loginId != ''">
					dept_name = #{aus.deptName}
				</if>
			</trim>
			where
			deptId=#{aus.deptId}
		</foreach>
	</update>
	<select id="findApproveUser" resultType="java.lang.Long">
		select user_id from approve_user
	</select>
	<select id="findApproveUserByDeptId" resultType="java.lang.Long">
		select user_id from approve_user where dept_id=#{deptId}
	</select>
	<select id="deleteApproveUserByUserId" parameterType="Long">
		delete from approve_user where user_id = #{userId}
	</select>
	<select id="deleteApproveUserByDeptId" parameterType="Long">
		delete from approve_user where dept_id = #{deptId}
	</select>
	<select id="deleteApproveUser" >
		delete from approve_user
	</select>

</mapper> 