mitao
2024-09-13 b3cef9d27013afee054bdd467defd0b6be218526
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
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.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 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>
 * 资讯评论回复表 服务实现类
 * </p>
 *
 * @author mitao
 * @since 2024-05-16
 */
@Service
public class ArticleCommentsServiceImpl extends
        ServiceImpl<ArticleCommentsMapper, ArticleComments> implements IArticleCommentsService {
 
    @Resource
    private ArticleMapper articleMapper;
    @Resource
    private MemberClient memberClient;
    @Override
    public void saveMemberArticleComments(ArticleCommentsDTO articleCommentsDTO) {
        ArticleComments articleComments=new ArticleComments();
        if (articleCommentsDTO.getId()!=null){
            articleComments=  this.getById(articleCommentsDTO.getId());
        }
        if (articleCommentsDTO.getMemberId()==null){
            throw new ServiceException("用户id错误");
        }
        if (articleCommentsDTO.getType()==1){
            articleComments.setMemberId(articleCommentsDTO.getMemberId());
            articleComments.setArticleId(articleCommentsDTO.getArticleId());
            articleComments.setContent(articleCommentsDTO.getContent());
            articleComments.setState(articleCommentsDTO.getState());
            articleComments.setType(1);
        }else{
            articleComments.setMemberId(articleCommentsDTO.getMemberId());
            articleComments.setArticleId(articleCommentsDTO.getArticleId());
            articleComments.setReplyId(articleCommentsDTO.getReplyId());
            articleComments.setContent(articleCommentsDTO.getContent());
            articleComments.setBmemberId(articleCommentsDTO.getBmemberId());
            articleComments.setState(articleCommentsDTO.getState());
            articleComments.setType(2);
        }
        Article byId = articleMapper.selectById(articleCommentsDTO.getArticleId());
        byId.setCommentCount(byId.getCommentCount()+1);
        byId.setReadStatus(byId.getReadStatus() == 2 ? 1 : 2);
        articleMapper.updateById(byId);
        this.saveOrUpdate(articleComments);
    }
 
    @Override
    public void delMemberArticleComments(ArticleCommentsDTO articleCommentsDTO) {
        ArticleComments byId1 = this.getById(articleCommentsDTO.getId());
 
        Article byId = articleMapper.selectById(byId1.getArticleId());
        byId.setCommentCount(byId.getCommentCount()-1);
        articleMapper.updateById(byId);
 
        this.removeById(articleCommentsDTO.getId());
    }
 
    /**
     * 查看详情-评论详情
     *
     * @param query 资讯评论回复查询对象
     * @return PageDTO<MgtArticleCommentsVO>
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public PageDTO<MgtArticleCommentsVO> getArticleCommentsPage(MgtArticleCommentsQuery query) {
        PageDTO<MgtArticleCommentsVO> result;
        Set<Long> memberIdSet = null;
        Page<ArticleComments> page = new Page<>(query.getPageCurr(), query.getPageSize());
        if (StringUtils.isNotBlank(query.getNickname())) {
            MemberDTO memberDTO = new MemberDTO();
            memberDTO.setNickname(query.getNickname());
            List<Member> data = memberClient.getMemberListByCondition(memberDTO,
                    SecurityConstants.INNER).getData();
            if (StringUtils.isEmpty(data)) {
                // 搜索结果为空,直接返回空
                return PageDTO.empty(page);
            }
            memberIdSet = data.stream().map(Member::getId).collect(Collectors.toSet());
        }
        page = this.lambdaQuery()
                .eq(ArticleComments::getArticleId, query.getArticleId())
                .in(StringUtils.isNotEmpty(memberIdSet), ArticleComments::getMemberId, memberIdSet)
                .orderByDesc(ArticleComments::getCreateTime)
                .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;
        }
        // 修改查看评论状态
        Article article = articleMapper.selectById(query.getArticleId());
        article.setReadStatus(2);
        articleMapper.updateById(article);
        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);
    }
}