<?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="cn.reghao.tnb.content.app.chat.db.mapper.GroupMemberMapper">
    <insert id="save" useGeneratedKeys="true" keyProperty="id">
        insert into chat_group_member
        (`id`,`deleted`,`create_time`,`update_time`,`group_id`,`user_id`,`nickname`,`owner`,`disturb`)
        values 
        (#{id},#{deleted},#{createTime},#{updateTime},#{groupId},#{userId},#{nickname},#{owner},#{disturb})
    </insert>
    <insert id="saveAll" useGeneratedKeys="true" keyProperty="id">
        insert ignore into chat_group_member
        (`id`,`deleted`,`create_time`,`update_time`,`group_id`,`user_id`,`nickname`,`owner`,`disturb`)
        values
        <foreach collection="list" item="item" index="index" separator=",">
            (#{item.id},#{item.deleted},#{item.createTime},#{item.updateTime},#{item.groupId},#{item.userId},#{item.nickname},#{item.owner},#{item.disturb})
        </foreach>
    </insert>

    <update id="updateSetMemberRemark">
        update chat_group_member set update_time=now(),nickname=#{nickname}
        where group_id=#{groupId} and user_id=#{userId}
    </update>

    <delete id="deleteGroupMembers">
        delete from chat_group_member
        where group_id=#{groupId} and user_id in
        <foreach collection="list" item="item" index="index" open="(" separator="," close=")">
            #{item}
        </foreach>
    </delete>

    <select id="findUserIdsByGroupId" resultType="java.lang.Long">
        select user_id from chat_group_member where group_id=#{groupId}
    </select>
    <select id="findByGroupAndUserId" resultType="cn.reghao.tnb.content.app.chat.model.po.GroupMember">
        select * from chat_group_member where group_id=#{groupId} and user_id=#{userId}
    </select>
    <select id="findByGroupId" resultType="cn.reghao.tnb.content.app.chat.model.dto.group.GroupMemberRet">
        select groupMember.id,groupMember.nickname as userCard,groupMember.owner
        from chat_group_member groupMember
        where groupMember.group_id=#{groupId}
    </select>
</mapper>
