<?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.fs.chat.mapper.ChatUserMapper">
    
    <resultMap type="ChatUser" id="ChatUserResult">
        <result property="userId"    column="user_id"    />
        <result property="externalUserid"    column="external_userid"    />
        <result property="avatar"    column="avatar"    />
        <result property="nickname"    column="nickname"    />
        <result property="gender"    column="gender"    />
        <result property="unionid"    column="unionid"    />
        <result property="createTime"    column="create_time"    />
    </resultMap>

    <sql id="selectChatUserVo">
        select user_id, external_userid, avatar, nickname, gender, unionid, create_time from chat_user
    </sql>

    <select id="selectChatUserList" parameterType="ChatUser" resultMap="ChatUserResult">
        <include refid="selectChatUserVo"/>
        <where>  
            <if test="externalUserid != null  and externalUserid != ''"> and external_userid = #{externalUserid}</if>
            <if test="avatar != null  and avatar != ''"> and avatar = #{avatar}</if>
            <if test="nickname != null  and nickname != ''"> and nickname like concat('%', #{nickname}, '%')</if>
            <if test="gender != null "> and gender = #{gender}</if>
            <if test="unionid != null  and unionid != ''"> and unionid = #{unionid}</if>
        </where>
    </select>
    
    <select id="selectChatUserByUserId" parameterType="Long" resultMap="ChatUserResult">
        <include refid="selectChatUserVo"/>
        where user_id = #{userId}
    </select>
        
    <insert id="insertChatUser" parameterType="ChatUser" useGeneratedKeys="true" keyProperty="userId">
        insert into chat_user
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="externalUserid != null">external_userid,</if>
            <if test="avatar != null">avatar,</if>
            <if test="nickname != null">nickname,</if>
            <if test="gender != null">gender,</if>
            <if test="unionid != null">unionid,</if>
            <if test="createTime != null">create_time,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="externalUserid != null">#{externalUserid},</if>
            <if test="avatar != null">#{avatar},</if>
            <if test="nickname != null">#{nickname},</if>
            <if test="gender != null">#{gender},</if>
            <if test="unionid != null">#{unionid},</if>
            <if test="createTime != null">#{createTime},</if>
         </trim>
    </insert>

    <update id="updateChatUser" parameterType="ChatUser">
        update chat_user
        <trim prefix="SET" suffixOverrides=",">
            <if test="externalUserid != null">external_userid = #{externalUserid},</if>
            <if test="avatar != null">avatar = #{avatar},</if>
            <if test="nickname != null">nickname = #{nickname},</if>
            <if test="gender != null">gender = #{gender},</if>
            <if test="unionid != null">unionid = #{unionid},</if>
            <if test="createTime != null">create_time = #{createTime},</if>
        </trim>
        where user_id = #{userId}
    </update>

    <delete id="deleteChatUserByUserId" parameterType="Long">
        delete from chat_user where user_id = #{userId}
    </delete>

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