<?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.qw.mapper.QwGroupChatUserMapper">

    <resultMap type="QwGroupChatUser" id="QwGroupChatUserResult">
        <result property="id"    column="id"    />
        <result property="chatId"    column="chat_id"    />
        <result property="userId"    column="user_id"    />
        <result property="type"    column="type"    />
        <result property="unionid"    column="unionid"    />
        <result property="joinTime"    column="join_time"    />
        <result property="joinScene"    column="join_scene"    />
        <result property="invitor"    column="invitor"    />
        <result property="groupNickname"    column="group_nickname"    />
        <result property="name"    column="name"    />
        <result property="companyId"    column="company_id"    />
        <result property="isOut"    column="is_out"    />
        <result property="corpId"    column="corp_id"    />
        <result property="outTime"    column="out_time"    />
        <result property="quitScene"    column="quit_scene"    />
    </resultMap>

    <sql id="selectQwGroupChatUserVo">
        select id, chat_id, user_id, type, unionid, join_time, join_scene, invitor, group_nickname, name, company_id, is_out, corp_id, out_time,quit_scene from qw_group_chat_user
    </sql>

    <select id="selectQwGroupChatUserList" parameterType="QwGroupChatUser" resultMap="QwGroupChatUserResult">
        <include refid="selectQwGroupChatUserVo"/>
        <where>
            <if test="chatId != null  and chatId != ''"> and chat_id = #{chatId}</if>
            <if test="userId != null  and userId != ''"> and user_id = #{userId}</if>
            <if test="type != null  and type != ''"> and type = #{type}</if>
            <if test="unionid != null "> and unionid = #{unionid}</if>
            <if test="joinTime != null "> and join_time = #{joinTime}</if>
            <if test="joinScene != null  and joinScene != ''"> and join_scene = #{joinScene}</if>
            <if test="invitor != null  and invitor != ''"> and invitor = #{invitor}</if>
            <if test="groupNickname != null  and groupNickname != ''"> and group_nickname like concat('%', #{groupNickname}, '%')</if>
            <if test="name != null  and name != ''"> and name like concat('%', #{name}, '%')</if>
            <if test="companyId != null "> and company_id = #{companyId}</if>
            <if test="isOut != null "> and is_out = #{isOut}</if>
            <if test="corpId != null  and corpId != ''"> and corp_id = #{corpId}</if>
            <if test="outTime != null "> and out_time = #{outTime}</if>
            <if test="quitScene != null "> and quit_scene = #{quitScene}</if>
        </where>
        order by join_time desc
    </select>

    <select id="selectQwGroupChatUserById" parameterType="Long" resultMap="QwGroupChatUserResult">
        <include refid="selectQwGroupChatUserVo"/>
        where id = #{id}
    </select>

    <select id="selectQwGroupChatUserByExternalUserId"  parameterType="QwGroupChatUser"  resultMap="QwGroupChatUserResult">
        <include refid="selectQwGroupChatUserVo"/>
        where chat_id = #{chatId} AND user_id=#{userId}  AND corp_id=#{corpId}
    </select>
    <select id="selectQwGroupChatUserByChatIds" resultType="com.fs.qw.domain.QwGroupChatUser">
        select * from qw_group_chat_user where is_out = 1 and type = 2 and chat_id in <foreach collection="ids" open="(" separator="," close=")" item="item">#{item}</foreach>
    </select>
    <select id="selectUserIsChat" resultType="com.fs.qw.domain.QwGroupChatUser">
        select * from qw_group_chat_user a where a.user_id = #{externalUserId}
    </select>
    <select id="selectByChatId" resultType="com.fs.qw.domain.QwGroupChatUser">
        select * from qw_group_chat_user a where a.chat_id = #{chatId} and type = 2
    </select>
    <select id="selectByIdAndRegister" resultType="com.fs.sop.domain.SopUserLogsInfo">
        select
        a.user_id as external_contact_id,
        a.chat_id,
        b.id external_id,
        b.user_id qw_user_id,
        a.corp_id,
        b.name external_user_name,
        b.fs_user_id
        from qw_group_chat_user a inner join qw_external_contact b on a.user_id = b.external_user_id and b.user_id = #{qwUserId}
        where a.id in <foreach collection="ids" item="item" open="(" separator="," close=")">#{item}</foreach>
        <if test="isRegister != null and isRegister == 1">
            b.fs_user_id is not null
        </if>
    </select>

    <insert id="insertOrUpdateQwGroupChatUser" parameterType="QwGroupChatUser" useGeneratedKeys="true" keyProperty="id">
        insert into qw_group_chat_user
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="chatId != null">chat_id,</if>
            <if test="userId != null">user_id,</if>
            <if test="type != null">type,</if>
            <if test="unionid != null">unionid,</if>
            <if test="joinTime != null">join_time,</if>
            <if test="joinScene != null">join_scene,</if>
            <if test="invitor != null">invitor,</if>
            <if test="groupNickname != null">group_nickname,</if>
            <if test="name != null">name,</if>
            <if test="companyId != null">company_id,</if>
            <if test="isOut != null">is_out,</if>
            <if test="corpId != null">corp_id,</if>
            <if test="outTime != null">out_time,</if>
            <if test="quitScene != null">quit_scene,</if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="chatId != null">#{chatId},</if>
            <if test="userId != null">#{userId},</if>
            <if test="type != null">#{type},</if>
            <if test="unionid != null">#{unionid},</if>
            <if test="joinTime != null">#{joinTime},</if>
            <if test="joinScene != null">#{joinScene},</if>
            <if test="invitor != null">#{invitor},</if>
            <if test="groupNickname != null">#{groupNickname},</if>
            <if test="name != null">#{name},</if>
            <if test="companyId != null">#{companyId},</if>
            <if test="isOut != null">#{isOut},</if>
            <if test="corpId != null">#{corpId},</if>
            <if test="outTime != null">#{outTime},</if>
            <if test="quitScene != null">#{quitScene},</if>
        </trim>
        on duplicate key update
        <trim suffixOverrides=",">
            <if test="type != null">type = #{type},</if>
            <if test="unionid != null">unionid = #{unionid},</if>
            <if test="joinTime != null">join_time = #{joinTime},</if>
            <if test="joinScene != null">join_scene = #{joinScene},</if>
            <if test="invitor != null">invitor = #{invitor},</if>
            <if test="groupNickname != null">group_nickname = #{groupNickname},</if>
            <if test="name != null">name = #{name},</if>
            <if test="companyId != null">company_id = #{companyId},</if>
            <if test="isOut != null">is_out = #{isOut},</if>
            <if test="corpId != null">corp_id = #{corpId},</if>
            <if test="outTime != null">out_time = #{outTime},</if>
            <if test="quitScene != null">quit_scene = #{quitScene},</if>
        </trim>
    </insert>

    <insert id="insertQwGroupChatUser" parameterType="QwGroupChatUser" useGeneratedKeys="true" keyProperty="id">
        insert into qw_group_chat_user
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="chatId != null">chat_id,</if>
            <if test="userId != null">user_id,</if>
            <if test="type != null">type,</if>
            <if test="unionid != null">unionid,</if>
            <if test="joinTime != null">join_time,</if>
            <if test="joinScene != null">join_scene,</if>
            <if test="invitor != null">invitor,</if>
            <if test="groupNickname != null">group_nickname,</if>
            <if test="name != null">name,</if>
            <if test="companyId != null">company_id,</if>
            <if test="isOut != null">is_out,</if>
            <if test="corpId != null">corp_id,</if>
            <if test="outTime != null">out_time,</if>
            <if test="quitScene != null">quit_scene,</if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="chatId != null">#{chatId},</if>
            <if test="userId != null">#{userId},</if>
            <if test="type != null">#{type},</if>
            <if test="unionid != null">#{unionid},</if>
            <if test="joinTime != null">#{joinTime},</if>
            <if test="joinScene != null">#{joinScene},</if>
            <if test="invitor != null">#{invitor},</if>
            <if test="groupNickname != null">#{groupNickname},</if>
            <if test="name != null">#{name},</if>
            <if test="companyId != null">#{companyId},</if>
            <if test="isOut != null">#{isOut},</if>
            <if test="corpId != null">#{corpId},</if>
            <if test="outTime != null">#{outTime},</if>
            <if test="quitScene != null">#{quitScene},</if>
        </trim>
    </insert>

    <update id="updateQwGroupChatUser" parameterType="QwGroupChatUser">
        update qw_group_chat_user
        <trim prefix="SET" suffixOverrides=",">
            <if test="chatId != null">chat_id = #{chatId},</if>
            <if test="userId != null">user_id = #{userId},</if>
            <if test="type != null">type = #{type},</if>
            <if test="unionid != null">unionid = #{unionid},</if>
            <if test="joinTime != null">join_time = #{joinTime},</if>
            <if test="joinScene != null">join_scene = #{joinScene},</if>
            <if test="invitor != null">invitor = #{invitor},</if>
            <if test="groupNickname != null">group_nickname = #{groupNickname},</if>
            <if test="name != null">name = #{name},</if>
            <if test="companyId != null">company_id = #{companyId},</if>
            <if test="isOut != null">is_out = #{isOut},</if>
            <if test="corpId != null">corp_id = #{corpId},</if>
            <if test="outTime != null">out_time = #{outTime},</if>
            <if test="quitScene != null">quit_scene = #{quitScene},</if>
        </trim>
        where id = #{id}
    </update>

    <update id="upDateDelGroupChatUser" parameterType="QwGroupChatUser">
        update qw_group_chat_user
        <trim prefix="SET" suffixOverrides=",">
            <if test="type != null">type = #{type},</if>
            <if test="unionid != null">unionid = #{unionid},</if>
            <if test="joinTime != null">join_time = #{joinTime},</if>
            <if test="joinScene != null">join_scene = #{joinScene},</if>
            <if test="invitor != null">invitor = #{invitor},</if>
            <if test="groupNickname != null">group_nickname = #{groupNickname},</if>
            <if test="name != null">name = #{name},</if>
            <if test="companyId != null">company_id = #{companyId},</if>
            <if test="isOut != null">is_out = #{isOut},</if>
            <if test="corpId != null">corp_id = #{corpId},</if>
            <if test="outTime != null">out_time = #{outTime},</if>
            <if test="quitScene != null">quit_scene = #{quitScene},</if>
        </trim>
        where chat_id = #{chatId} and user_id=#{userId}
    </update>


    <update id="groupChatUserJoinAgain" parameterType="QwGroupChatUser">
        update qw_group_chat_user
        <trim prefix="SET" suffixOverrides=",">
            type = #{type},
            unionid = #{unionid},
            join_time = #{joinTime},
            join_scene = #{joinScene},
            invitor = #{invitor},
            group_nickname = #{groupNickname},
            name = #{name},
            company_id = #{companyId},
            is_out = #{isOut},
            corp_id = #{corpId},
            out_time = #{outTime},
            quit_scene = #{quitScene}
        </trim>
        where chat_id = #{chatId} and user_id = #{userId}
    </update>

    <delete id="deleteQwGroupChatUserById" parameterType="Long">
        delete from qw_group_chat_user where id = #{id}
    </delete>

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