<?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" >
<!-- 定义该映射文件的命名空间，与 DormCleanDao 接口对应 -->
<mapper namespace="com.itheima.dao.DormCleanDao" >

    <!-- 分页查询宿舍卫生信息 -->
    <select id="getDormCleanList" parameterType="DormClean" resultType="DormClean">
        <!-- 查询宿舍卫生表中的数据 -->
        select * from d_dormgrade
        <where>
            <!-- 根据宿舍 ID 模糊查询 -->
            <if test="d_id!=null and d_id!=0">
                and d_id like '%${d_id}%'
            </if>
            <!-- 根据宿舍楼名称模糊查询 -->
            <if test="d_dormbuilding !=null and d_dormbuilding !=''">
                and d_dormbuilding like '%${d_dormbuilding}%'
            </if>
        </where>
        <!-- 按宿舍卫生记录 ID 升序排序 -->
        ORDER BY g_id asc
        <!-- 分页限制 -->
        limit #{currentPage},#{pageSize}
    </select>

    <!-- 查询宿舍卫生记录总数 -->
    <select id="totalCount" resultType="Integer">
        select count(g_id) from d_dormgrade
        <where>
            <!-- 根据宿舍 ID 模糊查询 -->
            <if test="d_id!=null and d_id!=0">
                and d_id like '%${d_id}%'
            </if>
            <!-- 根据宿舍楼名称模糊查询 -->
            <if test="d_dormbuilding !=null and d_dormbuilding !=''">
                and d_dormbuilding like '%${d_dormbuilding}%'
            </if>
        </where>
    </select>

    <!-- 添加宿舍卫生信息 -->
    <insert id="addDormClean" parameterType="DormClean" keyProperty="g_id" useGeneratedKeys="true">
        <!-- 插入宿舍卫生记录，包括宿舍 ID、宿舍楼名称、卫生评分和创建/更新时间 -->
        insert into d_dormgrade (d_id, d_dormbuilding, d_grade, create_time, update_time)
        values (#{d_id}, #{d_dormbuilding}, #{d_grade}, now(), now())
    </insert>

    <!-- 通过卫生记录 ID 删除宿舍卫生信息 -->
    <delete id="deleteDormClean" parameterType="Integer">
        delete from d_dormgrade where g_id=#{g_id}
    </delete>

    <!-- 根据卫生记录 ID 查询宿舍卫生信息 -->
    <select id="findDormCleanById" parameterType="Integer" resultType="DormClean">
        select * from d_dormgrade where g_id=#{g_id}
    </select>

    <!-- 修改宿舍卫生信息 -->
    <update id="updateDormClean" parameterType="DormClean">
        update d_dormgrade
        <set>
            <!-- 如果宿舍 ID 不为空，更新宿舍 ID -->
            <if test="d_id!=null and d_id!=0">
                d_id=#{d_id},
            </if>
            <!-- 如果宿舍楼名称不为空，更新宿舍楼名称 -->
            <if test="d_dormbuilding !=null and d_dormbuilding !=''">
                d_dormbuilding=#{d_dormbuilding},
            </if>
            <!-- 如果卫生评分不为空，更新卫生评分 -->
            <if test="d_grade!=null and d_grade!=0">
                d_grade=#{d_grade},
            </if>
            <!-- 更新修改时间为当前时间 -->
            <if test="update_time != null">
                update_time = now(),
            </if>
        </set>
        <!-- 根据卫生记录 ID 更新数据 -->
        where g_id = #{g_id}
    </update>

    <!-- 查询所有宿舍卫生记录 -->
    <select id="getAll" resultType="DormClean">
        select * from d_dormgrade;
    </select>

    <!-- 宿舍卫生信息结果映射 -->
    <resultMap type="com.itheima.po.DormClean" id="cardAndInfo2">
        <!-- 定义主键 -->
        <id property="d_id" column="d_id"/>
        <!-- 定义宿舍楼字段 -->
        <result property="d_dormbuilding" column="d_dormbuilding"/>
    </resultMap>

</mapper>
