libilibi项目总结(8)视频评论
【代码】easylive项目总结(8)视频评论。
·
video_comment
CREATE TABLE `video_comment` (
`comment_id` int NOT NULL AUTO_INCREMENT COMMENT '评论ID',
`p_comment_id` int NOT NULL COMMENT '父级评论ID',
`video_id` varchar(10) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NOT NULL COMMENT '视频ID',
`video_user_id` varchar(10) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NOT NULL COMMENT '视频用户ID',
`content` varchar(500) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci DEFAULT NULL COMMENT '回复内容',
`img_path` varchar(150) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci DEFAULT NULL COMMENT '图片',
`user_id` varchar(15) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NOT NULL COMMENT '用户ID',
`reply_user_id` varchar(15) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci DEFAULT NULL COMMENT '回复人ID',
`top_type` tinyint DEFAULT '0' COMMENT '0:未置顶 1:置顶',
`post_time` datetime NOT NULL COMMENT '发布时间',
`like_count` int DEFAULT '0' COMMENT '喜欢数量',
`hate_count` int DEFAULT '0' COMMENT '讨厌数量',
PRIMARY KEY (`comment_id`) USING BTREE,
KEY `idx_post_time` (`post_time`) USING BTREE,
KEY `idx_top` (`top_type`) USING BTREE,
KEY `idx_p_id` (`p_comment_id`) USING BTREE,
KEY `idx_user_id` (`user_id`) USING BTREE,
KEY `idx_video_id` (`video_id`) USING BTREE
) ENGINE=InnoDB AUTO_INCREMENT=15 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci ROW_FORMAT=DYNAMIC COMMENT='评论';
发布评论
Controller
@RequestMapping("/postComment")
@RecordUserMessage(messageType = MessageTypeEnum.COMMENT)
@GlobalInterceptor(checkLogin = true)
public ResponseVO postComment(@NotEmpty String videoId,
Integer replyCommentId,
@NotEmpty @Size(max = 500) String content,
@Size(max = 50) String imgPath) {
TokenUserInfoDto tokenUserInfoDto = getTokenUserInfoDto();
VideoComment comment = new VideoComment();
comment.setUserId(tokenUserInfoDto.getUserId());
comment.setAvatar(tokenUserInfoDto.getAvatar());
comment.setNickName(tokenUserInfoDto.getNickName());
comment.setVideoId(videoId);
comment.setContent(content);
comment.setImgPath(imgPath);
videoCommentService.postComment(comment, replyCommentId);
return getSuccessResponseVO(comment);
}
videoCommentService.postComment(comment, replyCommentId);
//发布评论
@Override
public void postComment(VideoComment comment, Integer replyCommentId) {
//查询评论对应的视频信息
VideoInfo videoInfo = videoInfoMapper.selectByVideoId(comment.getVideoId());
if(videoInfo==null){
throw new BusinessException(ResponseCodeEnum.CODE_600);
}
if(videoInfo.getInteraction()!=null && videoInfo.getInteraction().contains(Constants.ZERO.toString())){
throw new BusinessException("UP主已关闭评论区");
}
//此条评论不为顶级评论
if(replyCommentId!=null){
//查询回复的评论信息
VideoComment replayComment = this.getVideoCommentByCommentId(replyCommentId);
//回复的评论为空或者不为该视频的评论
if(replayComment==null ||!replayComment.getVideoId().equals(comment.getVideoId())){
throw new BusinessException(ResponseCodeEnum.CODE_600);
}
//回复的评论是顶级评论顶级评论
if(replayComment.getpCommentId()==0){
//就设置此条评论的父评论为回复的这条评论的id
comment.setpCommentId(replayComment.getCommentId());
}
else{
//否则设置此条评论的父评论为回复评论的父评论id
Integer pCommentId = replayComment.getpCommentId();
comment.setpCommentId(pCommentId);
}
//设置评论对应的用户的昵称和头像
UserInfo userInfo = userInfoMapper.selectByUserId(replayComment.getUserId());
comment.setReplyAvatar(userInfo.getAvatar());
comment.setReplyNickName(userInfo.getNickName());
}
//此条评论为顶级评论
else{
comment.setpCommentId(Constants.ZERO);
//视频的评论数只算顶级评论
videoInfoMapper.updateCountInfo(videoInfo.getVideoId(),
UserActionTypeEnum.VIDEO_COMMENT.getField(), Constants.ONE);
}
comment.setPostTime(new Date());
comment.setVideoUserId(videoInfo.getUserId());
this.videoCommentMapper.insert(comment);
}
查询评论
Controller
//查询评论
@RequestMapping("/loadComment")
public ResponseVO loadComment(@NotEmpty String videoId, Integer pageNo, Integer orderType) {
VideoInfo videoInfo = videoInfoService.getVideoInfoByVideoId(videoId);
//该视频已关闭评论区
if (videoInfo.getInteraction() != null && videoInfo.getInteraction().contains(Constants.ONE.toString())) {
return getSuccessResponseVO(new ArrayList<>());
}
VideoCommentQuery commentQuery = new VideoCommentQuery();
commentQuery.setVideoId(videoId);
//设置放回的评论列表为树形结构
commentQuery.setLoadChildren(true);
commentQuery.setPageNo(pageNo);
commentQuery.setPageSize(PageSize.SIZE15.getSize());
//查询顶级评论
commentQuery.setpCommentId(0);
//设置查看最热评论还是最新评论
String orderBy = orderType == null || orderType == 0 ? "like_count desc,comment_id desc" : "comment_id desc";
commentQuery.setOrderBy(orderBy);
//从数据库中分页查询评论
PaginationResultVO<VideoComment> commentData = videoCommentService.findListByPage(commentQuery);
//置顶评论
//分页查询页数为第一页时查询置顶评论
if (pageNo == null || pageNo == 1) {
//从数据库中查询置顶评论
List<VideoComment> topCommentList = topComment(videoId);
if (!topCommentList.isEmpty()) {
//从刚刚数据库中查询到的数据中删除这条置顶的评论
List<VideoComment> commentList =
commentData.getList().stream()
.filter(item -> !item.getCommentId().equals(topCommentList.get(0).getCommentId()))
.collect(Collectors.toList());
//将置顶评论放到列表开头
commentList.addAll(0, topCommentList);
//将commentData内的内容替换为commentList
commentData.setList(commentList);
}
}
VideoCommentResultVO resultVO = new VideoCommentResultVO();
resultVO.setCommentData(commentData);
//查询当前用户对应的评论点赞和不喜欢信息
List<UserAction> userActionList = new ArrayList<>();
TokenUserInfoDto tokenUserInfoDto = getTokenUserInfoDto();
if (tokenUserInfoDto != null) {
UserActionQuery userActionQuery = new UserActionQuery();
userActionQuery.setUserId(tokenUserInfoDto.getUserId());
userActionQuery.setVideoId(videoId);
userActionQuery.setActionTypeArray(new Integer[]{UserActionTypeEnum.COMMENT_LIKE.getType(),
UserActionTypeEnum.COMMENT_HATE.getType()});
userActionList = userActionService.findListByParam(userActionQuery);
}
resultVO.setUserActionList(userActionList);
return getSuccessResponseVO(resultVO);
}
videoCommentService.findListByPage(commentQuery);
@Override
public PaginationResultVO<VideoComment> findListByPage(VideoCommentQuery param) {
int count = this.findCountByParam(param);
int pageSize = param.getPageSize() == null ? PageSize.SIZE15.getSize() : param.getPageSize();
SimplePage page = new SimplePage(param.getPageNo(), count, pageSize);
param.setSimplePage(page);
List<VideoComment> list = this.findListByParam(param);
PaginationResultVO<VideoComment> result = new PaginationResultVO(count, page.getPageSize(), page.getPageNo(), page.getPageTotal(), list);
return result;
}
this.findListByParam(param);
public List<VideoComment> findListByParam(VideoCommentQuery param) {
if (param.getLoadChildren() != null && param.getLoadChildren()) {
return this.videoCommentMapper.selectListWithChildren(param);
}
return this.videoCommentMapper.selectList(param);
}
videoCommentMapper.selectListWithChildren(param);
<!--这里单独定义一个resultMap 如果放到 restMap中就会递归调用 -->
<resultMap id="base_result_map_children" type="com.easylive.entity.po.VideoComment" extends="base_result_map">
<collection property="children" column="comment_id" select="com.easylive.mappers.VideoCommentMapper.selectChildComment">
</collection>
</resultMap>
<select id="selectChildComment" resultMap="base_result_map">
select v.*,u.nick_name nickName,u.avatar avatar,u2.nick_name replyNickName,u2.avatar replyAvatar
from video_comment v
inner join user_info u on v.user_id = u.user_id
left join user_info u2 on u2.user_id = v.reply_user_id
where p_comment_id = #{commentId} order by v.comment_id asc
</select>
<select id="selectListWithChildren" resultMap="base_result_map_children">
SELECT
<include refid="base_column_list"/>,u.nick_name nickName,u.avatar avatar
FROM video_comment v left join user_info u on v.user_id = u.user_id
<include refid="query_condition"/>
<if test="query.orderBy!=null">
order by ${query.orderBy}
</if>
<if test="query.simplePage!=null">
limit #{query.simplePage.start},#{query.simplePage.end}
</if>
</select>
查询置顶评论
topComment(String videoId);
private List<VideoComment> topComment(String videoId) {
VideoCommentQuery commentQuery = new VideoCommentQuery();
commentQuery.setVideoId(videoId);
commentQuery.setTopType(CommentTopTypeEnum.TOP.getType());
commentQuery.setLoadChildren(true);
List<VideoComment> videoCommentList = videoCommentService.findListByParam(commentQuery);
return videoCommentList;
}
置顶评论
Contorller
//评论置顶
@RequestMapping("/topComment")
public ResponseVO topComment(@NotNull Integer commentId) {
TokenUserInfoDto tokenUserInfoDto = getTokenUserInfoDto();
videoCommentService.topComment(commentId, tokenUserInfoDto.getUserId());
return getSuccessResponseVO(null);
}
Service
//评论置顶
@Override
@Transactional(rollbackFor = Exception.class)
public void topComment(Integer commentId, String userId) {
VideoComment videoComment = videoCommentMapper.selectByCommentId(commentId);
if(videoComment==null){
throw new BusinessException(ResponseCodeEnum.CODE_600);
}
VideoInfo videoInfo = videoInfoMapper.selectByVideoId(videoComment.getVideoId());
if(!videoInfo.getUserId().equals(userId)){
throw new BusinessException(ResponseCodeEnum.CODE_600);
}
//先查询之前的置顶评论
VideoCommentQuery videoCommentQuery = new VideoCommentQuery();
videoCommentQuery.setTopType(CommentTopTypeEnum.TOP.getType());
videoCommentQuery.setVideoId(videoComment.getVideoId());
List<VideoComment> videoComments = videoCommentMapper.selectList(videoCommentQuery);
//取消之前的置顶评论
if(videoComments!=null){
this.cancelTopComment(videoComments.get(0).getCommentId(),userId);
}
//更新该条评论的置顶信息
videoComment.setTopType(CommentTopTypeEnum.TOP.getType());
videoCommentMapper.updateByCommentId(videoComment, commentId);
}
//取消评论置顶
@Override
public void cancelTopComment(Integer commentId, String userId) {
VideoComment videoComment = videoCommentMapper.selectByCommentId(commentId);
if(videoComment==null){
throw new BusinessException(ResponseCodeEnum.CODE_600);
}
VideoInfo videoInfo = videoInfoMapper.selectByVideoId(videoComment.getVideoId());
//不是该用户发布的视频
if(!videoInfo.getUserId().equals(userId)){
throw new BusinessException(ResponseCodeEnum.CODE_600);
}
//设置为不置顶
videoComment.setTopType(CommentTopTypeEnum.NO_TOP.getType());
videoCommentMapper.updateByCommentId(videoComment, commentId);
}
删除评论
Conroller
//删除评论
@RequestMapping("/userDelComment")
public ResponseVO userDelComment(@NotNull Integer commentId) {
TokenUserInfoDto tokenUserInfoDto = getTokenUserInfoDto();
VideoComment comment = new VideoComment();
videoCommentService.deleteComment(commentId, tokenUserInfoDto.getUserId());
return getSuccessResponseVO(comment);
}
videoCommentService.deleteComment(commentId, tokenUserInfoDto.getUserId());
//删除评论
@Override
public void deleteComment(Integer commentId, String userId) {
VideoComment videoComment = videoCommentMapper.selectByCommentId(commentId);
if(videoComment==null){
throw new BusinessException(ResponseCodeEnum.CODE_600);
}
VideoInfo videoInfo = videoInfoMapper.selectByVideoId(videoComment.getVideoId());
if(videoInfo==null){
throw new BusinessException(ResponseCodeEnum.CODE_600);
}
//管理员和up主能删评论
if(userId!=null && !videoInfo.getUserId().equals(userId) && !videoComment.getUserId().equals(userId) ){
throw new BusinessException(ResponseCodeEnum.CODE_600);
}
videoCommentMapper.deleteByCommentId(commentId);
//如果是顶级评论
if(videoComment.getpCommentId() == 0){
videoInfoMapper.updateCountInfo(videoInfo.getVideoId(),
UserActionTypeEnum.VIDEO_COMMENT.getField(),
-Constants.ONE);
//删除二级评论
VideoCommentQuery videoCommentQuery = new VideoCommentQuery();
videoCommentQuery.setpCommentId(commentId);
videoCommentMapper.deleteByParam(videoCommentQuery);
}
}
更多推荐


所有评论(0)