<?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="create_time_" property="createTime" />
		<result column="update_time_" property="updateTime" />
	</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="username_" property="user.username" />
		<result column="avatar_" property="user.avatar" />
	</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 ORDER BY id_
	</select>
	
	<!-- 向数据库增加一条记录 -->
	<insert id="insert" parameterType="com.ym.mec.biz.dal.entity.ImGroup" useGeneratedKeys="true" keyColumn="id" keyProperty="id">
		<!--
		<selectKey resultClass="int" keyProperty="id" > 
		SELECT SEQ_WSDEFINITION_ID.nextval AS ID FROM DUAL 
		</selectKey>
		-->
		INSERT INTO im_group (id_,name_,introduce_,member_num_,memo_,tags_,img_,type_,create_time_,update_time_)
		VALUES(#{id},#{name},#{introduce},#{memberNum},#{memo},#{tags},#{img},#{type},#{createTime},#{updateTime})
	</insert>
	
	<!-- 根据主键查询一条记录 -->
	<update id="update" parameterType="com.ym.mec.biz.dal.entity.ImGroup">
		UPDATE im_group <set>
		<if test="memberNum != null">
		member_num_ = #{memberNum},
		</if>
		<if test="id != null">
		id_ = #{id},
		</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>
		<if test="createTime != null">
		create_time_ = #{createTime},
		</if>
		</set> WHERE id_ = #{id} 
	</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>
	
	<!-- 分页查询 -->
	<select id="queryPage" resultMap="ImGroup" parameterType="map">
		SELECT * FROM im_group ORDER BY id_ <include refid="global.limit"/>
	</select>
	
	<!-- 查询当前表的总记录数 -->
	<select id="queryCount" resultType="int">
		SELECT COUNT(*) FROM im_group
	</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 igm.user_id_ = #{userId}
		<if test="search != null">
			and (ig.name_ like concat('%',#{search},'%') or ig.tags_ like concat('%',#{search},'%'))
		</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_
		FROM im_group_member igm left join sys_user u on igm.user_id_ = u.id_
		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.im_group_id_ = #{imGroupId} and igm.user_id_ = #{userId}
	</select>
</mapper>
