<?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.zhentao.mapper.ActivityRegistrationMapper">

    <resultMap id="BaseResultMap" type="com.zhentao.entity.ActivityRegistration">
            <id property="id" column="id" />
            <result property="activityId" column="activity_id" />
            <result property="userId" column="user_id" />
            <result property="registrationTime" column="registration_time" />
            <result property="status" column="status" />
            <result property="paymentStatus" column="payment_status" />
            <result property="createdTime" column="created_time" />
            <result property="updatedTime" column="updated_time" />
            <!-- 关联查询字段 -->
            <result property="activityName" column="activity_name" />
            <result property="userName" column="user_name" />
            <result property="userPhone" column="user_phone" />
            <result property="userGender" column="user_gender" />
    </resultMap>

    <resultMap id="RegistrationWithDetailsMap" type="com.zhentao.entity.ActivityRegistration">
            <id property="id" column="id" />
            <result property="activityId" column="activity_id" />
            <result property="userId" column="user_id" />
            <result property="registrationTime" column="registration_time" />
            <result property="status" column="status" />
            <result property="paymentStatus" column="payment_status" />
            <result property="createdTime" column="created_time" />
            <result property="updatedTime" column="updated_time" />
            <!-- 关联查询字段 -->
            <result property="activityName" column="activity_name" />
            <result property="userName" column="user_name" />
            <result property="userPhone" column="user_phone" />
            <result property="userGender" column="user_gender" />
    </resultMap>

    <sql id="Base_Column_List">
        ar.id,ar.activity_id,ar.user_id,ar.registration_time,ar.status,
        1 as payment_status,
        ar.created_time,ar.updated_time
    </sql>

    <!-- 关联查询字段 -->
    <sql id="Join_Column_List">
        <include refid="Base_Column_List"/>,
        a.name as activity_name,
        u.nickname as user_name,
        u.phone as user_phone,
        u.gender as user_gender
    </sql>

    <!-- 分页查询报名列表（关联查询活动和用户信息） -->
    <select id="selectRegistrationList" resultMap="RegistrationWithDetailsMap">
        SELECT
        <include refid="Join_Column_List"/>
        FROM activity_registration ar
        LEFT JOIN activity a ON ar.activity_id = a.id
        LEFT JOIN users u ON ar.user_id = u.user_id
        <where>
            <if test="activityId != null">
                AND ar.activity_id = #{activityId}
            </if>
            <if test="checkInStatus != null">
                AND ar.status = #{checkInStatus}
            </if>
            <!-- 支付状态筛选（如果数据库有payment_status字段，可以启用此条件） -->
            <!-- 
            <if test="paymentStatus != null">
                AND IFNULL(ar.payment_status, 1) = #{paymentStatus}
            </if>
            -->
            <if test="keyword != null and keyword != ''">
                AND (u.nickname LIKE CONCAT('%', #{keyword}, '%')
                OR u.phone LIKE CONCAT('%', #{keyword}, '%'))
            </if>
        </where>
        ORDER BY ar.registration_time DESC
    </select>
</mapper>
