mitao
2024-06-12 0d4f7cd29a978594453b102d4a767d64085df29d
ruoyi-modules/ruoyi-article/src/main/java/com/ruoyi/article/service/impl/ArticleCommentsServiceImpl.java
@@ -1,16 +1,31 @@
package com.ruoyi.article.service.impl;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.article.controller.forepart.dto.ArticleCommentsDTO;
import com.ruoyi.article.controller.management.dto.MgtArticleCommentsQuery;
import com.ruoyi.article.controller.management.vo.MgtArticleCommentsVO;
import com.ruoyi.article.domain.Article;
import com.ruoyi.article.domain.ArticleComments;
import com.ruoyi.article.controller.forepart.dto.ArticleCommentsDTO;
import com.ruoyi.article.mapper.ArticleCommentsMapper;
import com.ruoyi.article.mapper.ArticleMapper;
import com.ruoyi.article.service.IArticleCommentsService;
import com.ruoyi.common.core.constant.SecurityConstants;
import com.ruoyi.common.core.exception.ServiceException;
import org.springframework.stereotype.Service;
import com.ruoyi.common.core.utils.StringUtils;
import com.ruoyi.common.core.utils.page.PageDTO;
import com.ruoyi.system.api.domain.Member;
import com.ruoyi.system.api.domain.dto.MemberDTO;
import com.ruoyi.system.api.feignClient.MemberClient;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.function.Function;
import java.util.stream.Collectors;
import javax.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
/**
 * <p>
@@ -26,7 +41,8 @@
    @Resource
    private ArticleMapper articleMapper;
    @Resource
    private MemberClient memberClient;
    @Override
    public void saveMemberArticleComments(ArticleCommentsDTO articleCommentsDTO) {
        ArticleComments articleComments=new ArticleComments();
@@ -65,4 +81,77 @@
        this.removeById(articleCommentsDTO.getId());
    }
    /**
     * 查看详情-评论详情
     *
     * @param query 资讯评论回复查询对象
     * @return PageDTO<MgtArticleCommentsVO>
     */
    @Override
    public PageDTO<MgtArticleCommentsVO> getArticleCommentsPage(MgtArticleCommentsQuery query) {
        PageDTO<MgtArticleCommentsVO> result;
        Set<Long> memberIdSet = null;
        if (StringUtils.isNotBlank(query.getNickname())) {
            MemberDTO memberDTO = new MemberDTO();
            memberDTO.setNickname(query.getNickname());
            List<Member> data = memberClient.getMemberListByCondition(memberDTO,
                    SecurityConstants.INNER).getData();
            if (StringUtils.isNotEmpty(data)) {
                memberIdSet = data.stream().map(Member::getId)
                        .collect(Collectors.toSet());
            }
        }
        Page<ArticleComments> page = this.lambdaQuery()
                .eq(ArticleComments::getArticleId, query.getArticleId())
                .in(StringUtils.isNotEmpty(memberIdSet), ArticleComments::getMemberId, memberIdSet)
                .page(new Page<>(query.getPageCurr(), query.getPageSize()));
        if (StringUtils.isEmpty(page.getRecords())) {
            result = PageDTO.empty(page);
        } else {
            PageDTO<MgtArticleCommentsVO> pageVO = PageDTO.of(page,
                    MgtArticleCommentsVO.class);// 获取会员信息
            Set<Long> memIdSet = pageVO.getList().stream().map(MgtArticleCommentsVO::getMemberId)
                    .collect(Collectors.toSet());
            List<Member> memberList = memberClient.getMemberListByIds(memIdSet,
                    SecurityConstants.INNER).getData();
            Map<Long, Member> memberMap = memberList.stream()
                    .collect(Collectors.toMap(Member::getId, Function.identity()));
            pageVO.getList().forEach(item -> {
                Member member = memberMap.get(item.getMemberId());
                if (StringUtils.isNotNull(member)) {
                    item.setNickname(member.getNickname());
                    item.setPhone(member.getPhone());
                }
            });
            result = pageVO;
        }
        return result;
    }
    @Override
    @Transactional(propagation = Propagation.REQUIRES_NEW)
    public void delArticleComments(Long id) {
        ArticleComments articleComments = this.getById(id);
        if (StringUtils.isNull(articleComments)) {
            throw new ServiceException("评论不存在");
        }
        // 查询资讯
        Article article = articleMapper.selectById(articleComments.getArticleId());
        if (articleComments.getType() == 2) {
            this.removeById(id);
            article.setCommentCount(Math.max(article.getCommentCount() - 1, 0));
        } else if (articleComments.getType() == 1) {
            List<ArticleComments> list = this.lambdaQuery().eq(ArticleComments::getReplyId, id)
                    .list();
            List<Long> commentsIds = list.stream().map(ArticleComments::getId)
                    .collect(Collectors.toList());
            commentsIds.add(articleComments.getId());
            this.removeByIds(commentsIds);
            article.setCommentCount(Math.max((article.getCommentCount() - list.size()), 0));
        }
        articleMapper.updateById(article);
    }
}