<?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.admissionticket.mapper.AdmissionTicketMapper">

    <resultMap type="admissionTicket" id="AdmissionTicketResult">
        <result property="id" column="id"/>
        <result property="venueId" column="venue_id"/>
        <result property="siteName" column="site_name"/>
        <result property="siteType" column="site_type"/>
        <result property="session" column="session"/>
        <result property="price" column="price"/>
        <result property="ticketDate" column="ticket_date"/>
        <result property="admissionTicketStatus" column="admission_ticket_status"/>
        <result property="venueName"    column="venue_name"    />
        <result property="sort"    column="sort"    />
    </resultMap>

    <insert id="insertAdmissionTicketToList" parameterType="list">
        insert into beilv_admission_ticket
        (id, venue_id, site_name, site_type, session, price, ticket_date, admission_ticket_status, sort)
        values
        <foreach collection="list" item="ticket" separator=",">
            (
            #{ticket.id},
            #{ticket.venueId},
            #{ticket.siteName},
            #{ticket.siteType},
            #{ticket.session},
            #{ticket.price},
            #{ticket.ticketDate},
            #{ticket.admissionTicketStatus}
            #{ticket.sort}
            )
        </foreach>
    </insert>

    <sql id="selectBeilvAdmissionTicketVo">
        select id, venue_id, site_name, site_type, session, price, ticket_date, admission_ticket_status, sort
        from beilv_admission_ticket
    </sql>

    <select id="selectBeilvAdmissionTicketList" parameterType="AdmissionTicket" resultMap="AdmissionTicketResult">
        SELECT
        ts.id,
        ts.venue_id,
        bs.name as site_name,
        ts.site_type,
        ts.SESSION,
        ts.price,
        ts.ticket_date,
        bv.venue_name,
        ts.admission_ticket_status,
        ts.sort
        from beilv_admission_ticket ts
        left join beilv_venue bv on ts.venue_id = bv.id
        left join beilv_site bs on ts.site_name = bs.id
        <where>
            <if test="siteName != null and siteName != ''">and bs.id = #{siteName}</if>
            <if test="siteType != null and siteType != ''">and ts.site_type = #{siteType}</if>
            <if test="ticketDate != null ">and ts.ticket_date = #{ticketDate}</if>
            <if test="admissionTicketStatus != null  and admissionTicketStatus != ''">
                <choose>
                    <when test="admissionTicketStatus == -1">
                        and ts.admission_ticket_status in ('0', '1')
                    </when>
                    <otherwise>
                        and ts.admission_ticket_status = #{admissionTicketStatus}
                    </otherwise>
                </choose>
            </if>
        </where>
        order by site_name, sort
    </select>

    <select id="selectBeilvAdmissionTicketById" parameterType="String" resultMap="AdmissionTicketResult">
        SELECT
            ts.id,
            ts.venue_id,
            ts.site_name,
            ts.site_type,
            ts.SESSION,
            ts.price,
            ts.ticket_date,
            bv.venue_name,
            ts.admission_ticket_status
        from beilv_admission_ticket ts
                 left join beilv_venue bv on ts.venue_id = bv.id
        where ts.id = #{id}
    </select>

    <insert id="insertBeilvAdmissionTicket" parameterType="AdmissionTicket">
        insert into beilv_admission_ticket
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="id != null">id,</if>
            <if test="venueId != null">venue_id,</if>
            <if test="siteName != null">site_name,</if>
            <if test="siteType != null">site_type,</if>
            <if test="session != null">session,</if>
            <if test="price != null">price,</if>
            <if test="ticketDate != null">ticket_date,</if>
            <if test="admissionTicketStatus != null">admission_ticket_status,</if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="id != null">#{id},</if>
            <if test="venueId != null">#{venueId},</if>
            <if test="siteName != null">#{siteName},</if>
            <if test="siteType != null">#{siteType},</if>
            <if test="session != null">#{session},</if>
            <if test="price != null">#{price},</if>
            <if test="ticketDate != null">#{ticketDate},</if>
            <if test="admissionTicketStatus != null">#{admissionTicketStatus},</if>
        </trim>
    </insert>

    <update id="updateBeilvAdmissionTicket" parameterType="AdmissionTicket">
        update beilv_admission_ticket
        <trim prefix="SET" suffixOverrides=",">
            <if test="venueId != null">venue_id = #{venueId},</if>
            <if test="siteName != null">site_name = #{siteName},</if>
            <if test="siteType != null">site_type = #{siteType},</if>
            <if test="session != null">session = #{session},</if>
            <if test="price != null">price = #{price},</if>
            <if test="ticketDate != null">ticket_date = #{ticketDate},</if>
            <if test="admissionTicketStatus != null">admission_ticket_status = #{admissionTicketStatus},</if>
        </trim>
        where id = #{id}
    </update>

    <delete id="deleteBeilvAdmissionTicketById" parameterType="String">
        delete
        from beilv_admission_ticket
        where id = #{id}
    </delete>

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

    <update id="updateTicketStatus" parameterType="list">
        update beilv_admission_ticket set admission_ticket_status = '3'
        where id in
        <foreach item="id" collection="list" open="(" close=")" separator=",">
            #{id}
        </foreach>
    </update>

    <select id="isOk" parameterType="BootACourseBO" resultType="boolean">
        select CASE
                   WHEN admission_ticket_status = 0 THEN true
                   ELSE false
                   END AS result
        FROM
            beilv_admission_ticket
        WHERE
            id = #{ticketId}
    </select>

    <select id="getThresholdValue" parameterType="string" resultType="map">
        select bv.threshold_value as thresholdValue, bat.session, bat.ticket_date as ticketDate
        from beilv_admission_ticket bat
        left join beilv_venue bv on bat.venue_id = bv.id
        where bat.id = #{ticketId}
    </select>

</mapper>