天机学堂day5作业,分页查询回答或评论列表--管理端

InteractionReplyAdminController

/**
     * 分页查询回答或评论列表--管理端
     * @param query
     * @return
     */
    @ApiOperation("分页查询回答或评论列表--管理端")
    @GetMapping("/page")
    public PageDTO<ReplyVO> queryReplyAdminPage(ReplyPageQuery query) {
        return replyService.queryReplyAdminPage(query);
    }

IInteractionReplyService

/**
     * 分页查询回答或评论列表--管理端
     * @param query
     * @return
     */
    PageDTO<ReplyVO> queryReplyAdminPage(ReplyPageQuery query);

InteractionReplyServiceImpl

/**
     * 分页查询回答或评论列表--管理端
     * @param query
     * @return
     */
    @Override
    public PageDTO<ReplyVO> queryReplyAdminPage(ReplyPageQuery query) {
        //1、校验问题id是否存在
        if (query.getQuestionId() == null && query.getAnswerId() == null) {
            throw new BadRequestException("问题id和回答id不能为空!");
        }

        //2、分页查询 interaction_reply表
        Page<InteractionReply> page = this.lambdaQuery()
                .eq(query.getQuestionId() != null, InteractionReply::getQuestionId, query.getQuestionId())
                //如果回答id没传,则查询answer_id为0的数据,也就是回答
                .eq(InteractionReply::getAnswerId, query.getAnswerId() == null ? 0L : query.getAnswerId())
                .eq(InteractionReply::getHidden, false)
                .page(query.toMpPage( //先根据点赞数排序,点赞数相同,再按照创建时间排序
                        new OrderItem(Constant.DATA_FIELD_NAME_LIKED_TIME, false),
                        new OrderItem(Constant.DATA_FIELD_NAME_CREATE_TIME, true)));
        List<InteractionReply> records = page.getRecords();
        if (CollUtils.isEmpty(records)) {
            return PageDTO.empty(page);
        }

        //3、数据处理,需要查询:提问者信息、回复目标信息 (不管用户是否匿名,均要查询)
        Set<Long> uids = new HashSet<>();
        Set<Long> targetReplyIds = new HashSet<>();
        for (InteractionReply record : records) {
            uids.add(record.getUserId());
            uids.add(record.getTargetUserId());
            if (record.getTargetReplyId() != null && record.getTargetReplyId() > 0) {
                targetReplyIds.add(record.getTargetReplyId());
            }
        }

        //查询目标回复,查询目标回复的用户信息 (不管用户是否匿名,均要查询)
        if (targetReplyIds.size() > 0) {
            List<InteractionReply> targetReplies = this.listByIds(targetReplyIds);
            Set<Long> targetUserIds = targetReplies.stream()
                    .map(InteractionReply::getUserId)
                    .collect(Collectors.toSet());
            uids.addAll(targetUserIds);
        }

        //远程调用用户服务,查询用户信息
        List<UserDTO> userDTOList = userClient.queryUserByIds(uids);
        Map<Long, UserDTO> userDTOMap = new HashMap<>();
        if (userDTOList != null) {
            userDTOMap = userDTOList.stream().collect(Collectors.toMap(UserDTO::getId, c -> c));
        }

        //4、封装vo,并返回 (不管用户是否匿名,均要查询)
        List<ReplyVO> voList = new ArrayList<>();
        for (InteractionReply record : records) {
            ReplyVO vo = BeanUtils.copyBean(record, ReplyVO.class);
            UserDTO userDTO = userDTOMap.get(record.getUserId());
            if (userDTO != null) {
                vo.setUserName(userDTO.getName());
                vo.setUserIcon(userDTO.getIcon());
                vo.setUserType(userDTO.getType());
            }

            UserDTO targetUserDTO = userDTOMap.get(record.getTargetUserId());
            if (targetUserDTO != null) {
                vo.setTargetUserName(targetUserDTO.getName());
            }
            voList.add(vo);
        }
        return PageDTO.of(page, voList);
    }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值