<?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.platform.dao.SysDeptDao">

	<select id="queryObject" resultType="com.platform.entity.SysDeptEntity">
		select * from sys_dept where dept_id = #{value} and del_flag = 0
	</select>


	<select id="getDeptByParentDetpId" resultType="com.platform.entity.SysDeptEntity">
		select * from sys_dept where parent_id = #{parentId} and del_flag = 0
	</select>


	<select id="queryTotal" resultType="java.lang.Integer">
		select count(*) from sys_dept where del_flag = 0
	</select>

	<select id="queryList" resultType="com.platform.entity.SysDeptEntity">
		select d.*,(select p.name from sys_dept p where p.dept_id = d.parent_id) as parentName
		from sys_dept d where d.del_flag = 0
		<if test="deptFilter != null">
			and d.dept_id in (${deptFilter})
		</if>
		order by d.order_num asc,d.dept_id desc

		<if test="offset != null and limit != null">
			limit #{offset}, #{limit}
		</if>
	</select>

	<select id="queryListByName" resultType="com.platform.entity.SysDeptEntity">
		select d.*,(select p.name from sys_dept p where p.dept_id = d.parent_id) as parentName
		from sys_dept d where
		d.del_flag = 0
		<if test="name != null and name.trim() != ''">
			AND d.name LIKE concat('%',#{name},'%')
		</if>

		<if test="deptIdList != null">
			AND d.dept_id in
			<foreach item="id" collection="deptIdList" open="(" separator="," close=")">
				#{id}
			</foreach>
		</if>
		order by d.order_num asc,d.dept_id desc
	</select>

	<select id="queryListByNameCount" resultType="java.lang.Integer">
		select count(*)
		from sys_dept d where
		d.del_flag = 0
		<if test="name != null and name.trim() != ''">
			AND d.name LIKE concat('%',#{name},'%')
		</if>

		<if test="deptIdList != null">
			AND d.dept_id in
			<foreach item="id" collection="deptIdList" open="(" separator="," close=")">
				#{id}
			</foreach>
		</if>
	</select>
	 
	<insert id="save" parameterType="com.platform.entity.SysDeptEntity" useGeneratedKeys="true" keyProperty="deptId">
		insert into sys_dept
		(
			`parent_id`, 
			`name`, 
			`order_num`,
			`staff_num`,
			`cur_staff_num`,
			`expiration_date`,
			`logo_url`
		)
		values
		(
			#{parentId}, 
			#{name}, 
			#{orderNum},
			#{staffNum},
			#{curStaffNum},
			#{expirationDate},
			#{logoUrl}
		)
	</insert>
	 
	<update id="update" parameterType="com.platform.entity.SysDeptEntity">
		update sys_dept 
		<set>
			<if test="parentId != null">`parent_id` = #{parentId}, </if>
			<if test="name != null">`name` = #{name}, </if>
			<if test="orderNum != null">`order_num` = #{orderNum},</if>
			<if test="staffNum != null">`staff_num` = #{staffNum},</if>
			<if test="curStaffNum != null">`cur_staff_num` = #{curStaffNum},</if>
			<if test="expirationDate != null">`expiration_date` = #{expirationDate},</if>
			<if test="logoUrl != null">`logo_url` = #{logoUrl}</if>
		</set>
		where dept_id = #{deptId}
	</update>
	
	<update id="delete">
		update sys_dept set del_flag = -1 where dept_id = #{value}
	</update>
	
	<select id="queryDetpIdList" resultType="long">
		select dept_id from sys_dept where parent_id = #{value} and del_flag = 0
	</select>
</mapper>