<?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.dayou.mapper.DepartmentMapper">

    <!-- 通用查询映射结果 -->
    <resultMap id="BaseResultMap" type="com.dayou.entity.Department">
        <result column="id" property="id" />
        <result column="deleted" property="deleted" />
        <result column="created" property="created" />
        <result column="modified" property="modified" />
        <result column="parent_id" property="parentId" />
        <result column="name" property="name" />
        <result column="leader_id" property="leaderId" />
        <result column="phone" property="phone" />
    </resultMap>

    <!-- 通用查询结果列 -->
    <sql id="Base_Column_List">
        id,
        deleted,
        created,
        modified,
        parent_id, name, leader_id, phone
    </sql>

    <select id="xPage" parameterType="com.dayou.entity.Department" resultType="com.dayou.vo.DepartmentVO">
        SELECT d.id,d.parent_id,d.name,d.leader_id,d.phone,d.deleted,d.created,d.modified,d.if_allot,
               ( SELECT NAME FROM department WHERE id = d.parent_id ) AS parentName ,
               (select name from user where id = d.leader_id) as leaderName,
               (select count(distinct user_id) from user_post where post_id in ((select id from post where department_id = d.id and user_post.deleted = 0))) as peopleNum
        FROM
            department d where d.deleted = 0
        <if test="department!=null and department.departmentIds!=null and department.departmentIds.size!=0">
            and d.id in
            <foreach collection="department.departmentIds" open="(" close=")" separator="," item="departmentId">
                #{departmentId}
            </foreach>
        </if>
        order by d.id DESC
    </select>

    <select id="getPage" parameterType="com.dayou.entity.Department" resultType="com.dayou.vo.DepartmentVO">
        select d.*,u.name as leaderName from department d left join user u on u.id = d.leader_id
        where d.deleted = 0
    </select>
</mapper>
