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

	<resultMap type="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="delFlag"    column="del_flag"    />
		<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="orgGuid"   column="org_guid"  />
		<result property="parentGuid"       column="parent_guid"       />
		<result property="allPathName"     column="all_path_name"    />
		<result property="rankCode"   column="rank_code"  />
		<result property="rankName"       column="rank_name"       />
		<result property="originalSort"       column="original_sort"       />
		<result property="customsCode"       column="customs_code"       />
		<result property="innerSort"       column="inner_sort"       />
		<result property="orgClass"       column="org_class"       />
		<result property="globalSort"       column="global_sort"       />
		<result property="orgType"       column="org_type"       />
		<result property="orgCategory"       column="org_category"       />
        <result property="useFlag" column="use_flag" />
	</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.del_flag, d.create_by, d.create_time ,
               d.org_guid,d.parent_guid,d.all_path_name,d.rank_code,d.rank_name,d.customs_code,d.org_type,d.org_category, d.org_class,d.original_sort, d.use_flag
        from sys_dept d
    </sql>
    
	<select id="selectDeptList" parameterType="SysDept" resultMap="SysDeptResult">
        <include refid="selectDeptVo"/>
        where d.del_flag = '0'
		<if test="deptId != null and deptId != 0">
			AND dept_id = #{deptId}
		</if>
        <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="orgType != null and orgType != ''">
			AND org_type = #{orgType}
		</if>
		<if test="customsCode != null and customsCode != ''">
			AND customs_code = #{customsCode}
		</if>
		<if test="orgCategory != null and orgCategory != ''">
			AND org_category = #{orgCategory}
		</if>
		<if test="originalSort != null and originalSort != ''">
			AND original_sort like concat( #{originalSort}, '%')
		</if>
		<if test="ancestors != null and ancestors != ''">
			AND ancestors like concat( #{ancestors}, '%')
		</if>

		<!-- 数据范围过滤 order by d.original_sort-->
	<!--  ${params.dataScope}  -->

</select>

<select id="selectDeptListByRoleId" resultType="Long">
    select d.dept_id
    from sys_dept d
        left join sys_role_dept rd on d.dept_id = rd.dept_id
    where 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 rd.role_id = #{roleId})
        </if>
    order by d.parent_id, d.order_num
</select>

<select id="selectDeptById" parameterType="Long" resultMap="SysDeptResult">
    select d.*,
        (select dept_name from sys_dept where dept_id = d.parent_id) parent_name
    from sys_dept d
    where d.dept_id = #{deptId}
</select>

<select id="checkDeptExistUser" parameterType="Long" resultType="int">
    select count(1) from sys_user where dept_id = #{deptId} and del_flag = '0'
</select>

<select id="hasChildByDeptId" parameterType="Long" resultType="int">
    select count(1) from sys_dept
    where del_flag = '0' and parent_id = #{deptId} limit 1
</select>

<select id="selectChildrenDeptById" parameterType="Long" resultMap="SysDeptResult">
    select * from sys_dept where find_in_set(#{deptId}, ancestors)
</select>

<select id="selectNormalChildrenDeptById" parameterType="Long" resultType="int">
    select count(*) from sys_dept where status = 0 and del_flag = '0' and find_in_set(#{deptId}, ancestors)
</select>

<select id="checkDeptNameUnique" resultMap="SysDeptResult">
    <include refid="selectDeptVo"/>
    where dept_name=#{deptName} and parent_id = #{parentId} and del_flag = '0' limit 1
</select>

<insert id="insertDept" parameterType="SysDept">
     insert into sys_dept(
         <if test="deptId != null and deptId != 0">dept_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">order_num,</if>
         <if test="leader != null and leader != ''">leader,</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="createBy != null and createBy != ''">create_by,</if>
        <if test="delFlag != null and delFlag != ''">del_flag</if>

        <if test="orgGuid != null and orgGuid != ''">org_guid,</if>
        <if test="parentGuid != null and parentGuid != ''">parent_guid,</if>
        <if test="allPathName != null and allPathName != ''">all_path_name,</if>
        <if test="rankCode != null and rankCode != ''">rank_code,</if>
        <if test="rankName != null and rankName != ''">rank_name,</if>
        <if test="originalSort != null and originalSort != ''">original_sort,</if>
        <if test="customsCode != null and customsCode != ''">customs_code,</if>
        <if test="innerSort != null and innerSort != ''">inner_sort,</if>
        <if test="orgClass != null and orgClass != ''">org_class,</if>
        <if test="globalSort != null and globalSort != ''">global_sort,</if>
        <if test="orgType != null and orgType != ''">org_type,</if>
        <if test="h4aCreateTime != null and h4aCreateTime != ''">h4a_create_time,</if>
        <if test="modifyTime != null and modifyTime != ''">modify_time,</if>
        <if test="orgCategory != null and orgCategory != ''">org_category,</if>

         create_time,use_flag
     )values(
         <if test="deptId != null and deptId != 0">#{deptId},</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">#{orderNum},</if>
         <if test="leader != null and leader != ''">#{leader},</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="createBy != null and createBy != ''">#{createBy},</if>
        <if test="delFlag != null and delFlag != ''">#{delFlag}</if>


        <if test="orgGuid != null and orgGuid != ''">#{orgGuid},</if>
        <if test="parentGuid != null and parentGuid != ''">#{parentGuid},</if>
        <if test="allPathName != null and allPathName != ''">#{allPathName},</if>
        <if test="rankCode != null and rankCode != ''">#{rankCode},</if>
        <if test="rankName != null and rankName != ''">#{rankName},</if>
        <if test="originalSort != null and originalSort != ''">#{originalSort},</if>
        <if test="customsCode != null and customsCode != ''">#{customsCode},</if>
        <if test="innerSort != null and innerSort != ''">#{innerSort},</if>
        <if test="orgClass != null and orgClass != ''">#{orgClass},</if>
        <if test="globalSort != null and globalSort != ''">#{globalSort},</if>
        <if test="orgType != null and orgType != ''">#{orgType},</if>
        <if test="h4aCreateTime != null and h4aCreateTime != ''">#{h4aCreateTime},</if>
        <if test="modifyTime != null and modifyTime != ''">#{modifyTime},</if>
        <if test="orgCategory != null and orgCategory != ''">#{orgCategory},</if>

         sysdate(),0
     )
</insert>

<update id="updateDept" parameterType="SysDept">
     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">order_num = #{orderNum},</if>
         <if test="leader != null">leader = #{leader},</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="updateBy != null and updateBy != ''">update_by = #{updateBy},</if>
        <if test="delFlag != null and delFlag != ''">del_flag = #{delFlag},</if>

        <if test="orgGuid != null and orgGuid != ''">org_guid = #{orgGuid},</if>
        <if test="parentGuid != null and parentGuid != ''">parent_guid = #{parentGuid},</if>
        <if test="allPathName != null and allPathName != ''">all_path_name = #{allPathName},</if>
        <if test="rankCode != null and rankCode != ''">rank_code = #{rankCode},</if>
        <if test="rankName != null and rankName != ''">rank_name = #{rankName},</if>
        <if test="originalSort != null and originalSort != ''">original_sort = #{originalSort},</if>
        <if test="customsCode != null and customsCode != ''">customs_code = #{customsCode},</if>
        <if test="innerSort != null and innerSort != ''">inner_sort = #{innerSort},</if>
        <if test="orgClass != null and orgClass != ''">org_class = #{orgClass},</if>
        <if test="globalSort != null and globalSort != ''">global_sort = #{globalSort},</if>
        <if test="orgType != null and orgType != ''">org_type = #{orgType},</if>
        <if test="h4aCreateTime != null and h4aCreateTime != ''">h4a_create_time = #{h4aCreateTime},</if>
        <if test="modifyTime != null and modifyTime != ''">modify_time = #{modifyTime},</if>
        <if test="orgCategory != null and orgCategory != ''">org_category = #{orgCategory},</if>

         update_time = sysdate()
     </set>
     where dept_id = #{deptId}
</update>

<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="Long">
     update sys_dept set status = '0' where dept_id in
     <foreach collection="array" item="deptId" open="(" separator="," close=")">
        #{deptId}
    </foreach>
</update>

<delete id="deleteDeptById" parameterType="Long">
    update sys_dept set del_flag = '2' where dept_id = #{deptId}
</delete>

<update id="updateDelFlag">
    update sys_dept  set del_flag = '2'
</update>
<!-- 机构同步时，先获取所有机构  -->
	<select id="selectAllDept" resultMap="SysDeptResult">
		select * from sys_dept
	</select>

	<!-- 机构同步完成后处理父机构 Long id  -->
	<update id="updateAllDeptId" >
		update sys_dept d set d.parent_id= nvl((select dept_id from sys_dept c where d.parent_guid = c.org_guid),0)  ,
		d.ancestors = '0'
	</update>

	<!-- 更新祖级列表 -->
	<update id="updateAllAncestors" parameterType="int" >
		UPDATE sys_dept d SET ancestors = concat(nvl((select ancestors from sys_dept s where s.dept_id = d.parent_id ),0), ',', d.dept_id)
		WHERE length(ORIGINAL_SORT) = #{sortLen}
	</update>

	<select id="selectDeptByCode" parameterType="String" resultMap="SysDeptResult">
		select * from sys_dept where customs_code = #{code} and org_type in(1,2)
	</select>
	<select id="selectDeptByName" parameterType="String" resultMap="SysDeptResult">
		select * from sys_dept where dept_name = #{name}
	</select>

	<!-- 获取用户所属单位及下属部门-->
	<select id="userDept" parameterType="long" resultMap="SysDeptResult">
		select * from sys_dept where ( dept_id = #{deptId} or parent_id = #{deptId}) and del_flag = 0
		                         and org_category = 9 and use_flag = 0
	</select>
	<!-- 获取用户所属单位及下属部门 包含父级本单位	-->
	<select id="selectOrg" parameterType="long" resultMap="SysDeptResult">
		select * from sys_dept where org_category != 9 and del_flag = 0 and use_flag = 0
		<if test="orgId != null and orgId != ''">
			and ( dept_id = #{orgId} or parent_id = #{orgId})
		</if>
	</select>

	<select id="userDeptOrg" parameterType="long" resultMap="SysDeptResult">
		select * from sys_dept where ( dept_id = #{deptId} or (parent_id = #{deptId} and org_category = 9)) and del_flag = 0 and use_flag = 0
	</select>


	<!-- 获取用户所属单位 -->
	<select id="getUserOrg"  parameterType="String" resultMap="SysDeptResult">
		select * from sys_dept where dept_id in
		<foreach collection="array" item="deptId" open="(" separator="," close=")">
			#{deptId}
		</foreach>
		 and org_category != 9  and del_flag = 0 order by ancestors desc
	</select>

    <update id="changeUseFlag"   >
        update sys_dept set use_flag = #{useFlag} where find_in_set(#{deptId}, ancestors) and del_flag = 0
    </update>
</mapper>