package com.ruoyi.cai.manager;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyi.cai.domain.User;
import com.ruoyi.cai.domain.UserFollow;
import com.ruoyi.cai.notice.YunxinHttpService;
import com.ruoyi.cai.service.UserFollowService;
import com.ruoyi.cai.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

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

@Component
public class LoginAfterManager {
    @Autowired
    private YunxinHttpService yunxinHttpService;
    @Autowired
    private UserFollowService userFollowService;
    @Autowired
    private UserService userService;

    public void loginAfter(Long userId){
        // 给我的粉丝推送上线消息
        User user = userService.getById(userId);
        if(user == null){
            return;
        }
        Page<UserFollow> page = new Page<>();
        page.setSize(500);
        long current = 0L;
        while (true){
            current++;
            page.setCurrent(current);
            Page<UserFollow> pageRes = userFollowService.page(page, Wrappers.lambdaQuery(UserFollow.class)
                    .eq(UserFollow::getFollowUser, userId));
            List<UserFollow> records = pageRes.getRecords();
            if(records.isEmpty()){
                break;
            }
            List<Long> userIds = records.stream().map(UserFollow::getUserId).collect(Collectors.toList());
            yunxinHttpService.sendOnlineAttentionNotice(userIds,user);
        }


    }


}
