parent
d195b78053
commit
b71a1438d8
@ -0,0 +1,29 @@
|
||||
package com.luoo.comment.enums;
|
||||
|
||||
public enum CommentStatusEnum {
|
||||
|
||||
/**
|
||||
* 正常
|
||||
*/
|
||||
NORMAL(1),
|
||||
|
||||
/**
|
||||
* 删除
|
||||
*/
|
||||
DELETE(2),
|
||||
|
||||
/**
|
||||
* 审核中
|
||||
*/
|
||||
AUDITING(3);
|
||||
|
||||
private final int value;
|
||||
|
||||
CommentStatusEnum(int value) {
|
||||
this.value = value;
|
||||
}
|
||||
|
||||
public int getValue() {
|
||||
return value;
|
||||
}
|
||||
}
|
@ -0,0 +1,37 @@
|
||||
package com.luoo.comment.pojo;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
import java.io.Serializable;
|
||||
|
||||
@Data
|
||||
public class CommentDto implements Serializable {
|
||||
|
||||
/**
|
||||
* 文章 ID
|
||||
*/
|
||||
private String articleId;
|
||||
|
||||
/**
|
||||
* 评论内容
|
||||
*/
|
||||
private String content;
|
||||
|
||||
/**
|
||||
* 父节点 ID
|
||||
*/
|
||||
private String parentId;
|
||||
|
||||
/**
|
||||
* 目标 ID
|
||||
*/
|
||||
private String targetId;
|
||||
|
||||
/**
|
||||
* IP 地址
|
||||
*/
|
||||
private String ipAddress;
|
||||
|
||||
private String userId;
|
||||
|
||||
}
|
@ -0,0 +1,78 @@
|
||||
package com.luoo.comment.pojo;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
import java.util.Date;
|
||||
|
||||
@Data
|
||||
public class CommentVo {
|
||||
|
||||
private String id;
|
||||
/**
|
||||
* 评论内容
|
||||
*/
|
||||
private String content;
|
||||
/**
|
||||
* 发布时间
|
||||
*/
|
||||
private Date publishTime;
|
||||
/**
|
||||
* 用户 ID
|
||||
*/
|
||||
private String userId;
|
||||
/**
|
||||
* 昵称
|
||||
*/
|
||||
private String nickName;
|
||||
/**
|
||||
* 点赞数
|
||||
*/
|
||||
private Integer thumbup;
|
||||
/**
|
||||
* 转发数
|
||||
*/
|
||||
private Integer share;
|
||||
/**
|
||||
* 评论数量
|
||||
*/
|
||||
private Integer comment;
|
||||
/**
|
||||
* 状态
|
||||
*/
|
||||
private String state;
|
||||
|
||||
/**
|
||||
* 父节点 ID
|
||||
*/
|
||||
private String parentId;
|
||||
|
||||
/**
|
||||
* 文章
|
||||
*/
|
||||
private String articleId;
|
||||
|
||||
/**
|
||||
* 层级
|
||||
*/
|
||||
private Integer level;
|
||||
|
||||
/**
|
||||
* 目标ID
|
||||
*/
|
||||
private String targetId;
|
||||
|
||||
/**
|
||||
* 目标对象
|
||||
*/
|
||||
private CommentVo targetComment;
|
||||
|
||||
/**
|
||||
* 评论类型
|
||||
*/
|
||||
private String type;
|
||||
|
||||
/**
|
||||
* 城市
|
||||
*/
|
||||
private String city;
|
||||
}
|
@ -1,87 +1,26 @@
|
||||
package com.luoo.comment.service;
|
||||
|
||||
|
||||
import com.luoo.comment.dao.CommentDao;
|
||||
import com.luoo.comment.pojo.Comment;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import com.luoo.comment.pojo.CommentDto;
|
||||
import com.luoo.comment.pojo.CommentVo;
|
||||
import org.springframework.data.domain.Page;
|
||||
import org.springframework.data.domain.PageRequest;
|
||||
import org.springframework.data.domain.Pageable;
|
||||
import org.springframework.data.mongodb.core.MongoTemplate;
|
||||
import org.springframework.data.mongodb.core.query.Criteria;
|
||||
import org.springframework.data.mongodb.core.query.Query;
|
||||
import org.springframework.data.mongodb.core.query.Update;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
import util.IdWorker;
|
||||
|
||||
import java.util.Date;
|
||||
import java.util.List;
|
||||
|
||||
@Service
|
||||
@Transactional
|
||||
public class CommentService {
|
||||
public interface CommentService {
|
||||
|
||||
@Autowired
|
||||
private CommentDao commentDao;
|
||||
Comment findById(String id);
|
||||
|
||||
@Autowired
|
||||
private IdWorker idWorker;
|
||||
void save(CommentDto commentDto);
|
||||
|
||||
@Autowired
|
||||
private MongoTemplate mongoTemplate;
|
||||
public List<Comment> findAll() {
|
||||
return commentDao.findAll();
|
||||
}
|
||||
void update(Comment comment);
|
||||
|
||||
public Comment findById(String id) {
|
||||
return commentDao.findById(id).get();
|
||||
}
|
||||
void deleteById(String id);
|
||||
|
||||
public void save(Comment comment){
|
||||
comment.set_id(idWorker.nextId()+"");
|
||||
comment.setPublishtime(new Date());
|
||||
comment.setVisits(0);
|
||||
comment.setShare(0);
|
||||
comment.setThumbup(0);
|
||||
comment.setComment(0);
|
||||
comment.setState("1");
|
||||
// 如果当前添加的吐槽,有父节点,那么父节点的吐槽回复数要加一
|
||||
if (comment.getParentid()!=null && !"".equals(comment.getParentid())) {
|
||||
Query query = new Query();
|
||||
query.addCriteria(Criteria.where("_id").is(comment.getParentid()));
|
||||
Update update = new Update();
|
||||
update.inc("comment",1);
|
||||
mongoTemplate.updateFirst(query,update,"spit");
|
||||
}
|
||||
commentDao.save(comment);
|
||||
}
|
||||
Page<CommentVo> findByParentId(String parentId, int page, int size);
|
||||
|
||||
public void update(Comment comment) {
|
||||
commentDao.save(comment);
|
||||
}
|
||||
void thumbup(String commentId);
|
||||
|
||||
public void deleteById(String id) {
|
||||
commentDao.deleteById(id);
|
||||
}
|
||||
|
||||
|
||||
public Page<Comment> findByParentId(String parentid,int page,int size){
|
||||
|
||||
Pageable pageable = PageRequest.of(page-1,size);
|
||||
return commentDao.findByParentid(parentid,pageable);
|
||||
}
|
||||
|
||||
public void thumbup(String commentId) {
|
||||
|
||||
Query query = new Query();
|
||||
query.addCriteria(Criteria.where("_id").is("1"));
|
||||
Update update = new Update();
|
||||
update.inc("thumbup",1);
|
||||
mongoTemplate.updateFirst(query,update,"spit");
|
||||
}
|
||||
|
||||
public List<Comment> findByVolid(String volid) {
|
||||
return commentDao.findByArticleid(volid);
|
||||
}
|
||||
List<Comment> findByVolid(String volid);
|
||||
}
|
||||
|
@ -0,0 +1,136 @@
|
||||
package com.luoo.comment.service.impl;
|
||||
|
||||
|
||||
import cn.hutool.core.util.ObjectUtil;
|
||||
import com.luoo.comment.dao.CommentDao;
|
||||
import com.luoo.comment.enums.CommentStatusEnum;
|
||||
import com.luoo.comment.pojo.Comment;
|
||||
import com.luoo.comment.pojo.CommentDto;
|
||||
import com.luoo.comment.pojo.CommentVo;
|
||||
import com.luoo.comment.service.CommentService;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.data.domain.*;
|
||||
import org.springframework.data.mongodb.core.MongoTemplate;
|
||||
import org.springframework.data.mongodb.core.query.Criteria;
|
||||
import org.springframework.data.mongodb.core.query.Query;
|
||||
import org.springframework.data.mongodb.core.query.Update;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
import util.IdWorker;
|
||||
|
||||
import java.util.Date;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.function.Function;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
@Service
|
||||
@Transactional
|
||||
public class CommentServiceImpl implements CommentService {
|
||||
|
||||
@Autowired
|
||||
private CommentDao commentDao;
|
||||
|
||||
@Autowired
|
||||
private IdWorker idWorker;
|
||||
|
||||
@Autowired
|
||||
private MongoTemplate mongoTemplate;
|
||||
|
||||
|
||||
public Comment findById(String id) {
|
||||
return commentDao.findById(id).get();
|
||||
}
|
||||
|
||||
public void save(CommentDto commentDto){
|
||||
Comment comment = new Comment();
|
||||
comment.setContent(commentDto.getContent());
|
||||
comment.setPublishTime(new Date());
|
||||
comment.setUserId(commentDto.getUserId());
|
||||
comment.setComment(0);
|
||||
comment.setParentId(commentDto.getParentId());
|
||||
comment.setArticleId(commentDto.getArticleId());
|
||||
comment.setLevel(1);
|
||||
comment.setTargetId(commentDto.getTargetId());
|
||||
comment.setType(0);
|
||||
comment.setCity("上海");
|
||||
comment.setId(String.valueOf(idWorker.nextId()));
|
||||
comment.setShare(0);
|
||||
comment.setThumbup(0);
|
||||
comment.setComment(0);
|
||||
comment.setState(CommentStatusEnum.NORMAL.getValue());
|
||||
// 如果当前添加的吐槽,有父节点,那么父节点的吐槽回复数要加一
|
||||
if (ObjectUtil.isNotEmpty(comment.getParentId())) {
|
||||
// 更新父节点的评论回复数
|
||||
Query query = new Query();
|
||||
query.addCriteria(Criteria.where("_id").is(comment.getParentId()));
|
||||
Update update = new Update();
|
||||
update.inc("comment",1);
|
||||
mongoTemplate.updateFirst(query,update,Comment.class);
|
||||
}
|
||||
|
||||
if (ObjectUtil.isNotEmpty(comment.getTargetId())) {
|
||||
Comment parentIdComment = mongoTemplate.findById(comment.getTargetId(), Comment.class);
|
||||
comment.setLevel(parentIdComment.getLevel()+1);
|
||||
Query query = new Query();
|
||||
query.addCriteria(Criteria.where("_id").is(comment.getTargetId()));
|
||||
Update update = new Update();
|
||||
update.inc("comment",1);
|
||||
mongoTemplate.updateFirst(query,update,Comment.class);
|
||||
}
|
||||
commentDao.save(comment);
|
||||
}
|
||||
|
||||
public void update(Comment comment) {
|
||||
commentDao.save(comment);
|
||||
}
|
||||
|
||||
public void deleteById(String id) {
|
||||
commentDao.deleteById(id);
|
||||
}
|
||||
|
||||
|
||||
public Page<CommentVo> findByParentId(String parentId,int page,int size){
|
||||
Pageable pageable = PageRequest.of(page, size);
|
||||
Query query = new Query();
|
||||
Criteria criteria = Criteria.where("parentId").is(parentId);
|
||||
query.addCriteria(criteria);
|
||||
Sort sort = Sort.by(Sort.Direction.ASC, "age");
|
||||
query.with(sort);
|
||||
query.with(pageable);
|
||||
List<Comment> comments = mongoTemplate.find(query, Comment.class);
|
||||
|
||||
List<CommentVo> commentVos = comments.stream().map(Comment::convertVo).collect(Collectors.toList());
|
||||
|
||||
List<String> targetComments = comments.stream().map(Comment::getTargetId).collect(Collectors.toList());
|
||||
|
||||
Map<String, CommentVo> idCommentMap = commentVos.stream().collect(Collectors.toMap(CommentVo::getId, Function.identity()));
|
||||
|
||||
long total = mongoTemplate.count(query, Comment.class);
|
||||
|
||||
// boolean isChange = targetComments.removeAll(idCommentMap.keySet());
|
||||
|
||||
for (CommentVo commentVo : commentVos) {
|
||||
if (commentVo.getLevel() > 2 && ObjectUtil.isNotEmpty(commentVo.getTargetId())) {
|
||||
CommentVo targetCommentVo = idCommentMap.get(commentVo.getTargetId());
|
||||
if (targetCommentVo != null) {
|
||||
commentVo.setTargetComment(targetCommentVo);
|
||||
}
|
||||
}
|
||||
}
|
||||
return new PageImpl<>(commentVos, pageable, total);
|
||||
}
|
||||
|
||||
public void thumbup(String commentId) {
|
||||
|
||||
Query query = new Query();
|
||||
query.addCriteria(Criteria.where("_id").is("1"));
|
||||
Update update = new Update();
|
||||
update.inc("thumbup",1);
|
||||
mongoTemplate.updateFirst(query,update,"spit");
|
||||
}
|
||||
|
||||
public List<Comment> findByVolid(String volid) {
|
||||
return commentDao.findByArticleId(volid);
|
||||
}
|
||||
}
|
Loading…
Reference in new issue