cloudroam
2025-06-06 70f642c9a5090f051814d46c8f5bbce5e366ba89
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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
package com.mzl.flower.service.film.impl;
import com.github.pagehelper.PageInfo;
import com.github.pagehelper.PageHelper;
import com.mzl.flower.config.exception.ValidationException;
import com.mzl.flower.config.security.SecurityUtils;
import com.mzl.flower.constant.Constants;
import com.mzl.flower.dto.request.film.CommentSearchDTO;
import com.mzl.flower.dto.response.current.CurrentUserDTO;
import com.mzl.flower.dto.response.customer.CustomerDTO;
import com.mzl.flower.dto.response.film.CommentDTO;
import com.mzl.flower.dto.security.UserDTO;
import com.mzl.flower.entity.film.CommentPo;
import com.mzl.flower.entity.film.CommentPoExample;
import com.mzl.flower.entity.system.User;
import com.mzl.flower.enums.SortRuleEnum;
import com.mzl.flower.mapper.customer.CustomerMapper;
import com.mzl.flower.mapper.film.CommentMS;
import com.mzl.flower.mapper.film.CommentPoExMapper;
import com.mzl.flower.mapper.film.CommentPoMapper;
import com.mzl.flower.service.film.CommentLikesService;
import com.mzl.flower.service.film.CommentService;
import com.mzl.flower.service.system.UserService;
import com.mzl.flower.utils.CommentTreeUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;
 
import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.*;
import java.util.stream.Collectors;
 
@Slf4j
@Component
@Service
public class CommentServiceImpl implements CommentService {
    @Resource
    private CommentPoMapper commentPoMapper;
 
    @Resource
    private UserService userService;
 
    @Resource
    private CommentPoExMapper commentPoExMapper;
 
    @Resource
    private CustomerMapper customerMapper;
 
    @Resource
    private CommentService commentService;
    @Resource
    private CommentLikesService commentLikesService;
 
    /**
     * 获取文章的评论信息
     *
     * @param commentSearchDTO
     * @return
     */
 
    @Override
    public List<CommentDTO> getCommentByFilmId(CommentSearchDTO commentSearchDTO) {
 
        CurrentUserDTO currentUser = userService.getCurrentUser();
        // 直接调用 mapper 方法获取评论列表
        List<CommentPo> commentPos = commentPoMapper.selectByArticleId(commentSearchDTO.getFilmId());
 
        List<CommentDTO> commentDTOS = CommentMS.INSTANCE.toDTO(commentPos);
 
        if (CollectionUtils.isNotEmpty(commentDTOS)) {
            //构建评论信息
            buildCommentInfo(commentDTOS, currentUser);
        }
 
        commentDTOS = CommentTreeUtils.toTree(commentDTOS);
 
        // 排序逻辑保持不变
        if (SortRuleEnum.hottest.equals(commentSearchDTO.getSortRule())) {
            commentDTOS = commentDTOS.stream()
                    .sorted(Comparator.comparing(CommentDTO::getLikeCount, Comparator.nullsLast(Comparator.reverseOrder()))
                            .thenComparing(CommentDTO::getRepliesCount, Comparator.nullsLast(Comparator.reverseOrder())))
                    .collect(Collectors.toList());
        } else if (SortRuleEnum.newest.equals(commentSearchDTO.getSortRule())) {
            commentDTOS = commentDTOS.stream()
                    .sorted(Comparator.comparing(CommentDTO::getCreateTime, Comparator.nullsLast(Comparator.reverseOrder())))
                    .collect(Collectors.toList());
        }
 
        return commentDTOS;
    }
 
    /**
     * 获取所有通过审核文章的评论信息
     *
     * @param startTime
     * @param endTime
     * @return
     */
    @Override
    public List<CommentDTO> getAllArticleComment(LocalDateTime startTime, LocalDateTime endTime) {
        return CommentMS.INSTANCE.toDTO(commentPoExMapper.getAllArticleComment(startTime, endTime));
    }
 
    @Override
    public List<CommentDTO> getAllCommentReply(LocalDateTime startTime, LocalDateTime endTime) {
        return CommentMS.INSTANCE.toDTO(commentPoExMapper.getAllCommentReply(startTime, endTime));
    }
 
    /**
     * 获取最新评论信息
     *
     * @param commentSearchDTO
     * @return
     */
    @Override
    public PageInfo<CommentDTO> getLatestComment(CommentSearchDTO commentSearchDTO) {
        PageHelper.startPage(commentSearchDTO.getCurrentPage(), commentSearchDTO.getPageSize());
        List<CommentPo> commentPos = commentPoExMapper.selectLatestComments(commentSearchDTO.getContent(), commentSearchDTO.getCreateBy());
        PageInfo<CommentDTO> pageInfo = CommentMS.INSTANCE.toPage(new PageInfo<>(commentPos));
        if (CollectionUtils.isNotEmpty(pageInfo.getList())) {
            // 构建评论信息
            buildCommentInfo(pageInfo.getList(), null);
        }
        return pageInfo;
    }
 
    /**
     * 获取文章的评论数量
     *
     * @param articleId
     * @return
     */
    @Override
    public Long getCommentCountByArticle(Integer articleId) {
        CommentPoExample example = new CommentPoExample();
        example.createCriteria().andIsDeletedEqualTo(false)
                .andStateEqualTo(true)
                .andArticleIdEqualTo(articleId);
 
        return commentPoMapper.countByExample(example);
    }
 
