<?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="net.lab1024.smartadmin.module.system.department.DepartmentDao">


    <resultMap id="DepartmentVO"
               type="net.lab1024.smartadmin.module.system.department.domain.dto.DepartmentVO"></resultMap>


    <sql id="baseSql">
        d.id,
        d.name,
        d.short_name,
        d.manager_id,
        d.parent_id,
        d.sort,
        d.update_time,
        d.create_time
    </sql>

    <select id="listAll" resultMap="DepartmentVO">
        SELECT
        <include refid="baseSql"></include>,
        e.actual_name as managerName,
        pd.`name` as parentName
        FROM
        t_department d
        left join t_employee e on d.manager_id= e.id
        left join t_department pd on d.parent_id = pd.id
        order by sort asc
    </select>

    <select id="selectByParentId" resultMap="DepartmentVO">
        SELECT
        <include refid="baseSql"></include>
        FROM
        t_department d
        where
        d.parent_id = #{departmentId}
        order by sort desc
    </select>


    <select id="countSubDepartment" resultType="java.lang.Integer">
        SELECT
        count(1)
        FROM
        t_department
        WHERE
        parent_id = #{deptId}
    </select>


</mapper>