package com.izouma.dingdong.repo;

import cn.hutool.core.collection.CollUtil;
import com.izouma.dingdong.domain.Chat;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;


@RunWith(SpringRunner.class)
@SpringBootTest
public class ChatRepoTest {
    @Autowired
    private ChatRepo chatRepo;

    @Test
    public void show() {
        List<Chat> cs = chatRepo.findAllBySendUserIdAndReceiveUserId(82L, 158L);
        List<Chat> chats = chatRepo.findAllBySendUserIdAndReceiveUserId(158L, 82L);
        chats.addAll(cs);
        System.out.println(chats);

        System.out.println(cs.get(0).getSendUser().getAvatar());
    }

    @Test
    public void showMy() {
        List<Chat> chats = chatRepo.findAllBySendUserIdOrReceiveUserId(82L, 82L);
        Set<Long> sends = chats.stream().map(Chat::getSendUserId).collect(Collectors.toSet());
        Set<Long> reces = chats.stream().map(Chat::getReceiveUserId).collect(Collectors.toSet());
        sends.addAll(reces);
        sends.remove(82L);
       // System.out.println(sends);

        List<Chat> ch = new ArrayList<>();

        sends.forEach(s -> {
            List<Chat> c1 = chatRepo.findAllBySendUserIdAndReceiveUserId(s, 82L);
            List<Chat> c2 = chatRepo.findAllBySendUserIdAndReceiveUserId(82L, s);
            c1.addAll(c2);

            c1.sort((a,b)->b.getId().compareTo(a.getId()));
            //CollUtil.sort(c1,(a,b)->b.getId().compareTo(a.getId()));

            ch.add(c1.get(0));
        });

        System.out.println(ch);

        //System.out.println(chatRepo.findAllBySendUserIdOrReceiveUserId(82L,82L));
    }
}
