<?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.amms.mapper.ReservationVisitorMapper">

    <resultMap type="com.amms.domain.ReservationVisitor" id="ReservationVisitorResult">
        <result property="id"    column="id"    />
        <result property="reservationId"    column="reservation_id"    />
        <result property="realName"    column="real_name"    />
        <result property="idCard"    column="id_card"    />
        <result property="phone"    column="phone"    />
        <result property="visitorQrCode"    column="visitor_qr_code"    />
        <result property="verifyStatus"    column="verify_status"    />
        <result property="verifyTime"    column="verify_time"    />
    </resultMap>

    <sql id="selectReservationVisitorVo">
        select id, reservation_id, real_name, id_card, phone, visitor_qr_code, verify_status, verify_time from reservation_visitor
    </sql>

    <select id="selectReservationVisitorList" parameterType="com.amms.domain.ReservationVisitor" resultMap="ReservationVisitorResult">
        <include refid="selectReservationVisitorVo"/>
        <where>
            <if test="reservationId != null "> and reservation_id = #{reservationId}</if>
            <if test="realName != null  and realName != ''"> and real_name like concat('%', #{realName}, '%')</if>
            <if test="idCard != null  and idCard != ''"> and id_card = #{idCard}</if>
            <if test="phone != null  and phone != ''"> and phone = #{phone}</if>
            <if test="visitorQrCode != null  and visitorQrCode != ''"> and visitor_qr_code = #{visitorQrCode}</if>
            <if test="verifyStatus != null "> and verify_status = #{verifyStatus}</if>
            <if test="verifyTime != null "> and verify_time = #{verifyTime}</if>
        </where>
    </select>

    <select id="selectReservationVisitorListByReserveId" parameterType="Long" resultMap="ReservationVisitorResult">
        <include refid="selectReservationVisitorVo"/>
        where reservation_id = #{reservationId}
    </select>

    <select id="selectReservationVisitorById" parameterType="Long" resultMap="ReservationVisitorResult">
        <include refid="selectReservationVisitorVo"/>
        where id = #{id}
    </select>

    <insert id="insertReservationVisitor" parameterType="com.amms.domain.ReservationVisitor" useGeneratedKeys="true" keyProperty="id">
        insert into reservation_visitor
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="reservationId != null">reservation_id,</if>
            <if test="realName != null and realName != ''">real_name,</if>
            <if test="idCard != null and idCard != ''">id_card,</if>
            <if test="phone != null and phone != ''">phone,</if>
            <if test="visitorQrCode != null">visitor_qr_code,</if>
            <if test="verifyStatus != null">verify_status,</if>
            <if test="verifyTime != null">verify_time,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="reservationId != null">#{reservationId},</if>
            <if test="realName != null and realName != ''">#{realName},</if>
            <if test="idCard != null and idCard != ''">#{idCard},</if>
            <if test="phone != null and phone != ''">#{phone},</if>
            <if test="visitorQrCode != null">#{visitorQrCode},</if>
            <if test="verifyStatus != null">#{verifyStatus},</if>
            <if test="verifyTime != null">#{verifyTime},</if>
         </trim>
    </insert>

    <update id="updateReservationVisitor" parameterType="com.amms.domain.ReservationVisitor">
        update reservation_visitor
        <trim prefix="SET" suffixOverrides=",">
            <if test="reservationId != null">reservation_id = #{reservationId},</if>
            <if test="realName != null and realName != ''">real_name = #{realName},</if>
            <if test="idCard != null and idCard != ''">id_card = #{idCard},</if>
            <if test="phone != null and phone != ''">phone = #{phone},</if>
            <if test="visitorQrCode != null">visitor_qr_code = #{visitorQrCode},</if>
            <if test="verifyStatus != null">verify_status = #{verifyStatus},</if>
            <if test="verifyTime != null">verify_time = #{verifyTime},</if>
        </trim>
        where id = #{id}
    </update>

    <delete id="deleteReservationVisitorById" parameterType="Long">
        delete from reservation_visitor where id = #{id}
    </delete>

    <!-- 根据预约ID批量删除游客预约明细 -->
    <delete id="deleteReservationVisitorsByReservationId" parameterType="Long">
        delete from reservation_visitor where reservation_id = #{reservationId}
    </delete>
</mapper>
