<?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.jiayue.center.mapper.SysDeptMapper">

	<resultMap type="SysDept" id="SysDeptResult">
		<id     property="deptId"         column="dept_id"        />
		<result property="deptName"       column="dept_name"      />
		<result property="parentName"     column="parent_name"    />
		<result property="parentId"       column="parent_id"      />
		<result property="orderNum"       column="order_num"      />
		<result property="createBy"       column="create_by"      />
		<result property="createTime"     column="create_time"    />
		<result property="updateTime"     column="update_time"    />
		<result property="updateBy"       column="update_by"      />
		<result property="remark"         column="remark"         />
		<result property="delFlag"         column="del_flag"         />
	</resultMap>

	<sql id="selectDeptVo">
        select dept_id, dept_name, parent_id, order_num, create_time
		from sys_dept
    </sql>

	<select id="selectDeptList" parameterType="SysDept" resultMap="SysDeptResult">
		<include refid="selectDeptVo"/>
		<where>
			del_flag=0
			<if test="deptName != null and deptName != ''">
				AND dept_name like concat('%', #{deptName}, '%')
			</if>
		</where>
		order by parent_id, order_num
	</select>

	<select id="selectDeptTreeAll" resultMap="SysDeptResult">
		select distinct m.dept_id, m.parent_id, m.dept_name, m.order_num, m.create_time
		from sys_dept m where del_flag=0
		order by m.parent_id, m.order_num
	</select>


	<select id="selectDeptById" parameterType="Long" resultMap="SysDeptResult">
		<include refid="selectDeptVo"/>
		where dept_id = #{deptId} and del_flag=0
	</select>


	<select id="hasChildByDeptId" resultType="Integer">
		select count(1) from sys_dept where parent_id = #{deptId} and del_flag=0
	</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="orderNum != null">order_num,</if>
		<if test="remark != null and remark != ''">remark,</if>
		<if test="createBy != null and createBy != ''">create_by,</if>
		create_time,
		del_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="orderNum != null">#{orderNum},</if>
		<if test="remark != null and remark != ''">#{remark},</if>
		<if test="createBy != null and createBy != ''">#{createBy},</if>
		sysdate(),
		0
		)
	</insert>

	<update id="updateDept" parameterType="SysDept">
		update sys_dept
		<set>
			<if test="deptName != null and deptName != ''">dept_name = #{deptName},</if>
			<if test="parentId != null">parent_id = #{parentId},</if>
			<if test="orderNum != null">order_num = #{orderNum},</if>
			<if test="remark != null and remark != ''">remark = #{remark},</if>
			<if test="updateBy != null and updateBy != ''">update_by = #{updateBy},</if>
			update_time = sysdate()
		</set>
		where dept_id = #{deptId} and del_flag=0
	</update>


	<delete id="deleteDeptById">
	    update sys_dept set del_flag=1,update_by=#{updateBy},update_time=sysdate() where dept_id = #{deptId} and del_flag=0
	</delete>


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