<?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.teamUser.mapper.TeamUserMapper">

    <resultMap type="TeamUserVO" id="TeamUserResult" autoMapping="true">
        <result property="id"    column="id"    />
        <result property="teamId"    column="team_id"    />
        <result property="userId"    column="user_id"    />
        <result property="inviterId"    column="inviter_id"    />
        <result property="role"    column="role"    />
        <result property="joinType"    column="join_type"    />
        <result property="status"    column="status"    />
        <result property="createTime"    column="create_time"    />
    </resultMap>

    <sql id="selectTeamUserVo">
        select
            id,
            team_id,
            user_id,
            inviter_id,
            role,
            join_type,
            status,
            create_time
        from bst_team_user
    </sql>

    <sql id="searchCondition">
        <if test="query.teamId != null "> and team_id = #{query.teamId}</if>
        <if test="query.userId != null "> and user_id = #{query.userId}</if>
        <if test="query.inviterId != null "> and inviter_id = #{query.inviterId}</if>
        <if test="query.role != null "> and role = #{query.role}</if>
        <if test="query.joinType != null  and query.joinType != ''"> and join_type = #{query.joinType}</if>
        <if test="query.status != null  and query.status != ''"> and status = #{query.status}</if>
        ${query.params.dataScope}
    </sql>

    <select id="selectTeamUserList" parameterType="TeamUserQuery" resultMap="TeamUserResult">
        <include refid="selectTeamUserVo"/>
        <where>
            <include refid="searchCondition"/>
        </where>
    </select>

    <select id="selectTeamUserById" parameterType="Long" resultMap="TeamUserResult">
        <include refid="selectTeamUserVo"/>
        where id = #{id}
    </select>

    <select id="selectTeamUserListByTeamId" resultType="com.ruoyi.bst.teamUser.domain.TeamUserVO">
        <include refid="selectTeamUserVo"/>
        where team_id = #{teamId}
    </select>

    <insert id="insertTeamUser" parameterType="TeamUser" useGeneratedKeys="true" keyProperty="id">
        insert into bst_team_user
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="teamId != null">team_id,</if>
            <if test="userId != null">user_id,</if>
            <if test="inviterId != null">inviter_id,</if>
            <if test="role != null">role,</if>
            <if test="joinType != null">join_type,</if>
            <if test="status != null">status,</if>
            <if test="createTime != null">create_time,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="teamId != null">#{teamId},</if>
            <if test="userId != null">#{userId},</if>
            <if test="inviterId != null">#{inviterId},</if>
            <if test="role != null">#{role},</if>
            <if test="joinType != null">#{joinType},</if>
            <if test="status != null">#{status},</if>
            <if test="createTime != null">#{createTime},</if>
         </trim>
    </insert>

    <insert id="batchInsert" parameterType="TeamUser" useGeneratedKeys="true" keyProperty="id">
        insert into bst_team_user
        <trim prefix="(" suffix=")" suffixOverrides=",">
            team_id,
            user_id,
            inviter_id,
            role,
            join_type,
            status,
            create_time,
        </trim>
        values
        <foreach collection="list" item="i" separator=",">
            <trim prefix="(" suffix=")" suffixOverrides=",">
                <if test="i.teamId != null ">#{i.teamId},</if>
                <if test="i.teamId == null ">default,</if>
                <if test="i.userId != null ">#{i.userId},</if>
                <if test="i.userId == null ">default,</if>
                <if test="i.inviterId != null ">#{i.inviterId},</if>
                <if test="i.inviterId == null ">default,</if>
                <if test="i.role != null ">#{i.role},</if>
                <if test="i.role == null ">default,</if>
                <if test="i.joinType != null ">#{i.joinType},</if>
                <if test="i.joinType == null ">default,</if>
                <if test="i.status != null ">#{i.status},</if>
                <if test="i.status == 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_user
        <trim prefix="SET" suffixOverrides=",">
            <foreach open="team_id = CASE id" collection="list" item="item" close="END,">
                <choose>
                    <when test="item.teamId != null ">
                        WHEN #{item.id} THEN #{item.teamId}
                    </when>
                    <otherwise>
                        WHEN #{item.id} THEN `team_id`
                    </otherwise>
                </choose>
            </foreach>
            <foreach open="user_id = CASE id" collection="list" item="item" close="END,">
                <choose>
                    <when test="item.userId != null ">
                        WHEN #{item.id} THEN #{item.userId}
                    </when>
                    <otherwise>
                        WHEN #{item.id} THEN `user_id`
                    </otherwise>
                </choose>
            </foreach>
            <foreach open="inviter_id = CASE id" collection="list" item="item" close="END,">
                <choose>
                    <when test="item.inviterId != null ">
                        WHEN #{item.id} THEN #{item.inviterId}
                    </when>
                    <otherwise>
                        WHEN #{item.id} THEN `inviter_id`
                    </otherwise>
                </choose>
            </foreach>
            <foreach open="role = CASE id" collection="list" item="item" close="END,">
                <choose>
                    <when test="item.role != null ">
                        WHEN #{item.id} THEN #{item.role}
                    </when>
                    <otherwise>
                        WHEN #{item.id} THEN `role`
                    </otherwise>
                </choose>
            </foreach>
            <foreach open="join_type = CASE id" collection="list" item="item" close="END,">
                <choose>
                    <when test="item.joinType != null ">
                        WHEN #{item.id} THEN #{item.joinType}
                    </when>
                    <otherwise>
                        WHEN #{item.id} THEN `join_type`
                    </otherwise>
                </choose>
            </foreach>
            <foreach open="status = CASE id" collection="list" item="item" close="END,">
                <choose>
                    <when test="item.status != null ">
                        WHEN #{item.id} THEN #{item.status}
                    </when>
                    <otherwise>
                        WHEN #{item.id} THEN `status`
                    </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="updateTeamUser" parameterType="TeamUser">
        update bst_team_user
        <trim prefix="SET" suffixOverrides=",">
            <include refid="updateColumns"/>
        </trim>
        where id = #{data.id}
    </update>

    <sql id="updateColumns">
        <if test="data.teamId != null">team_id = #{data.teamId},</if>
        <if test="data.userId != null">user_id = #{data.userId},</if>
        <if test="data.inviterId != null">inviter_id = #{data.inviterId},</if>
        <if test="data.role != null">role = #{data.role},</if>
        <if test="data.joinType != null">join_type = #{data.joinType},</if>
        <if test="data.status != null">status = #{data.status},</if>
        <if test="data.createTime != null">create_time = #{data.createTime},</if>
    </sql>

    <delete id="deleteTeamUserById" parameterType="Long">
        delete from bst_team_user where id = #{id}
    </delete>

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