    /**
     * 获取评论数量
     *
     * @return
     */
    @Override
    public Long getTotal() {
        CommentPoExample example = new CommentPoExample();
        example.createCriteria().andIsDeletedEqualTo(false)
                .andStateEqualTo(true);
        return commentPoMapper.countByExample(example);
    }
 
    /**
     * 创建评论
     *
     * @param commentDTO
     * @return
     */
    @Override
    public Boolean create(CommentDTO commentDTO) {
        if (StringUtils.isEmpty(commentDTO.getContent())) {
            throw new ValidationException("评论内容不能为空");
        }
        //todo 评论内风险校验,评论内容可以是图片资源,考虑增加一个contextType字段用来区分资源类型
        CommentPo commentPo = new CommentPo();
        BeanUtils.copyProperties(commentDTO, commentPo);
        commentPo.setState(true);
        commentPo.create(SecurityUtils.getUserId());
        if (commentPoMapper.insert(commentPo) <= 0) {
            throw new ValidationException("添加评论失败");
        }
        return true;
    }
 
    /**
     * 删除评论
     *
     * @param commentId
     * @return
     */
    @Override
    public Boolean delete(Integer commentId) {
        List<Integer> commentIds = new ArrayList<>();
        List<CommentDTO> children = new ArrayList<>();
        // 通过父级ID获取子级评论信息
        this.getAllChildrenByPreId(children, commentId);
        if (CollectionUtils.isNotEmpty(children)) {
            commentIds.addAll(children.stream().map(CommentDTO::getId).collect(Collectors.toList()));
        }
        commentIds.add(commentId);
        CommentPoExample example = new CommentPoExample();
        example.createCriteria().andIdIn(commentIds);
 
        CommentPo commentPo = new CommentPo();
        commentPo.setDeleted(true);
        commentPo.setUpdateTime(LocalDateTime.now());
        if (commentPoMapper.updateByExampleSelective(commentPo, example) <= 0) {
            throw new ValidationException("删除评论失败");
        }
        return true;
    }
 
    /**
     * 通过父级ID获取子级评论信息
     *
     * @param result 存放结果
     * @param preId
     * @return
     */
    @Override
    public void getAllChildrenByPreId(List<CommentDTO> result, Integer preId) {
        CommentPoExample example = new CommentPoExample();
        example.createCriteria().andIsDeletedEqualTo(false)
                .andStateEqualTo(true)
                .andPreIdEqualTo(preId);
        List<CommentDTO> commentDTOS = CommentMS.INSTANCE.toDTO(commentPoMapper.selectByExample(example));
        if (CollectionUtils.isNotEmpty(commentDTOS)) {
            result.addAll(commentDTOS);
            commentDTOS.forEach(commentDTO -> {
                this.getAllChildrenByPreId(result, commentDTO.getId());
            });
        }
    }
 
    @Override
    public String getArticleIdByCommentId(Integer commentId) {
        CommentPo commentPo = commentPoMapper.selectByPrimaryKey(commentId);
        return commentPo == null ? null : commentPo.getCreateBy();
    }
 
    @Override
    public CommentDTO getById(Integer commentId) {
        return CommentMS.INSTANCE.toDTO(commentPoMapper.selectByPrimaryKey(commentId));
    }
 
    /**
     * 构建评论信息
     *
     * @param commentDTOS
     * @param currentUser
     */
 
    private void buildCommentInfo(List<CommentDTO> commentDTOS, CurrentUserDTO currentUser) {
        if (CollectionUtils.isEmpty(commentDTOS)) {
            return;
        }
 
        List<String> userIds = commentDTOS.stream()
                .map(CommentDTO::getCreateBy)
                .distinct()
                .collect(Collectors.toList());
 
        Map<String, List<User>> idUsers = userService.getByIds(userIds).stream()
                .collect(Collectors.groupingBy(User::getId));
 
        Map<Integer, List<CommentDTO>> preIdMap = commentDTOS.stream()
                .collect(Collectors.groupingBy(CommentDTO::getParentId));
 
        commentDTOS.forEach(commentDTO -> {
            // 设置用户名和头像
            User userInfo = idUsers.getOrDefault(commentDTO.getCreateBy(), Collections.emptyList()).stream()
                    .findFirst()
                    .orElse(null);
            if (userInfo != null) {
                //获取用户信息
                CustomerDTO currentCustomer = customerMapper.getCurrentCustomer(commentDTO.getCreateBy());
                if(!ObjectUtils.isEmpty(currentCustomer)){
                    commentDTO.setCommentUserName(currentCustomer.getNickName());
                    commentDTO.setPicture(currentCustomer.getCover());
                    User user = userService.findByTel(currentCustomer.getTel(), Constants.USER_TYPE.customer.name());
                    if(!ObjectUtils.isEmpty(user)){
                        commentDTO.setLevel(user.getVipGrade());
                    }
                }
            }
 
            // 获取点赞数
            commentDTO.setLikeCount(commentLikesService.getLikeCountCommentId(commentDTO.getId()));
 
            // 是否已点赞,这里面传入的是用户ID
            if (currentUser != null) {
                commentDTO.setIsLike(commentLikesService.isLike(commentDTO.getId(), currentUser.getId()));
            }
 
            // 回复数量
            commentDTO.setRepliesCount(preIdMap.getOrDefault(commentDTO.getId(), Collections.emptyList()).size());
        });
    }
 
}