<?xml version="1.0" encoding="UTF-8" ?>
<!-- 声明 MyBatis 映射文件的 DOCTYPE，指定其遵循 MyBatis 3.0 的 DTD -->
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" 
    "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<!-- 定义该映射文件的命名空间，与 DormitoryDao 接口对应 -->
<mapper namespace="com.itheima.dao.DormitoryDao" >

    <!-- 分页查询宿舍信息 -->
    <select id="getDormitoryList" parameterType="Dormitory" resultType="Dormitory">
        <!-- 查询宿舍信息表中的数据 -->
        select * from d_dormitoryinfo
        <where>
            <!-- 根据宿舍管理员姓名模糊查询 -->
            <if test="a_name!=null and a_name!=''">
                and a_name like '%${a_name}%'
            </if>
            <!-- 根据宿舍 ID 模糊查询 -->
            <if test="s_dormitoryid!=null and s_dormitoryid!=0">
                and s_dormitoryid like '%${s_dormitoryid}%'
            </if>
            <!-- 根据宿舍楼名称模糊查询 -->
            <if test="d_dormbuilding !=null and d_dormbuilding !=''">
                and d_dormbuilding like '%${d_dormbuilding}%'
            </if>
        </where>
        <!-- 按宿舍 ID 升序排序 -->
        ORDER BY d_id asc
        <!-- 分页限制 -->
        limit #{currentPage}, #{pageSize}
    </select>

    <!-- 查询宿舍记录总数 -->
    <select id="totalCount" resultType="Integer">
        <!-- 查询宿舍信息表中的记录数 -->
        select count(s_dormitoryid) from d_dormitoryinfo
        <where>
            <!-- 根据宿舍管理员姓名模糊查询 -->
            <if test="a_name!=null and a_name!=''">
                and a_name like '%${a_name}%'
            </if>
            <!-- 根据宿舍 ID 模糊查询 -->
            <if test="s_dormitoryid!=null and s_dormitoryid!=0">
                and s_dormitoryid like '%${s_dormitoryid}%'
            </if>
            <!-- 根据宿舍楼名称模糊查询 -->
            <if test="d_dormbuilding !=null and d_dormbuilding !=''">
                and d_dormbuilding like '%${d_dormbuilding}%'
            </if>
        </where>
    </select>

    <!-- 添加宿舍信息 -->
    <insert id="addDormitory" parameterType="Dormitory" keyProperty="d_id" useGeneratedKeys="true">
        <!-- 插入宿舍信息，包括宿舍 ID、宿舍楼名称、床位总数、空余床位数和管理员姓名 -->
        insert into d_dormitoryinfo (s_dormitoryid, d_dormbuilding, d_bedtotal, d_bed, a_name)
        values (#{s_dormitoryid}, #{d_dormbuilding}, #{d_bedtotal}, #{d_bed}, #{a_name})
    </insert>

    <!-- 根据宿舍记录 ID 删除宿舍信息 -->
    <delete id="deleteDormitory" parameterType="Integer">
        delete from d_dormitoryinfo where d_id=#{d_id}
    </delete>

    <!-- 根据宿舍记录 ID 查询宿舍信息 -->
    <select id="findDormitoryById" parameterType="Integer" resultType="Dormitory">
        select * from d_dormitoryinfo where d_id=#{d_id}
    </select>

    <!-- 修改宿舍信息 -->
    <update id="updateDormitory" parameterType="Dormitory">
        update d_dormitoryinfo
        <set>
            <!-- 如果宿舍 ID 不为空，更新宿舍 ID -->
            <if test="s_dormitoryid!=null and s_dormitoryid!=0">
                s_dormitoryid=#{s_dormitoryid},
            </if>
            <!-- 如果宿舍楼名称不为空，更新宿舍楼名称 -->
            <if test="d_dormbuilding !=null and d_dormbuilding !=''">
                d_dormbuilding=#{d_dormbuilding},
            </if>
            <!-- 如果床位总数不为空，更新床位总数 -->
            <if test="d_bedtotal !=null and d_bedtotal !=''">
                d_bedtotal=#{d_bedtotal},
            </if>
            <!-- 如果空余床位数不为空，更新空余床位数 -->
            <if test="d_bed !=null and d_bed !=''">
                d_bed=#{d_bed},
            </if>
            <!-- 如果管理员姓名不为空，更新管理员姓名 -->
            <if test="a_name !=null and a_name !=''">
                a_name=#{a_name},
            </if>
        </set>
        <!-- 根据宿舍记录 ID 更新数据 -->
        where d_id = #{d_id}
    </update>

    <!-- 宿舍人员信息查询结果映射 -->
    <resultMap type="com.itheima.po.Dormitory" id="cardAndInfo2">
        <!-- 定义主键 -->
        <id property="d_id" column="d_id"/>
        <!-- 定义宿舍 ID 字段 -->
        <result property="s_dormitoryid" column="s_dormitoryid"/>
        <!-- 定义宿舍楼字段 -->
        <result property="d_dormbuilding" column="d_dormbuilding"/>
        <!-- 定义床位总数字段 -->
        <result property="d_bedtotal" column="d_bedtotal"/>
        <!-- 定义空余床位数字段 -->
        <result property="d_bed" column="d_bed"/>
        <!-- 定义管理员姓名字段 -->
        <result property="a_name" column="a_name"/>
        <!-- 定义宿舍对应的学生信息集合 -->
        <collection property="students" ofType="com.itheima.po.Student" javaType="ArrayList">
            <!-- 定义学生信息的主键和字段 -->
            <id property="s_id" column="s_id"/>
            <result property="s_studentid" column="s_studentid"/>
            <result property="s_name" column="s_name"/>
            <result property="s_sex" column="s_sex"/>
            <result property="s_age" column="s_age"/>
            <result property="s_phone" column="s_phone"/>
            <result property="s_classid" column="s_classid"/>
            <result property="s_classname" column="s_classname"/>
            <result property="s_dormitoryid" column="s_dormitoryid"/>
        </collection>
    </resultMap>

    <!-- 根据宿舍 ID 查询宿舍及其学生信息 -->
    <select id="findDormitoryStudent" parameterType="Dormitory" resultMap="cardAndInfo2">
        SELECT dormitoryinfos.*, students.*
        FROM d_dormitoryinfo dormitoryinfos
        JOIN d_student students
        ON dormitoryinfos.s_dormitoryid = students.s_dormitoryid
        WHERE dormitoryinfos.s_dormitoryid = #{s_dormitoryid}
    </select>

    <!-- 查询所有宿舍记录 -->
    <select id="getAll" resultType="Dormitory">
        select * from d_dormitoryinfo;
    </select>

</mapper>
