<?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"    />
    </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 from 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>
    </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 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>
         </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>
         </trim>
    </insert>

    <update id="updateCompetition" parameterType="Competition">
        update 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>
        </trim>
        where id = #{id}
    </update>

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

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

    <update id="publishCompetition" parameterType="Competition">
        update competition set competition_state = #{competitionState}
        where id = #{id}
    </update>

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

</mapper>