<?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.ImGroupDao">
	
	<resultMap type="com.ym.mec.biz.dal.entity.ImGroup" id="ImGroup">
		<result column="id_" property="id" />
		<result column="name_" property="name" />
		<result column="introduce_" property="introduce" />
		<result column="member_num_" property="memberNum" />
		<result column="memo_" property="memo" />
		<result column="tags_" property="tags" />
		<result column="img_" property="img" />
		<result column="type_" property="type" />
		<result column="group_type_" property="groupType" />
		<result column="create_time_" property="createTime" />
		<result column="update_time_" property="updateTime" />
        <result column="tenant_id_" property="tenantId"/>
	</resultMap>
	
	<resultMap type="com.ym.mec.biz.dal.dto.ImGroupMemberDto" id="ImGroupMemberDto">
		<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="member_rank_setting_id_" property="memberRankSettingId" />
		<result column="subject_name_" property="subjectName" />
		<result column="subject_id_" property="subjectId" />
		<result column="username_" property="user.username" />
		<result column="avatar_" property="user.avatar" />
		<result column="phone_" property="user.phone" />
	</resultMap>
	
	<!-- 根据主键查询一条记录 -->
	<select id="get" resultMap="ImGroup" >
		SELECT * FROM im_group WHERE id_ = #{id} 
	</select>
	
	<select id="getLocked" resultMap="ImGroup" >
		SELECT * FROM im_group WHERE id_ = #{id} for update
	</select>
	
	<!-- 全查询 -->
	<select id="findAll" resultMap="ImGroup">
		SELECT * FROM im_group where tenant_id_ = #{tenantId} ORDER BY id_
	</select>
	
	<!-- 向数据库增加一条记录 -->
	<insert id="insert" parameterType="com.ym.mec.biz.dal.entity.ImGroup" useGeneratedKeys="true" keyColumn="id" keyProperty="id">
		INSERT INTO im_group (id_,name_,introduce_,member_num_,memo_,tags_,img_,type_,create_time_,update_time_,tenant_id_,group_type_)
		VALUES(#{id},#{name},#{introduce},#{memberNum},#{memo},#{tags},#{img},#{type},#{createTime},#{updateTime},#{tenantId},#{groupType})
	</insert>
	
	<!-- 根据主键查询一条记录 -->
	<update id="update" parameterType="com.ym.mec.biz.dal.entity.ImGroup">
		UPDATE im_group <set>
		<if test="groupType != null">
			group_type_ = #{groupType},
		</if>
		<if test="memberNum != null">
			member_num_ = member_num_ + #{memberNum},
		</if>
		<if test="tags != null">
		tags_ = #{tags},
		</if>
		<if test="introduce != null">
		introduce_ = #{introduce},
		</if>
		<if test="updateTime != null">
		update_time_ = #{updateTime},
		</if>
		<if test="memo != null">
		memo_ = #{memo},
		</if>
		<if test="name != null">
		name_ = #{name},
		</if>
		<if test="img != null">
		img_ = #{img},
		</if>
		<if test="type != null">
		type_ = #{type},
		</if>
		</set> WHERE id_ = #{id} and tenant_id_ = #{tenantId}
	</update>
    <update id="updateNickname">
		UPDATE im_group_member SET nickname_ = #{nickName} WHERE user_id_ = #{userId}
	</update>
	<update id="updateUserFriendNickname">
		UPDATE im_user_friend SET friend_nickname_ = #{nickName} WHERE friend_id_ = #{userId};
	</update>
	<update id="updateMemberNum">
		UPDATE im_group ig
		LEFT JOIN (SELECT COUNT(id_) num_,im_group_id_ FROM im_group_member GROUP BY im_group_id_) igm ON ig.id_ = igm.im_group_id_
		SET member_num_ = igm.num_
		WHERE ig.id_ = igm.im_group_id_
	</update>

	<!-- 根据主键删除一条记录 -->
	<delete id="delete" >
		DELETE FROM im_group WHERE id_ = #{id} 
	</delete>
	<sql id="queryPageSql">
		<where>
			<if test="userId != null">
				AND igm.user_id_ = #{userId}
			</if>
			<if test="search != null and search != ''">
				AND (ig.name_ like concat('%',#{search},'%') or ig.memo_ like concat('%',#{search},'%'))
			</if>
			<if test="ignoreIds != null and ignoreIds != ''">
				AND NOT FIND_IN_SET(ig.id_,#{ignoreIds})
			</if>
			<if test="targetIds != null and targetIds != ''">
				AND FIND_IN_SET(ig.id_,#{targetIds})
			</if>
			<if test="groupType != null">
				AND ig.group_type_ = #{groupType}
			</if>
		</where>
	</sql>
	
	<!-- 分页查询 -->
	<select id="queryPage" resultMap="ImGroup" parameterType="map">
		SELECT ig.* FROM im_group ig
		LEFT JOIN im_group_member igm ON igm.im_group_id_ = ig.id_
		<include refid="queryPageSql" />
		GROUP BY ig.id_
		ORDER BY id_
		<include refid="global.limit"/>
	</select>
	
	<!-- 查询当前表的总记录数 -->
	<select id="queryCount" resultType="int">
		SELECT COUNT(DISTINCT ig.id_) FROM im_group ig
		LEFT JOIN im_group_member igm ON igm.im_group_id_ = ig.id_
		<include refid="queryPageSql" />
	</select>
	
	<select id="queryByUserId" resultMap="ImGroup" parameterType="map">
		SELECT ig.* FROM im_group_member igm
		LEFT JOIN im_group ig ON igm.im_group_id_ = ig.id_
		where 1 =1
		<if test="userId != null">
			AND igm.user_id_ = #{userId}
		</if>
		<if test="search != null and search != ''">
			AND (ig.name_ like concat('%',#{search},'%') or ig.tags_ like concat('%',#{search},'%'))
		</if>
		<if test="groupType != null and groupType != ''">
			AND ig.group_type_ = #{groupType}
		</if>
	</select>
	
	<select id="queryMemberById" resultMap="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_
		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_
		WHERE igm.im_group_id_ = #{imGroupId}
	</select>
	
	<select id="queryMember" resultMap="ImGroupMemberDto" parameterType="map">
		SELECT igm.*,u.avatar_,CASE WHEN find_in_set('STUDENT',u.user_type_) THEN u.username_ ELSE u.real_name_ END username_
		FROM im_group_member igm left join sys_user u on igm.user_id_ = u.id_
		WHERE igm.user_id_ = #{userId}
		<if test="imGroupId != null and imGroupId != ''">
			AND igm.im_group_id_ = #{imGroupId}
		</if>
		<if test="imGroupId == null or imGroupId == ''">
			LIMIT 1
		</if>
	</select>

	<resultMap id="NameDto" type="com.ym.mec.biz.dal.dto.NameDto">
		<result property="id" column="id_" />
		<result property="name" column="name_" />
		<result property="type" column="type_"/>
	</resultMap>
    <select id="queryNameByIds" resultMap="NameDto">
		select id_,name_,3 type_ from im_group where FIND_IN_SET(id_,#{ids})
	</select>
</mapper>
