<?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.bootdo.system.dao.DeptDao">

    <select id="get" resultType="com.bootdo.system.domain.DeptDO">
        select `dept_id`,
               `parent_id`,
               `name`,
               `order_num`,
               `del_flag`
        from sys_dept
        where dept_id = #{value}
    </select>

    <select id="list" resultType="com.bootdo.system.domain.DeptDO">
        select `dept_id`,`parent_id`,`name`,`order_num`,`del_flag` from
        sys_dept
        <where>
            <if test="deptId != null and deptId != ''">and dept_id = #{deptId}</if>
            <if test="parentId != null and parentId != ''">and parent_id = #{parentId}</if>
            <if test="name != null and name != ''">and name = #{name}</if>
            <if test="orderNum != null and orderNum != ''">and order_num = #{orderNum}</if>
            <if test="delFlag != null and delFlag != ''">and del_flag = #{delFlag}</if>
        </where>
        <choose>
            <when test="sort != null and sort.trim() != ''">
                order by ${sort} ${order}
            </when>
            <otherwise>
                order by order_num asc
            </otherwise>
        </choose>
        <if test="offset != null and limit != null">
            limit #{offset}, #{limit}
        </if>
    </select>

    <select id="count" resultType="int">
        select count(*) from sys_dept
        <where>
            <if test="deptId != null and deptId != ''">and dept_id = #{deptId}</if>
            <if test="parentId != null and parentId != ''">and parent_id = #{parentId}</if>
            <if test="name != null and name != ''">and name = #{name}</if>
            <if test="orderNum != null and orderNum != ''">and order_num = #{orderNum}</if>
            <if test="delFlag != null and delFlag != ''">and del_flag = #{delFlag}</if>
        </where>
    </select>

    <insert id="save" parameterType="com.bootdo.system.domain.DeptDO"
            useGeneratedKeys="true" keyProperty="deptId">
        insert into sys_dept
        (`parent_id`,
         `name`,
         `order_num`,
         `del_flag`)
        values (#{parentId},
                #{name},
                #{orderNum},
                #{delFlag})
    </insert>

    <update id="update" parameterType="com.bootdo.system.domain.DeptDO">
        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="delFlag != null">`del_flag` = #{delFlag}</if>
        </set>
        where dept_id = #{deptId}
    </update>

    <delete id="remove">
        delete
        from sys_dept
        where dept_id = #{value}
    </delete>

    <delete id="batchRemove">
        delete from sys_dept where dept_id in
        <foreach item="deptId" collection="array" open="(" separator=","
                 close=")">
            #{deptId}
        </foreach>
    </delete>

    <select id="listParentDept" resultType="long">
        select DISTINCT parent_id
        from sys_dept
    </select>

    <select id="getDeptUserNumber" resultType="int">
        select count(*)
        from sys_user
        where dept_id = #{value}
    </select>


    <select id="queryDept" resultType="com.bootdo.system.domain.DeptDO">
        SELECT b.`dept_id` as deptId, b.`parent_id` as parentId, b.`name` as name, b.`order_num` as orderNum, b.`del_flag` as delFlag
        FROM sys_dept b,
             (SELECT `dept_id`, `parent_id`, `name`, `order_num`, `del_flag`
              FROM sys_dept
              WHERE dept_id = #{value}) a
        WHERE a.parent_id = b.parent_id AND b.dept_id != #{value}
        order by b.order_num asc
    </select>


</mapper>