<?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.yida.data.school.facility.mapper.EduDormitoryMapper">
    <resultMap id="dormMap" type="com.yida.data.common.core.entity.school.EduDormitory">
        <id column="id" property="id"/>
        <result column="create_date" property="createDate"/>
        <result column="name" property="name"/>
        <result column="master_id" property="masterId"/>
        <result column="floor_num" property="floorNum"/>
        <result column="school_id" property="schoolId"/>
        <collection property="dormRoomList" ofType="com.yida.data.common.core.entity.school.EduDormitoryRoom"
                    select="listRoomByDorm"
                    column="id">
        </collection>
    </resultMap>

    <select id="listRoomByDorm" resultType="com.yida.data.common.core.entity.school.EduDormitoryRoom">
        select *
        from edu_dormitory_room
        where dorm_id = #{id}
    </select>

    <select id="listDormByMaster" resultType="com.yida.data.common.core.entity.school.EduDormitory">
        select DISTINCT d.id,
                        d.create_date,
                        d.create_id,
                        d.update_date,
                        d.update_id,
                        d.del_flag,
                        d.name,
                        d.floor_num,
                        d.school_id,
                        m.master_id
        from edu_dormitory d
                 left join edu_dormitory_master m on d.id = m.dormitory_id
        where d.del_flag = 0
          and m.del_flag = 0
          and m.master_id = #{masterId}
    </select>

    <select id="listDormPage" resultType="com.yida.data.school.vo.index.DormInfoVO">
        select a.id dormId,a.name dormName,a.floor_num,
        ifnull(sum( if( b.id is null, 0,1 )), 0) dormRoomNum,
        ifnull(sum( if( b.check_in_num >0 , 1,0 )), 0) checkInDormRoomNum,
        ifnull(sum(b.bed_num),0) bedNum,
        ifnull(sum(b.check_in_num),0) checkNum,master_id
        from edu_dormitory a
        left join edu_dormitory_room b on a.id = b.dorm_id and b.del_flag = 0
        where a.del_flag = 0
        <if test="dto.schoolId != null">
            and a.school_id=#{dto.schoolId}
        </if>
        <if test="dto.masterId != null">
            and a.id in
            <foreach collection="dto.dormIds" open="(" close=")" item="dormId" separator=",">
                #{dormId}
            </foreach>
        </if>
        <if test="dto.keyword != null and dto.keyword != ''">
            and a.name like concat('%',#{dto.keyword},'%')
        </if>
        group by a.id,a.create_date
        order by a.create_date desc
    </select>
</mapper>
