<?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="beilv.stadium.mapper.StadiumMapper">

    <resultMap type="Stadium" id="StadiumResult">
        <result property="id" column="id"/>
        <result property="userId" column="user_id"/>
        <result property="contactPerple" column="contact_perple"/>
        <result property="contactNumber" column="contact_number"/>
        <result property="registrationTime" column="registration_time"/>
        <result property="paymentTime" column="payment_time"/>
        <result property="cancellationTime" column="cancellation_time"/>
        <result property="refundTime" column="refund_time"/>
        <result property="paymentStatus" column="payment_status"/>
        <result property="orderId" column="order_id"/>
        <result property="competitionId" column="competition_id"/>
        <result property="orderType" column="order_type"/>
        <result property="refundInstructions" column="refund_instructions"/>
    </resultMap>


    <insert id="toBeStadium" parameterType="stadium">
        insert into beilv_book_a_race
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="userId != null">user_id,</if>
            <if test="contactPerple != null">contact_perple,</if>
            <if test="contactNumber != null">contact_number,</if>
            <if test="registrationTime != null">registration_time,</if>
            <if test="paymentTime != null">payment_time,</if>
            <if test="cancellationTime != null">cancellation_time,</if>
            <if test="refundTime != null">refund_time,</if>
            <if test="paymentStatus != null">payment_status,</if>
            <if test="orderId != null">order_id,</if>
            <if test="competitionId != null">competition_id,</if>
            <if test="orderType != null">order_type,</if>
            <if test="refundInstructions != null">refund_instructions,</if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="userId != null">#{userId},</if>
            <if test="contactPerple != null">#{contactPerple},</if>
            <if test="contactNumber != null">#{contactNumber},</if>
            <if test="registrationTime != null">#{registrationTime},</if>
            <if test="paymentTime != null">#{paymentTime},</if>
            <if test="cancellationTime != null">#{cancellationTime},</if>
            <if test="refundTime != null">#{refundTime},</if>
            <if test="paymentStatus != null">#{paymentStatus},</if>
            <if test="orderId != null">#{orderId},</if>
            <if test="competitionId != null">#{competitionId},</if>
            <if test="orderType != null">#{orderType},</if>
            <if test="refundInstructions != null">#{refundInstructions},</if>
        </trim>
    </insert>

    <update id="cancellStadium" parameterType="stadium">
        update beilv_book_a_race
        <trim prefix="SET" suffixOverrides=",">
            <if test="paymentTime != null">payment_time = #{paymentTime},</if>
            <if test="cancellationTime != null">cancellation_time = #{cancellationTime},</if>
            <if test="refundTime != null">refund_time = #{refundTime},</if>
            <if test="paymentStatus != null">payment_status = #{paymentStatus},</if>
            <if test="refundInstructions != null">refund_instructions = #{refundInstructions},</if>
        </trim>
        <where>
            <if test="orderId != null and orderId != ''">
                and order_id = #{orderId}
            </if>
            <if test="id != null and id != ''">
                and id = #{id}
            </if>
        </where>
    </update>

    <select id="getStadimList" parameterType="stadiumBO" resultMap="StadiumResult">
        select
            user_id, contact_perple, contact_number, registration_time, payment_time,
            cancellation_time, refund_time, payment_status, order_id, competition_id, order_type, refund_instructions
        from beilv_book_a_race
        <where>
            <if test="userId != null and userId != ''">
                and user_id = #{userId}
            </if>
            <if test="orderType != null and orderType != ''">
                and order_type = #{orderType}
            </if>
            <if test="paymentStatus != null and paymentStatus != ''">
                and paymen_status = #{paymentStatus}
            </if>
        </where>
    </select>

    <select id="paymentVerification" parameterType="stadium" resultType="boolean">
        SELECT
            CASE
                WHEN a.team_max > b.isPay THEN true
                ELSE false
                END AS is_available
        FROM
                (SELECT team_max FROM beilv_competition WHERE id = #{competitionId}) a,
                (SELECT COUNT(id) AS isPay FROM beilv_book_a_race WHERE competition_id = #{competitionId} AND (payment_status = 'payment_status_have_paid' OR payment_status = 'payment_status_to_be_paid	')  and order_type = '1') b;
    </select>

    <select id="getTakePartList" parameterType="stadium" resultMap="StadiumResult">
        select * from beilv_book_a_race
        <where>
            <if test="competitionId != null and competitionId != ''">
                and competition_id = #{competitionId}
            </if>
            <if test="orderType != null and orderType != ''">
                and order_type = #{orderType}
            </if>
        </where>
        order by registration_time desc
    </select>

</mapper>