<?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.competition.mapper.CompetitionMapper">
    
    <resultMap type="Competition" id="CompetitionResult">
        <result property="id"    column="id"    />
        <result property="competitionTitle"    column="competition_title"    />
        <result property="competitionType"    column="competition_type"    />
        <result property="personMax"    column="person_max"    />
        <result property="teamMax"    column="team_max"    />
        <result property="competitionPlace"    column="competition_place"    />
        <result property="applyStartTime"    column="apply_start_time"    />
        <result property="applyEndTime"    column="apply_end_time"    />
        <result property="applyBeforeTime"    column="apply_before_time"    />
        <result property="competitionMaximum"    column="competition_maximum"    />
        <result property="competitionExpense"    column="competition_expense"    />
        <result property="competitionDetails"    column="competition_details"    />
        <result property="registrationNotes"    column="registration_notes"    />
        <result property="competitionState"    column="competition_state"    />
        <result property="viewingTicket"    column="viewing_ticket"    />
        <result property="createTime"    column="create_time"    />
        <result property="createBy"    column="create_by"    />
        <result property="createName"    column="create_name"    />
        <result property="updateTime"    column="update_time"    />
        <result property="updateBy"    column="update_by"    />
        <result property="updateName"    column="update_name"    />
        <result property="publishTime"    column="publish_time"    />
        <result property="publishBy"    column="publish_by"    />
        <result property="publishName"    column="publish_name"    />
    </resultMap>

    <sql id="selectCompetitionVo">
        select id, competition_title, competition_type, person_max, team_max,
               competition_place, apply_start_time, apply_end_time, apply_before_time,
               competition_maximum, competition_expense, viewing_ticket,
               competition_details, registration_notes, competition_state,
               create_time, create_by, create_name, update_time, update_by, update_name, publish_time, publish_by, publish_name
        from beilv_competition
    </sql>

    <select id="selectCompetitionList" parameterType="Competition" resultMap="CompetitionResult">
        <include refid="selectCompetitionVo"/>
        <where>  
            <if test="competitionTitle != null  and competitionTitle != ''"> and competition_title like concat('%', #{competitionTitle}, '%')</if>
            <if test="competitionType != null  and competitionType != ''"> and competition_type = #{competitionType}</if>
            <if test="competitionPlace != null  and competitionPlace != ''"> and competition_place like concat('%', #{competitionPlace}, '%')</if>
            <if test="applyStartTime != null "> and apply_start_time = #{applyStartTime}</if>
            <if test="competitionState != null  and competitionState != ''"> and competition_state = #{competitionState}</if>
        </where>
        order by create_time desc
    </select>
    
    <select id="selectCompetitionById" parameterType="Integer" resultMap="CompetitionResult">
        <include refid="selectCompetitionVo"/>
        where id = #{id}
    </select>

    <insert id="insertCompetition" parameterType="Competition" useGeneratedKeys="true" keyProperty="id">
        insert into beilv_competition
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="competitionTitle != null">competition_title,</if>
            <if test="competitionType != null">competition_type,</if>
            <if test="personMax != null">person_max,</if>
            <if test="teamMax != null">team_max,</if>
            <if test="competitionPlace != null">competition_place,</if>
            <if test="applyStartTime != null">apply_start_time,</if>
            <if test="applyEndTime != null">apply_end_time,</if>
            <if test="applyBeforeTime != null">apply_before_time,</if>
            <if test="competitionMaximum != null">competition_maximum,</if>
            <if test="competitionExpense != null">competition_expense,</if>
            <if test="viewingTicket != null">viewing_ticket,</if>
            <if test="competitionDetails != null">competition_details,</if>
            <if test="registrationNotes != null">registration_notes,</if>
            <if test="competitionState != null">competition_state,</if>
            <if test="createTime != null">create_time,</if>
            <if test="updateTime != null">update_time,</if>
            <if test="publishTime != null">publish_time,</if>
            <if test="createBy != null">create_by,</if>
            <if test="updateBy != null">update_by,</if>
            <if test="publishBy != null">publish_by,</if>
            <if test="createName != null">create_name,</if>
            <if test="updateName != null">update_name,</if>
            <if test="publishName != null">publish_name,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="competitionTitle != null">#{competitionTitle},</if>
            <if test="competitionType != null">#{competitionType},</if>
            <if test="personMax != null">#{personMax},</if>
            <if test="teamMax != null">#{teamMax},</if>
            <if test="competitionPlace != null">#{competitionPlace},</if>
            <if test="applyStartTime != null">#{applyStartTime},</if>
            <if test="applyEndTime != null">#{applyEndTime},</if>
            <if test="applyBeforeTime != null">#{applyBeforeTime},</if>
            <if test="competitionMaximum != null">#{competitionMaximum},</if>
            <if test="competitionExpense != null">#{competitionExpense},</if>
            <if test="viewingTicket != null">#{viewingTicket},</if>
            <if test="competitionDetails != null">#{competitionDetails},</if>
            <if test="registrationNotes != null">#{registrationNotes},</if>
            <if test="competitionState != null">#{competitionState},</if>
            <if test="createTime != null">#{createTime},</if>
            <if test="updateTime != null">#{updateTime},</if>
            <if test="publishTime != null">#{publishTime},</if>
            <if test="createBy != null">#{createBy},</if>
            <if test="updateBy != null">#{updateBy},</if>
            <if test="publishBy != null">#{publishBy},</if>
            <if test="createName != null">#{createName},</if>
            <if test="updateName != null">#{updateName},</if>
            <if test="publishName != null">#{publishName},</if>
         </trim>
    </insert>

    <update id="updateCompetition" parameterType="Competition">
        update beilv_competition
        <trim prefix="SET" suffixOverrides=",">
            <if test="competitionTitle != null">competition_title = #{competitionTitle},</if>
            <if test="competitionType != null">competition_type = #{competitionType},</if>
            <if test="personMax != null">person_max = #{personMax},</if>
            <if test="teamMax != null">team_max = #{teamMax},</if>
            <if test="competitionPlace != null">competition_place = #{competitionPlace},</if>
            <if test="applyStartTime != null">apply_start_time = #{applyStartTime},</if>
            <if test="applyEndTime != null">apply_end_time = #{applyEndTime},</if>
            <if test="applyBeforeTime != null">apply_before_time = #{applyBeforeTime},</if>
            <if test="competitionMaximum != null">competition_maximum = #{competitionMaximum},</if>
            <if test="competitionExpense != null">competition_expense = #{competitionExpense},</if>
            <if test="viewingTicket != null">viewing_ticket = #{viewingTicket},</if>
            <if test="competitionDetails != null">competition_details = #{competitionDetails},</if>
            <if test="registrationNotes != null">registration_notes = #{registrationNotes},</if>
            <if test="competitionState != null">competition_state = #{competitionState},</if>
            <if test="updateTime != null">update_time = #{updateTime},</if>
            <if test="updateBy != null">update_by = #{updateBy},</if>
            <if test="updateName != null">update_name = #{updateName},</if>
        </trim>
        where id = #{id}
    </update>

    <delete id="deleteCompetitionById" parameterType="Integer">
        delete from beilv_competition where id = #{id}
    </delete>

    <delete id="deleteCompetitionByIds" parameterType="String">
        delete from beilv_competition where id in
        <foreach item="id" collection="array" open="(" separator="," close=")">
            #{id}
        </foreach>
    </delete>

    <update id="publishCompetition" parameterType="Competition">
        update beilv_competition set
        competition_state = #{competitionState},
        publish_time = #{publishTime},
        publish_by = #{publishBy},
        publish_name = #{publishName}
        where id = #{id}
    </update>

    <select id="getCompetitionList" resultMap="CompetitionResult">
        select * from beilv_competition where competition_state = 'competiton_state_2'
    </select>

    <select id="selectStadumIsOpenById" parameterType="string" resultType="boolean">
        SELECT
            CASE
                WHEN COUNT(1) > 0 THEN false
                ELSE true
                END AS result
        FROM
            beilv_book_a_race
        WHERE
            payment_status = 'payment_status_have_paid'
          AND competition_id = #{id}
    </select>

    <update id="updateBookARace" parameterType="int">
        update beilv_book_a_race
        set payment_status = 'payment_status_verification'
        where competition_id = #{id} and payment_status = 'payment_status_have_paid'
    </update>

    <select id="getCompetitionInfo" parameterType="string" resultMap="CompetitionResult">
        select bc.* from beilv_book_a_race bar left join beilv_competition bc on bar.competition_id = bc.id
        where bar.order_id = #{orderId}
    </select>

</mapper>