<?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, fk_client_id, fk_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.fk_client_id_friend = client.id
                              inner join im_client claimer on friend.fk_client_id_claimer = claimer.id
        where friend.fk_client_id = #{clientId} and fk_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.fk_client_id_friend = client.id
        where friend.fk_client_id = #{clientId} and friend.state = 2
    </select>

    <select id="getUnsureFriendPageList" 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.fk_client_id_friend = client.id
                inner join im_client claimer on friend.fk_client_id_claimer = claimer.id
        where friend.fk_client_id = #{clientId} and friend.state = 1
    </select>

    <select id="filterNegativeFriends" resultType="java.lang.Long">
        select
            fk_client_id_friend
        from im_friend
        where fk_client_id = #{clientId} and fk_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 fk_client_id = #{clientId} and fk_client_id_friend in (
        select id from im_client where (fk_appid, client_id) in
        <foreach collection="friendClientIds" item="friendCLientId" open="(" close=")" separator=",">
            (#{fkAppId}, #{friendCLientId})
        </foreach>
        )
    </update>

</mapper>
