<?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.ImUserFriendDao">
	
	<resultMap type="com.ym.mec.biz.dal.entity.ImUserFriend" id="ImUserFriend">
		<result column="id_" property="id" />
		<result column="user_id_" property="userId" />
		<result column="friend_id_" property="friendId" />
		<result column="friend_nickname_" property="friendNickname" />
		<result column="memo_" property="memo" />
		<result column="tags_" property="tags" />
		<result column="member_rank_setting_id_" property="memberRankSettingId" />
		<result column="subject_name_" property="subjectName" />
		<result column="subject_id_" property="subjectId" />
		<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.ImUserFriendDto" id="ImUserFriendDto" extends="ImUserFriend">
		<result column="real_name_" property="friend.realName" />
		<result column="avatar_" property="friend.avatar" />
		<result column="user_type_" property="friend.userType" />
		<result column="phone_" property="friend.phone" />
	</resultMap>
	
	<!-- 根据主键查询一条记录 -->
	<select id="get" resultMap="ImUserFriend" >
		SELECT * FROM im_user_friend WHERE id_ = #{id} 
	</select>
	
	<!-- 全查询 -->
	<select id="findAll" resultMap="ImUserFriend">
		SELECT * FROM im_user_friend where tenant_id_ = #{tenantId} ORDER BY id_
	</select>
	
	<!-- 向数据库增加一条记录 -->
	<insert id="insert" parameterType="com.ym.mec.biz.dal.entity.ImUserFriend" useGeneratedKeys="true" keyColumn="id" keyProperty="id">
		INSERT INTO im_user_friend (id_,user_id_,friend_id_,friend_nickname_,memo_,tags_,create_time_,update_time_,tenant_id_)
		VALUES(#{id},#{userId},#{friendId},#{friendNickname},#{memo},#{tags},NOW(),NOW(),#{tenantId})
	</insert>

	<insert id="insertByBasicUser">
		INSERT INTO im_user_friend (user_id_,friend_id_,friend_nickname_,tags_,memo_,create_time_,update_time_,tenant_id_)
		VALUES (#{teacherId},#{basicUserDto.userId},#{basicUserDto.name},#{basicUserDto.subjectName},#{basicUserDto.musicGroupName},NOW(),NOW(),#{basicUserDto.tenantId})
	</insert>

	<!-- 根据主键查询一条记录 -->
	<update id="update" parameterType="com.ym.mec.biz.dal.entity.ImUserFriend">
		UPDATE im_user_friend <set>
		<if test="userId != null">
		user_id_ = #{userId},
		</if>
		<if test="friendId != null">
		friend_id_ = #{friendId},
		</if>
		<if test="friendNickname != null">
		friend_nickname_ = #{friendNickname},
		</if>
		<if test="memo != null">
		memo_ = #{memo},
		</if>
		<if test="tags != null">
		tags_ = #{tags},
		</if>
		update_time_ = NOW()
		</set> WHERE id_ = #{id} and tenant_id_ = #{tenantId}
	</update>
	<update id="updateNullNickName">
		UPDATE im_user_friend iuf
		LEFT JOIN sys_user su ON su.id_ = iuf.friend_id_
		SET iuf.friend_nickname_ = CASE WHEN su.real_name_ IS NULL OR su.real_name_ = '' THEN su.username_ ELSE su.real_name_ END
		WHERE iuf.friend_nickname_ IS NULL
	</update>

	<!-- 根据主键删除一条记录 -->
	<delete id="delete" >
		DELETE FROM im_user_friend WHERE id_ = #{id} 
	</delete>
    <delete id="deleteByUserId">
		DELETE FROM im_user_friend WHERE user_id_ = #{teacherId}
	</delete>

    <!-- 分页查询 -->
	<select id="queryPage" resultMap="ImUserFriend" parameterType="map">
		SELECT * FROM im_user_friend where tenant_id_ = #{tenantId} ORDER BY id_ <include refid="global.limit"/>
	</select>
	
	<!-- 查询当前表的总记录数 -->
	<select id="queryCount" resultType="int">
		SELECT COUNT(*) FROM im_user_friend where tenant_id_ = #{tenantId}
	</select>
	
	<select id="query" resultMap="ImUserFriend" parameterType="map">
		SELECT * FROM im_user_friend where user_id_ = #{userId} and friend_id_ = #{friendId} LIMIT 1
	</select>
	
	<select id="queryFriendListByUserId" resultMap="ImUserFriendDto" parameterType="map">
		SELECT iuf.*,u.real_name_,u.avatar_,u.phone_,u.user_type_,st.member_rank_setting_id_,s.name_ subject_name_,st.subject_id_list_ subject_id_
		FROM im_user_friend iuf
		LEFT JOIN sys_user u ON iuf.friend_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 iuf.user_id_ = #{userId}
		<if test="search != null">
			and (u.real_name_ like concat('%',#{search},'%') or iuf.friend_nickname_ like concat('%',#{search},'%'))
		</if>
	</select>
	
	<select id="queryFriendDetail" resultMap="ImUserFriendDto" parameterType="map">
		SELECT iuf.*,u.real_name_,u.avatar_,u.phone_,u.user_type_,s.subject_id_list_ subject_id_,s.member_rank_setting_id_,sb.name_ subject_name_
		FROM im_user_friend iuf
		LEFT JOIN sys_user u ON iuf.friend_id_ = u.id_
		LEFT JOIN student s ON s.user_id_ = u.id_
		LEFT JOIN subject sb ON s.subject_id_list_ = sb.id_
		WHERE iuf.user_id_ = #{userId} AND iuf.friend_id_ = #{friendUserId}
	</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_"/>
		<result property="avatar" column="avatar_"/>
	</resultMap>
    <select id="queryNameByIds" resultMap="NameDto">
		select iuf.friend_id_ id_,iuf.friend_nickname_ name_,1 type_,su.avatar_
		from im_user_friend iuf
		left join sys_user su on iuf.friend_id_ = su.id_
		where iuf.user_id_ = #{userId} and find_in_set(friend_id_,#{ids})
	</select>
</mapper>
