package cn.celess.blog.service.serviceimpl; import cn.celess.blog.enmu.ResponseEnum; import cn.celess.blog.entity.Comment; import cn.celess.blog.entity.model.CommentModel; import cn.celess.blog.entity.request.CommentReq; import cn.celess.blog.exception.MyException; import cn.celess.blog.mapper.ArticleMapper; import cn.celess.blog.mapper.CommentMapper; import cn.celess.blog.service.CommentService; import cn.celess.blog.service.UserService; import cn.celess.blog.util.DateFormatUtil; import cn.celess.blog.util.RedisUserUtil; import com.github.pagehelper.Page; import com.github.pagehelper.PageHelper; import com.github.pagehelper.PageInfo; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import javax.servlet.http.HttpServletRequest; import java.util.ArrayList; import java.util.Date; import java.util.List; /** * @author : xiaohai * @date : 2019/03/29 17:05 */ @Service public class CommentServiceImpl implements CommentService { @Autowired CommentMapper commentMapper; @Autowired UserService userService; @Autowired ArticleMapper articleMapper; @Autowired HttpServletRequest request; @Autowired RedisUserUtil redisUserUtil; @Override public CommentModel create(CommentReq reqBody) { if (reqBody == null) { throw new MyException(ResponseEnum.PARAMETERS_ERROR); } long authorID = redisUserUtil.get().getId(); Comment pComment = null; if (reqBody.getPid() != null && reqBody.getPid() != -1) { pComment = commentMapper.findCommentById(reqBody.getPid()); } if (reqBody.getPid() == null) { reqBody.setPid(-1L); } //不是一级评论 if (reqBody.getPid() != -1 && pComment == null) { //父评论不存在 throw new MyException(ResponseEnum.PARAMETERS_ERROR); } Comment comment = new Comment(); comment.setAuthorID(authorID); comment.setType(reqBody.getComment()); if (reqBody.getComment()) { //若为评论 if (reqBody.getArticleID() <= 0) { throw new MyException(ResponseEnum.PARAMETERS_ERROR); } comment.setArticleID(reqBody.getArticleID()); } else { comment.setArticleID(-1L); } comment.setContent(reqBody.getContent()); comment.setPid(reqBody.getPid()); comment.setDate(new Date()); comment.setResponseId(""); commentMapper.insert(comment); if (reqBody.getPid() != -1) { commentMapper.updateResponder(pComment.getResponseId() + comment.getId() + ",", reqBody.getPid()); } return trans(comment); } @Override public boolean delete(long id) { boolean b = commentMapper.existsById(id); if (!b) { throw new MyException(ResponseEnum.COMMENT_NOT_EXIST); } commentMapper.delete(id); return true; } @Override public CommentModel update(CommentReq reqBody) { if (reqBody.getId() == null) { throw new MyException(ResponseEnum.PARAMETERS_ERROR.getCode(), "id不可为空"); } Comment comment = commentMapper.findCommentById(reqBody.getId()); if (!comment.getContent().equals(reqBody.getContent())) { commentMapper.updateContent(reqBody.getContent(), reqBody.getId()); comment.setContent(reqBody.getContent()); } if (!comment.getResponseId().equals(reqBody.getResponseId())) { commentMapper.updateResponder(reqBody.getResponseId(), reqBody.getId()); comment.setResponseId(reqBody.getResponseId()); } return trans(comment); } @Override public PageInfo retrievePage(Boolean isComment, int page, int count) { PageHelper.startPage(page, count); List commentList = commentMapper.findAllByType(isComment); return pageTrans(commentList); } @Override public List retrievePageByPid(long pid) { List commentList = commentMapper.findAllByPId(pid); List modelList = new ArrayList<>(); commentList.forEach(m -> modelList.add(trans(m))); return modelList; } @Override public PageInfo retrievePageByArticle(long articleID, long pid, int page, int count) { PageHelper.startPage(page, count); List commentList = commentMapper.findAllByArticleIDAndPId(articleID, pid); return pageTrans(commentList); } @Override public PageInfo retrievePageByTypeAndPid(Boolean isComment, int pid, int page, int count) { PageHelper.startPage(page, count); List commentList = commentMapper.findCommentsByTypeAndPId(isComment, pid); return pageTrans(commentList); } @Override public PageInfo retrievePageByAuthor(Boolean isComment, int page, int count) { PageHelper.startPage(page, count); List commentList = commentMapper.findAllByAuthorIDAndType(redisUserUtil.get().getId(), isComment); return pageTrans(commentList); } @Override public PageInfo retrievePageByType(Boolean isComment, int page, int count) { PageHelper.startPage(page, count); List commentList = commentMapper.findAllByType(isComment); return pageTrans(commentList); } private CommentModel trans(Comment comment) { CommentModel commentModel = new CommentModel(); commentModel.setId(comment.getId()); commentModel.setComment(comment.getType()); commentModel.setContent(comment.getContent()); commentModel.setArticleID(comment.getArticleID()); commentModel.setDate(DateFormatUtil.get(comment.getDate())); commentModel.setResponseId(comment.getResponseId()); commentModel.setPid(comment.getPid()); commentModel.setAuthorName(userService.getNameById(comment.getAuthorID())); commentModel.setAuthorAvatarImgUrl("http://cdn.celess.cn/" + userService.getAvatarImg(comment.getAuthorID())); if (comment.getType() && commentModel.getArticleID() > 0) { commentModel.setArticleTitle(articleMapper.getTitleById(comment.getArticleID())); } return commentModel; } private PageInfo pageTrans(List commentList) { PageInfo p = new PageInfo(commentList); List modelList = new ArrayList<>(); commentList.forEach(l -> { CommentModel model = trans(l); model.setRespComment(this.retrievePageByPid(model.getId())); modelList.add(model); }); p.setList(modelList); return p; } }