<?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.site.mapper.BeilvSiteMapper">
    
    <resultMap type="BeilvSite" id="BeilvSiteResult">
        <result property="id"    column="id"    />
        <result property="name"    column="name"    />
        <result property="siteType"    column="site_type"    />
        <result property="cover"    column="cover"    />
        <result property="intro"    column="intro"    />
        <result property="price"    column="price"    />
        <result property="warmReminder"    column="warm_reminder"    />
        <result property="siteStatus"    column="site_status"    />
        <result property="venueId"    column="venue_id"    />
        <result property="venueName"    column="venue_name"    />
    </resultMap>

    <sql id="selectBeilvSiteVo">
        select id, name, site_type, cover, intro, price, warm_reminder, site_status, venue_id from beilv_site
    </sql>

    <select id="selectBeilvSiteList" parameterType="BeilvSite" resultMap="BeilvSiteResult">
        SELECT
        bs.id,
        bs.NAME,
        bs.site_type,
        bs.cover,
        bs.intro,
        bs.price,
        bs.warm_reminder,
        bs.site_status,
        bs.venue_id,
        bv.venue_name
        FROM
        beilv_site bs
        left join beilv_venue bv on bs.venue_id = bv.id
        <where>  
            <if test="name != null  and name != ''"> and bs.name like concat('%', #{name}, '%')</if>
            <if test="siteType != null  and siteType != ''"> and bs.site_type = #{siteType}</if>
            <if test="venueId != null  and venueId != ''"> and bs.venue_id = #{venueId}</if>
        </where>
    </select>
    
    <select id="selectBeilvSiteById" parameterType="Long" resultMap="BeilvSiteResult">
        SELECT
            bs.id,
            bs.NAME,
            bs.site_type,
            bs.cover,
            bs.intro,
            bs.price,
            bs.warm_reminder,
            bs.site_status,
            bs.venue_id,
            bv.venue_name
        FROM
            beilv_site bs
                left join beilv_venue bv on bs.venue_id = bv.id
        where bs.id = #{id}
    </select>

    <insert id="insertBeilvSite" parameterType="BeilvSite" useGeneratedKeys="true" keyProperty="id">
        insert into beilv_site
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="name != null">name,</if>
            <if test="siteType != null">site_type,</if>
            <if test="cover != null">cover,</if>
            <if test="intro != null">intro,</if>
            <if test="price != null">price,</if>
            <if test="warmReminder != null">warm_reminder,</if>
            <if test="siteStatus != null">site_status,</if>
            <if test="venueId != null">venue_id,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="name != null">#{name},</if>
            <if test="siteType != null">#{siteType},</if>
            <if test="cover != null">#{cover},</if>
            <if test="intro != null">#{intro},</if>
            <if test="price != null">#{price},</if>
            <if test="warmReminder != null">#{warmReminder},</if>
            <if test="siteStatus != null">#{siteStatus},</if>
            <if test="venueId != null">#{venueId},</if>
         </trim>
    </insert>

    <update id="updateBeilvSite" parameterType="BeilvSite">
        update beilv_site
        <trim prefix="SET" suffixOverrides=",">
            <if test="name != null">name = #{name},</if>
            <if test="siteType != null">site_type = #{siteType},</if>
            <if test="cover != null">cover = #{cover},</if>
            <if test="intro != null">intro = #{intro},</if>
            <if test="price != null">price = #{price},</if>
            <if test="warmReminder != null">warm_reminder = #{warmReminder},</if>
            <if test="siteStatus != null">site_status = #{siteStatus},</if>
            <if test="venueId != null">venue_id = #{venueId},</if>
        </trim>
        where id = #{id}
    </update>

    <delete id="deleteBeilvSiteById" parameterType="Long">
        delete from beilv_site where id = #{id}
    </delete>

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

    <select id="selectOne" parameterType="beilvSite" resultType="boolean">
        select case when count(1) > 0 then true
        else false
        end as result
        from beilv_site
        <where>
            <if test="name != null and name != ''">
                and `name` = #{name}
            </if>
            <if test="siteType != null and siteType != ''">
                and site_type = #{siteType}
            </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="BeilvSiteResult">
        select * from beilv_site
    </select>

    <select id="countSiteByVenueId" resultType="Integer">
        select count(1) from beilv_site where venue_id = #{venueId}
    </select>

</mapper>