<?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.ReservationMapper">

    <resultMap type="com.amms.domain.Reservation" id="ReservationResult">
        <result property="id"    column="id"    />
        <result property="userId"    column="user_id"    />
        <result property="username"    column="username"    />
        <result property="nickname"    column="nickname"    />
        <result property="timeSlotId"    column="time_slot_id"    />
        <result property="totalVisitors"    column="total_visitors"    />
        <result property="reserveTime"    column="reserve_time"    />
        <result property="qrCode"    column="qr_code"    />
        <result property="status"    column="status"    />
        <result property="createTime"    column="create_time"    />
        <result property="updateTime"    column="update_time"    />
        <result property="updater"    column="updater"    />
        <result property="remark"    column="remark"    />
        <association property="reservationTimeSlot" column="time_slot_id" select="com.amms.mapper.ReservationTimeSlotMapper.selectReservationTimeSlotById"/>
    </resultMap>

    <sql id="selectReservationVo">
        select r.id,
               r.user_id,
               u.username as username,
               u.nickname as nickname,
               r.time_slot_id,
               r.total_visitors,
               r.reserve_time,
               r.qr_code,
               r.status,
               r.create_time,
               r.update_time,
               r.updater,
               r.remark
        from reservation r
        left join sys_user u on r.user_id = u.id
    </sql>

    <select id="selectReservationList" parameterType="com.amms.domain.Reservation" resultMap="ReservationResult">
        <include refid="selectReservationVo"/>
        <where>
            <if test="userId != null "> and r.user_id = #{userId}</if>
            <if test="username != null and username != ''"> and u.username like concat('%', #{username}, '%')</if>
            <if test="nickname != null and nickname != ''"> and u.nickname like concat('%', #{nickname}, '%')</if>
            <if test="timeSlotId != null "> and r.time_slot_id = #{timeSlotId}</if>
            <if test="totalVisitors != null "> and r.total_visitors = #{totalVisitors}</if>
            <if test="reserveTime != null "> and r.reserve_time = #{reserveTime}</if>
            <if test="qrCode != null  and qrCode != ''"> and r.qr_code = #{qrCode}</if>
            <if test="status != null "> and r.status = #{status}</if>
            <if test="updater != null "> and r.updater = #{updater}</if>
            <if test="startDate != null">
                and exists (
                    select 1 from reservation_time_slot ts
                    where ts.id = r.time_slot_id and ts.date <![CDATA[ >= ]]> #{startDate}
                )
            </if>
            <if test="endDate != null">
                and exists (
                    select 1 from reservation_time_slot ts
                    where ts.id = r.time_slot_id and ts.date <![CDATA[ <= ]]> #{endDate}
                )
            </if>
        </where>
    </select>

    <select id="selectReservationById" parameterType="Long" resultMap="ReservationResult">
        <include refid="selectReservationVo"/>
        where r.id = #{id}
    </select>

    <insert id="insertReservation" parameterType="com.amms.domain.Reservation" useGeneratedKeys="true" keyProperty="id">
        insert into reservation
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="userId != null">user_id,</if>
            <if test="timeSlotId != null">time_slot_id,</if>
            <if test="totalVisitors != null">total_visitors,</if>
            <if test="reserveTime != null">reserve_time,</if>
            <if test="qrCode != null">qr_code,</if>
            <if test="status != null">status,</if>
            <if test="createTime != null">create_time,</if>
            <if test="updateTime != null">update_time,</if>
            <if test="updater != null">updater,</if>
            <if test="remark != null">remark,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="userId != null">#{userId},</if>
            <if test="timeSlotId != null">#{timeSlotId},</if>
            <if test="totalVisitors != null">#{totalVisitors},</if>
            <if test="reserveTime != null">#{reserveTime},</if>
            <if test="qrCode != null">#{qrCode},</if>
            <if test="status != null">#{status},</if>
            <if test="createTime != null">#{createTime},</if>
            <if test="updateTime != null">#{updateTime},</if>
            <if test="updater != null">#{updater},</if>
            <if test="remark != null">#{remark},</if>
         </trim>
    </insert>

    <update id="updateReservation" parameterType="com.amms.domain.Reservation">
        update reservation
        <trim prefix="SET" suffixOverrides=",">
            <if test="userId != null">user_id = #{userId},</if>
            <if test="timeSlotId != null">time_slot_id = #{timeSlotId},</if>
            <if test="totalVisitors != null">total_visitors = #{totalVisitors},</if>
            <if test="reserveTime != null">reserve_time = #{reserveTime},</if>
            <if test="qrCode != null">qr_code = #{qrCode},</if>
            <if test="status != null">status = #{status},</if>
            <if test="createTime != null">create_time = #{createTime},</if>
            <if test="updateTime != null">update_time = #{updateTime},</if>
            <if test="updater != null">updater = #{updater},</if>
            <if test="remark != null">remark = #{remark},</if>
        </trim>
        where id = #{id}
    </update>

    <delete id="deleteReservationById" parameterType="Long">
        delete from reservation where id = #{id}
    </delete>
</mapper>
