<?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.yonge.cooleshow.biz.dal.dao.ImGroupMemberDao">
    <resultMap id="BaseResultMap" type="com.yonge.cooleshow.biz.dal.entity.ImGroupMember">
        <id column="id_" jdbcType="INTEGER" property="id"/>
        <result column="group_id_" jdbcType="VARCHAR" property="groupId"/>
        <result column="user_id_" jdbcType="INTEGER" property="userId"/>
        <result column="nickname_" jdbcType="VARCHAR" property="nickname"/>
        <result column="is_admin_" jdbcType="VARCHAR" property="isAdmin"/>
        <result column="role_type_" jdbcType="VARCHAR" property="roleType"/>
        <result column="create_time_" jdbcType="TIMESTAMP" property="createTime"/>
        <result column="update_time_" jdbcType="TIMESTAMP" property="updateTime"/>
    </resultMap>

    <sql id="Base_Column_List">
        id_ , group_id_, user_id_, nickname_, is_admin_, role_type_, create_time_, update_time_
    </sql>

    <insert id="insertBatch" keyColumn="id_" keyProperty="id" useGeneratedKeys="true"
            parameterType="com.yonge.cooleshow.biz.dal.entity.ImGroupMember">
        insert into im_group_member(group_id_, user_id_,avatar_, nickname_, is_admin_, role_type_, create_time_,
        update_time_)
        values
        <foreach collection="entities" item="entity" separator=",">
            (#{entity.groupId}, #{entity.userId}, #{entity.avatar}, #{entity.nickname}, #{entity.isAdmin}, #{entity.roleType},NOW(), NOW())
        </foreach>
        ON DUPLICATE KEY UPDATE
        user_id_ = VALUES(user_id_),
        nickname_ = VALUES(nickname_),
        role_type_ = VALUES(role_type_)
    </insert>
    <delete id="delByGroupId">
        DELETE FROM im_group_member WHERE group_id_ = #{groupId}
    </delete>
    <delete id="delByGroupIdAndUserId">
        DELETE FROM im_group_member WHERE group_id_ = #{groupId} AND user_id_ = #{userId}
    </delete>
    <select id="findOne" resultMap="BaseResultMap">
        SELECT <include refid="Base_Column_List"/> FROM im_group_member
        <where>
            <if test="groupId != null">
                AND group_id_ = #{groupId}
            </if>
            <if test="userId != null">
                AND user_id_ = #{userId}
            </if>
        </where>
        LIMIT 1
    </select>
    <resultMap id="GroupMember" type="io.rong.models.group.GroupMember">
        <result property="id" column="user_id_"/>
        <result property="groupId" column="group_id_"/>
    </resultMap>
    <select id="queryGroupMember" resultMap="GroupMember">
        SELECT user_id_,group_id_ FROM im_group_member WHERE group_id_ = #{groupId}
    </select>
    <resultMap id="ImGroupMember" type="com.yonge.cooleshow.biz.dal.entity.ImGroupMember">
        <result property="groupId" column="group_id_"/>
        <result property="userId" column="user_id_"/>
        <result property="isAdmin" column="is_admin_"/>
        <result property="nickname" column="nick_name_"/>
        <result property="avatar" column="avatar_"/>
        <result property="roleType" column="role_type_"/>
        <result property="createTime" column="now_"/>
        <result property="updateTime" column="now_"/>
    </resultMap>
    <select id="initImGroupMember" resultMap="ImGroupMember">
        SELECT #{imGroupId} group_id_,su.id_ user_id_, 0 is_admin_,su.username_ nick_name_,
               #{roleType} role_type_,NOW() now_,su.avatar_
        FROM sys_user su
        WHERE su.id_ IN
        <foreach collection="userIds" open="(" close=")" item="userId" separator=",">
            #{userId}
        </foreach>
    </select>
    <select id="findByUserIdAndGroupId" resultMap="ImGroupMember">
        SELECT <include refid="Base_Column_List"/> FROM im_group_member WHERE group_id_ = #{groupId} AND user_id_ = #{userId} LIMIT 1
    </select>
</mapper>
