<?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.ym.mec.biz.dal.dao.ImGroupMemberDao">
	
	<resultMap type="com.ym.mec.biz.dal.entity.ImGroupMember" id="ImGroupMember">
		<result column="id_" property="id" />
		<result column="im_group_id_" property="imGroupId" />
		<result column="user_id_" property="userId" />
		<result column="nickname_" property="nickname" />
		<result column="is_admin_" property="isAdmin" />
		<result column="role_type_" property="roleType"/>
		<result column="create_time_" property="createTime" />
		<result column="update_time_" property="updateTime" />
	</resultMap>
	
	<!-- 根据主键查询一条记录 -->
	<select id="get" resultMap="ImGroupMember" >
		SELECT * FROM im_group_member WHERE id_ = #{id} 
	</select>
	
	<!-- 全查询 -->
	<select id="findAll" resultMap="ImGroupMember">
		SELECT * FROM im_group_member ORDER BY id_
	</select>
	
	<!-- 向数据库增加一条记录 -->
	<insert id="insert" parameterType="com.ym.mec.biz.dal.entity.ImGroupMember" useGeneratedKeys="true" keyColumn="id" keyProperty="id">
		INSERT INTO im_group_member (im_group_id_,user_id_,nickname_,is_admin_,role_type_,create_time_,update_time_) 
		VALUES(#{imGroupId},#{userId},#{nickname},#{isAdmin},#{roleType},NOW(),NOW())
	</insert>
	
	<insert id="batchInsert" parameterType="com.ym.mec.biz.dal.entity.ImGroupMember" useGeneratedKeys="true" keyColumn="id" keyProperty="id">
		INSERT INTO im_group_member (im_group_id_,user_id_,nickname_,is_admin_,role_type_,create_time_,update_time_) VALUES
		 <foreach collection="list" item="item" separator=",">
		(#{item.imGroupId},#{item.userId},#{item.nickname},#{item.isAdmin},#{item.roleType},NOW(),NOW())
		</foreach>
	</insert>
	
	<!-- 根据主键查询一条记录 -->
	<update id="update" parameterType="com.ym.mec.biz.dal.entity.ImGroupMember">
		UPDATE im_group_member <set>
		<if test="roleType != null">
		role_type_ = #{roleType},
		</if>
		<if test="userId != null">
		user_id_ = #{userId},
		</if>
		<if test="nickname != null">
		nickname_ = #{nickname},
		</if>
		<if test="isAdmin != null">
		is_admin_ = #{isAdmin},
		</if>
		<if test="imGroupId != null">
		im_group_id_ = #{imGroupId},
		</if>
		update_time_ = NOW()
		</set> WHERE id_ = #{id} 
	</update>
	
	<update id="batchUpdate" parameterType="com.ym.mec.biz.dal.entity.ImGroupMember">
		<foreach collection="list" item="item" index="index" open="" close="" separator=";">
			UPDATE im_group_member 
			<set>
				<if test="item.roleType != null">
				role_type_ = #{item.roleType},
				</if>
				<if test="item.userId != null">
				user_id_ = #{item.userId},
				</if>
				<if test="item.nickname != null">
				nickname_ = #{item.nickname},
				</if>
				<if test="item.isAdmin != null">
				is_admin_ = #{item.isAdmin},
				</if>
				<if test="item.imGroupId != null">
				im_group_id_ = #{item.imGroupId},
				</if>
				update_time_ = NOW()
			</set>
			WHERE id_ = #{item.id}
		</foreach> 
	</update>
	
	<!-- 根据主键删除一条记录 -->
	<delete id="delete" >
		DELETE FROM im_group_member WHERE id_ = #{id} 
	</delete>
	
	<!-- 分页查询 -->
	<select id="queryPage" resultMap="ImGroupMember" parameterType="map">
		SELECT * FROM im_group_member ORDER BY id_ <include refid="global.limit"/>
	</select>
	
	<!-- 查询当前表的总记录数 -->
	<select id="queryCount" resultType="int">
		SELECT COUNT(*) FROM im_group_member
	</select>
	
	<delete id="deleteByImGroupId" >
		DELETE FROM im_group_member WHERE im_group_id_ = #{imGroupId}
	</delete>
	
	<delete id="batchDelete" >
		DELETE FROM im_group_member WHERE id_ in
		<foreach collection="list" item="item" open="(" separator="," close=")">
            #{item.id}
        </foreach>
	</delete>
	<delete id="batchDeleteByGroupIdAndUserId">
		<foreach collection="list" item="item" separator=";">
			DELETE FROM im_group_member WHERE im_group_id_ = #{item.imGroupId} AND user_id_ = #{item.userId}
		</foreach>
	</delete>
	<delete id="delRepeat">
		DELETE FROM im_group_member
				WHERE id_ IN (SELECT a.id_ FROM (SELECT id_ FROM im_group_member GROUP BY im_group_id_,user_id_ HAVING COUNT(user_id_) > 1) a)
	</delete>

	<select id="queryByImGroupIdAndUserId" resultMap="ImGroupMember" parameterType="map">
		SELECT * FROM im_group_member where find_in_set(im_group_id_,#{imGroupIdList}) and find_in_set(user_id_,#{userIdList})
	</select>
	<select id="isExit" resultType="java.lang.Boolean">
		SELECT COUNT(DISTINCT userId) FROM im_group_member WHERE im_group_id_ = #{groupId} AND user_id_ = #{userId}
	</select>
</mapper>
