<?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="com.ruoyi.bst.booth.mapper.BoothMapper">

    <resultMap type="BoothVO" id="BoothResult" autoMapping="true">
        <result property="boothId"    column="booth_id"    />
        <result property="partId"    column="part_id"    />
        <result property="picture"    column="picture"    />
        <result property="createTime"    column="create_time"    />
        <result property="userId"    column="user_id"    />
        <result property="expiredTime"    column="expired_time"    />
        <result property="orderNum"    column="order_num"    />
        <result property="boothName"    column="booth_name"    />
    </resultMap>

    <sql id="selectBoothVo">
        select
            bb.booth_id,
            bb.part_id,
            bb.booth_name,
            bb.picture,
            bb.create_time,
            bb.user_id,
            bb.expired_time,
            bb.order_num,
            bs.store_id,
            bp.part_name as part_name,
            bp_parent.part_name as parent_name,
            bs.store_name as store_name,
            bs.user_id as mch_id,
            mch_su.user_name as mch_name
        from  <include refid="searchTables"/>
    </sql>

    <sql id="searchTables">
       bst_booth bb
        left join bst_part bp on bp.part_id = bb.part_id
         left join bst_part bp_parent on bp.parent_id = bp_parent.part_id
        left join bst_store bs on bp.store_id = bs.store_id
        left join sys_user mch_su on bs.user_id = mch_su.user_id
    </sql>

    <sql id="searchCondition">
        <if test="query.partId != null "> and bb.part_id = #{query.partId}</if>
        <if test="query.userId != null "> and bb.user_id = #{query.userId}</if>
        <if test="query.deleted != null "> and bb.deleted = #{query.deleted}</if>
        <if test="query.boothId != null "> and bb.booth_id = #{query.boothId}</if>
        <if test="query.boothName != null  and query.boothName != ''"> and bb.booth_name like concat('%', #{query.boothName}, '%')</if>
        <if test="query.picture != null  and query.picture != ''"> and bb.picture = #{query.picture}</if>
        <if test="query.partName != null  and query.partName != ''"> and bp.part_name like concat('%', #{query.partName}, '%') </if>
        <if test="query.time != null"> and bb.expired_time &gt;= #{query.time} </if>
        <if test="query.storeName != null  and query.storeName != ''"> and bs.store_name like concat('%', #{query.storeName}, '%') </if>
        ${@com.ruoyi.framework.util.DataScopeUtil@create(query.scope)
        .userSetAlias("bs.user_id")
        .storeAlias("bs.store_id",query.storePermissions)
        .build()
        }
        ${query.params.dataScope}
    </sql>

    <select id="selectBoothList" parameterType="BoothQuery" resultMap="BoothResult">
        <include refid="selectBoothVo"/>
        <where>
            <include refid="searchCondition"/>
        </where>
        order by order_num asc
    </select>

    <select id="selectBoothByBoothId" parameterType="Long" resultMap="BoothResult">
        <include refid="selectBoothVo"/>
        where booth_id = #{boothId}
    </select>

    <!-- selectIdByQuery -->
    <select id="selectIdByQuery" resultType="Long">
        select bb.booth_id from
        <include refid="searchTables"/>
        <where>
            <include refid="searchCondition"/>
        </where>
    </select>

    <select id="selectCount" resultType="java.lang.Integer">
        select count(bb.booth_id)
        from <include refid="searchTables"/>
        <where>
            <if test="query.boothId != null "> and bb.booth_id != #{query.boothId}</if>
        </where>
    </select>

    <select id="selectBoothByUserId" resultType="com.ruoyi.bst.booth.domain.BoothVO">
        <include refid="selectBoothVo"/>
        where bb.user_id = #{userId}
    </select>

    <select id="selectBoothByBoothNo" resultType="com.ruoyi.bst.booth.domain.BoothVO">
        <include refid="selectBoothVo"/>
        where bb.booth_name = #{boothName}
    </select>

    <insert id="insertBooth" parameterType="Booth" useGeneratedKeys="true" keyProperty="boothId">
        insert into bst_booth
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="partId != null">part_id,</if>
            <if test="picture != null">picture,</if>
            <if test="createTime != null">create_time,</if>
            <if test="orderNum != null">order_num,</if>
            <if test="boothName != null">booth_name,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="partId != null">#{partId},</if>
            <if test="picture != null">#{picture},</if>
            <if test="createTime != null">#{createTime},</if>
            <if test="orderNum != null">#{orderNum},</if>
            <if test="boothName != null">#{boothName},</if>
         </trim>
    </insert>

    <insert id="batchInsert" parameterType="Booth" useGeneratedKeys="true" keyProperty="boothId">
        insert into bst_booth
        <trim prefix="(" suffix=")" suffixOverrides=",">
            part_id,
            booth_name,
            picture,
            create_time,
        </trim>
        values
        <foreach collection="list" item="i" separator=",">
            <trim prefix="(" suffix=")" suffixOverrides=",">
                <if test="i.partId != null ">#{i.partId},</if>
                <if test="i.partId == null ">default,</if>
                <if test="i.boothName != null ">#{i.boothName},</if>
                <if test="i.boothName == null ">default,</if>
                <if test="i.picture != null ">#{i.picture},</if>
                <if test="i.picture == null ">default,</if>
                <if test="i.createTime != null ">#{i.createTime},</if>
                <if test="i.createTime == null ">default,</if>
            </trim>
        </foreach>
    </insert>

    <update id="batchUpdate">
        update bst_booth
        <trim prefix="SET" suffixOverrides=",">
            <foreach open="part_id = CASE booth_id" collection="list" item="item" close="END,">
                <choose>
                    <when test="item.partId != null ">
                        WHEN #{item.booth_id} THEN #{item.partId}
                    </when>
                    <otherwise>
                        WHEN #{item.booth_id} THEN `part_id`
                    </otherwise>
                </choose>
            </foreach>
            <foreach open="booth_name = CASE booth_id" collection="list" item="item" close="END,">
                <choose>
                    <when test="item.boothName != null ">
                        WHEN #{item.booth_id} THEN #{item.boothName}
                    </when>
                    <otherwise>
                        WHEN #{item.booth_id} THEN `booth_name`
                    </otherwise>
                </choose>
            </foreach>
            <foreach open="picture = CASE booth_id" collection="list" item="item" close="END,">
                <choose>
                    <when test="item.picture != null ">
                        WHEN #{item.booth_id} THEN #{item.picture}
                    </when>
                    <otherwise>
                        WHEN #{item.booth_id} THEN `picture`
                    </otherwise>
                </choose>
            </foreach>
            <foreach open="create_time = CASE booth_id" collection="list" item="item" close="END,">
                <choose>
                    <when test="item.createTime != null ">
                        WHEN #{item.booth_id} THEN #{item.createTime}
                    </when>
                    <otherwise>
                        WHEN #{item.booth_id} THEN `create_time`
                    </otherwise>
                </choose>
            </foreach>
        </trim>
        where booth_id in
        <foreach item="item" collection="list" open="(" separator="," close=")">
            #{item.boothId}
        </foreach>
    </update>

    <update id="updateBooth" parameterType="Booth">
        update bst_booth
        <trim prefix="SET" suffixOverrides=",">
            <include refid="updateColumns"/>
        </trim>
        where booth_id = #{data.boothId}
    </update>

    <sql id="updateColumns">
        <if test="data.partId != null">part_id = #{data.partId},</if>
        <if test="data.boothName != null">booth_name = #{data.boothName},</if>
        <if test="data.picture != null">picture = #{data.picture},</if>
        <if test="data.createTime != null">create_time = #{data.createTime},</if>
        <if test="data.userId != null">user_id = #{data.userId},</if>
        <if test="data.userId == null">user_id = null,</if>
        <if test="data.expiredTime != null">expired_time = #{data.expiredTime},</if>
        <if test="data.orderNum != null">order_num = #{data.orderNum},</if>
    </sql>

    <delete id="deleteBoothByBoothId" parameterType="Long">
        delete from bst_booth where booth_id = #{boothId}
    </delete>

    <delete id="deleteBoothByBoothIds" parameterType="String">
        delete from bst_booth where booth_id in
        <foreach item="boothId" collection="ids" open="(" separator="," close=")">
            #{boothId}
        </foreach>
    </delete>
</mapper>
