<?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.campus.activity.mapper.RegistrationMapper">

    <select id="selectMyRegistrations" resultType="com.campus.activity.vo.RegistrationVO">
        SELECT r.id,
               r.activity_id as activityId,
               r.ticket_code as ticketCode,
               r.ticket_pdf_url as ticketPdfUrl,
               r.status,
               r.created_at as createdAt,
               r.canceled_at as canceledAt,
               a.title as activityTitle,
               a.start_time as activityStartTime,
               a.end_time as activityEndTime,
               a.location as activityLocation
        FROM registration r
        LEFT JOIN activity a ON r.activity_id = a.id
        WHERE r.user_id = #{userId}
        <if test="status != null">
            AND r.status = #{status}
        </if>
        ORDER BY r.created_at DESC
    </select>

    <select id="selectActivityRegistrations" resultType="com.campus.activity.vo.RegistrationVO">
        SELECT r.id,
               r.activity_id as activityId,
               r.ticket_code as ticketCode,
               r.ticket_pdf_url as ticketPdfUrl,
               r.status,
               r.created_at as createdAt,
               r.canceled_at as canceledAt,
               a.title as activityTitle,
               a.start_time as activityStartTime,
               a.end_time as activityEndTime,
               a.location as activityLocation
        FROM registration r
        LEFT JOIN activity a ON r.activity_id = a.id
        WHERE r.activity_id = #{activityId}
        ORDER BY r.created_at DESC
    </select>

    <select id="selectByTicketCode" resultType="com.campus.activity.entity.Registration">
        SELECT *
        FROM registration
        WHERE ticket_code = #{ticketCode}
          AND status = 1
    </select>

</mapper>