<?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.system.mapper.ScoreRoomUserMapper">
    
    <resultMap type="ScoreRoomUser" id="ScoreRoomUserResult">
        <result property="roomUserId"    column="room_user_id"    />
        <result property="roomId"    column="room_id"    />
        <result property="userId"    column="user_id"    />
        <result property="score"    column="score"    />
        <result property="playerType"    column="player_type"    />
        <result property="nickName"    column="nick_name"    />
        <result property="avatars"    column="avatars"    />
    </resultMap>

    <sql id="selectScoreRoomUserVo">
        select room_user_id, room_id, user_id, score, player_type, nick_name, avatars from score_room_user
    </sql>

    <select id="selectScoreRoomUserList" parameterType="ScoreRoomUser" resultMap="ScoreRoomUserResult">
        <include refid="selectScoreRoomUserVo"/>
        <where>  
            <if test="roomId != null "> and room_id = #{roomId}</if>
            <if test="userId != null "> and user_id = #{userId}</if>
            <if test="score != null "> and score = #{score}</if>
            <if test="playerType != null  and playerType != ''"> and player_type = #{playerType}</if>
            <if test="nickName != null  and nickName != ''"> and nick_name like concat('%', #{nickName}, '%')</if>
            <if test="avatars != null  and avatars != ''"> and avatars = #{avatars}</if>
        </where>
    </select>
    
    <select id="selectScoreRoomUserByRoomUserId" parameterType="Long" resultMap="ScoreRoomUserResult">
        <include refid="selectScoreRoomUserVo"/>
        where room_user_id = #{roomUserId}
    </select>
    <select id="selectExitRepeatData" resultType="java.lang.Integer">
        SELECT EXISTS (
            SELECT 1
            FROM score_room_user
            WHERE room_id = #{roomId} AND user_id = #{userId}
        ) AS is_exists;
    </select>

    <insert id="insertScoreRoomUser" parameterType="ScoreRoomUser" useGeneratedKeys="true" keyProperty="roomUserId">
        insert into score_room_user
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="roomId != null">room_id,</if>
            <if test="userId != null">user_id,</if>
            <if test="score != null">score,</if>
            <if test="playerType != null">player_type,</if>
            <if test="nickName != null">nick_name,</if>
            <if test="avatars != null">avatars,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="roomId != null">#{roomId},</if>
            <if test="userId != null">#{userId},</if>
            <if test="score != null">#{score},</if>
            <if test="playerType != null">#{playerType},</if>
            <if test="nickName != null">#{nickName},</if>
            <if test="avatars != null">#{avatars},</if>
         </trim>
    </insert>

    <update id="updateScoreRoomUser" parameterType="ScoreRoomUser">
        update score_room_user
        <trim prefix="SET" suffixOverrides=",">
            <if test="roomId != null">room_id = #{roomId},</if>
            <if test="userId != null">user_id = #{userId},</if>
            <if test="score != null">score = #{score},</if>
            <if test="playerType != null">player_type = #{playerType},</if>
            <if test="nickName != null">nick_name = #{nickName},</if>
            <if test="avatars != null">avatars = #{avatars},</if>
        </trim>
        where room_user_id = #{roomUserId}
    </update>

    <delete id="deleteScoreRoomUserByRoomUserId" parameterType="Long">
        delete from score_room_user where room_user_id = #{roomUserId}
    </delete>

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