<?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.team.mapper.TeamMapper">

    <resultMap type="TeamVO" id="TeamResult" autoMapping="true">
        <result property="id"    column="id"    />
        <result property="storeId"    column="store_id"    />
        <result property="boothId"    column="booth_id"    />
        <result property="name"    column="name"    />
        <result property="prefer"    column="prefer"    />
        <result property="type"    column="type"    />
        <result property="topic"    column="topic"    />
        <result property="limitNum"    column="limit_num"    />
        <result property="bookTime"    column="book_time"    />
        <result property="createTime"    column="create_time"    />
        <result property="status"    column="status"    />
    </resultMap>

    <sql id="selectTeamVo">
        select
            id,
            store_id,
            booth_id,
            name,
            prefer,
            type,
            topic,
            limit_num,
            book_time,
            create_time,
            status
        from bst_team
    </sql>

    <sql id="searchCondition">
        <if test="query.storeId != null "> and store_id = #{query.storeId}</if>
        <if test="query.id != null "> and id = #{query.id}</if>
        <if test="query.boothId != null "> and booth_id = #{query.boothId}</if>
        <if test="query.name != null  and query.name != ''"> and name like concat('%', #{query.name}, '%')</if>
        <if test="query.prefer != null  and query.prefer != ''"> and prefer = #{query.prefer}</if>
        <if test="query.type != null  and query.type != ''"> and type = #{query.type}</if>
        <if test="query.topic != null  and query.topic != ''"> and topic like concat('%', #{query.topic}, '%')</if>
        <if test="query.limitNum != null "> and limit_num = #{query.limitNum}</if>
        <if test="query.bookTime != null "> and book_time = #{query.bookTime}</if>
        <if test="query.status != null "> and status = #{query.status}</if>
        ${query.params.dataScope}
    </sql>

    <select id="selectTeamList" parameterType="TeamQuery" resultMap="TeamResult">
        <include refid="selectTeamVo"/>
        <where>
            <include refid="searchCondition"/>
        </where>
    </select>

    <select id="selectTeamById" parameterType="Long" resultMap="TeamResult">
        <include refid="selectTeamVo"/>
        where id = #{id}
    </select>

    <insert id="insertTeam" parameterType="Team" useGeneratedKeys="true" keyProperty="id">
        insert into bst_team
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="storeId != null">store_id,</if>
            <if test="boothId != null">booth_id,</if>
            <if test="name != null">name,</if>
            <if test="prefer != null">prefer,</if>
            <if test="type != null and type != ''">type,</if>
            <if test="topic != null">topic,</if>
            <if test="limitNum != null">limit_num,</if>
            <if test="bookTime != null">book_time,</if>
            <if test="createTime != null">create_time,</if>
            <if test="createDate != null">create_time,</if>
            <if test="status != null">status,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="storeId != null">#{storeId},</if>
            <if test="boothId != null">#{boothId},</if>
            <if test="name != null">#{name},</if>
            <if test="prefer != null">#{prefer},</if>
            <if test="type != null and type != ''">#{type},</if>
            <if test="topic != null">#{topic},</if>
            <if test="limitNum != null">#{limitNum},</if>
            <if test="bookTime != null">#{bookTime},</if>
            <if test="createTime != null">#{createTime},</if>
            <if test="createDate != null">#{createDate},</if>
            <if test="status != null">#{status},</if>
         </trim>
    </insert>

    <insert id="batchInsert" parameterType="Team" useGeneratedKeys="true" keyProperty="id">
        insert into bst_team
        <trim prefix="(" suffix=")" suffixOverrides=",">
            store_id,
            booth_id,
            name,
            prefer,
            type,
            topic,
            limit_num,
            book_time,
            create_time,
        </trim>
        values
        <foreach collection="list" item="i" separator=",">
            <trim prefix="(" suffix=")" suffixOverrides=",">
                <if test="i.storeId != null ">#{i.storeId},</if>
                <if test="i.storeId == null ">default,</if>
                <if test="i.boothId != null ">#{i.boothId},</if>
                <if test="i.boothId == null ">default,</if>
                <if test="i.name != null ">#{i.name},</if>
                <if test="i.name == null ">default,</if>
                <if test="i.prefer != null ">#{i.prefer},</if>
                <if test="i.prefer == null ">default,</if>
                <if test="i.type != null  and i.type != ''">#{i.type},</if>
                <if test="i.type == null  or i.type == ''">default,</if>
                <if test="i.topic != null ">#{i.topic},</if>
                <if test="i.topic == null ">default,</if>
                <if test="i.limitNum != null ">#{i.limitNum},</if>
                <if test="i.limitNum == null ">default,</if>
                <if test="i.bookTime != null ">#{i.bookTime},</if>
                <if test="i.bookTime == 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_team
        <trim prefix="SET" suffixOverrides=",">
            <foreach open="store_id = CASE id" collection="list" item="item" close="END,">
                <choose>
                    <when test="item.storeId != null ">
                        WHEN #{item.id} THEN #{item.storeId}
                    </when>
                    <otherwise>
                        WHEN #{item.id} THEN `store_id`
                    </otherwise>
                </choose>
            </foreach>
            <foreach open="booth_id = CASE id" collection="list" item="item" close="END,">
                <choose>
                    <when test="item.boothId != null ">
                        WHEN #{item.id} THEN #{item.boothId}
                    </when>
                    <otherwise>
                        WHEN #{item.id} THEN `booth_id`
                    </otherwise>
                </choose>
            </foreach>
            <foreach open="name = CASE id" collection="list" item="item" close="END,">
                <choose>
                    <when test="item.name != null ">
                        WHEN #{item.id} THEN #{item.name}
                    </when>
                    <otherwise>
                        WHEN #{item.id} THEN `name`
                    </otherwise>
                </choose>
            </foreach>
            <foreach open="prefer = CASE id" collection="list" item="item" close="END,">
                <choose>
                    <when test="item.prefer != null ">
                        WHEN #{item.id} THEN #{item.prefer}
                    </when>
                    <otherwise>
                        WHEN #{item.id} THEN `prefer`
                    </otherwise>
                </choose>
            </foreach>
            <foreach open="type = CASE id" collection="list" item="item" close="END,">
                <choose>
                    <when test="item.type != null  and item.type != ''">
                        WHEN #{item.id} THEN #{item.type}
                    </when>
                    <otherwise>
                        WHEN #{item.id} THEN `type`
                    </otherwise>
                </choose>
            </foreach>
            <foreach open="topic = CASE id" collection="list" item="item" close="END,">
                <choose>
                    <when test="item.topic != null ">
                        WHEN #{item.id} THEN #{item.topic}
                    </when>
                    <otherwise>
                        WHEN #{item.id} THEN `topic`
                    </otherwise>
                </choose>
            </foreach>
            <foreach open="limit_num = CASE id" collection="list" item="item" close="END,">
                <choose>
                    <when test="item.limitNum != null ">
                        WHEN #{item.id} THEN #{item.limitNum}
                    </when>
                    <otherwise>
                        WHEN #{item.id} THEN `limit_num`
                    </otherwise>
                </choose>
            </foreach>
            <foreach open="book_time = CASE id" collection="list" item="item" close="END,">
                <choose>
                    <when test="item.bookTime != null ">
                        WHEN #{item.id} THEN #{item.bookTime}
                    </when>
                    <otherwise>
                        WHEN #{item.id} THEN `book_time`
                    </otherwise>
                </choose>
            </foreach>
            <foreach open="create_time = CASE id" collection="list" item="item" close="END,">
                <choose>
                    <when test="item.createTime != null ">
                        WHEN #{item.id} THEN #{item.createTime}
                    </when>
                    <otherwise>
                        WHEN #{item.id} THEN `create_time`
                    </otherwise>
                </choose>
            </foreach>
        </trim>
        where id in
        <foreach item="item" collection="list" open="(" separator="," close=")">
            #{item.id}
        </foreach>
    </update>

    <update id="updateTeam" parameterType="Team">
        update bst_team
        <trim prefix="SET" suffixOverrides=",">
            <include refid="updateColumns"/>
        </trim>
        where id = #{data.id}
    </update>

    <sql id="updateColumns">
        <if test="data.storeId != null">store_id = #{data.storeId},</if>
        <if test="data.boothId != null">booth_id = #{data.boothId},</if>
        <if test="data.name != null">name = #{data.name},</if>
        <if test="data.prefer != null">prefer = #{data.prefer},</if>
        <if test="data.type != null and data.type != ''">type = #{data.type},</if>
        <if test="data.topic != null">topic = #{data.topic},</if>
        <if test="data.limitNum != null">limit_num = #{data.limitNum},</if>
        <if test="data.bookTime != null">book_time = #{data.bookTime},</if>
        <if test="data.createTime != null">create_time = #{data.createTime},</if>
        <if test="data.status != null">status = #{data.status},</if>
        <if test="data.createDate != null">create_time = #{data.createDate},</if>
    </sql>

    <delete id="deleteTeamById" parameterType="Long">
        delete from bst_team where id = #{id}
    </delete>

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