<?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="org.jeecg.modules.system.mapper.SysDepartMapper">

	<select id="findAll" resultType="org.jeecg.modules.system.entity.SysDepart">
		SELECT
			*
		FROM
			sys_depart
		WHERE
			del_flag = 0
			AND org_type = '2'
	</select>


    <select id="queryUserDeparts" parameterType="String" resultType="org.jeecg.modules.system.entity.SysDepart">
		select a.* from sys_depart a
			join sys_user_depart b on a.id = b.dep_id
			where b.user_id = #{userId}
	</select>
    <select id="getAllDepart" resultType="org.jeecg.modules.system.entity.SysDepart">
		SELECT * FROM sys_depart where del_flag=0 ORDER BY org_type ASC
	</select>
    <insert id="addDepart">
		INSERT INTO sys_depart (
			id,
			parent_id,
			depart_name,
			depart_name_en,
			depart_name_abbr,
			depart_order,
			description,
			org_type,
			org_code,
			mobile,
			fax,
			address,
			memo,
			status,
			del_flag,
			create_by,
			create_time,
			update_by,
			update_time,
			pk_personnel
			)
		VALUES
			(
			#{id},
			#{parentId},
			#{departName},
			#{departNameEn},
			#{departNameAbbr},
			#{departOrder},
			#{description},
			#{orgType},
			#{orgCode},
			#{mobile},
			#{fax},
			#{address},
			#{memo},
			#{status},
			#{delFlag},
			#{createBy},
			#{createTime},
			#{updateBy},
			#{updateTime},
			#{pkPersonnel}
			)
	</insert>

	<update id="updateDepart">
		UPDATE sys_depart set
		  	parent_id = #{parentId},
			depart_name = #{departName},
			depart_order =#{departOrder},
			mobile =#{mobile},
			fax =#{fax},
			address =#{address},
			memo =#{memo},
			update_by =#{updateBy},
			update_time =#{updateTime},
			pk_personnel =#{pkPersonnel}
			<!--depart_name_en =#{departNameEn},
			depart_name_abbr =#{departNameAbbr},
			description =#{description},
			org_type =#{orgType},
			org_code =#{orgCode},
			status =#{status},
			del_flag =#{delFlag},
			create_by =#{createBy},
			create_time =#{createTime},-->
		where id =#{id}
	</update>

	<select id="getByType" resultType="org.jeecg.modules.system.entity.SysDepart">
		select * from sys_depart where org_type=#{id} and parent_id <![CDATA[ <> ]]> 'fe33687fb0a645ccb71a2873460a3d7d'
	</select>

	<select id="getByOrgId" resultType="org.jeecg.modules.system.entity.SysDepart">
		select * from sys_depart where parent_id=#{0}
	</select>

	<select id="getBatchByPerId" resultType="org.jeecg.modules.system.entity.SysDepart">
		SELECT
		d.id,d.depart_name
		FROM
			sys_depart d
			LEFT JOIN fd_personnel p ON d.id = p.pk_depart
		WHERE
			d.parent_id <![CDATA[ <> ]]> 'fe33687fb0a645ccb71a2873460a3d7d'
			AND
			p.id in
		<foreach item="per" index="index" collection="list"
				 open="(" separator="," close=")">
			#{per.id}
		</foreach>
		GROUP BY
			d.depart_name,d.id
	</select>

	<select id="getBatchByPerIdAndOrgId" resultType="org.jeecg.modules.system.entity.SysDepart" parameterType="java.util.List">
		SELECT
		d.id,d.depart_name
		FROM
		sys_depart d
		LEFT JOIN fd_personnel p ON d.id = p.pk_depart
		WHERE
		d.parent_id = '9e16e9ef42db4f77899cb7ec4a72cbab'
		AND
		d.depart_name not like '%苏州%'
		AND
		d.depart_name not like '%郑州%'
		AND
		d.depart_name not like '%临床%'
		AND
		p.id in
		<foreach item="per" index="index" collection="list"
				 open="(" separator="," close=")">
			#{per.id}
		</foreach>
		GROUP BY
		d.depart_name,d.id
	</select>

	<select id="getBatchByPerIdSuZhou" resultType="org.jeecg.modules.system.entity.SysDepart" parameterType="java.util.List">
		SELECT
		d.id,d.depart_name
		FROM
		sys_depart d
		LEFT JOIN fd_personnel p ON d.id = p.pk_depart
		WHERE
		d.parent_id = 'ea429f286aa041a9876dede367d0c7e5'
		AND
		d.depart_name not like '%临床%'
		AND
		p.id in
		<foreach item="per" index="index" collection="list"
				 open="(" separator="," close=")">
			#{per.id}
		</foreach>
		GROUP BY
		d.depart_name,d.id
	</select>

	<select id="getBatchByPerIdZhengZhou" resultType="org.jeecg.modules.system.entity.SysDepart" parameterType="java.util.List">
		SELECT
		d.id,d.depart_name
		FROM
		sys_depart d
		LEFT JOIN fd_personnel p ON d.id = p.pk_depart
		WHERE
		d.parent_id = '9e16e9ef42db4f77899cb7ec4a72cbab'
		AND
		d.depart_name like '%临床%'
		AND
		p.id in
		<foreach item="per" index="index" collection="list"
				 open="(" separator="," close=")">
			#{per.id}
		</foreach>
		GROUP BY
		d.depart_name,d.id
	</select>
</mapper>