Commit dde995b4 authored by 万成波's avatar 万成波

评论优化

parent 3499093c
...@@ -13,7 +13,7 @@ import lombok.Getter; ...@@ -13,7 +13,7 @@ import lombok.Getter;
@AllArgsConstructor @AllArgsConstructor
public enum CommentStatus { public enum CommentStatus {
SC(0, "删除"), ZC(1, "正常"), YC(2, "隐藏"); SC(0, "删除"), ZC(1, "正常");
private final int status; private final int status;
......
...@@ -171,6 +171,21 @@ public class MBbsMomentController { ...@@ -171,6 +171,21 @@ public class MBbsMomentController {
} }
/**
* 查询动态评论
*
* @return 动态
*/
@MobileAuth
@GetMapping("/comments")
public AjaxResult getMomentComments(@RequestParam Long momentId) {
String userName = MobileTokenHelper.getUserName();
PageUtils.clearPage();
List<BbsCommentDetailVO> dbComments = this.momentCommentService.getMomentComments(momentId, userName);
return AjaxResult.success(dbComments);
}
/** /**
* 评论动态 * 评论动态
* *
...@@ -189,17 +204,17 @@ public class MBbsMomentController { ...@@ -189,17 +204,17 @@ public class MBbsMomentController {
/** /**
* 查询动态评论 * 删除评论
* *
* @return 动态 * @param commentId 评论Id
* @return 删除结果
*/ */
@MobileAuth @MobileAuth
@GetMapping("/comments") @DeleteMapping("/comment/delete/{commentId}")
public AjaxResult getMomentComments(@RequestParam Long momentId) { public AjaxResult deleteComment(@PathVariable Long commentId) {
String userName = MobileTokenHelper.getUserName(); String userName = MobileTokenHelper.getUserName();
PageUtils.clearPage(); this.momentCommentService.deleteComment(commentId, userName);
List<BbsCommentDetailVO> dbComments = this.momentCommentService.getMomentComments(momentId, userName); return AjaxResult.success();
return AjaxResult.success(dbComments);
} }
} }
...@@ -60,8 +60,8 @@ public class BbsMomentComment extends BaseEntity { ...@@ -60,8 +60,8 @@ public class BbsMomentComment extends BaseEntity {
@ApiModelProperty("评论图片地址(1-3张)") @ApiModelProperty("评论图片地址(1-3张)")
private String imgsUrl; private String imgsUrl;
/** 评论状态:0 删除、1 正常、2 隐藏 */ /** 评论状态:0 删除、1 正常 */
@Excel(name = "评论状态:0 删除、1 正常、2 隐藏") @Excel(name = "评论状态:0 删除、1 正常")
@ApiModelProperty("评论状态:0 删除、1 正常、2 隐藏") @ApiModelProperty("评论状态:0 删除、1 正常、2 隐藏")
private Integer status; private Integer status;
......
...@@ -22,9 +22,12 @@ public interface BbsMomentCommentMapper extends BaseMapper<BbsMomentComment> { ...@@ -22,9 +22,12 @@ public interface BbsMomentCommentMapper extends BaseMapper<BbsMomentComment> {
List<BbsMomentComment> selectBbsMomentComments(@Param("commentId") Long commentId); List<BbsMomentComment> selectBbsMomentComments(@Param("commentId") Long commentId);
List<BbsMomentComment> selectComments(@Param("momentIds") List<Long> momentIds, @Param("rows") int rows); List<BbsMomentComment> selectMomentLimitComments(@Param("momentIds") List<Long> momentIds,
@Param("rows") int rows,
@Param("userName") String userName);
List<BbsCommentDetailVO> selectMomentComments(@Param("momentId") Long momentId, @Param("userName") String userName); List<BbsCommentDetailVO> selectMomentComments(@Param("momentId") Long momentId,
@Param("userName") String userName);
} }
...@@ -33,20 +33,21 @@ public interface IBbsMomentCommentService extends IService<BbsMomentComment> { ...@@ -33,20 +33,21 @@ public interface IBbsMomentCommentService extends IService<BbsMomentComment> {
/** /**
* 查询动态评论 * 删除评论
* *
* @param momentIds 动态Ids * @param commentId 评论Id
* @return 投票选项
*/ */
List<BbsMomentComment> getComments(List<Long> momentIds); void deleteComment(Long commentId);
/** /**
* 删除评论 * 查询动态评论
* *
* @param commentId 评论Id * @param momentIds 动态Ids
* @param userName 用户名
* @return 投票选项
*/ */
void deleteComment(Long commentId); List<BbsMomentComment> getMomentLimitComments(List<Long> momentIds, String userName);
/** /**
...@@ -58,4 +59,13 @@ public interface IBbsMomentCommentService extends IService<BbsMomentComment> { ...@@ -58,4 +59,13 @@ public interface IBbsMomentCommentService extends IService<BbsMomentComment> {
*/ */
List<BbsCommentDetailVO> getMomentComments(Long momentId, String userName); List<BbsCommentDetailVO> getMomentComments(Long momentId, String userName);
/**
* 删除评论
*
* @param commentId 评论Id
* @param userName 用户名
*/
void deleteComment(Long commentId, String userName);
} }
package com.tangguo.service.impl; package com.tangguo.service.impl;
import cn.hutool.core.collection.CollUtil; import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tangguo.common.enums.CommentStatus; import com.tangguo.common.enums.CommentStatus;
import com.tangguo.common.exception.ServiceException; import com.tangguo.common.exception.ServiceException;
...@@ -57,22 +58,6 @@ public class BbsMomentCommentServiceImpl extends ServiceImpl<BbsMomentCommentMap ...@@ -57,22 +58,6 @@ public class BbsMomentCommentServiceImpl extends ServiceImpl<BbsMomentCommentMap
} }
/**
* 查询动态评论
*
* @param momentIds 动态Ids
* @return 投票选项
*/
@Override
public List<BbsMomentComment> getComments(List<Long> momentIds) {
List<BbsMomentComment> dbComments = new ArrayList<>(30);
if (CollUtil.isNotEmpty(momentIds)) {
dbComments = this.baseMapper.selectComments(momentIds, 3);
}
return dbComments;
}
/** /**
* 删除评论 * 删除评论
* *
...@@ -93,6 +78,23 @@ public class BbsMomentCommentServiceImpl extends ServiceImpl<BbsMomentCommentMap ...@@ -93,6 +78,23 @@ public class BbsMomentCommentServiceImpl extends ServiceImpl<BbsMomentCommentMap
} }
/**
* 查询动态评论
*
* @param momentIds 动态Ids
* @param userName 用户名
* @return 投票选项
*/
@Override
public List<BbsMomentComment> getMomentLimitComments(List<Long> momentIds, String userName) {
List<BbsMomentComment> dbComments = new ArrayList<>(30);
if (CollUtil.isNotEmpty(momentIds)) {
dbComments = this.baseMapper.selectMomentLimitComments(momentIds, 3, userName);
}
return dbComments;
}
/** /**
* 查询动态评论 * 查询动态评论
* *
...@@ -105,4 +107,29 @@ public class BbsMomentCommentServiceImpl extends ServiceImpl<BbsMomentCommentMap ...@@ -105,4 +107,29 @@ public class BbsMomentCommentServiceImpl extends ServiceImpl<BbsMomentCommentMap
return this.baseMapper.selectMomentComments(momentId, userName); return this.baseMapper.selectMomentComments(momentId, userName);
} }
/**
* 删除评论
*
* @param commentId 评论Id
* @param userName 用户名
*/
@Override
public void deleteComment(Long commentId, String userName) {
BbsMomentComment dbComment = this.getOne(
Wrappers.lambdaQuery(BbsMomentComment.class).eq(BbsMomentComment::getId, commentId)
);
if (Objects.isNull(dbComment)) {
throw new ServiceException("删除失败,未查询到当前动态评论数据。");
}
if (!dbComment.getUserName().equals(userName)) {
throw new ServiceException("删除失败,没有对当前动态评论的操作权限。");
}
BbsMomentComment updComment = new BbsMomentComment();
updComment.setId(dbComment.getId());
updComment.setStatus(CommentStatus.SC.getStatus());
this.updateById(updComment);
}
} }
...@@ -21,7 +21,6 @@ import com.tangguo.domain.vo.*; ...@@ -21,7 +21,6 @@ import com.tangguo.domain.vo.*;
import com.tangguo.mapper.BbsMomentMapper; import com.tangguo.mapper.BbsMomentMapper;
import com.tangguo.service.*; import com.tangguo.service.*;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.checkerframework.checker.units.qual.C;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional; import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StopWatch; import org.springframework.util.StopWatch;
...@@ -105,7 +104,7 @@ public class BbsMomentServiceImpl extends ServiceImpl<BbsMomentMapper, BbsMoment ...@@ -105,7 +104,7 @@ public class BbsMomentServiceImpl extends ServiceImpl<BbsMomentMapper, BbsMoment
@Override @Override
public List<BbsMomentListVO> getUserMoments(String userName, String content) { public List<BbsMomentListVO> getUserMoments(String userName, String content) {
List<BbsMomentListVO> dbMoments = this.baseMapper.selectUserMoments(userName, content); List<BbsMomentListVO> dbMoments = this.baseMapper.selectUserMoments(userName, content);
this.fillMoment(dbMoments); this.fillMoment(dbMoments, userName);
return dbMoments; return dbMoments;
} }
...@@ -120,7 +119,7 @@ public class BbsMomentServiceImpl extends ServiceImpl<BbsMomentMapper, BbsMoment ...@@ -120,7 +119,7 @@ public class BbsMomentServiceImpl extends ServiceImpl<BbsMomentMapper, BbsMoment
@Override @Override
public List<BbsMomentListVO> getMoments(String userName, String content) { public List<BbsMomentListVO> getMoments(String userName, String content) {
List<BbsMomentListVO> dbMoments = this.baseMapper.selectMoments(userName, content); List<BbsMomentListVO> dbMoments = this.baseMapper.selectMoments(userName, content);
this.fillMoment(dbMoments); this.fillMoment(dbMoments, userName);
return dbMoments; return dbMoments;
} }
...@@ -135,7 +134,7 @@ public class BbsMomentServiceImpl extends ServiceImpl<BbsMomentMapper, BbsMoment ...@@ -135,7 +134,7 @@ public class BbsMomentServiceImpl extends ServiceImpl<BbsMomentMapper, BbsMoment
@Override @Override
public BbsMomentListVO getMoment(Long momentId, String userName) { public BbsMomentListVO getMoment(Long momentId, String userName) {
BbsMomentListVO dbMoment = this.baseMapper.selectMoment(momentId, userName); BbsMomentListVO dbMoment = this.baseMapper.selectMoment(momentId, userName);
this.fillMoment(Collections.singletonList(dbMoment)); this.fillMoment(Collections.singletonList(dbMoment), userName);
dbMoment.setComments(null); dbMoment.setComments(null);
return dbMoment; return dbMoment;
} }
...@@ -394,18 +393,18 @@ public class BbsMomentServiceImpl extends ServiceImpl<BbsMomentMapper, BbsMoment ...@@ -394,18 +393,18 @@ public class BbsMomentServiceImpl extends ServiceImpl<BbsMomentMapper, BbsMoment
Integer isEnableVote = bo.getIsEnableVote(); Integer isEnableVote = bo.getIsEnableVote();
newMoment.setIsEnableVote(isEnableVote); newMoment.setIsEnableVote(isEnableVote);
if (EnableStatus.QY.getStatus() == isEnableVote) { if (EnableStatus.QY.getStatus() == isEnableVote) {
newMoment.setVoteTitle(bo.getVoteTitle()); VoteOptionType voteType = VoteOptionType.getVoteOptionType(bo.getVoteOptionType());
VoteOptionType optionType = VoteOptionType.getVoteOptionType(bo.getVoteOptionType()); if (Objects.isNull(voteType)) {
if (Objects.isNull(optionType)) {
throw new ServiceException("发布失败,动态投票选项类型错误。"); throw new ServiceException("发布失败,动态投票选项类型错误。");
} else {
newMoment.setVoteOptionType(voteType.name());
newMoment.setVoteTitle(bo.getVoteTitle());
} }
newMoment.setVoteOptionType(optionType.name());
List<CreateMomentBO.VoteOption> options = bo.getVoteOptions(); List<CreateMomentBO.VoteOption> options = bo.getVoteOptions();
if (CollUtil.isEmpty(options)) { if (CollUtil.isEmpty(options)) {
throw new ServiceException("发布失败,动态投票选项不能为空。"); throw new ServiceException("发布失败,动态投票选项不能为空。");
} } else {
for (CreateMomentBO.VoteOption option : options) { for (CreateMomentBO.VoteOption option : options) {
long optionCount = options.stream() long optionCount = options.stream()
.filter(o -> o.getName().equals(option.getName())).count(); .filter(o -> o.getName().equals(option.getName())).count();
...@@ -414,6 +413,7 @@ public class BbsMomentServiceImpl extends ServiceImpl<BbsMomentMapper, BbsMoment ...@@ -414,6 +413,7 @@ public class BbsMomentServiceImpl extends ServiceImpl<BbsMomentMapper, BbsMoment
} }
} }
} }
}
return newMoment; return newMoment;
} }
...@@ -422,8 +422,9 @@ public class BbsMomentServiceImpl extends ServiceImpl<BbsMomentMapper, BbsMoment ...@@ -422,8 +422,9 @@ public class BbsMomentServiceImpl extends ServiceImpl<BbsMomentMapper, BbsMoment
* 填充动态附件、投票选项和评论 * 填充动态附件、投票选项和评论
* *
* @param moments 动态 * @param moments 动态
* @param userName 用户名
*/ */
private void fillMoment(List<BbsMomentListVO> moments) { private void fillMoment(List<BbsMomentListVO> moments, String userName) {
log.info("=> 填充动态附件、投票选项和评论"); log.info("=> 填充动态附件、投票选项和评论");
StopWatch stopWatch = new StopWatch(); StopWatch stopWatch = new StopWatch();
stopWatch.start(); stopWatch.start();
...@@ -448,7 +449,7 @@ public class BbsMomentServiceImpl extends ServiceImpl<BbsMomentMapper, BbsMoment ...@@ -448,7 +449,7 @@ public class BbsMomentServiceImpl extends ServiceImpl<BbsMomentMapper, BbsMoment
// 查询动态评论 // 查询动态评论
CompletableFuture<Map<Long, List<BbsMomentComment>>> cf = CompletableFuture.supplyAsync(() -> { CompletableFuture<Map<Long, List<BbsMomentComment>>> cf = CompletableFuture.supplyAsync(() -> {
List<BbsMomentComment> opts = this.commentService.getComments(momentIds); List<BbsMomentComment> opts = this.commentService.getMomentLimitComments(momentIds, userName);
return opts.stream().collect(Collectors.groupingBy(BbsMomentComment::getMomentId)); return opts.stream().collect(Collectors.groupingBy(BbsMomentComment::getMomentId));
}); });
......
...@@ -39,20 +39,41 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" ...@@ -39,20 +39,41 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
</select> </select>
<select id="selectComments" resultType="com.tangguo.domain.BbsMomentComment"> <select id="selectMomentLimitComments" resultType="com.tangguo.domain.BbsMomentComment">
SELECT c.* FROM ( SELECT c.* FROM (
SELECT SELECT
*, c.id,
c.create_time,
c.moment_id,
c.nike_name,
c.user_name,
c.content,
c.parent_id,
c.reply_nike_name,
ROW_NUMBER() OVER (PARTITION BY moment_id ORDER BY create_time) AS rn ROW_NUMBER() OVER (PARTITION BY moment_id ORDER BY create_time) AS rn
FROM FROM
bbs_moment_comment bbs_moment_comment c
INNER JOIN
(
SELECT
id, is_enable_featured_comment
FROM
bbs_moment
WHERE WHERE
parent_id IS NULL id IN
AND
moment_id IN
<foreach collection="momentIds" item="id" open="(" separator="," close=")"> <foreach collection="momentIds" item="id" open="(" separator="," close=")">
#{id} #{id}
</foreach> </foreach>
) m ON m.id = c.moment_id
WHERE
c.parent_id IS NULL
AND
c.status = 1
AND
(
m.is_enable_featured_comment = 1
OR (m.is_enable_featured_comment = 0 AND (c.user_name = #{userName} OR c.is_featured = 1))
)
) AS c ) AS c
WHERE rn &lt;= #{rows}; WHERE rn &lt;= #{rows};
</select> </select>
...@@ -71,8 +92,18 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" ...@@ -71,8 +92,18 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
IF(c.user_name = #{userName}, 1, 0) AS is_self IF(c.user_name = #{userName}, 1, 0) AS is_self
FROM FROM
bbs_moment_comment c bbs_moment_comment c
INNER JOIN
bbs_moment m ON m.id = c.moment_id
WHERE WHERE
c.moment_id = #{momentId} m.id = #{momentId}
AND
c.status = 1
AND
(
m.user_name = #{userName}
OR m.is_enable_featured_comment = 0
OR (m.is_enable_featured_comment = 1 AND (c.user_name = #{userName} OR c.is_featured = 1))
)
ORDER BY ORDER BY
c.create_time c.create_time
</select> </select>
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment