<?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" />
        <result column="tenant_id_" property="tenantId"/>
	</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 where tenant_id_ = #{tenantId} 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_,tenant_id_) 
		VALUES(#{imGroupId},#{userId},#{nickname},#{isAdmin},#{roleType},NOW(),NOW(),#{tenantId})
	</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_,tenant_id_) VALUES
		 <foreach collection="list" item="item" separator=",">
		(#{item.imGroupId},#{item.userId},#{item.nickname},#{item.isAdmin},#{item.roleType},NOW(),NOW(),#{item.tenantId})
		</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} and tenant_id_ = #{tenantId}
	</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} and tenant_id_ = #{item.tenantId}
		</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 where tenant_id_ = #{tenantId} ORDER BY id_ <include refid="global.limit"/>
	</select>
	
	<!-- 查询当前表的总记录数 -->
	<select id="queryCount" resultType="int">
		SELECT COUNT(*) FROM im_group_member  where tenant_id_ = #{tenantId}
	</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>
    <select id="queryMembers" resultMap="ImGroupMember">
		SELECT #{groupId} im_group_id_,CASE WHEN su.username_ IS NULL OR su.username_ = '' THEN su.real_name_ ELSE su.username_ END nickname_,
		       #{tenantId} tenant_id_,su.id_ user_id_
		FROM sys_user su
		WHERE su.id_ IN
		<foreach collection="userIdList" item="item" open="(" separator="," close=")">
			#{item}
		</foreach>
	</select>
	<sql id="queryGroupMemberPageSql">
		<where>
			<if test="imGroupId != null and imGroupId != ''">
				AND igm.im_group_id_ = #{imGroupId}
			</if>
			<if test="roleType != null and roleType != ''">
				<if test="roleType == '学员'">
					AND (igm.role_type_ IS NULL OR igm.role_type_ = '')
				</if>
				<if test="roleType != '学员'">
					AND igm.role_type_ = #{roleType}
				</if>
			</if>
			<if test="search != null and search != ''">
				AND (igm.nickname_ LIKE CONCAT('%',#{search},'%') OR igm.user_id_ = #{search} OR u.phone_ LIKE CONCAT('%',#{search},'%'))
			</if>
		</where>
	</sql>
    <select id="queryGroupMemberPage" resultMap="com.ym.mec.biz.dal.dao.ImGroupDao.ImGroupMemberDto">
		SELECT igm.*,u.avatar_,CASE WHEN find_in_set('STUDENT',u.user_type_) THEN u.username_ ELSE u.real_name_ END username_,
			   st.member_rank_setting_id_,s.name_ subject_name_,st.subject_id_list_ subject_id_,u.phone_
		FROM im_group_member igm
		LEFT JOIN sys_user u ON igm.user_id_ = u.id_
		LEFT JOIN student st ON st.user_id_ = u.id_
		LEFT JOIN `subject` s ON s.id_ = st.subject_id_list_
		<include refid="queryGroupMemberPageSql"/>
		<include refid="global.limit"/>
	</select>
	<select id="countGroupMember" resultType="java.lang.Integer">
		SELECT COUNT(igm.id_)
		FROM im_group_member igm
		LEFT JOIN sys_user u ON igm.user_id_ = u.id_
		<include refid="queryGroupMemberPageSql"/>
	</select>
</mapper>
