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

    <!-- 分页查询访客信息 -->
    <select id="getVisitorList" parameterType="Visitor" resultType="Visitor">
        <!-- 查询访客表 -->
        select * from d_visitor
        <where>
            <!-- 根据访客姓名进行模糊查询 -->
            <if test="v_name!=null and v_name!='' ">
                and v_name like '%${v_name}%'
            </if>
            <!-- 根据访客电话进行模糊查询 -->
            <if test="v_phone!=null and v_phone!=0">
                and v_phone like '%${v_phone}%'
            </if>
        </where>
        <!-- 按访客 ID 升序排序 -->
        ORDER BY v_id asc
        <!-- 实现分页功能 -->
        limit #{currentPage}, #{pageSize}
    </select>

    <!-- 查询访客记录总数 -->
    <select id="totalCount" resultType="Integer">
        <!-- 计算访客记录总数 -->
        select count(v_id) from d_visitor
        <where>
            <!-- 根据访客姓名进行模糊查询 -->
            <if test="v_name!=null and v_name!='' ">
                and v_name like '%${v_name}%'
            </if>
            <!-- 根据访客电话进行模糊查询 -->
            <if test="v_phone!=null and v_phone!=0">
                and v_phone like '%${v_phone}%'
            </if>
        </where>
    </select>

    <!-- 添加访客信息 -->
    <insert id="addVisitor" parameterType="Visitor" keyProperty="v_id" useGeneratedKeys="true">
        <!-- 插入访客信息，包括访客姓名、电话、宿舍 ID、宿舍楼号和创建时间 -->
        insert into d_visitor (v_name, v_phone, v_dormitoryid, v_dormbuilding, create_time)
        values (#{v_name}, #{v_phone}, #{v_dormitoryid}, #{v_dormbuilding}, now())
    </insert>

    <!-- 查询所有访客信息 -->
    <select id="getAll" resultType="Visitor">
        select * from d_visitor;
    </select>

</mapper>
