<?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.session.mapper.BeilvSessionMapper">
    
    <resultMap type="BeilvSession" id="BeilvSessionResult">
        <result property="id"    column="id"    />
        <result property="siteType"    column="site_type"    />
        <result property="session"    column="session"    />
        <result property="price"    column="price"    />
        <result property="venueId"    column="venue_id"    />
        <result property="sort"    column="sort"    />
        <result property="venueName"    column="venue_name"    />
    </resultMap>

    <sql id="selectBeilvSessionVo">
        select id, site_type, session, price, venue_id,sort from beilv_session
    </sql>

    <select id="selectBeilvSessionList" parameterType="BeilvSession" resultMap="BeilvSessionResult">
        SELECT
        bs.id,
        bs.site_type,
        bs.SESSION,
        bs.price,
        bs.venue_id,
        bs.sort,
        bv.venue_name
        FROM
        beilv_session bs
        left join beilv_venue bv on bs.venue_id = bv.id
        <where>  
            <if test="siteType != null  and siteType != ''"> and site_type = #{siteType}</if>
            <if test="session != null  and session != ''"> and session = #{session}</if>
            <if test="price != null "> and price = #{price}</if>
            <if test="venueId != null "> and venue_id = #{venueId}</if>
        </where>
        order by sort ASC
    </select>
    
    <select id="selectBeilvSessionById" parameterType="Long" resultMap="BeilvSessionResult">
        SELECT bs.id,
               bs.site_type,
               bs.SESSION,
               bs.price,
               bs.venue_id,
               bs.sort,
               bv.venue_name
        FROM beilv_session bs
                 left join beilv_venue bv on bs.venue_id = bv.id
        where bs.id = #{id}
    </select>

    <insert id="insertBeilvSession" parameterType="BeilvSession">
        insert into beilv_session
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="id != null">id,</if>
            <if test="siteType != null">site_type,</if>
            <if test="session != null">session,</if>
            <if test="price != null">price,</if>
            <if test="venueId != null">venue_id,</if>
            <if test="sort != null">sort,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="id != null">#{id},</if>
            <if test="siteType != null">#{siteType},</if>
            <if test="session != null">#{session},</if>
            <if test="price != null">#{price},</if>
            <if test="venueId != null">#{venueId},</if>
            <if test="sort != null">#{sort},</if>
         </trim>
    </insert>

    <update id="updateBeilvSession" parameterType="BeilvSession">
        update beilv_session
        <trim prefix="SET" suffixOverrides=",">
            <if test="siteType != null">site_type = #{siteType},</if>
            <if test="session != null">session = #{session},</if>
            <if test="price != null">price = #{price},</if>
            <if test="venueId != null">venue_id = #{venueId},</if>
            <if test="sort != null">sort = #{sort},</if>
        </trim>
        where id = #{id}
    </update>

    <delete id="deleteBeilvSessionById" parameterType="Long">
        delete from beilv_session where id = #{id}
    </delete>

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

    <select id="selectOne" parameterType="beilvSession" resultType="boolean">
        select case
                   when count(1) > 0 then true
                   else false
                   end as result
        from beilv_session
        <where>
            <if test="siteType != null and siteType != ''">
                and site_type = #{siteType}
            </if>
            <if test="session != null and session != ''">
                and `session` = #{session}
            </if>
            <if test="venueId != null and venueId != ''">
                and venue_id = #{venueId}
            </if>
            <if test="id != null and id != ''">
                and id != #{id}
            </if>
        </where>
    </select>

    <select id="getAllToList" resultMap="BeilvSessionResult">
        select * from beilv_session
    </select>

</mapper>