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

评论优化

parent 3499093c
......@@ -13,7 +13,7 @@ import lombok.Getter;
@AllArgsConstructor
public enum CommentStatus {
SC(0, "删除"), ZC(1, "正常"), YC(2, "隐藏");
SC(0, "删除"), ZC(1, "正常");
private final int status;
......
......@@ -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 {
/**
* 查询动态评论
* 删除评论
*
* @return 动态
* @param commentId 评论Id
* @return 删除结果
*/
@MobileAuth
@GetMapping("/comments")
public AjaxResult getMomentComments(@RequestParam Long momentId) {
@DeleteMapping("/comment/delete/{commentId}")
public AjaxResult deleteComment(@PathVariable Long commentId) {
String userName = MobileTokenHelper.getUserName();
PageUtils.clearPage();
List<BbsCommentDetailVO> dbComments = this.momentCommentService.getMomentComments(momentId, userName);
return AjaxResult.success(dbComments);
this.momentCommentService.deleteComment(commentId, userName);
return AjaxResult.success();
}
}
......@@ -60,8 +60,8 @@ public class BbsMomentComment extends BaseEntity {
@ApiModelProperty("评论图片地址(1-3张)")
private String imgsUrl;
/** 评论状态:0 删除、1 正常、2 隐藏 */
@Excel(name = "评论状态:0 删除、1 正常、2 隐藏")
/** 评论状态:0 删除、1 正常 */
@Excel(name = "评论状态:0 删除、1 正常")
@ApiModelProperty("评论状态:0 删除、1 正常、2 隐藏")
private Integer status;
......
......@@ -22,9 +22,12 @@ public interface BbsMomentCommentMapper extends BaseMapper<BbsMomentComment> {
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> {
/**
* 查询动态评论
* 删除评论
*
* @param momentIds 动态Ids
* @return 投票选项
* @param commentId 评论Id
*/
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> {
*/
List<BbsCommentDetailVO> getMomentComments(Long momentId, String userName);
/**
* 删除评论
*
* @param commentId 评论Id
* @param userName 用户名
*/
void deleteComment(Long commentId, String userName);
}
package com.tangguo.service.impl;
import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tangguo.common.enums.CommentStatus;
import com.tangguo.common.exception.ServiceException;
......@@ -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
}
/**
* 查询动态评论
*
* @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
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.*;
import com.tangguo.mapper.BbsMomentMapper;
import com.tangguo.service.*;
import lombok.extern.slf4j.Slf4j;
import org.checkerframework.checker.units.qual.C;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StopWatch;
......@@ -105,7 +104,7 @@ public class BbsMomentServiceImpl extends ServiceImpl<BbsMomentMapper, BbsMoment
@Override
public List<BbsMomentListVO> getUserMoments(String userName, String content) {
List<BbsMomentListVO> dbMoments = this.baseMapper.selectUserMoments(userName, content);
this.fillMoment(dbMoments);
this.fillMoment(dbMoments, userName);
return dbMoments;
}
......@@ -120,7 +119,7 @@ public class BbsMomentServiceImpl extends ServiceImpl<BbsMomentMapper, BbsMoment
@Override
public List<BbsMomentListVO> getMoments(String userName, String content) {
List<BbsMomentListVO> dbMoments = this.baseMapper.selectMoments(userName, content);
this.fillMoment(dbMoments);
this.fillMoment(dbMoments, userName);
return dbMoments;
}
......@@ -135,7 +134,7 @@ public class BbsMomentServiceImpl extends ServiceImpl<BbsMomentMapper, BbsMoment
@Override
public BbsMomentListVO getMoment(Long momentId, String userName) {
BbsMomentListVO dbMoment = this.baseMapper.selectMoment(momentId, userName);
this.fillMoment(Collections.singletonList(dbMoment));
this.fillMoment(Collections.singletonList(dbMoment), userName);
dbMoment.setComments(null);
return dbMoment;
}
......@@ -394,23 +393,24 @@ public class BbsMomentServiceImpl extends ServiceImpl<BbsMomentMapper, BbsMoment
Integer isEnableVote = bo.getIsEnableVote();
newMoment.setIsEnableVote(isEnableVote);
if (EnableStatus.QY.getStatus() == isEnableVote) {
newMoment.setVoteTitle(bo.getVoteTitle());
VoteOptionType optionType = VoteOptionType.getVoteOptionType(bo.getVoteOptionType());
if (Objects.isNull(optionType)) {
VoteOptionType voteType = VoteOptionType.getVoteOptionType(bo.getVoteOptionType());
if (Objects.isNull(voteType)) {
throw new ServiceException("发布失败,动态投票选项类型错误。");
} else {
newMoment.setVoteOptionType(voteType.name());
newMoment.setVoteTitle(bo.getVoteTitle());
}
newMoment.setVoteOptionType(optionType.name());
List<CreateMomentBO.VoteOption> options = bo.getVoteOptions();
if (CollUtil.isEmpty(options)) {
throw new ServiceException("发布失败,动态投票选项不能为空。");
}
for (CreateMomentBO.VoteOption option : options) {
long optionCount = options.stream()
.filter(o -> o.getName().equals(option.getName())).count();
if (optionCount > 1) {
throw new ServiceException("发布失败,动态投票选项重复。");
} else {
for (CreateMomentBO.VoteOption option : options) {
long optionCount = options.stream()
.filter(o -> o.getName().equals(option.getName())).count();
if (optionCount > 1) {
throw new ServiceException("发布失败,动态投票选项重复。");
}
}
}
}
......@@ -422,8 +422,9 @@ public class BbsMomentServiceImpl extends ServiceImpl<BbsMomentMapper, BbsMoment
* 填充动态附件、投票选项和评论
*
* @param moments 动态
* @param userName 用户名
*/
private void fillMoment(List<BbsMomentListVO> moments) {
private void fillMoment(List<BbsMomentListVO> moments, String userName) {
log.info("=> 填充动态附件、投票选项和评论");
StopWatch stopWatch = new StopWatch();
stopWatch.start();
......@@ -448,7 +449,7 @@ public class BbsMomentServiceImpl extends ServiceImpl<BbsMomentMapper, BbsMoment
// 查询动态评论
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));
});
......
......@@ -39,20 +39,41 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
</select>
<select id="selectComments" resultType="com.tangguo.domain.BbsMomentComment">
<select id="selectMomentLimitComments" resultType="com.tangguo.domain.BbsMomentComment">
SELECT c.* FROM (
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
FROM
bbs_moment_comment
bbs_moment_comment c
INNER JOIN
(
SELECT
id, is_enable_featured_comment
FROM
bbs_moment
WHERE
id IN
<foreach collection="momentIds" item="id" open="(" separator="," close=")">
#{id}
</foreach>
) m ON m.id = c.moment_id
WHERE
parent_id IS NULL
c.parent_id IS NULL
AND
c.status = 1
AND
moment_id IN
<foreach collection="momentIds" item="id" open="(" separator="," close=")">
#{id}
</foreach>
(
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
WHERE rn &lt;= #{rows};
</select>
......@@ -71,8 +92,18 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
IF(c.user_name = #{userName}, 1, 0) AS is_self
FROM
bbs_moment_comment c
INNER JOIN
bbs_moment m ON m.id = c.moment_id
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
c.create_time
</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