对List集合分页处理--项目实战

封装的参数类

@Data
@Schema(description = "关注粉丝分页列表")
public class PageFollowsRequest {
    @Schema(description = "用户ID")
    private Long accountId;

    @Schema(description = "用户ID")
    private int pagesize;

    @Schema(description = "用户ID")
    private int currentPage;
}

第一种方法

    @Operation(summary = "查看用户的粉丝用户列表")
    @GetMapping("/AccountFans")
    public Result<List<AccountResponse>> AccountFans(@Validated     PageFollowsRequest pageFollowsRequest) {
        Long accountId=pageFollowsRequest.getAccountId();
        int pagesize=pageFollowsRequest.getPagesize();
        int currentPage=pageFollowsRequest.getCurrentPage();
        return Result.ok(accountService.getAccountRelationsFan(accountId,pagesize,currentPage));
    }




 //粉丝列表
    @Override
    public List<AccountResponse> getAccountRelationsFan(Long accountId, int pagesize, int currentPage) {
        //用来查看当前用户
        Long currentLoginAccountId = loginContext.getAccountId();
        // 查看关注用户id
        List<AccountFollow> followList = accountFollowService.list(
                Wrappers.<AccountFollow>lambdaQuery()
                        .eq(AccountFollow::getAccountId, accountId)
        );
        // 获取粉丝用户id
        List<AccountFollow> fansList = accountFollowService.list(
                Wrappers.<AccountFollow>lambdaQuery()
                        .eq(AccountFollow::getFollowId, accountId)
        );
        // 获取关注用户id
        Set<Long> followIds = followList.stream().map(AccountFollow::getFollowId).collect(Collectors.toSet());
        // 获取粉丝用户id
        Set<Long> fansIds = fansList.stream().map(AccountFollow::getAccountId).collect(Collectors.toSet());
        if (CollUtil.isEmpty(fansIds)) {
            return Collections.emptyList();
        }
        // 查询粉丝列表(最终返回结果)----做分页处理
        List<AccountResponse> followAccounts = accountMapper.selectFollow(fansIds);
        // 交集  互相关注
        Set<Long> intersectionIds = CollUtil.intersectionDistinct(followIds, fansIds);
        // 如果是当前登录的用户
        if (currentLoginAccountId.equals(accountId)) {
            return followAccounts.stream()
                    .peek(accountResponse -> {
                        if (intersectionIds.contains(accountResponse.getId())) {
                            accountResponse.setRelation(AccountResponse.MUTUAL_FOLLOW);
                        } else {
                            accountResponse.setRelation(AccountResponse.UNFOLLOW);
                        }
                    }).collect(Collectors.toList());
        }


        //查看我的关注用户id
        List<AccountFollow> currentFollowList = accountFollowService.list(
                Wrappers.<AccountFollow>lambdaQuery()
                        .eq(AccountFollow::getAccountId, currentLoginAccountId)
        );
        //查看我的粉丝用户id
        List<AccountFollow> currentFansList = accountFollowService.list(
                Wrappers.<AccountFollow>lambdaQuery()
                        .eq(AccountFollow::getFollowId, currentLoginAccountId)
        );

        //我的关注ID列表
        Set<Long> myFollowIds = currentFollowList.stream().map(AccountFollow::getFollowId).collect(Collectors.toSet());
        //我的粉思Id列表
        Set<Long> myFansIds = currentFansList.stream().map(AccountFollow::getAccountId).collect(Collectors.toSet());
        //我的交集
        Set<Long> myIntersection = CollUtil.intersectionDistinct(myFollowIds, myFansIds);

        // 所有交集关系 -- 关注关系
        Set<Long> allIntersection = CollUtil.intersectionDistinct(fansIds, myFollowIds);

        // 互相关注关系
        Set<Long> mutualIntersection = CollUtil.intersectionDistinct(allIntersection, myIntersection);

        allIntersection.removeAll(mutualIntersection);

        for (AccountResponse followAccount : followAccounts) {
            Long id = followAccount.getId();
            if (allIntersection.contains(id)) {
                // 已关注
                followAccount.setRelation(AccountResponse.FOLLOWED);
            } else if (mutualIntersection.contains(id)) {
                // 互相关注
                followAccount.setRelation(AccountResponse.MUTUAL_FOLLOW);
            } else {
                // 未关注
                followAccount.setRelation(AccountResponse.UNFOLLOW);
            }
        }

        //分页处理
        List<AccountResponse> currentPageList = new ArrayList<>();

        if (followAccounts != null && followAccounts.size() > 0) {

            int currIdx = (currentPage > 1 ? (currentPage - 1) * pagesize : 0);

            for (int i = 0; i < pagesize && i < followAccounts.size() - currIdx; i++) {

                AccountResponse data = followAccounts.get(currIdx + i);

                currentPageList.add(data);

            }

        }
        return currentPageList;
    }

