<?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="create_time_" property="createTime" />
		<result column="update_time_" property="updateTime" />
	</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 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_)
		VALUES(#{id},#{userId},#{friendId},#{friendNickname},#{memo},#{tags},NOW(),NOW())
	</insert>

	<insert id="insertByBasicUser">
		INSERT INTO im_user_friend (user_id_,friend_id_,friend_nickname_,tags_,memo_,create_time_,update_time_)
		VALUES (#{teacherId},#{basicUserDto.userId},#{basicUserDto.name},#{basicUserDto.subjectName},#{basicUserDto.musicGroupName},NOW(),NOW())
	</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}
	</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 ORDER BY id_ <include refid="global.limit"/>
	</select>
	
	<!-- 查询当前表的总记录数 -->
	<select id="queryCount" resultType="int">
		SELECT COUNT(*) FROM im_user_friend
	</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_
		FROM im_user_friend iuf
		LEFT JOIN sys_user u ON iuf.friend_id_ = u.id_
		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_ FROM im_user_friend iuf left join sys_user u on iuf.friend_id_ = u.id_
		where iuf.user_id_ = #{userId} and iuf.friend_id_ = #{friendUserId}
	</select>
</mapper>
