123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134 |
- package com.nosum.deliver.posts.service.impl;
- import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
- import com.nosum.deliver.auth.dao.AuthUserDao;
- import com.nosum.deliver.auth.domain.po.AuthUser;
- import com.nosum.common.base.domain.Result;
- import com.nosum.common.base.domain.vo.UserSessionVO;
- import com.nosum.common.base.service.impl.BaseServiceImpl;
- import com.nosum.common.constant.Constants;
- import com.nosum.common.enums.ErrorEnum;
- import com.nosum.common.util.ExceptionUtil;
- import com.nosum.common.util.PageUtil;
- import com.nosum.common.util.SessionUtil;
- import com.nosum.deliver.posts.dao.PostsCommentsDao;
- import com.nosum.deliver.posts.dao.PostsDao;
- import com.nosum.deliver.posts.domain.po.PostsComments;
- import com.nosum.deliver.posts.domain.vo.PostsCommentsVO;
- import com.nosum.deliver.posts.service.PostsCommentsService;
- import org.apache.commons.lang3.StringUtils;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.stereotype.Service;
- import java.time.LocalDateTime;
- import java.util.List;
- import java.util.Optional;
- /**
- * <p>
- * 评论表 服务实现类
- * </p>
- * @author sumbytes
- * @since 2019-09-03
- */
- @Service
- public class PostsCommentsServiceImpl extends BaseServiceImpl<PostsCommentsDao, PostsComments> implements PostsCommentsService {
- @Autowired
- private PostsCommentsDao postsCommentsDao;
- @Autowired
- private PostsDao postsDao;
- @Autowired
- private AuthUserDao authUserDao;
- @Override
- public Result savePostsComments(PostsCommentsVO postsCommentsVO) {
- UserSessionVO userSessionInfo = SessionUtil.getUserSessionInfo();
- PostsComments postsComments = new PostsComments();
- postsComments.setAuthorId(userSessionInfo.getId());
- postsComments.setContent(postsCommentsVO.getContent());
- postsComments.setPostsId(postsCommentsVO.getPostsId());
- postsComments.setCreateTime(LocalDateTime.now());
- String treePath;
- if (postsCommentsVO.getParentId() == null) {
- this.postsCommentsDao.insert(postsComments);
- treePath = postsComments.getId() + Constants.TREE_PATH;
- } else {
- PostsComments parentPostsComments = this.postsCommentsDao.selectById(postsCommentsVO.getParentId());
- if (parentPostsComments == null) {
- ExceptionUtil.rollback(ErrorEnum.DATA_NO_EXIST);
- }
- postsComments.setParentId(postsCommentsVO.getParentId());
- this.postsCommentsDao.insert(postsComments);
- treePath = parentPostsComments.getTreePath() + postsComments.getId() + Constants.TREE_PATH;
- }
- this.postsCommentsDao.updateById(postsComments.setTreePath(treePath));
- this.postsDao.incrementComments( postsCommentsVO.getPostsId());
- return Result.createWithSuccessMessage();
- }
- @Override
- public Result replyComments(PostsCommentsVO postsCommentsVO) {
- // 查询用户信息
- UserSessionVO userSessionInfo = SessionUtil.getUserSessionInfo();
- AuthUser authUser = authUserDao.selectById(userSessionInfo.getId());
- PostsComments postsComments=postsCommentsDao.selectById(postsCommentsVO.getParentId());
- // 准备更新前的参数内容
- postsComments.setParentId(postsCommentsVO.getParentId())
- .setContent(postsCommentsVO.getContent())
- .setAuthorId(authUser.getId())
- .setCreateTime(LocalDateTime.now())
- .setId(null);
- // 添加评论
- this.postsCommentsDao.insert(postsComments);
- // 增加完成后会默认获取id,此时需要将id,增加到层级结构当中
- this.postsCommentsDao.updateById(postsComments.setTreePath(postsComments.getTreePath() + postsComments.getId() + Constants.TREE_PATH));
- // 为文章添加评论数
- this.postsDao.incrementComments( postsCommentsVO.getPostsId());
- return Result.createWithSuccessMessage();
- }
- @Override
- public Result getPostsCommentsByPostsIdList(PostsCommentsVO postsCommentsVO) {
- Page page = Optional.ofNullable(PageUtil.checkAndInitPage(postsCommentsVO)).orElse(PageUtil.initPage());
- List<PostsCommentsVO> postsCommentsVOLis = this.postsCommentsDao.selectPostsCommentsByPostsIdList(page, postsCommentsVO.getPostsId());
- return Result.createWithPaging(postsCommentsVOLis, PageUtil.initPageInfo(page));
- }
- @Override
- public Result getPostsCommentsList(PostsCommentsVO postsCommentsVO) {
- Page page = Optional.ofNullable(PageUtil.checkAndInitPage(postsCommentsVO)).orElse(PageUtil.initPage());
- if (StringUtils.isNotBlank(postsCommentsVO.getKeywords())) {
- postsCommentsVO.setKeywords("%" + postsCommentsVO.getKeywords() + "%");
- }
- List<PostsCommentsVO> postsCommentsVOLis = this.postsCommentsDao.selectPostsCommentsList(page, postsCommentsVO);
- return Result.createWithPaging(postsCommentsVOLis, PageUtil.initPageInfo(page));
- }
- @Override
- public Result deletePostsComments(Long id) {
- this.postsCommentsDao.deleteById(id);
- return Result.createWithSuccessMessage();
- }
- @Override
- public Result getPostsComment(Long id) {
- ExceptionUtil.isRollback(id==null,ErrorEnum.PARAM_ERROR);
- List<PostsCommentsVO> postsCommentsVOLis = this.postsCommentsDao.selectPostsCommentsList(new PostsCommentsVO().setId(id));
- if (postsCommentsVOLis!=null && postsCommentsVOLis.size()>0){
- return Result.createWithModel(postsCommentsVOLis.get(0));
- }
- return Result.createWithError();
- }
- }
|