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

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

	<select id="queryList" resultType="com.kmall.admin.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>
        <choose>
            <when test="sidx != null and sidx.trim() != ''">
                order by d.${sidx} ${order}
            </when>
			<otherwise>
                order by d.order_num asc
			</otherwise>
        </choose>
		<if test="offset != null and limit != null">
			limit #{offset}, #{limit}
		</if>
	</select>
	 
	<insert id="save" parameterType="com.kmall.admin.entity.SysDeptEntity" useGeneratedKeys="true" keyProperty="deptId">
		insert into sys_dept
		(
			`parent_id`, 
			`name`, 
			`order_num`
		)
		values
		(
			#{parentId}, 
			#{name}, 
			#{orderNum}
		)
	</insert>
	 
	<update id="update" parameterType="com.kmall.admin.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>
		</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>