<?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.RoomBedMapper">
    <select id="getPage" parameterType="com.xjrsoft.module.room.dto.RoomBedPageDto" resultType="com.xjrsoft.module.room.vo.RoomBedPageVo">
        SELECT t1.id,t1.sort_code,t3.name AS build_name,t2.floor_number,t2.room_name,t1.bed_number,t4.name AS student_name,t5.student_id,
        t4.mobile,t9.name as gender_cn,t7.name AS grade_name,t8.name AS class_name FROM room_bed t1
        LEFT JOIN room t2 ON t1.room_id = t2.id
        LEFT JOIN base_office_build t3 ON t2.office_build_id = t3.id
        LEFT JOIN xjr_user t4 ON t1.student_user_id = t4.id
        LEFT JOIN base_student t5 ON t1.student_user_id = t5.user_id
        LEFT JOIN base_student_school_roll t6 ON t1.student_user_id = t6.user_id
        LEFT JOIN base_grade t7 ON t6.grade_id = t7.id
        LEFT JOIN base_class t8 ON t6.class_id = t8.id
        LEFT JOIN xjr_dictionary_detail t9 ON t2.gender = t9.code AND t9.item_id = 2023000000000000004
        where t1.delete_mark = 0 and t2.delete_mark = 0
        <if test="dto.officeBuildId != null">
            and t2.office_build_id = #{dto.officeBuildId}
        </if>
        <if test="dto.floorNumber != null">
            and t2.floor_number = #{dto.floorNumber}
        </if>
        <if test="dto.roomId != null">
            and t2.room_id = #{dto.roomId}
        </if>
        <if test="dto.gender != null and dto.gender != ''">
            and t2.gender = #{dto.gender}
        </if>
        <if test="dto.gradeId != null">
            and t7.id = #{dto.gradeId}
        </if>
        <if test="dto.classId != null">
            and t8.id = #{dto.classId}
        </if>
        <if test="dto.studentName != null">
            and t4.name like concat('%',#{dto.studentName},'%')
        </if>
        <if test="dto.studentId != null">
            and t5.student_id like concat('%',#{dto.studentId},'%')
        </if>
        <if test="dto.identity != null and dto.identity != ''">
            and t2.check_in_status like concat('%',#{dto.identity},'%')
        </if>
        ORDER BY t1.sort_code
    </select>

    <select id="getList" parameterType="com.xjrsoft.module.room.dto.RoomBedPageDto" resultType="com.xjrsoft.module.room.vo.RoomBedExcelVo">
        SELECT t1.sort_code,t3.name AS build_name,t2.floor_number,t2.room_name,t1.bed_number,t4.name AS student_name,t5.student_id,
        t4.mobile,t9.name as gender_cn,t7.name AS grade_name,t8.name AS class_name FROM room_bed t1
        LEFT JOIN room t2 ON t1.room_id = t2.id
        LEFT JOIN base_office_build t3 ON t2.office_build_id = t3.id
        LEFT JOIN xjr_user t4 ON t1.student_user_id = t4.id
        LEFT JOIN base_student t5 ON t1.student_user_id = t5.user_id
        LEFT JOIN base_student_school_roll t6 ON t1.student_user_id = t6.user_id
        LEFT JOIN base_grade t7 ON t6.grade_id = t7.id
        LEFT JOIN base_class t8 ON t6.class_id = t8.id
        LEFT JOIN xjr_dictionary_detail t9 ON t2.gender = t9.code AND t9.item_id = 2023000000000000004
        where t1.delete_mark = 0 and t2.delete_mark = 0
        <if test="dto.officeBuildId != null">
            and t2.office_build_id = #{dto.officeBuildId}
        </if>
        <if test="dto.floorNumber != null">
            and t2.floor_number = #{dto.floorNumber}
        </if>
        <if test="dto.roomId != null">
            and t2.room_id = #{dto.roomId}
        </if>
        <if test="dto.gender != null and dto.gender != ''">
            and t2.gender = #{dto.gender}
        </if>
        <if test="dto.gradeId != null">
            and t7.id = #{dto.gradeId}
        </if>
        <if test="dto.classId != null">
            and t8.id = #{dto.classId}
        </if>
        <if test="dto.studentName != null">
            and t4.name like concat('%',#{dto.studentName},'%')
        </if>
        <if test="dto.studentId != null">
            and t5.student_id like concat('%',#{dto.studentId},'%')
        </if>
        <if test="dto.identity != null and dto.identity != ''">
            and t2.check_in_status like concat('%',#{dto.identity},'%')
        </if>
        ORDER BY t1.sort_code
    </select>

    <select id="getMaxSortCode" resultType="java.lang.Integer">
        SELECT IFNULL(MAX(sort_code),0) FROM room_bed WHERE delete_mark = 0
    </select>

    <select id="getDistributeClassInfo" parameterType="com.xjrsoft.module.room.dto.DistributeClassPageDto" resultType="com.xjrsoft.module.room.vo.DistributeClassPageVo">
        SELECT t1.id,t2.name AS grade_name,t3.name AS org_name,t1.name AS class_name,
        (SELECT COUNT(*) FROM base_student c1
        LEFT JOIN base_student_school_roll c2 ON c1.user_id = c2.user_id
        LEFT JOIN xjr_dictionary_detail c3 ON c2.stduy_status = c3.code AND c3.item_id = 2023000000000000030
        left join xjr_user c4 on c1.user_id = c4.id
        WHERE c1.delete_mark = 0 AND c2.delete_mark = 0
        AND c3.code = 'FB3002' AND c2.class_id = t1.id
        AND c2.archives_status = 'FB2901'
        <if test="dto.gender != null">
            and c4.gender = #{dto.gender}
        </if>
        ) AS need_count,
        (
        SELECT COUNT(*) FROM room_bed a1
        LEFT JOIN base_student_school_roll a2 ON a1.student_user_id = a2.user_id
        left join xjr_user c4 on a2.user_id = c4.id
        WHERE a1.delete_mark = 0 AND a2.delete_mark = 0
        AND a2.class_id = t1.id
        <if test="dto.gender != null">
            and c4.gender = #{dto.gender}
        </if>
        ) AS distribute_count FROM base_class t1
        LEFT JOIN base_grade t2 ON t1.grade_id = t2.id
        LEFT JOIN xjr_department t3 ON t1.org_id = t3.id
        WHERE t1.delete_mark = 0
        and (
        SELECT COUNT(*) FROM base_student c1
        LEFT JOIN base_student_school_roll c2 ON c1.user_id = c2.user_id
        LEFT JOIN xjr_dictionary_detail c3 ON c2.stduy_status = c3.code AND c3.item_id = 2023000000000000030
        left join xjr_user c4 on c4.id = c1.user_id
        WHERE c1.delete_mark = 0 AND c2.delete_mark = 0
        AND c3.code = 'FB3002' AND c2.class_id = t1.id
        AND c2.archives_status = 'FB2901'
        <if test="dto.gender != null">
            and c4.gender = #{dto.gender}
        </if>
        ) >
        (
        SELECT COUNT(*) FROM room_bed a1
        LEFT JOIN base_student_school_roll a2 ON a1.student_user_id = a2.user_id
        left join room a3 on a3.id = a1.room_id
        WHERE a1.delete_mark = 0 AND a2.delete_mark = 0
        AND a2.class_id = t1.id
        <if test="dto.gender != null">
            and a3.gender = #{dto.gender}
        </if>
        )
        <if test="dto.gradeId != null">
            and t1.grade_id = #{dto.gradeId}
        </if>
        <if test="dto.classId != null">
            and t1.id = #{dto.classId}
        </if>

    </select>
    <!--查询分配床位班级列表的各个人数 - 开始 -->

    <!-- 查询班上需要住宿的人数 -->
    <select id="getAllStayCount" resultType="com.xjrsoft.module.room.vo.ClassStudentCountVo">
        SELECT t1.id as class_id,COUNT(*) as student_count FROM base_class t1
        LEFT JOIN base_student_school_roll t2 ON t1.id = t2.class_id
        LEFT JOIN xjr_user t3 ON t2.user_id = t3.id
        WHERE t1.delete_mark = 0
        AND t2.delete_mark = 0 AND t3.delete_mark = 0
        AND t2.archives_status = 'FB2901'
        AND t2.stduy_status = 'FB3002' GROUP BY t1.id
    </select>

    <!-- 需要安排的男生总人数 -->
    <select id="getAllStayMaleCount" resultType="com.xjrsoft.module.room.vo.ClassStudentCountVo">
        SELECT t1.id as class_id,COUNT(*) as student_count FROM base_class t1
        LEFT JOIN base_student_school_roll t2 ON t1.id = t2.class_id
        LEFT JOIN xjr_user t3 ON t2.user_id = t3.id
        WHERE t1.delete_mark = 0
        AND t2.delete_mark = 0 AND t3.delete_mark = 0
        AND t3.gender = 'SB10001'
        AND t2.archives_status = 'FB2901'
        AND t2.stduy_status = 'FB3002' GROUP BY t1.id
    </select>
    <!-- 需要安排的女生总人数 -->
    <select id="getAllStayFemaleCount" resultType="com.xjrsoft.module.room.vo.ClassStudentCountVo">
        SELECT t1.id as class_id,COUNT(*) as student_count FROM base_class t1
        LEFT JOIN base_student_school_roll t2 ON t1.id = t2.class_id
        LEFT JOIN xjr_user t3 ON t2.user_id = t3.id
        WHERE t1.delete_mark = 0
        AND t2.delete_mark = 0 AND t3.delete_mark = 0
        AND t3.gender = 'SB10002'
        AND t2.archives_status = 'FB2901'
        AND t2.stduy_status = 'FB3002' GROUP BY t1.id
    </select>
    <!-- 已分配的总人数 -->
    <select id="getAllArrangedCount" resultType="com.xjrsoft.module.room.vo.ClassStudentCountVo">
        SELECT a1.id as class_id,COUNT(*) as student_count FROM base_class a1
        LEFT JOIN base_student_school_roll a2 ON a1.id = a2.class_id
        LEFT JOIN room_bed a3 ON a2.user_id = a3.student_user_id
        LEFT JOIN xjr_user a4 ON a3.student_user_id = a4.id
        WHERE a1.delete_mark = 0 AND a2.delete_mark = 0
        AND a4.delete_mark = 0
        GROUP BY a1.id
    </select>
    <!-- 已分配的男生人数 -->
    <select id="getAllArrangedMaleCount" resultType="com.xjrsoft.module.room.vo.ClassStudentCountVo">
        SELECT a1.id as class_id,COUNT(*) as student_count FROM base_class a1
        LEFT JOIN base_student_school_roll a2 ON a1.id = a2.class_id
        LEFT JOIN room_bed a3 ON a2.user_id = a3.student_user_id
        LEFT JOIN xjr_user a4 ON a3.student_user_id = a4.id AND a4.gender = 'SB10001'
        WHERE a1.delete_mark = 0 AND a2.delete_mark = 0
        AND a4.delete_mark = 0
        GROUP BY a1.id
    </select>
    <!-- 已分配的女生人数 -->
    <select id="getAllArrangedFemaleCount" resultType="com.xjrsoft.module.room.vo.ClassStudentCountVo">
        SELECT a1.id as class_id,COUNT(*) as student_count FROM base_class a1
        LEFT JOIN base_student_school_roll a2 ON a1.id = a2.class_id
        LEFT JOIN room_bed a3 ON a2.user_id = a3.student_user_id
        LEFT JOIN xjr_user a4 ON a3.student_user_id = a4.id AND a4.gender = 'SB10002'
        WHERE a1.delete_mark = 0 AND a2.delete_mark = 0
        AND a4.delete_mark = 0
        GROUP BY a1.id
    </select>
    <!--查询分配床位班级列表的各个人数 - 结束 -->

    <select id="getDistributeRoomBedInfo" parameterType="com.xjrsoft.module.room.dto.DistributeRoomBedPageDto" resultType="com.xjrsoft.module.room.vo.DistributeRoomBedPageVo">
        SELECT t1.id,t1.sort_code,t2.name AS build_name,t1.floor_number,t1.room_name,t4.name AS gender_cn, t3.name AS check_in_status_cn,
        t1.bed_count,(
        SELECT COUNT(*) FROM room_bed
        WHERE delete_mark = 0 AND room_id = t1.id
        AND student_user_id IS NOT NULL
        ) AS into_count,(
        SELECT GROUP_CONCAT(DISTINCT(c3.name)) FROM room_bed c1
        LEFT JOIN base_student_school_roll c2 ON c1.student_user_id = c2.user_id
        LEFT JOIN base_class c3 ON c2.class_id = c3.id
        WHERE c1.delete_mark = 0 AND c1.room_id = t1.id
        AND c1.student_user_id IS NOT NULL
        ) AS into_class 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
        and
            (
                SELECT COUNT(*) FROM room_bed
                WHERE delete_mark = 0 AND room_id = t1.id
                  AND student_user_id IS NOT NULL
            ) != t1.bed_count
        <if test="dto.gender != null and dto.gender != ''">
            and t1.gender = #{dto.gender}
        </if>
        <if test="dto.officeBuildId != null">
            and t2.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.roomName != null and dto.roomName !=''">
            and t1.room_name like concat('%',#{dto.roomName},'%')
        </if>
        ORDER BY t1.sort_code
    </select>

    <select id="getBedInfo" resultType="com.xjrsoft.module.room.vo.RoomBedVo">
        SELECT t1.* FROM room_bed t1
        LEFT JOIN room t2 ON t1.room_id = t2.id
        WHERE t1.delete_mark = 0 AND t1.room_id IN
        <foreach item="roomId" index="index" collection="ids" open="(" close=")" separator=",">
            #{roomId}
        </foreach>
        ORDER BY t2.sort_code,t1.sort_code
    </select>

    <select id="getDistributeResult" parameterType="com.xjrsoft.module.room.dto.DistributeRoomBedDto" resultType="com.xjrsoft.module.room.vo.DistributeResultListVo">
        SELECT t5.class_id,t4.name,t6.name AS gender_cn,t3.name AS build_name,t2.room_name,t1.bed_number FROM room_bed t1
        LEFT JOIN room t2 ON t1.room_id = t2.id
        LEFT JOIN base_office_build t3 ON t2.office_build_id = t3.id
        LEFT JOIN xjr_user t4 ON t1.student_user_id = t4.id
        LEFT JOIN base_student_school_roll t5 ON t5.user_id = t4.id
        LEFT JOIN xjr_dictionary_detail t6 ON t2.gender = t6.code AND t6.item_id = 2023000000000000004
        WHERE t1.student_user_id IS NOT NULL AND t1.delete_mark = 0
        AND t1.room_id IN
        <foreach item="roomId" index="index" collection="dto.roomIds" open="(" close=")" separator=",">
            #{roomId}
        </foreach>
        AND t5.class_id IN
        <foreach item="classId" index="index" collection="dto.classIds" open="(" close=")" separator=",">
            #{classId}
        </foreach>
        ORDER BY t2.sort_code,t1.sort_code
    </select>

    <select id="getClassStudetBed" parameterType="com.xjrsoft.module.room.dto.AdjustClassPageDto" resultType="com.xjrsoft.module.room.vo.AdjustBedClassStudentPageVo">
        SELECT t1.user_id,t4.id AS class_id, t4.name AS class_name,t2.name AS student_name,
        REPLACE(REPLACE(t2.gender,'SB10001','男'),'SB10002','女') AS gender_cn,t7.name AS build_name,t6.room_name,t5.bed_number
        ,(SELECT (SUM(factrecarmny) >= SUM(factar)) FROM pb_v_xsxxsfytb WHERE Studentcode = t2.credential_number) AS pay_status
        FROM base_student t1
        LEFT JOIN xjr_user t2 ON t1.user_id = t2.id
        LEFT JOIN base_student_school_roll t3 ON t1.user_id = t3.user_id
        LEFT JOIN base_class t4 ON t3.class_id = t4.id
        LEFT JOIN room_bed t5 ON t5.student_user_id = t1.user_id
        LEFT JOIN room t6 ON t5.room_id = t6.id
        LEFT JOIN base_office_build t7 ON t6.office_build_id = t7.id
        WHERE t1.delete_mark = 0 AND t2.delete_mark = 0
        AND t3.stduy_status = 'FB3002'
        AND t3.archives_status = 'FB2901'
        <if test="dto.gradeId != null">
            and t4.grade_id = #{dto.gradeId}
        </if>
        <if test="dto.classId != null">
            and t4.id = #{dto.classId}
        </if>
        <if test="dto.gender != null and dto.gender != ''">
            and t2.gender = #{dto.gender}
        </if>
        <if test="dto.isDistribute != null">
            <if test="dto.isDistribute == 1">
                and t5.student_user_id is not null
            </if>
            <if test="dto.isDistribute == 0">
                and t5.student_user_id is null
            </if>
        </if>
        <if test="dto.studentName != null and dto.studentName != ''">
            and t2.name like concat('%', #{dto.studentName}, '%')
        </if>
        <if test="dto.payStatus != null">
            and (SELECT (SUM(factrecarmny) >= SUM(factar)) FROM pb_v_xsxxsfytb WHERE Studentcode = t2.credential_number) = #{dto.payStatus}
        </if>
    </select>

    <select id="getBedStudentInfo" parameterType="com.xjrsoft.module.room.dto.AdjustBedPageDto" resultType="com.xjrsoft.module.room.vo.AdjustBedStudentPageVo">
        SELECT t1.id,t3.name AS build_name,t2.room_name,t5.name AS gender_cn,t1.bed_number,t4.name AS student_name,
        t4.id as student_user_id,t2.is_max,t2.id as room_id,t8.name as class_name,t9.name as grade_name,t10.id as teacher_id,t10.name as teacher_name FROM room_bed t1
        LEFT JOIN room t2 ON t1.room_id = t2.id
        LEFT JOIN base_office_build t3 ON t2.office_build_id = t3.id
        LEFT JOIN xjr_user t4 ON t1.student_user_id = t4.id
        LEFT JOIN xjr_dictionary_detail t5 ON t2.gender = t5.code AND t5.item_id = 2023000000000000004
        LEFT JOIN base_student_school_roll t7 ON t4.id = t7.user_id
        left join base_class t8 on t7.class_id = t8.id
        LEFT JOIN base_grade t9 ON t7.grade_id = t9.id
        left join xjr_user t10 on t8.teacher_id = t10.id
        WHERE t1.delete_mark = 0 AND t2.delete_mark = 0
        <if test="dto.isEmptyRoom != null">
            <if test="dto.isEmptyRoom == 1">
                and t2.bed_count = (SELECT COUNT(*) FROM room_bed WHERE delete_mark = 0 AND student_user_id IS NULL AND is_check_in = 0 AND room_id = t2.id)
            </if>
            <if test="dto.isEmptyRoom == 0">
                and t2.bed_count != (SELECT COUNT(*) FROM room_bed WHERE delete_mark = 0 AND student_user_id IS NULL AND is_check_in = 0 AND room_id = t2.id)
            </if>
        </if>
        <if test="dto.roomName != null">
            and t2.room_name like concat('%', #{dto.roomName}, '%')
        </if>
        <if test="dto.gender != null and dto.gender != ''">
            and t2.gender  = #{dto.gender}
        </if>
        <if test="dto.isMax != null">
            and t2.is_max = #{dto.isMax}
        </if>
        <if test="dto.floorNumber != null">
            and t2.floor_number = #{dto.floorNumber}
        </if>
        <if test="dto.floorNumber != null">
            and t2.floor_number = #{dto.floorNumber}
        </if>
        <if test="dto.classId != null">
            AND t2.id IN (
            SELECT DISTINCT c1.room_id FROM room_bed c1
            INNER JOIN base_student_school_roll c2 ON c1.student_user_id = c2.user_id
            WHERE c2.class_id = #{dto.classId}
            )
        </if>
        <if test="dto.buildId != null">
            and t3.id = #{dto.buildId}
        </if>
        <if test="dto.roomId != null">
            and t2.id = #{dto.roomId}
        </if>
        ORDER BY t2.sort_code,t1.sort_code
    </select>

    <select id="getNoBedStudent" parameterType="com.xjrsoft.module.room.dto.AdjustBedPageDto" resultType="com.xjrsoft.module.room.vo.NoBedStudentPageVo">
        SELECT t1.user_id, t2.name AS student_name,REPLACE(REPLACE(t2.gender,'SB10001','男'),'SB10002','女') as gender_cn,t4.name AS class_name FROM base_student t1
        INNER JOIN xjr_user t2 ON t1.user_id = t2.id
        LEFT JOIN base_student_school_roll t3 ON t1.user_id = t3.user_id
        LEFT JOIN base_class t4 ON t3.class_id = t4.id
        WHERE t1.delete_mark = 0 AND t2.delete_mark = 0
        AND t3.stduy_status = 'FB3001'
        AND t2.archives_status = 'FB2901'
        AND t1.user_id NOT IN (
            SELECT student_user_id FROM room_bed WHERE delete_mark = 0 AND student_user_id IS NOT NULL
        )
    </select>
    <select id="getBedInfoByUserId" resultType="com.xjrsoft.module.room.vo.RoomBedInfoVo">
        SELECT t3.name as build_name,t2.room_name,t1.bed_number,t1.id FROM room_bed t1
        LEFT JOIN room t2 ON t1.room_id = t2.id
        LEFT JOIN base_office_build t3 ON t2.office_build_id = t3.id
        WHERE t3.build_type = 'FB3604'
        AND t1.student_user_id = #{id}
    </select>
    <select id="getStudentPayStatus" parameterType="com.xjrsoft.module.room.dto.DistributeRoomBedDto" resultType="com.xjrsoft.module.room.vo.StudentPayStatusVo">
        SELECT DISTINCT t1.id,(t3.factrecarmny >= t3.factar) AS pay_status FROM xjr_user t1
        INNER JOIN base_student t2 ON t1.id = t2.user_id
        INNER JOIN pb_v_xsxxsfytb t3 ON t1.credential_number = t3.Studentcode
        INNER JOIN base_student_school_roll t4 ON t1.id = t4.user_id
        WHERE t1.delete_mark = 0 and t4.stduy_status = 'FB3002' AND t3.feeitemcode = 103042766003001
        <if test="dto.classIds != null">
            and t4.class_id in
            <foreach item="classId" index="index" collection="dto.classIds" open="(" close=")" separator=",">
                #{classId}
            </foreach>
        </if>
    </select>

    <select id="getClassTeacherInfo" parameterType="com.xjrsoft.module.room.dto.AdjustClassPageDto" resultType="com.xjrsoft.module.room.vo.AdjustBedClassPageVo">
        SELECT t1.id,t1.name,t2.id AS teacher_id, t2.name AS teacher_name FROM base_class t1
        LEFT JOIN xjr_user t2 ON t1.teacher_id = t2.id
        WHERE t1.delete_mark = 0
        <if test="dto.gradeId != null">
            and t1.grade_id = #{dto.gradeId}
        </if>
        <if test="dto.classId != null">
            and t1.id = #{dto.classId}
        </if>
    </select>

    <select id="getRoomNameByStudentUserId" resultType="java.lang.String">
        SELECT t1.room_name FROM room t1
        INNER JOIN room_bed t2 ON t1.id = t2.room_id
        WHERE t1.delete_mark = 0 AND t1.delete_mark = 0
        AND t2.student_user_id = #{id}
    </select>
</mapper>