<?xml version="1.0" encoding="UTF-8"?>
<!--
  ~ Copyright (c) 2020 yifu4cloud Authors. All Rights Reserved.
  ~
  ~ Licensed under the Apache License, Version 2.0 (the "License");
  ~ you may not use this file except in compliance with the License.
  ~ You may obtain a copy of the License at
  ~
  ~     http://www.apache.org/licenses/LICENSE-2.0
  ~
  ~ Unless required by applicable law or agreed to in writing, software
  ~ distributed under the License is distributed on an "AS IS" BASIS,
  ~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  ~ See the License for the specific language governing permissions and
  ~ limitations under the License.
  -->

<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.yifu.cloud.plus.v1.yifu.admin.mapper.SysDeptMapper">
	<!-- 通用查询映射结果 -->
	<resultMap id="BaseResultMap" type="com.yifu.cloud.plus.v1.yifu.admin.api.entity.SysDept">
		<id column="dept_id" property="deptId"/>
		<result column="name" property="name"/>
		<result column="parent_id" property="parentId"/>
		<result column="dept_dn" property="deptDn"/>
		<result column="sort_order" property="sortOrder"/>
		<result column="create_by" property="createBy"/>
		<result column="create_time" property="createTime"/>
		<result column="update_by" property="updateBy"/>
		<result column="update_time" property="updateTime"/>
		<result column="del_flag" property="delFlag"/>
		<result column="project_no" property="projectNo"/>
		<result column="client" property="client"/>
		<result column="ancestors" property="ancestors"/>
	</resultMap>

	<select id="selectCountId" resultType="com.yifu.cloud.plus.v1.yifu.admin.api.entity.SysDept">
		SELECT
		*
		from sys_dept a
		<where>
			a.dept_id = #{deptId}
		</where>
	</select>

	<select id="selectDeptDn" resultType="com.yifu.cloud.plus.v1.yifu.admin.api.entity.SysDept">
		SELECT
		*
		from sys_dept a
		<where>
			a.dept_dn = #{deptDn}
		</where>
	</select>

	<select id="getDeptListByDeptId" resultType="com.yifu.cloud.plus.v1.yifu.admin.api.entity.SysDept">
		select
			dept_id as deptId,
			name as deptName
		from
			sys_dept
		where
			del_flag = '0'
		  and (dept_id = #{deptId} or ancestors like concat(#{deptIdSplit}, '%'))
	</select>

	<select id="getDeptListByDeptIdSet" resultType="com.yifu.cloud.plus.v1.yifu.admin.api.entity.SysDept">
		select
			dept_id as deptId,
			name as deptName
		from
			sys_dept
		where
			del_flag = '0'
		  <if test="departIdSet != null and departIdSet.size > 0">
			AND dept_id in
			<foreach item="idStr" index="index" collection="departIdSet" open="(" separator=","
					 close=")">
				#{idStr}
			</foreach>
		</if>
	</select>

	<select id="cspDeptList" resultType="com.yifu.cloud.plus.v1.yifu.admin.api.vo.SysDeptVo">
		select d.dept_id, d.name, d.parent_id,d.ancestors,sort_order
		from sys_dept d
		where d.del_flag = '0'
		<if test="dept.projectNo != null and dept.projectNo != ''">
			AND d.project_no = #{dept.projectNo}
		</if>
		<if test="dept.deptId != null and dept.deptId != 0">
			AND d.dept_id = #{dept.deptId}
		</if>
		<if test="dept.parentId != null and dept.parentId != 0">
			AND d.parent_id = #{dept.parentId}
		</if>
		<if test="dept.name != null and dept.name != ''">
			AND d.name like concat('%', #{dept.name}, '%')
		</if>
		order by d.sort_order asc,d.create_time desc
	</select>

	<select id="cspDeptUserList" resultType="com.yifu.cloud.plus.v1.yifu.admin.api.vo.SysDeptVo">
		select d.dept_id, d.name, d.parent_id,d.ancestors,sort_order,
		(select count(r.id) from sys_user_dept_permission r left join sys_user u on u.user_id = r.user_id where r.dept_id = d.dept_id and u.del_flag = '0') as peoplenum
		from sys_dept d
		where d.del_flag = '0'
		<if test="dept.projectNo != null and dept.projectNo != ''">
			AND d.project_no = #{dept.projectNo}
		</if>
		order by d.sort_order asc,d.create_time desc
	</select>

	<select id="cspDeptListByDeptIdSet" resultType="com.yifu.cloud.plus.v1.yifu.admin.api.vo.SysDeptVo">
		select d.dept_id, d.name, d.parent_id,d.ancestors,sort_order
		from sys_dept d
		where d.del_flag = '0'
		<if test="dept.projectNo != null and dept.projectNo != ''">
			AND d.project_no = #{dept.projectNo}
		</if>
		<if test="departIdSet != null and departIdSet.size > 0">
			AND d.dept_id in
			<foreach item="idStr" index="index" collection="departIdSet" open="(" separator=","
					 close=")">
				#{idStr}
			</foreach>
		</if>
		order by d.sort_order asc,d.create_time desc
	</select>

	<select id="checkDeptNameUnique" resultMap="BaseResultMap">
		select dept_id,name from sys_dept
		where project_no = #{projectNo} and name=#{deptName} and parent_id = #{parentId} and del_flag = '0'  limit 1
	</select>

	<select id="checkDeptExistUser" resultType="int">
		select count(1) from sys_user_dept_permission where dept_id = #{deptId}
	</select>

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

	<select id="cspDeptCount" resultType="int">
		select count(1) from sys_dept where del_flag = '0' and project_no = #{projectNo}
	</select>

	<update id="updateDeptById">
		update sys_dept
		<trim prefix="set" suffixOverrides=",">
			<if test="dept.name != null">name=#{dept.name},</if>
			<if test="dept.parentId != null">parent_id=#{dept.parentId},</if>
			<if test="dept.deptDn != null">dept_dn=#{dept.deptDn},</if>
			del_flag = '0',
			update_time = now()
		</trim>
		WHERE dept_id=#{dept.deptId}
	</update>


	<update id="batchUpdateSort">
		UPDATE sys_dept
		<trim prefix="SET" suffixOverrides=",">
			<trim prefix="sort_order = CASE" suffix="END,">
				<foreach collection="moveVos" item="m">
					WHEN dept_id = #{m.id} THEN #{m.sort}
				</foreach>
			</trim>
		</trim>
		WHERE dept_id IN
		<foreach collection="moveVos" item="m" open="(" separator="," close=")">
			#{m.id}
		</foreach>
	</update>

	<select id="cspDelDeptIds" resultType="com.yifu.cloud.plus.v1.yifu.admin.api.entity.SysDept">
		select dept_id from sys_dept where del_flag = '1' AND project_no = #{projectNo}
	</select>

	<update id="updateAllAncestors">
		update sys_dept   set  ancestors = REPLACE(ancestors,#{oldStr},#{newStr})
		where project_no = #{projectNo} and ancestors like concat(#{oldStr}, '%') and dept_id != #{deptId}
	</update>

</mapper>
