<?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.wecloud.im.friend.mapper.ImFriendMapper">

    <!-- 通用查询结果列 -->
    <sql id="Base_Column_List">
        id, client_id, client_id_friend, friend_name,reject_remark,request_remark,state,create_time, update_time
    </sql>

    <select id="getFriendInfo" resultType="com.wecloud.im.friend.param.ImFriendApplyDto">
        select
            friend.id, client.client_id as friendClientId, friend.friend_name, claimer.client_id as claimerClientId,
            friend.reject_remark, friend.request_remark, friend.state, friend.create_time
        from im_friend friend inner join im_client client on friend.client_id_friend = client.client_id
                              inner join im_client claimer on friend.client_id_claimer = claimer.client_id
        where friend.client_id = #{clientId} and client_id_friend = #{friendClientId} and friend.state = 2
    </select>

    <select id="getImFriendPageList" resultType="com.wecloud.im.friend.param.ImFriendBaseDto">
        select
            friend.id, client.client_id as friendClientId, friend.friend_name, friend.state
        from im_friend friend inner join im_client client on friend.client_id_friend = client.client_id
        where friend.client_id = #{clientId} and friend.state = 2
    </select>

    <select id="getApplyFriendPageList" resultType="com.wecloud.im.friend.param.ImFriendApplyDto">
        select friend.id,
               client.client_id   as friendClientId,
               friend.friend_name as friendName,
               claimer.client_id  as claimerClientId,
               friend.reject_remark,
               friend.request_remark,
               friend.state,
               friend.update_time,
               friend.create_time
        from im_friend friend
                 inner join im_client client on friend.client_id_friend = client.client_id
                 inner join im_client claimer on friend.client_id_claimer = claimer.client_id
        where friend.client_id = #{clientId}
          and friend.client_id_claimer != #{clientId}
        order by friend.update_time desc
    </select>

    <select id="filterNegativeFriends" resultType="java.lang.String">
        select
            client_id_friend
        from im_friend
        where client_id = #{clientId} and client_id_friend in
        <foreach collection="friendClientIds" item="friendClientId" open="(" close=")" separator=",">
            #{friendClientId}
        </foreach>
    </select>

    <update id="batchDeleteFriend">
        update im_friend set state = 4 where client_id = #{clientId} and client_id_friend in (
        select client_id from im_client where (fk_appid, client_id) in
        <foreach collection="friendClientIds" item="friendClientId" open="(" close=")" separator=",">
            (#{fkAppId}, #{friendClientId})
        </foreach>
        )
    </update>

</mapper>
