<?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.bootacourse.mapper.BootACourseMapper">

    <resultMap type="BootACourse" id="BootACourseResult">
        <result property="id" column="id"/>
        <result property="userId" column="user_id"/>
        <result property="contactPeople" column="contact_people"/>
        <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="ticketId" column="ticket_id"/>
        <result property="orderType" column="order_type"/>
        <result property="refundInstructions" column="refund_instructions"/>
        <result property="clubCardId" column="club_card_id"/>
        <result property="refund" column="refund"/>
        <result property="verificationTime" column="verification_time"/>

        <result property="session" column="session"/>
        <result property="ticketDate" column="ticket_date"/>
        <result property="siteName" column="site_name"/>
        <result property="cover" column="cover"/>
    </resultMap>


    <insert id="addCourse" parameterType="bootACourse">
        insert into beilv_book_a_course
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="userId != null">user_id,</if>
            <if test="contactPeople != null">contact_people,</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="ticketId != null">ticket_id,</if>
            <if test="orderType != null">order_type,</if>
            <if test="refundInstructions != null">refund_instructions,</if>
            <if test="clubCardId != null">club_card_id,</if>
            <if test="refund != null">refund,</if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="userId != null">#{userId},</if>
            <if test="contactPeople != null">#{contactPeople},</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="ticketId != null">#{ticketId},</if>
            <if test="orderType != null">#{orderType},</if>
            <if test="refundInstructions != null">#{refundInstructions},</if>
            <if test="clubCardId != null">#{clubCardId},</if>
            <if test="refund != null">#{refund},</if>
        </trim>
    </insert>

    <update id="updateCourse" parameterType="bootACourse">
        update beilv_book_a_course
        <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="getCourseList" parameterType="bootACourseBO" resultMap="BootACourseResult">
        select
        bbac.user_id, bbac.contact_people, bbac.contact_number, bbac.registration_time, bbac.payment_time, bbac.cancellation_time, bbac.refund_time,
        bbac.payment_status, bbac.order_id, bbac.ticket_id, bbac.order_type, bbac.refund_instructions, bbac.club_card_id, bbac.refund, bbac.verification_time,
        bat.session, bat.ticket_date, bs.name as site_name, bs.cover
        from beilv_book_a_course bbac
        left join beilv_admission_ticket bat on bbac.ticket_id = bat.id
        left join beilv_site bs on bat.site_id = bs.id
        <where>
            <if test="userId != null and userId != ''">
                and bbac.user_id = #{userId}
            </if>
            <if test="paymentStatus != null and paymentStatus != ''">
                and bbac.paymen_status = #{paymentStatus}
            </if>
        </where>
        order by bbac.registration_time desc
    </select>

    <select id="getCourseInfo" parameterType="bootACourseBO" resultMap="BootACourseResult">
        select * from beilv_book_a_course
        <where>
            <if test="userId != null and userId != ''">
                and user_id = #{userId}
            </if>
            <if test="orderId != null and orderId != ''">
                and order_id = #{orderId}
            </if>
        </where>
        order by registration_time desc
    </select>

    <select id="selectPaidOrders" parameterType="bootACourse" resultMap="BootACourseResult">
        SELECT
            *
        FROM
            beilv_book_a_course
        WHERE
            payment_status = 'payment_status_have_paid'
    </select>

    <update id="updateOrderStatusToVerification" parameterType="bootACourse">
        UPDATE beilv_book_a_course
        SET payment_status = 'payment_status_verification',
        verification_time = #{verificationTime}
        WHERE order_id IN
        <foreach collection="orderIds" item="orderId" open="(" separator="," close=")">
            #{orderId}
        </foreach>
    </update>
</mapper>