<?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.srs.dormitory.mapper.DmsDormitoryMapper">

    <!-- 通用查询映射结果 -->
    <resultMap id="BaseResultMap" type="com.srs.dormitory.domain.DmsDormitory">
        <id column="id" property="id" />
        <result column="floor_id" property="floorId" />
        <result column="room_no" property="roomNo" />
        <result column="bed_num" property="bedNum" />
        <result column="occupancy" property="occupancy" />
        <result column="status" property="status" />
        <result column="remark" property="remark" />
        <result column="create_by" property="createBy" />
        <result column="create_time" property="createTime" />
        <result column="update_by" property="updateBy" />
        <result column="update_time" property="updateTime" />
        <result column="del_flag" property="delFlag" />
        <result column="is_new" property="isNew" />
        <result column="gender" property="gender" />
    </resultMap>
    <resultMap id="dormitoryResultMap" type="com.srs.dormitory.domain.Vo.DormitoryVo">
        <id property="id" column="id"/>
        <result property="roomNo" column="room_no"/>
        <result property="buildingName" column="building_name"/>
        <result property="campusName" column="campus_name"/>
        <result property="bedNum" column="bed_num"/>
        <result property="occupancy" column="occupancy"/>
        <result property="status" column="status"/>
        <result property="createBy" column="create_by"/>
        <result property="createTime" column="create_time"/>
        <result column="is_new" property="isNew" />
        <result column="gender" property="gender" />
        <result column="live_count" property="liveCount"/>
        <collection property="photoList" ofType="com.srs.dormitory.domain.Vo.PhotoVO" column="id"
                    select="com.srs.dormitory.mapper.DmsPhotosMapper.selectByDormId"/>
    </resultMap>

    <resultMap id="photoResultMap" type="com.srs.dormitory.domain.Vo.PhotoVO">
        <id property="id" column="photoId"/>
        <result property="url" column="photoUrl"/>
    </resultMap>

    <select id="listDormCount" resultType="DmsDormitory">
        select count(a.dormitory_id) as occupancy,a.dormitory_id as id
        from srs_dormitory_student as a
        GROUP BY a.dormitory_id
    </select>

    <select id="getCanDormList" resultMap="dormitoryResultMap">
        select a.*,
        case b.occupancy is null
        when true then 0
        else b.occupancy end as occupancy
        from view_dorm_info as a
        left join view_dorm_stu_count as b on a.id = b.dormitory_id
        <where>
            AND a.is_new = 1
            <if test="dormitoryId != null">AND a.id = #{dormitoryId}</if>
            <if test="floorId != null">AND a.floor_id = #{floorId}</if>
            <if test="buildingId != null">AND a.building_id = #{buildingId}</if>
        </where>
        order by a.id asc
    </select>

    <select id="getAllDormList" resultType="DomInfo" parameterType="DmsSearch">
        select a.*,
        case b.occupancy is null
        when true then 0
        else b.occupancy end as occupancy,
        (
        select count(a1.id)
        from dms_manage_application as a1 where a1.target_dorm_id = a.id and a1.`status` in (0,1,2)
        ) as manage_num,
        (
        select count(a2.id)
        from dms_new_task as a2 where a2.task_status = 2 and a2.new_dorm_id = a.id
        ) as task_num

        from view_dorm_info as a
        left join view_dorm_stu_count as b on a.id = b.dormitory_id
        <where>
            <if test="dormitoryId != null">AND a.id = #{dormitoryId}</if>
            <if test="floorId != null">AND a.floor_id = #{floorId}</if>
            <if test="buildingId != null">AND a.building_id = #{buildingId}</if>
            <if test="gender != null">AND a.gender = #{gender}</if>
        </where>
        order by a.id asc
    </select>

    <select id="getDormitoryList" resultMap="dormitoryResultMap">
        SELECT b.id, b.room_no, b.building_name,b.park_name,b.campus_name,
        b.bed_num,b.charge_standard,b.is_new,b.gender,
        case a.occupancy is null
        when true then 0
        else a.occupancy end as occupancy
        FROM view_dorm_info as b
        left join view_dorm_stu_count as a on b.id = a.dormitory_id
        <where>
            <if test="dormitoryId != null">AND b.id = #{dormitoryId}</if>
            <if test="floorId != null">AND b.floor_id = #{floorId}</if>
            <if test="buildingId != null">AND b.building_id = #{buildingId}</if>
        </where>
        order by b.id asc
    </select>
    <select id="getDormitoryInfoList" resultType="java.util.Map">
        SELECT a.id,a.room_no as roomNo,a.gender,a.bed_num bedNum,a.building_id buildingId,
        a.floor,a.floor_id floorId,a.`building_name` AS buildingName,a.`park_name` AS parkName,a.`campus_name` AS campusName,
        a.is_new,a.gender,a.remark,
        case b.occupancy is null
        when true then 0
        else b.occupancy end as occupancy
        FROM view_dorm_info as a
        left join view_dorm_stu_count as b on a.id = b.dormitory_id
        <where>
            <if test="dormitoryId != null">and a.id = #{dormitoryId}</if>
            <if test="floorId != null">and a.floor_id = #{floorId}</if>
            <if test="buildingId != null">and a.building_id = #{buildingId}</if>
            <if test="parkId != null">and a.park_id = #{parkId}</if>
            <if test="campusId != null">and a.campus_id = #{campusId}</if>
        </where>
    </select>
    <select id="getDormitoryInfoListByIds" resultType="java.util.Map">
        SELECT d.id,d.room_no roomNo,dbi.gender,d.`status`,d.bed_num bedNum,d.occupancy,
               df.floor,db.`name` AS buildingName,
               dp.`name` AS parkName,c.`name` AS campusName,d.is_new
        FROM dms_dormitory d
                 LEFT JOIN dms_dormitory_floor df ON d.floor_id = df.id
                 LEFT JOIN dms_dormitory_building db ON df.building_id = db.id
                 LEFT JOIN dms_dormitory_building_info dbi ON dbi.building_id = db.id
                 LEFT JOIN dms_dormitory_park dp ON dp.id = db.park_id
                 LEFT JOIN dms_campus c ON c.id = dp.campus_id
        WHERE d.id IN
        <foreach item="id" collection="ids" open="(" separator="," close=")">
            #{id}
        </foreach>
        AND dbi.gender = #{gender}
    </select>
    <select id="getHasBedDormitory" resultType="com.srs.dormitory.domain.DmsDormitory">
        SELECT *
        FROM `dms_dormitory` d
        LEFT JOIN dms_dormitory_class dc ON d.id = dc.dormitory_id
        <where>
            <if test="BJDM != null">AND dc.BJDM = #{BJDM}</if>
            <if test="isMixed != null">AND dc.is_mixed = #{isMixed}</if>
        </where>
    </select>
    <select id="exportDormitoryList" resultType="com.srs.dormitory.domain.Vo.DormitoryVo">
        SELECT a.id,a.room_no roomNo,a.gender,a.bed_num bedNum,b.occupancy,a.building_id buildingId,a.charge_standard,
        a.floor,a.floor_id floorId,a.`building_name` AS buildingName,a.`park_name` AS parkName,a.`campus_name` AS campusName,a.is_new
        FROM view_dorm_info as a
        left join view_dorm_stu_count as b on a.id = b.dormitory_id
        <where>
            <if test="dormitoryId != null">and a.id = #{dormitoryId}</if>
            <if test="floorId != null">and a.floor_id = #{floorId}</if>
            <if test="buildingId != null">and a.building_id = #{buildingId}</if>
            <if test="parkId != null">and a.park_id = #{parkId}</if>
            <if test="campusId != null">and a.campus_id = #{campusId}</if>
        </where>
        order by a.campus_id
    </select>
    <select id="selectByStuNo" resultType="com.srs.dormitory.domain.DmsDormitory">
        SELECT d.*
        FROM dms_dormitory d
                 LEFT JOIN srs_dormitory_student ds ON d.id = ds.dormitory_id
        where ds.stu_no = #{stuNo}
    </select>
    <select id="selectAllByBuildingId" resultType="com.srs.dormitory.domain.DmsDormitory">
        SELECT d.*
        FROM `dms_dormitory` d
                 LEFT JOIN dms_dormitory_floor f ON f.id = d.floor_id
                 LEFT JOIN dms_dormitory_building b ON f.building_id = b.id
        WHERE b.id = #{buildingId}
    </select>
    <select id="getStudentById" resultType="java.util.Map">
        SELECT s.stu_name as stuName,ds.stu_no as stuNo,ds.is_dormitory_head as isDormitoryHead,s.stu_phone as stuPhone,a.white_photo,s.class_name,s.teacher_name
        FROM `dms_dormitory` d
                 LEFT JOIN srs_dormitory_student ds ON d.id = ds.dormitory_id
                 LEFT JOIN view_stu_info s ON s.stu_no = ds.stu_no
                 left join cph_stu_extra_info as a on s.stu_no = a.stu_no
        where d.id = #{id}
    </select>


    <select id="listBuildingInfo" resultType="DomInfo">
        select a.`name` as building_name,b.`name` as park_name,c.`name` as campus_name,a.id,a.image,
               (
                   select sum(a1.bed_num)
                   FROM dms_dormitory as a1
                            left join dms_dormitory_floor as b1 on a1.floor_id = b1.id
                   where b1.building_id = a.id
               ) as bed_count,
               (
                   select sum(a2.occupancy)
                   from view_dorm_stu_count as a2
                            left join dms_dormitory as b2 on a2.dormitory_id = b2.id
                            left join dms_dormitory_floor as c2 on b2.floor_id = c2.id
                   where c2.building_id = a.id
               ) as occupancy,
               (select count(1)
                FROM dms_dormitory as a1
                         left join dms_dormitory_floor as b1 on a1.floor_id = b1.id
                where b1.building_id = a.id ) as total_room,
               ( select count(1)
                 FROM dms_dormitory as a1
                          left join dms_dormitory_floor as b1 on a1.floor_id = b1.id
                 where b1.building_id = a.id and a1.id not in(select dormitory_id from srs_dormitory_student)
               ) as vacant_room
        from dms_dormitory_building as a
                 left join dms_dormitory_park as b on a.park_id = b.id
                 left join dms_campus as c on b.campus_id = c.id
    </select>

    <select id="listRoomDeptLive" resultType="DomInfo" parameterType="DmsSearch">
        select a.id,b.floor,a.room_no,a.bed_num,a.charge_standard,a.gender,
        case
            when  c.occupancy is null then 0
            else  c.occupancy
        end
        as occupancy,
        a.is_new,
        (
            select GROUP_CONCAT(b1.dept_name , '(',a1.live_in,')' separator '&lt;br/&gt;') as live_in_dept from view_dorm_dept_count as a1
            left join sys_dept as b1 on a1.dept_id = b1.dept_id
            where a1.dormitory_id = a.id
        ) as live_in_dept,
        (
            select GROUP_CONCAT(b1.grade_name , '(',a1.live_count,')' separator '&lt;br/&gt;') as live_count from view_dorm_grade_count a1
            left join srs_grade b1 on a1.grade_id=b1.grade_id
            where a1.dormitory_id = a.id
        ) as live_count,
        d.`name` as building_name,
        e.`name` as park_name,
        f.`name` as campus_name
        from dms_dormitory as a
        left join dms_dormitory_floor as b on a.floor_id = b.id
        left join view_dorm_stu_count as c on a.id = c.dormitory_id
        left join dms_dormitory_building as d on b.building_id = d.id
        left join dms_dormitory_park as e on d.park_id = e.id
        left join dms_campus as f on e.campus_id = f.id

        <where>
            <if test="floorId != null">and b.id = #{floorId}</if>
            <if test="buildingId != null">and b.building_id = #{buildingId}</if>
            <if test="roomNo != null">and a.room_no = #{roomNo}</if>
            <if test="parkId != null">and e.id = #{parkId}</if>
            <if test="campusId != null">and f.id = #{campusId}</if>

            <if test="deptId != null or majorId != null or classId != null or stuNo != null or stuName != null or tNo != null or teacherName != null or gradeId != null">
                and a.id in (
                    select a2.dormitory_id
                    from srs_dormitory_student as a2
                    left join srs_student as b2 on a2.stu_no = b2.stu_no
                    left join srs_class as c2 on b2.class_id = c2.class_id
                    left join srs_majors as d2 on c2.major_id = d2.major_id
                    left join cph_teacher as e2 on c2.teacher_id = e2.teacher_id
                    <where>
                        <if test="deptId != null">and d2.college_id = #{deptId}</if>
                        <if test="majorId != null">and d2.major_id = #{majorId}</if>
                        <if test="classId != null">and c2.class_id = #{classId}</if>
                        <if test="stuNo != null">and a2.stu_no = #{stuNo}</if>
                        <if test="stuName != null">and b2.`name` = #{stuName}</if>
                        <if test="tNo != null">and e2.employee_id = #{tNo}</if>
                        <if test="teacherName != null">and e2.`name` = #{teacherName}</if>
                        <if test="gradeId != null">and c2.grade_id = #{gradeId}</if>
                    </where>
                )
            </if>

        </where>
        order by a.id

    </select>

    <select id="exportListRoomDeptLive" resultType="ExportDormInfo" parameterType="DmsSearch">
        select a.id,b.floor,a.room_no,a.bed_num,a.charge_standard,a.gender,
        case
        when  c.occupancy is null then 0
        else  c.occupancy
        end
        as occupancy,
        a.is_new,
        (
        select GROUP_CONCAT(b1.dept_name , '(',a1.live_in,')' separator '&lt;br/&gt;') as live_in_dept from view_dorm_dept_count as a1
        left join sys_dept as b1 on a1.dept_id = b1.dept_id
        where a1.dormitory_id = a.id
        ) as live_in_dept,
        d.`name` as building_name,
        e.`name` as park_name,
        f.`name` as campus_name
        from dms_dormitory as a
        left join dms_dormitory_floor as b on a.floor_id = b.id
        left join view_dorm_stu_count as c on a.id = c.dormitory_id
        left join dms_dormitory_building as d on b.building_id = d.id
        left join dms_dormitory_park as e on d.park_id = e.id
        left join dms_campus as f on e.campus_id = f.id

        <where>
            <if test="floorId != null">and b.id = #{floorId}</if>
            <if test="buildingId != null">and b.building_id = #{buildingId}</if>
            <if test="roomNo != null">and a.room_no = #{roomNo}</if>
            <if test="parkId != null">and e.id = #{parkId}</if>
            <if test="campusId != null">and f.id = #{campusId}</if>

            <if test="deptId != null or majorId != null or classId != null or stuNo != null or stuName != null or tNo != null or teacherName != null or gradeId != null">
                and a.id in (
                select a2.dormitory_id
                from srs_dormitory_student as a2
                left join srs_student as b2 on a2.stu_no = b2.stu_no
                left join srs_class as c2 on b2.class_id = c2.class_id
                left join srs_majors as d2 on c2.major_id = d2.major_id
                left join cph_teacher as e2 on c2.teacher_id = e2.teacher_id
                <where>
                    <if test="deptId != null">and d2.college_id = #{deptId}</if>
                    <if test="majorId != null">and d2.major_id = #{majorId}</if>
                    <if test="classId != null">and c2.class_id = #{classId}</if>
                    <if test="stuNo != null">and a2.stu_no = #{stuNo}</if>
                    <if test="stuName != null">and b2.`name` = #{stuName}</if>
                    <if test="tNo != null">and e2.employee_id = #{tNo}</if>
                    <if test="teacherName != null">and e2.`name` = #{teacherName}</if>
                    <if test="gradeId != null">and c2.grade_id = #{gradeId}</if>
                </where>
                )
            </if>

        </where>
        order by a.id
    </select>

    <select id="exportFdy" parameterType="DmsSearch" resultType="DmsFdyStu">
        select a.*,
               case b.all_in is null
                   when true then 0
                   else b.all_in
                   end as all_in,

               case b.boy_in is null
                   when true then 0
                   else b.boy_in
                   end as boy_in,

               case b.girl_in is null
                   when true then 0
                   else b.girl_in
                   end as girl_in,

               case c.`name` is null
                   when true then '暂无'
                   else c.`name`
                   end as teacher_name,

               case d.class_name is null
                   when true then '未知班级'
                   else d.class_name
                   end as class_name,

               case f.dept_name is null
                   when true then '未知学院'
                   else f.dept_name
                   end as dept_name

        from view_class_stu_count as a
                 left join view_class_stu_in_count as b on a.class_id = b.class_id
                 left join cph_teacher as c on a.teacher_id = c.teacher_id
                 left join srs_class as d on a.class_id = d.class_id
                 left join srs_majors as e on d.major_id = e.major_id
                 left join sys_dept as f on e.college_id = f.dept_id

        ORDER BY a.teacher_id desc
    </select>

    <select id="exportDept" parameterType="DmsSearch" resultType="DmsFdyStu">
        select t2.dept_name,t2.grade_name,t2.`all`,

               case t1.all_in is NULL
                   when true then 0
                   else t1.all_in
                   end as all_in,

               case t1.campus_name is NULL
                   when true then "无"
                   else t1.campus_name
                   end as campus_name

        from (
                 select d.dept_id,e.grade_id,sum(a.class_count) as `all`,d.dept_name,e.grade_name
                 from view_stu_class_count as a
                          left join srs_class as b on a.class_id = b.class_id
                          left join srs_majors as c on b.major_id = c.major_id
                          left join sys_dept as d on c.college_id = d.dept_id
                          left join srs_grade as e on b.grade_id = e.grade_id
                 GROUP BY d.dept_id, e.grade_id
                 order by d.dept_id
             ) as t2
                 left join (
            select c1.campus_name,b1.dept_name,b1.grade_name,count(a1.stu_no) as all_in,b1.dept_id,b1.grade_id
            from srs_dormitory_student as a1
                     left join view_stu_info as b1 on a1.stu_no = b1.stu_no
                     left join view_dorm_info as c1 on a1.dormitory_id = c1.id
            GROUP BY c1.campus_id,b1.dept_id,b1.grade_id
        ) as t1 on t2.grade_id = t1.grade_id and t2.dept_id = t1.dept_id

    </select>

    <select id="listRoomStu" parameterType="DmsSearch" resultType="DomInfo">
        select a.stu_no,b.`name` as stu_name , c.class_name ,d.major_name,e.dept_name,
               f.room_no,g.floor,a.is_dormitory_head , h.avatar
        from srs_dormitory_student as a
                 left join srs_student as b on a.stu_no = b.stu_no
                 left join srs_class as c on b.class_id = c.class_id
                 left join srs_majors as d on c.major_id = d.major_id
                 left join sys_dept as e on d.college_id = e.dept_id
                 left join dms_dormitory as f on a.dormitory_id = f.id
                 left join dms_dormitory_floor as g on f.floor_id = g.id
                 left join sys_user as h on a.stu_no = h.user_name
        <where>
            <if test="floorId != null">and g.id = #{floorId}</if>
            <if test="buildingId != null">and g.building_id = #{buildingId}</if>
            <if test="roomNo != null">and f.room_no = #{roomNo}</if>
            <if test="dormitoryId != null">and a.dormitory_id = #{dormitoryId}</if>
        </where>

    </select>

    <select id="countParkBed" parameterType="DmsSearch" resultType="DomInfo">
        select e.`name` as park_name,sum(a.bed_num) as bed_num,sum(b.occupancy) as occupancy
        from dms_dormitory as a
                 left join view_dorm_stu_count as b on a.id = b.dormitory_id
                 left join dms_dormitory_floor as c on a.floor_id = c.id
                 left join dms_dormitory_building as d on c.building_id = d.id
                 left join dms_dormitory_park as e on d.park_id = e.id
        GROUP BY e.id
    </select>

    <select id="findNotStu" resultType="SrsDormitoryStudent">
        select a.*
        from srs_dormitory_student as a
        left join srs_student as b on a.stu_no = b.stu_no
        where b.`status` &lt;&gt; "01"
    </select>

    <select id="countStuIn" resultType="DomInfo">
        select count(1) as bed_num
        from srs_student as a where a.`status` = '01'
        union
        SELECT sum(a.bed_num) as bed_num
        from dms_dormitory as a
        union
        select count(1) as bed_num
        from srs_dormitory_student as b
    </select>

    <select id="countDeptBed" resultType="DomInfo">
        select e.dept_name,count(a.stu_no) as bed_num
        from srs_dormitory_student as a
                 left join srs_student as b on a.stu_no = b.stu_no
                 left join srs_class as c on b.class_id = c.class_id
                 left join srs_majors as d on c.major_id = d.major_id
                 left join sys_dept as e on d.college_id = e.dept_id
        GROUP BY e.dept_id
        ORDER BY e.dept_id
    </select>

    <select id="countGenderBed" resultType="DomInfo">
        select concat(a.gender,'生在籍人数') as gender,count(stu_no) as bed_num
        from srs_student as a where a.`status` = '01'
        GROUP BY a.gender
        union
        select concat(b.gender,'生入住人数') as gender, count(a.stu_no) as bed_num
        from srs_dormitory_student as a
                 left join srs_student as b on a.stu_no = b.stu_no
        GROUP BY b.gender
    </select>

    <select id="listDormInfo" parameterType="DmsSearch" resultType="DomInfo">
        select a.*,b.occupancy
        from view_dorm_info as a
        left join view_dorm_stu_count as b on a.id = b.dormitory_id
        <where>
            <if test="floorId != null">and a.floor_id = #{floorId}</if>
            <if test="dormitoryId != null">and a.id = #{dormitoryId}</if>
            <if test="buildingId != null">and a.building_id = #{buildingId}</if>
            <if test="roomNo != null">and a.room_no = #{roomNo}</if>
            <if test="campusId != null">and a.campus_id = #{campusId}</if>
            <if test="parkId != null">and a.park_id = #{parkId}</if>
            <if test="roomNo != null">and a.room_no = #{roomNo}</if>
            <if test="isNew != null">and a.is_new = #{isNew}</if>
        </where>
        order by a.id
    </select>

    <update id="closeFull">
        update
            dms_dormitory as a
                left join view_dorm_stu_count as b on a.id = b.dormitory_id
        set is_new = 0 where a.bed_num = b.occupancy
    </update>

    <update id="openNotFull">
        update
            dms_dormitory as a
                left join view_dorm_stu_count as b on a.id = b.dormitory_id
        set is_new = 1 where a.id in
                             (select t1.id
                              from
                                  (
                                      select a.id,a.bed_num,
                                             case b.occupancy is null
                                                 when true then 0
                                                 else b.occupancy end as occupancy
                                      from view_dorm_info as a
                                               left join view_dorm_stu_count as b on a.id = b.dormitory_id
                                  ) as t1 where t1.bed_num > t1.occupancy)
    </update>

    <select id="countParkStuIn" resultType="DomInfo">
        select b.`name` as campus_name, a.`name` as park_name ,
               (
                   select sum(a1.bed_num) from view_dorm_info as a1
                   where a1.park_id = a.id
               ) as `all`,
               case
                   (
                       select sum(a2.occupancy) from view_dorm_stu_count as a2
                                                         left join view_dorm_info as b2 on a2.dormitory_id = b2.id
                       where b2.park_id = a.id
                   ) is null
                   when true then 0
                   else (
                       select sum(a2.occupancy) from view_dorm_stu_count as a2
                                                         left join view_dorm_info as b2 on a2.dormitory_id = b2.id
                       where b2.park_id = a.id
                   ) end as all_in,
               case
                   (
                       SELECT
                           sum(
                                   CASE WHEN ( `b3`.`gender` = '男' ) THEN 1 ELSE 0 END
                               ) AS `boy_in`
                       FROM srs_dormitory_student as a3
                                left join srs_student as b3 on a3.stu_no = b3.stu_no
                                left join view_dorm_info as c3 on a3.dormitory_id = c3.id
                       where c3.park_id = a.id
                   ) is null
                   when true then 0
                   else (
                       SELECT
                           sum(
                                   CASE WHEN ( `b3`.`gender` = '男' ) THEN 1 ELSE 0 END
                               ) AS `boy_in`
                       FROM srs_dormitory_student as a3
                                left join srs_student as b3 on a3.stu_no = b3.stu_no
                                left join view_dorm_info as c3 on a3.dormitory_id = c3.id
                       where c3.park_id = a.id
                   ) end as boy_in,

               case
                   (
                       SELECT
                           sum(
                                   CASE WHEN ( `b4`.`gender` = '女' ) THEN 1 ELSE 0 END
                               ) AS `boy_in`
                       FROM srs_dormitory_student as a4
                                left join srs_student as b4 on a4.stu_no = b4.stu_no
                                left join view_dorm_info as c4 on a4.dormitory_id = c4.id
                       where c4.park_id = a.id
                   ) is null
                   when true then 0
                   else (
                       SELECT
                           sum(
                                   CASE WHEN ( `b4`.`gender` = '女' ) THEN 1 ELSE 0 END
                               ) AS `boy_in`
                       FROM srs_dormitory_student as a4
                                left join srs_student as b4 on a4.stu_no = b4.stu_no
                                left join view_dorm_info as c4 on a4.dormitory_id = c4.id
                       where c4.park_id = a.id
                   ) end as girl_in,
            a.id as park_id,
            NOW() as make_time

        from dms_dormitory_park as a
        left join dms_campus as b on a.campus_id = b.id

    </select>

    <select id="countRecordConfirmStatus" resultType="DomInfo">
        select t1.*,
        (
        select count(a1.id) as all_in
        from dms_new_record as a1
        left join view_stu_info as b1 on a1.stu_no = b1.stu_no
        where a1.apply_status = 3 and b1.dept_id = t1.dept_id
        ) as all_in
        from (
        select count(a.id) as `all`,b.dept_name,b.dept_id
        from dms_new_record as a
        left join view_stu_info as b on a.stu_no = b.stu_no
        where a.apply_status != 3
        GROUP BY b.dept_id
        ) as t1


    </select>

    <select id="viewDormInfo" resultType="DomInfo">
        select a.*
        from view_dorm_info as a
    </select>

    <select id="getDormHas" parameterType="Long" resultType="DomInfo">
        select a.*,b.occupancy
        from view_dorm_info as a
        left join view_dorm_stu_count as b on a.id = b.dormitory_id
        where a.id = #{id}
    </select>

    <select id="listViewDormInfoByIds" parameterType="List" resultType="DmsDormitory">
        SELECT a.*,
        CASE
        WHEN b.occupancy IS NOT NULL THEN b.occupancy
        ELSE 0
        END AS occupancy
        FROM view_dorm_info AS a
        LEFT JOIN view_dorm_stu_count AS b ON a.id = b.dormitory_id
        where a.id in
        <foreach item="item" collection="ids" open="(" separator="," close=")">
            #{item}
        </foreach>
    </select>

    <select id="getDormitoryById" parameterType="Long" resultType="DmsDormitory">
        select a.*,
               case b.occupancy
                   when b.occupancy is not null then b.occupancy
                   else 0
                   end
                   as occupancy from view_dorm_info as a
                                         left join view_dorm_stu_count as b on a.id = b.dormitory_id
        where a.id = #{id}
    </select>

</mapper>
