<?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.SysDeptMapper">

	<resultMap type="com.ruoyi.common.web.domain.entity.SysDept" id="SysDeptResult">
		<id     property="deptId"     column="dept_id"     />
		<result property="parentId"   column="parent_id"   />
		<result property="ancestors"  column="ancestors"   />
		<result property="deptName"   column="dept_name"   />
		<result property="orderNum"   column="order_num"   />
		<result property="leader"     column="leader"      />
		<result property="phone"      column="phone"       />
		<result property="email"      column="email"       />
		<result property="status"     column="status"      />
		<result property="parentName" column="parent_name" />
		<result property="createBy"   column="create_by"   />
		<result property="createTime" column="create_time" />
		<result property="updateBy"   column="update_by"   />
		<result property="updateTime" column="update_time" />
		<result property="delFlag"    column="del_flag"    />
		<result property="remark"     column="remark"      />
	</resultMap>

	<sql id="selectDeptVo">
        SELECT d.dept_id, d.parent_id, d.ancestors, d.dept_name, d.order_num, d.leader, d.phone, d.email, d.status, d.create_by, d.create_time, d.del_flag
        FROM sys_dept d
    </sql>

	<select id="selectDeptList" parameterType="com.ruoyi.common.web.domain.entity.SysDept" resultMap="SysDeptResult">
		<include refid="selectDeptVo"/>
		WHERE d.del_flag = #{delFlag}
		<if test="sysDept.deptId != null and sysDept.deptId != 0">
			AND dept_id = #{sysDept.deptId}
		</if>
		<if test="sysDept.parentId != null and sysDept.parentId != 0">
			AND parent_id = #{sysDept.parentId}
		</if>
		<if test="sysDept.deptName != null and sysDept.deptName != ''">
			AND dept_name LIKE concat('%', #{sysDept.deptName}, '%')
		</if>
		<if test="sysDept.status != null and sysDept.status != ''">
			AND status = #{sysDept.status}
		</if>
		<!-- 数据权限过滤 -->
		${sysDept.dataScopeSql}
		ORDER BY d.parent_id, d.order_num
	</select>

	<select id="selectDeptListByRoleId" resultType="java.lang.Long">
		SELECT d.dept_id
		FROM sys_dept d
		LEFT JOIN sys_role_dept rd ON d.dept_id = rd.dept_id
		where d.del_flag = #{delFlag}
		AND rd.role_id = #{roleId}
		<if test="deptCheckStrictly">
			AND d.dept_id NOT IN
				(
					SELECT d.parent_id
					FROM sys_dept d
					INNER JOIN sys_role_dept rd ON d.dept_id = rd.dept_id
					AND d.del_flag = #{delFlag}
					AND rd.role_id = #{roleId}
				)
		</if>
		ORDER BY d.parent_id, d.order_num
	</select>

	<select id="selectChildrenDeptById" parameterType="java.lang.Long" resultMap="SysDeptResult">
		SELECT *
		FROM sys_dept
		WHERE find_in_set(#{deptId}, ancestors)
	</select>

	<select id="selectNormalChildrenDeptById" resultType="int">
		SELECT COUNT(*)
		FROM sys_dept
		WHERE status = #{status}
		AND del_flag = #{delFlag}
		AND find_in_set(#{deptId}, ancestors)
	</select>

	<update id="updateDeptChildren" parameterType="java.util.List">
		UPDATE sys_dept
		SET ancestors =
		<foreach collection="depts" item="item" index="index" separator=" " open="case dept_id" close="end">
			WHEN #{item.deptId} THEN #{item.ancestors}
		</foreach>
		WHERE dept_id IN
		<foreach collection="depts" item="item" index="index" separator="," open="(" close=")">
			#{item.deptId}
		</foreach>
	</update>

	<update id="updateDeptStatusNormal" parameterType="java.lang.Long">
		UPDATE sys_dept
		SET status = #{status}
		WHERE dept_id IN
		<foreach collection="deptIds" item="deptId" open="(" separator="," close=")">
			#{deptId}
		</foreach>
	</update>

</mapper>