<?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.xjrsoft.module.room.mapper.RoomMapper">
    <select id="getPage" parameterType="com.xjrsoft.module.room.dto.RoomPageDto" resultType="com.xjrsoft.module.room.vo.RoomPageVo">
        SELECT t1.id,t1.sort_code,t2.name AS bulid_name,t1.floor_number,t1.room_name,t3.name AS check_in_status_cn,t4.name AS gender_cn,t1.bed_count,
        (SELECT GROUP_CONCAT(c2.name) FROM room_student_appoint c1
        LEFT JOIN xjr_user c2 ON c1.student_user_id = c2.id
        WHERE c1.delete_mark = 0 AND c1.room_id = t1.id) AS student_appoint_names,
        (SELECT GROUP_CONCAT(c2.name) FROM room_teacher_appoint c1
        LEFT JOIN xjr_user c2 ON c1.user_id = c2.id
        WHERE c1.delete_mark = 0 AND c1.room_id = t1.id) AS teacher_appoint_names,t1.is_max,t1.remark,
        ifnull((
        SELECT COUNT(*) FROM room_bed a1
        INNER JOIN xjr_user a2 ON a1.student_user_id = a2.id
        WHERE a1.delete_mark = 0 AND a2.delete_mark = 0 AND a1.room_id = t1.id
        AND a1.student_user_id IS NOT NULL
        ), 0) AS student_count,
        ifnull((
        SELECT COUNT(*) FROM room_bed
        WHERE delete_mark = 0 AND room_id = t1.id
        AND student_user_id IS NULL
        ), 0) AS empty_count FROM room t1
        LEFT JOIN base_office_build t2 ON t1.office_build_id = t2.id
        LEFT JOIN xjr_dictionary_detail t3 ON t1.check_in_status = t3.code AND t3.item_id = 1739843696019435521
        LEFT JOIN xjr_dictionary_detail t4 ON t1.gender = t4.code AND t4.item_id = 2023000000000000004
        WHERE t1.delete_mark = 0
        <if test="dto.officeBuildId != null">
            and t1.office_build_id = #{dto.officeBuildId}
        </if>
        <if test="dto.floorNumber != null">
            and t1.floor_number = #{dto.floorNumber}
        </if>
        <if test="dto.checkInStatus != null and dto.checkInStatus != ''">
            and t1.check_in_status = #{dto.checkInStatus}
        </if>
        <if test="dto.gender != null and dto.gender != ''">
            and t1.gender = #{dto.gender}
        </if>
        ORDER BY t1.sort_code
    </select>
    <select id="getList" parameterType="com.xjrsoft.module.room.dto.RoomPageDto" resultType="com.xjrsoft.module.room.vo.RoomExcelVo">
        SELECT t1.id,t1.sort_code,t2.name AS bulid_name,t1.floor_number,t1.room_name,t3.name AS check_in_status_cn,t4.name AS gender_cn,t1.bed_count,
        (SELECT GROUP_CONCAT(c2.name) FROM room_student_appoint c1
        LEFT JOIN xjr_user c2 ON c1.student_user_id = c2.id
        WHERE c1.delete_mark = 0 AND c1.room_id = t1.id) AS student_appoint_names,
        (SELECT GROUP_CONCAT(c2.name) FROM room_teacher_appoint c1
        LEFT JOIN xjr_user c2 ON c1.user_id = c2.id
        WHERE c1.delete_mark = 0 AND c1.room_id = t1.id) AS teacher_appoint_names,t1.is_max,t1.remark FROM room t1
        LEFT JOIN base_office_build t2 ON t1.office_build_id = t2.id
        LEFT JOIN xjr_dictionary_detail t3 ON t1.check_in_status = t3.code AND t3.item_id = 1739843696019435521
        LEFT JOIN xjr_dictionary_detail t4 ON t1.gender = t4.code AND t4.item_id = 2023000000000000004
        WHERE t1.delete_mark = 0
        <if test="dto.officeBuildId != null">
            and t1.office_build_id = #{dto.officeBuildId}
        </if>
        <if test="dto.floorNumber != null">
            and t1.floor_number = #{dto.floorNumber}
        </if>
        <if test="dto.checkInStatus != null and dto.checkInStatus != ''">
            and t1.check_in_status = #{dto.checkInStatus}
        </if>
        <if test="dto.gender != null and dto.gender != ''">
            and t1.gender = #{dto.gender}
        </if>
        ORDER BY t1.sort_code
    </select>
    <select id="getMaxSortCode" resultType="java.lang.Integer">
        SELECT IFNULL(MAX(sort_code),1) FROM room WHERE delete_mark = 0
    </select>

    <select id="getRoomClassCount" resultType="com.xjrsoft.module.room.vo.RoomClassCountVo">
        SELECT id,(
        SELECT COUNT(*) FROM (
        SELECT COUNT(*) FROM base_class t1
        LEFT JOIN base_student_school_roll t2 ON t2.class_id = t1.id
        LEFT JOIN room_bed t3 ON t2.user_id = t3.student_user_id
        WHERE t3.room_id = room.id GROUP BY t1.id
        ) class_data
        ) AS class_count FROM room
        where delete_mark = 0
        <if test="ids != null and ids.size() > 0">
            and id in
            <foreach item="roomId" index="index" collection="ids" open="(" close=")" separator=",">
                #{roomId}
            </foreach>
        </if>
    </select>

    <select id="getRoomClassExistent" resultType="com.xjrsoft.module.room.vo.RoomClassExistentVo">
        SELECT id,(
        SELECT GROUP_CONCAT(DISTINCT t1.class_id) FROM base_student_school_roll t1
        LEFT JOIN room_bed t3 ON t1.user_id = t3.student_user_id
        WHERE t3.room_id = room.id AND t3.delete_mark = 0
        ) AS class_count FROM room
        where delete_mark = 0
        <if test="ids != null and ids.size() > 0">
            and id in
            <foreach item="roomId" index="index" collection="ids" open="(" close=")" separator=",">
                #{roomId}
            </foreach>
        </if>
    </select>

    <!-- 将有多个班级的寝室设置为混合寝室 -->
    <update id="setIsmaxTrue">
        UPDATE room SET is_max = 1 WHERE delete_mark = 0
        AND (
        SELECT COUNT(*) FROM (
        SELECT COUNT(*) FROM base_student_school_roll t1
        LEFT JOIN room_bed t3 ON t1.user_id = t3.student_user_id
        WHERE t3.room_id = room.id AND t3.delete_mark = 0 GROUP BY t1.class_id
        ) class_data
        ) > 1
    </update>
    <!-- 将没有多个班级的混合寝室清除掉 -->
    <update id="setIsmaxFalse">
        UPDATE room SET is_max = 0 WHERE delete_mark = 0
        AND 1 >(
        SELECT COUNT(*) FROM (
        SELECT COUNT(*) FROM base_student_school_roll t1
        LEFT JOIN room_bed t3 ON t1.user_id = t3.student_user_id
        WHERE t3.room_id = room.id AND t3.delete_mark = 0 GROUP BY t1.class_id
        ) class_data
        )
    </update>
</mapper>