<?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.ygtx.system.mapper.SysUserDeptMapper">

	<resultMap type="SysUserDept" id="SysUserDeptResult">
		<result property="userId"     column="user_id"      />
		<result property="deptId"     column="dept_id"      />
		<result property="postId"     column="post_id"      />
		<result property="isMain"     column="is_main"      />
	</resultMap>
	
	<delete id="deleteUserDeptByUserId" parameterType="Long">
		delete from sys_user_dept where user_id = #{userId}
	</delete>
	
	<delete id="deleteUserDept" parameterType="Long">
		delete from sys_user_dept where user_id in
		<foreach collection="array" item="userId" open="(" separator="," close=")">
			#{userId}
		</foreach>
	</delete>
	
	<select id="selectCountUserDeptByDeptId" resultType="Integer">
	    select count(1) from sys_user_dept where dept_id = #{deptId}
	</select>
	
	<insert id="batchUserDept">
		insert into sys_user_dept(user_id, dept_id, post_id, is_main) values
		<foreach item="item" index="index" collection="list" separator=",">
			(#{item.userId},#{item.deptId},#{item.postId},#{item.isMain})
		</foreach>
	</insert>
	
	<select id="selectDeptListByUserId" parameterType="Long" resultType="Long">
		select dept_id from sys_user_dept where user_id = #{userId}
	</select>
	
	<select id="selectUserDeptListByUserId" parameterType="Long" resultMap="SysUserDeptResult">
		select user_id, dept_id, post_id, is_main from sys_user_dept where user_id = #{userId}
	</select>
	
	<select id="selectMainUserDeptByUserId" parameterType="Long" resultMap="SysUserDeptResult">
		select user_id, dept_id, post_id, is_main from sys_user_dept where user_id = #{userId} and is_main = '1'
	</select>
	
</mapper>