<?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.yonge.cooleshow.biz.dal.dao.ImUserFriendDao">
    <resultMap id="BaseResultMap" type="com.yonge.cooleshow.biz.dal.entity.ImUserFriend">
        <id column="id_" jdbcType="INTEGER" property="id"/>
        <result column="user_id_" jdbcType="INTEGER" property="userId"/>
        <result column="friend_id_" jdbcType="INTEGER" property="friendId"/>
        <result column="friend_nickname_" jdbcType="VARCHAR" property="friendNickname"/>
        <result column="friend_avatar_" jdbcType="VARCHAR" property="friendAvatar"/>
        <result column="memo_" jdbcType="VARCHAR" property="memo"/>
        <result column="create_time_" jdbcType="TIMESTAMP" property="createTime"/>
        <result column="update_time_" jdbcType="TIMESTAMP" property="updateTime"/>
    </resultMap>

    <sql id="Base_Column_List">
        id_, user_id_, friend_id_, friend_nickname_,friend_avatar_, memo_, create_time_, update_time_
    </sql>

    <insert id="insertBatch" keyColumn="id_" keyProperty="id" useGeneratedKeys="true"
            parameterType="com.yonge.cooleshow.biz.dal.entity.ImUserFriend">
        INSERT INTO im_user_friend(user_id_, client_type_, friend_id_, friend_type_, friend_nickname_,friend_avatar_, memo_, create_time_, update_time_) VALUES
        <foreach collection="entities" item="entity" separator=",">
            (#{entity.userId}, #{entity.clientType}, #{entity.friendId}, #{entity.friendType}, #{entity.friendNickname}, #{entity.friendAvatar}, #{entity.memo}, #{entity.createTime}, #{entity.updateTime})
        </foreach>
        ON DUPLICATE KEY UPDATE user_id_ = VALUES(user_id_), client_type_ = VALUES(client_type_), friend_id_ = VALUES(friend_id_), friend_type_ = VALUES(friend_type_)
    </insert>
    <select id="findByUserIdAndFriendId" resultMap="BaseResultMap">
        SELECT <include refid="Base_Column_List"/> FROM im_user_friend WHERE user_id_ = #{userId} AND friend_id_ = #{friendId} LIMIT 1
    </select>

    <delete id="delTeacherFriendByTenantId">
        DELETE t1
        FROM im_user_friend t1
                 LEFT JOIN teacher t2 ON t1.friend_id_ = t2.user_id_
                 LEFT JOIN teacher t3 ON t1.user_id_ = t3.user_id_
        WHERE (t1.user_id_ = #{userId} and t1.client_type_ = #{clientType} and t2.tenant_id_ = #{tenantId})
           OR (t1.friend_id_ = #{userId} and t1.friend_type_ = #{clientType} and t3.tenant_id_ = #{tenantId})
    </delete>

    <delete id="delStudentFriendByTenantId">
        DELETE t1
        FROM im_user_friend t1
                 LEFT JOIN student t2 ON t1.friend_id_ = t2.user_id_
                 LEFT JOIN student t3 ON t1.user_id_ = t3.user_id_
        WHERE (t1.user_id_ = #{userId} and t1.client_type_ = #{clientType} and t2.tenant_id_ = #{tenantId})
           OR (t1.friend_id_ = #{userId} and t1.friend_type_ = #{clientType} and t3.tenant_id_ = #{tenantId})
    </delete>
</mapper>
