<?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.ics.system.mapper.DeptMapper">

	<resultMap type="com.ics.system.domain.Dept" id="DeptResult">
		<id     property="id"     	  column="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="leaderId"   column="leader_id"   />
		<result property="tenantId"   column="tenant_id"   />
		<result property="phone"      column="phone"       />
		<result property="email"      column="email"       />
		<result property="status"     column="status"      />
		<result property="isDefault"  column="is_default"  />
		<result property="parentName" column="parent_name" />
		<result property="version"    column="version"     />
        <result property="deleteFlag" column="delete_flag" />
		<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="tenantId"   column="tenant_id"   />
	</resultMap>
	
	<sql id="selectDeptVo">
		SELECT d.id,
			   d.parent_id,
			   d.ancestors,
			   d.dept_name,
			   d.order_num,
			   d.leader,
			   d.leader_id,
			   d.phone,
			   d.email,
			   d.status,
			   d.is_default,
		       d.tenant_id,
			   d.create_by,
			   d.create_time
		FROM sys_dept d
    </sql>
    
	<select id="selectRoleDeptTree" parameterType="Long" resultType="String">
		SELECT concat(d.id, d.dept_name) AS dept_name
		FROM sys_dept d
				 LEFT JOIN sys_role_dept rd ON d.id = rd.dept_id
		WHERE  rd.role_id = #{roleId}
		ORDER BY d.parent_id, d.order_num
	</select>
	
	<select id="selectDeptList" parameterType="com.ics.system.domain.Dept" resultMap="DeptResult">
        <include refid="selectDeptVo"/>
		<where>
			<if test="parentId != null and parentId != 0">
				AND parent_id = #{parentId}
			</if>
			<if test="deptName != null and deptName != ''">
				AND dept_name like concat('%', #{deptName}, '%')
			</if>
			<if test="status != null and status != ''">
				AND status = #{status}
			</if>
			<if test="ancestors != null and ancestors != ''">
                AND ancestors like concat('%', #{ancestors}, '%')
			</if>
		</where>
		ORDER BY order_num
    </select>
	
	<select id="checkDeptExistUser" parameterType="Long" resultType="int">
		SELECT COUNT(1) FROM sys_user WHERE id = #{id}
	</select>
	
	<select id="selectDeptCount" parameterType="com.ics.system.domain.Dept" resultType="int">
		SELECT COUNT(1) FROM sys_dept
		<where>
			<if test="id != null and id != 0"> AND id = #{id} </if>
			<if test="parentId != null and parentId != 0"> AND parent_id = #{parentId} </if>
		</where>
	</select>
	
	<select id="checkDeptNameUnique" resultMap="DeptResult">
	    <include refid="selectDeptVo"/>
		WHERE dept_name=#{deptName} AND parent_id = #{parentId}
	</select>

	<select id="selectDeptById" parameterType="Long" resultMap="DeptResult">
		SELECT d.id,
			   d.parent_id,
			   d.ancestors,
			   d.dept_name,
			   d.order_num,
			   d.leader,
			   d.leader_id,
			   d.phone,
			   d.email,
			   d.status,
			   d.is_default,
			   (SELECT dept_name FROM sys_dept WHERE id = d.parent_id) parent_name
		FROM sys_dept d
		WHERE d.id = #{id}
	</select>

	<select id="selectChildrenDeptById" parameterType="Long" resultMap="DeptResult">
		SELECT * FROM sys_dept
		<where>
			FIND_IN_SET(#{id},ancestors)
		</where>
	</select>

	<select id="selectRoleDeptIds" parameterType="Long" resultType="String">
		SELECT d.id
		FROM sys_dept d
				 LEFT JOIN sys_role_dept rd ON d.id = rd.dept_id
		WHERE rd.role_id = #{roleId}
		ORDER BY d.parent_id, d.order_num
	</select>

	<select id="findDefault" resultMap="DeptResult">
		<include refid="selectDeptVo"/>
		WHERE d.is_default = TRUE
	</select>
	
	<insert id="insertDept" parameterType="com.ics.system.domain.Dept" useGeneratedKeys="true" keyProperty="id">
 		insert into sys_dept(
 			<if test="id != null and id != 0">id,</if>
 			<if test="parentId != null and parentId != 0">parent_id,</if>
 			<if test="deptName != null and deptName != ''">dept_name,</if>
 			<if test="ancestors != null and ancestors != ''">ancestors,</if>
 			<if test="orderNum != null and orderNum != ''">order_num,</if>
 			<if test="leader != null and leader != ''">leader,</if>
 			<if test="leaderId != null">leader_id,</if>
		    <if test="tenantId != null">tenant_id,</if>
 			<if test="phone != null and phone != ''">phone,</if>
 			<if test="email != null and email != ''">email,</if>
 			<if test="status != null">status,</if>
			<if test="isDefault != null">is_default,</if>
 		    <if test="tenantId != null and tenantId != ''">tenant_id,</if>
 			<if test="createBy != null and createBy != ''">create_by,</if>
 			create_time
 		)values(
 			<if test="id != null and id != 0">#{id},</if>
 			<if test="parentId != null and parentId != 0">#{parentId},</if>
 			<if test="deptName != null and deptName != ''">#{deptName},</if>
 			<if test="ancestors != null and ancestors != ''">#{ancestors},</if>
 			<if test="orderNum != null and orderNum != ''">#{orderNum},</if>
 			<if test="leader != null and leader != ''">#{leader},</if>
 			<if test="leaderId != null">#{leaderId},</if>
		    <if test="tenantId != null">#{tenantId},</if>
 			<if test="phone != null and phone != ''">#{phone},</if>
 			<if test="email != null and email != ''">#{email},</if>
 			<if test="status != null">#{status},</if>
			<if test="isDefault != null">#{isDefault},</if>
		    <if test="tenantId != null">#{tenantId},</if>
 			<if test="createBy != null and createBy != ''">#{createBy},</if>
 			sysdate()
 		)
	</insert>
	
	<update id="updateDept" parameterType="com.ics.system.domain.Dept">
 		UPDATE sys_dept
 		<set>
 			<if test="parentId != null and parentId != 0">parent_id = #{parentId},</if>
 			<if test="deptName != null and deptName != ''">dept_name = #{deptName},</if>
 			<if test="ancestors != null and ancestors != ''">ancestors = #{ancestors},</if>
 			<if test="orderNum != null and orderNum != ''">order_num = #{orderNum},</if>
 			<if test="leader != null">leader = #{leader},</if>
 			<if test="leaderId != null">leader_id = #{leaderId},</if>
 			<if test="phone != null">phone = #{phone},</if>
 			<if test="email != null">email = #{email},</if>
 			<if test="status != null and status != ''">status = #{status},</if>
			<if test="isDefault != null ">is_default = #{isDefault},</if>
 			<if test="updateBy != null and updateBy != ''">update_by = #{updateBy},</if>
 			update_time = sysdate()
 		</set>
 		WHERE id = #{id}
	</update>

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

	<delete id="deleteDeptById" parameterType="Long">
		DELETE FROM sys_dept WHERE id = #{id}
	</delete>

	<update id="updateDeptStatus" parameterType="com.ics.system.domain.Dept">
 	    UPDATE sys_dept
 	    <set>
 	        <if test="status != null and status != ''">status = #{status},</if>
 	        <if test="updateBy != null and updateBy != ''">update_by = #{updateBy},</if>
 	        update_time = sysdate()
        </set>
 	    WHERE id IN (${ancestors})
	</update>

	<update id="clearDefault">
        UPDATE sys_dept SET is_default = FALSE WHERE is_default = TRUE
    </update>

    <!--查询子部门id-->
	<select id="selectChildrenIds" parameterType="String" resultMap="DeptResult">
		SELECT id FROM sys_dept WHERE ancestors LIKE concat('%', #{ancestors}, '%')
	</select>


	<!--查询对应的园区id-->
	<select id="selectParent" parameterType="Long" resultType="Long">
		SELECT id FROM sys_dept WHERE parent_id = 0 AND tenant_id = #{tenantId}
	</select>

</mapper> 