第二种方法


@Operation(summary = "查看用户的关注用户列表")
    @GetMapping("/AccountFollow")
    public Result<List<AccountResponse>> AccountsFollow(@Validated PageFollowsRequest pageFollowsRequest) {
      Long accountId=pageFollowsRequest.getAccountId();
      int pagesize=pageFollowsRequest.getPagesize();
      int currentPage=pageFollowsRequest.getCurrentPage();
        return Result.ok(accountService.getAccountRelationsFollow(accountId,pagesize,currentPage));
    }




    //关注列表
    @Override
    public List<AccountResponse> getAccountRelationsFollow(Long accountId, int pagesize, int currentPage) {
        //用来查看当前用户
        Long currentLoginAccountId = loginContext.getAccountId();
        // 查看关注用户id
        List<AccountFollow> followList = accountFollowService.list(
                Wrappers.<AccountFollow>lambdaQuery()
                        .eq(AccountFollow::getAccountId, accountId)
        );
        // 获取粉丝用户id
        List<AccountFollow> fansList = accountFollowService.list(
                Wrappers.<AccountFollow>lambdaQuery()
                        .eq(AccountFollow::getFollowId, accountId)
        );
        // 获取关注用户id
        Set<Long> followIds = followList.stream().map(AccountFollow::getFollowId).collect(Collectors.toSet());
        // 获取粉丝用户id
        Set<Long> fansIds = fansList.stream().map(AccountFollow::getAccountId).collect(Collectors.toSet());
        if (CollUtil.isEmpty(followIds)) {
            return Collections.emptyList();
        }
        // 查询关注列表
        List<AccountResponse> followAccounts = accountMapper.selectFollow(followIds);
        // 交集  互相关注
        Set<Long> intersectionIds = CollUtil.intersectionDistinct(followIds, fansIds);
        // 如果是当前登录的用户
        if (currentLoginAccountId.equals(accountId)) {
            return followAccounts.stream()
                    .peek(accountResponse -> {
                        if (intersectionIds.contains(accountResponse.getId())) {
                            accountResponse.setRelation(AccountResponse.MUTUAL_FOLLOW);
                        } else {
                            accountResponse.setRelation(AccountResponse.FOLLOWED);
                        }
                    }).collect(Collectors.toList());
        }


        //查看我的关注用户id
        List<AccountFollow> currentFollowList = accountFollowService.list(
                Wrappers.<AccountFollow>lambdaQuery()
                        .eq(AccountFollow::getAccountId, currentLoginAccountId)
        );
        //查看我的粉丝用户id
        List<AccountFollow> currentFansList = accountFollowService.list(
                Wrappers.<AccountFollow>lambdaQuery()
                        .eq(AccountFollow::getFollowId, currentLoginAccountId)
        );

        //我的关注ID列表
        Set<Long> myFollowIds = currentFollowList.stream().map(AccountFollow::getFollowId).collect(Collectors.toSet());
        //我的粉思Id列表
        Set<Long> myFansIds = currentFansList.stream().map(AccountFollow::getAccountId).collect(Collectors.toSet());
        //我的交集
        Set<Long> myIntersection = CollUtil.intersectionDistinct(myFollowIds, myFansIds);

        // 所有交集关系 -- 关注关系
        Set<Long> allIntersection = CollUtil.intersectionDistinct(followIds, myFollowIds);

        // 互相关注关系
        Set<Long> mutualIntersection = CollUtil.intersectionDistinct(allIntersection, myIntersection);

        allIntersection.removeAll(mutualIntersection);

        for (AccountResponse followAccount : followAccounts) {
            Long id = followAccount.getId();
            if (allIntersection.contains(id)) {
                // 已关注
                followAccount.setRelation(AccountResponse.FOLLOWED);
            } else if (mutualIntersection.contains(id)) {
                // 互相关注
                followAccount.setRelation(AccountResponse.MUTUAL_FOLLOW);
            } else {
                // 未关注
                followAccount.setRelation(AccountResponse.UNFOLLOW);
            }
        }
        //分页
        int totalcount = followAccounts.size();
        int pagecount = 0;
        List<AccountResponse> subList;
        int m = totalcount % pagesize;
        if (m > 0) {
            pagecount = totalcount / pagesize + 1;
        } else {
            pagecount = totalcount / pagesize;
        }
        if (m == 0) {
            subList = followAccounts.subList((currentPage - 1) * pagesize, pagesize * (currentPage));
        } else {
            if (currentPage == pagecount) {
                subList = followAccounts.subList((currentPage - 1) * pagesize, totalcount);
            } else {
                subList = followAccounts.subList((currentPage - 1) * pagesize, pagesize * (currentPage));
            }
        }
        return subList;
    }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值