feat(resource): 调整评论和回复

master
yang 2025-02-12 18:15:09 +08:00
parent e679fd847e
commit 570c7bb656
7 changed files with 174 additions and 115 deletions

View File

@ -43,8 +43,8 @@ public class WorkFlowCommentController {
* /
*/
@ApiOperation(value = "工作流点赞/取消")
@GetMapping("/like/{workFlowId}")
public AjaxResult like(@PathVariable Long workFlowId) {
@GetMapping("/like")
public AjaxResult like(@Valid @NotNull(message = "模型id不能为空") Long workFlowId) {
workFlowLikeService.like(workFlowId);
return AjaxResult.success();
}

View File

@ -72,6 +72,11 @@ public class ModelCommentVo {
@ApiModelProperty(value = "评论点赞数")
private Integer likeNum;
/**
*
*/
private Integer isLike;
/**
*
*/

View File

@ -72,6 +72,11 @@ public class ModelImageCommentVo {
@ApiModelProperty(value = "评论点赞数")
private Integer likeNum;
/**
*
*/
private Integer isLike;
/**
*
*/

View File

@ -72,6 +72,11 @@ public class WorkFlowCommentVo {
@ApiModelProperty(value = "评论点赞数")
private Integer likeNum;
/**
*
*/
private Integer isLike;
/**
*
*/

View File

@ -6,6 +6,7 @@ import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.mcwl.common.core.domain.entity.SysUser;
import com.mcwl.common.utils.SecurityUtils;
import com.mcwl.resource.domain.ModelComment;
import com.mcwl.resource.domain.ModelCommentLike;
import com.mcwl.resource.domain.ModelImageComment;
import com.mcwl.resource.domain.dto.ModelCommentRes;
import com.mcwl.resource.domain.dto.ModelImageCommentRes;
@ -14,6 +15,7 @@ import com.mcwl.resource.domain.vo.ModelImageCommentVo;
import com.mcwl.resource.mapper.ModelCommentMapper;
import com.mcwl.resource.mapper.ModelImageCommentMapper;
import com.mcwl.resource.mapper.ModelImageMapper;
import com.mcwl.resource.service.ModelCommentLikeService;
import com.mcwl.resource.service.ModelCommentService;
import com.mcwl.system.service.ISysUserService;
import org.springframework.beans.factory.annotation.Autowired;
@ -32,9 +34,13 @@ public class ModelCommentServiceImpl extends ServiceImpl<ModelCommentMapper, Mod
@Autowired
private ISysUserService sysUserService;
@Autowired
private ModelCommentMapper modelCommentMapper;
@Autowired
private ModelCommentLikeService modelCommentLikeService;
@Autowired
private ModelImageMapper modelImageMapper;
@ -54,6 +60,7 @@ public class ModelCommentServiceImpl extends ServiceImpl<ModelCommentMapper, Mod
modelCommentMapper.insert(modelComment);
}
/**
*
*
@ -89,8 +96,41 @@ public class ModelCommentServiceImpl extends ServiceImpl<ModelCommentMapper, Mod
sysUserService.listByIds(userIds).stream()
.collect(Collectors.toMap(SysUser::getUserId, Function.identity()));
// 4. 构建评论树结构
return buildCommentTree(parentComments, childComments, userMap);
// 4. 收集所有评论ID父评论+子评论)
List<Long> allCommentIds = collectAllCommentIds(parentComments, childComments);
// 5. 批量查询点赞状态(仅当用户已登录)
Map<Long, Integer> likeStatusMap = SecurityUtils.getUserId() != null ?
batchGetLikeStatus(allCommentIds, SecurityUtils.getUserId()) :
new HashMap<>();
// 6. 构建评论树结构(传递点赞状态)
return buildCommentTree(parentComments, childComments, userMap, likeStatusMap);
}
/**
* ID+
*/
private List<Long> collectAllCommentIds(List<ModelComment> parentComments, List<ModelComment> childComments) {
List<Long> commentIds = new ArrayList<>();
parentComments.forEach(c -> commentIds.add(c.getId()));
childComments.forEach(c -> commentIds.add(c.getId()));
return commentIds;
}
private Map<Long, Integer> batchGetLikeStatus(List<Long> commentIds, Long currentUserId) {
if (commentIds.isEmpty()) {
return Collections.emptyMap();
}
// 批量查询点赞状态
List<ModelCommentLike> likes = modelCommentLikeService.list(new LambdaQueryWrapper<ModelCommentLike>()
.eq(ModelCommentLike::getUserId, currentUserId)
.in(ModelCommentLike::getModelCommentId, commentIds));
// 构建点赞状态映射
return likes.stream()
.collect(Collectors.toMap(ModelCommentLike::getModelCommentId, like -> 1));
}
/**
@ -135,24 +175,20 @@ public class ModelCommentServiceImpl extends ServiceImpl<ModelCommentMapper, Mod
*/
private List<ModelCommentVo> buildCommentTree(List<ModelComment> parentComments,
List<ModelComment> childComments,
Map<Long, SysUser> userMap) {
// 按父ID分组子评论
Map<Long, SysUser> userMap,
Map<Long, Integer> likeStatusMap) {
Map<Long, List<ModelComment>> childMap = new HashMap<>();
for (ModelComment child : childComments) {
Long parentId = child.getParentId();
childMap.computeIfAbsent(parentId, k -> new ArrayList<>()).add(child);
childMap.computeIfAbsent(child.getParentId(), k -> new ArrayList<>()).add(child);
}
// 构建VO树
List<ModelCommentVo> result = new ArrayList<>();
for (ModelComment parent : parentComments) {
// 转换父评论VO
ModelCommentVo parentVo = convertToVo(parent, userMap);
// 获取子评论VO列表
ModelCommentVo parentVo = convertToVo(parent, userMap, likeStatusMap);
List<ModelComment> children = childMap.getOrDefault(parent.getId(), new ArrayList<>());
List<ModelCommentVo> childVos = new ArrayList<>();
for (ModelComment child : children) {
childVos.add(convertToVo(child, userMap));
childVos.add(convertToVo(child, userMap, likeStatusMap));
}
parentVo.setContentList(childVos);
result.add(parentVo);
@ -160,15 +196,13 @@ public class ModelCommentServiceImpl extends ServiceImpl<ModelCommentMapper, Mod
return result;
}
/**
* VO
*/
private ModelCommentVo convertToVo(ModelComment comment, Map<Long, SysUser> userMap) {
private ModelCommentVo convertToVo(ModelComment comment,
Map<Long, SysUser> userMap,
Map<Long, Integer> likeStatusMap) {
SysUser user = userMap.get(comment.getUserId());
if (user == null) {
user = createDefaultUser(); // 处理用户信息缺失
user = createDefaultUser();
}
ModelCommentVo vo = new ModelCommentVo();
vo.setUserId(comment.getUserId());
vo.setUserName(user.getUserName());
@ -177,6 +211,7 @@ public class ModelCommentServiceImpl extends ServiceImpl<ModelCommentMapper, Mod
vo.setContent(comment.getContent());
vo.setReplyUserId(comment.getReplyUserId());
vo.setLikeNum(comment.getLikeNum());
vo.setIsLike(likeStatusMap.getOrDefault(comment.getId(), 0));
vo.setCreateTime(comment.getCreateTime());
return vo;
}
@ -192,68 +227,4 @@ public class ModelCommentServiceImpl extends ServiceImpl<ModelCommentMapper, Mod
}
/**
* ModelCommentVo
*
* @param modelComment
* @return ModelCommentVo
*/
@NotNull
private ModelCommentVo getModelCommentVo(ModelComment modelComment) {
Long userId = modelComment.getUserId();
SysUser sysUser = sysUserService.selectUserById(userId);
// 构建ModelCommentVo对象
ModelCommentVo modelCommentVo = new ModelCommentVo();
modelCommentVo.setUserId(userId);
modelCommentVo.setUserName(sysUser.getUserName());
modelCommentVo.setUserAvatar(sysUser.getAvatar());
modelCommentVo.setCommentId(modelComment.getId());
modelCommentVo.setContent(modelComment.getContent());
// 查询子评论
modelCommentVo.setContentList(getContentList(modelComment.getId()));
modelCommentVo.setLikeNum(modelComment.getLikeNum());
modelCommentVo.setCreateTime(modelComment.getCreateTime());
return modelCommentVo;
}
/**
*
*
* @param modelCommentId id
* @return List<ModelCommentVo>
*/
private List<ModelCommentVo> getContentList(Long modelCommentId) {
List<ModelCommentVo> modelCommentVoList = new ArrayList<>();
if (Objects.isNull(modelCommentId)) {
return modelCommentVoList;
}
// 查询子评论
LambdaQueryWrapper<ModelComment> lqw = new LambdaQueryWrapper<ModelComment>()
.eq(ModelComment::getParentId, modelCommentId)
.orderByDesc(ModelComment::getCreateTime);
List<ModelComment> modelCommentList = modelCommentMapper.selectList(lqw);
for (ModelComment modelComment : modelCommentList) {
Long userId = modelComment.getUserId();
SysUser sysUser = sysUserService.selectUserById(userId);
ModelCommentVo modelCommentVo = new ModelCommentVo();
modelCommentVo.setUserId(userId);
modelCommentVo.setUserName(sysUser.getUserName());
modelCommentVo.setUserAvatar(sysUser.getAvatar());
modelCommentVo.setCommentId(modelComment.getId());
modelCommentVo.setContent(modelComment.getContent());
modelCommentVo.setLikeNum(modelComment.getLikeNum());
modelCommentVo.setCreateTime(modelComment.getCreateTime());
modelCommentVoList.add(modelCommentVo);
}
return modelCommentVoList;
}
}

View File

@ -5,12 +5,13 @@ import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.mcwl.common.core.domain.entity.SysUser;
import com.mcwl.common.utils.SecurityUtils;
import com.mcwl.resource.domain.ModelImage;
import com.mcwl.resource.domain.ModelImageComment;
import com.mcwl.resource.domain.*;
import com.mcwl.resource.domain.dto.ModelImageCommentRes;
import com.mcwl.resource.domain.vo.ModelImageCommentVo;
import com.mcwl.resource.domain.vo.WorkFlowCommentVo;
import com.mcwl.resource.mapper.ModelImageCommentMapper;
import com.mcwl.resource.mapper.ModelImageMapper;
import com.mcwl.resource.service.ModelImageCommentLikeService;
import com.mcwl.resource.service.ModelImageCommentService;
import com.mcwl.resource.service.ModelImageService;
import com.mcwl.system.service.ISysUserService;
@ -31,6 +32,8 @@ public class ModelImageCommentServiceImpl extends ServiceImpl<ModelImageCommentM
private final ISysUserService sysUserService;
private final ModelImageCommentLikeService modelImageCommentLikeService;
@Override
public void comment(ModelImageCommentRes modelImageCommentRes) {
Long parentId = modelImageCommentRes.getParentId();
@ -83,8 +86,42 @@ public class ModelImageCommentServiceImpl extends ServiceImpl<ModelImageCommentM
sysUserService.listByIds(userIds).stream()
.collect(Collectors.toMap(SysUser::getUserId, Function.identity()));
// 4. 构建评论树结构
return buildCommentTree(parentComments, childComments, userMap);
// 4. 收集所有评论ID父评论+子评论)
List<Long> allCommentIds = collectAllCommentIds(parentComments, childComments);
// 5. 批量查询点赞状态(仅当用户已登录)
Map<Long, Integer> likeStatusMap = SecurityUtils.getUserId() != null ?
batchGetLikeStatus(allCommentIds, SecurityUtils.getUserId()) :
new HashMap<>();
// 6. 构建评论树结构(传递点赞状态)
return buildCommentTree(parentComments, childComments, userMap, likeStatusMap);
}
/**
* ID+
*/
private List<Long> collectAllCommentIds(List<ModelImageComment> parentComments, List<ModelImageComment> childComments) {
List<Long> commentIds = new ArrayList<>();
parentComments.forEach(c -> commentIds.add(c.getId()));
childComments.forEach(c -> commentIds.add(c.getId()));
return commentIds;
}
private Map<Long, Integer> batchGetLikeStatus(List<Long> commentIds, Long currentUserId) {
if (commentIds.isEmpty()) {
return Collections.emptyMap();
}
// 批量查询点赞状态
List<ModelImageCommentLike> likes = modelImageCommentLikeService.list(new LambdaQueryWrapper<ModelImageCommentLike>()
.eq(ModelImageCommentLike::getUserId, currentUserId)
.in(ModelImageCommentLike::getModelImageCommentId, commentIds));
// 构建点赞状态映射
return likes.stream()
.collect(Collectors.toMap(ModelImageCommentLike::getModelImageCommentId, like -> 1));
}
/**
@ -129,24 +166,20 @@ public class ModelImageCommentServiceImpl extends ServiceImpl<ModelImageCommentM
*/
private List<ModelImageCommentVo> buildCommentTree(List<ModelImageComment> parentComments,
List<ModelImageComment> childComments,
Map<Long, SysUser> userMap) {
// 按父ID分组子评论
Map<Long, SysUser> userMap,
Map<Long, Integer> likeStatusMap) {
Map<Long, List<ModelImageComment>> childMap = new HashMap<>();
for (ModelImageComment child : childComments) {
Long parentId = child.getParentId();
childMap.computeIfAbsent(parentId, k -> new ArrayList<>()).add(child);
childMap.computeIfAbsent(child.getParentId(), k -> new ArrayList<>()).add(child);
}
// 构建VO树
List<ModelImageCommentVo> result = new ArrayList<>();
for (ModelImageComment parent : parentComments) {
// 转换父评论VO
ModelImageCommentVo parentVo = convertToVo(parent, userMap);
// 获取子评论VO列表
ModelImageCommentVo parentVo = convertToVo(parent, userMap, likeStatusMap);
List<ModelImageComment> children = childMap.getOrDefault(parent.getId(), new ArrayList<>());
List<ModelImageCommentVo> childVos = new ArrayList<>();
for (ModelImageComment child : children) {
childVos.add(convertToVo(child, userMap));
childVos.add(convertToVo(child, userMap, likeStatusMap));
}
parentVo.setContentList(childVos);
result.add(parentVo);
@ -157,7 +190,9 @@ public class ModelImageCommentServiceImpl extends ServiceImpl<ModelImageCommentM
/**
* VO
*/
private ModelImageCommentVo convertToVo(ModelImageComment comment, Map<Long, SysUser> userMap) {
private ModelImageCommentVo convertToVo(ModelImageComment comment,
Map<Long, SysUser> userMap,
Map<Long, Integer> likeStatusMap) {
SysUser user = userMap.get(comment.getUserId());
if (user == null) {
user = createDefaultUser(); // 处理用户信息缺失
@ -171,6 +206,7 @@ public class ModelImageCommentServiceImpl extends ServiceImpl<ModelImageCommentM
vo.setContent(comment.getContent());
vo.setReplyUserId(comment.getReplyUserId());
vo.setLikeNum(comment.getLikeNum());
vo.setIsLike(likeStatusMap.getOrDefault(comment.getId(), 0));
vo.setCreateTime(comment.getCreateTime());
return vo;
}

View File

@ -5,12 +5,13 @@ import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.mcwl.common.core.domain.entity.SysUser;
import com.mcwl.common.utils.SecurityUtils;
import com.mcwl.resource.domain.ModelImageComment;
import com.mcwl.resource.domain.WorkFlowComment;
import com.mcwl.resource.domain.*;
import com.mcwl.resource.domain.dto.WorkFlowCommentRes;
import com.mcwl.resource.domain.vo.ModelCommentVo;
import com.mcwl.resource.domain.vo.ModelImageCommentVo;
import com.mcwl.resource.domain.vo.WorkFlowCommentVo;
import com.mcwl.resource.mapper.WorkFlowCommentMapper;
import com.mcwl.resource.service.WorkFlowCommentLikeService;
import com.mcwl.resource.service.WorkFlowCommentService;
import com.mcwl.system.service.ISysUserService;
import org.springframework.beans.factory.annotation.Autowired;
@ -40,6 +41,9 @@ public class WorkFlowCommentServiceImpl extends ServiceImpl<WorkFlowCommentMappe
@Autowired
private WorkFlowCommentMapper workFlowCommentMapper;
@Autowired
private WorkFlowCommentLikeService workFlowCommentLikeService;
@Override
public void comment(WorkFlowCommentRes workFlowCommentRes) {
@ -88,8 +92,42 @@ public class WorkFlowCommentServiceImpl extends ServiceImpl<WorkFlowCommentMappe
.collect(Collectors.toMap(SysUser::getUserId, Function.identity()));
// 4. 构建评论树结构
return buildCommentTree(parentComments, childComments, userMap);
// 4. 收集所有评论ID父评论+子评论)
List<Long> allCommentIds = collectAllCommentIds(parentComments, childComments);
// 5. 批量查询点赞状态(仅当用户已登录)
Map<Long, Integer> likeStatusMap = SecurityUtils.getUserId() != null ?
batchGetLikeStatus(allCommentIds, SecurityUtils.getUserId()) :
new HashMap<>();
// 6. 构建评论树结构(传递点赞状态)
return buildCommentTree(parentComments, childComments, userMap, likeStatusMap);
}
/**
* ID+
*/
private List<Long> collectAllCommentIds(List<WorkFlowComment> parentComments, List<WorkFlowComment> childComments) {
List<Long> commentIds = new ArrayList<>();
parentComments.forEach(c -> commentIds.add(c.getId()));
childComments.forEach(c -> commentIds.add(c.getId()));
return commentIds;
}
private Map<Long, Integer> batchGetLikeStatus(List<Long> commentIds, Long currentUserId) {
if (commentIds.isEmpty()) {
return Collections.emptyMap();
}
// 批量查询点赞状态
List<WorkFlowCommentLike> likes = workFlowCommentLikeService.list(new LambdaQueryWrapper<WorkFlowCommentLike>()
.eq(WorkFlowCommentLike::getUserId, currentUserId)
.in(WorkFlowCommentLike::getWorkFlowCommentId, commentIds));
// 构建点赞状态映射
return likes.stream()
.collect(Collectors.toMap(WorkFlowCommentLike::getWorkFlowCommentId, like -> 1));
}
/**
@ -134,24 +172,20 @@ public class WorkFlowCommentServiceImpl extends ServiceImpl<WorkFlowCommentMappe
*/
private List<WorkFlowCommentVo> buildCommentTree(List<WorkFlowComment> parentComments,
List<WorkFlowComment> childComments,
Map<Long, SysUser> userMap) {
// 按父ID分组子评论
Map<Long, SysUser> userMap,
Map<Long, Integer> likeStatusMap) {
Map<Long, List<WorkFlowComment>> childMap = new HashMap<>();
for (WorkFlowComment child : childComments) {
Long parentId = child.getParentId();
childMap.computeIfAbsent(parentId, k -> new ArrayList<>()).add(child);
childMap.computeIfAbsent(child.getParentId(), k -> new ArrayList<>()).add(child);
}
// 构建VO树
List<WorkFlowCommentVo> result = new ArrayList<>();
for (WorkFlowComment parent : parentComments) {
// 转换父评论VO
WorkFlowCommentVo parentVo = convertToVo(parent, userMap);
// 获取子评论VO列表
WorkFlowCommentVo parentVo = convertToVo(parent, userMap, likeStatusMap);
List<WorkFlowComment> children = childMap.getOrDefault(parent.getId(), new ArrayList<>());
List<WorkFlowCommentVo> childVos = new ArrayList<>();
for (WorkFlowComment child : children) {
childVos.add(convertToVo(child, userMap));
childVos.add(convertToVo(child, userMap, likeStatusMap));
}
parentVo.setContentList(childVos);
result.add(parentVo);
@ -162,7 +196,9 @@ public class WorkFlowCommentServiceImpl extends ServiceImpl<WorkFlowCommentMappe
/**
* VO
*/
private WorkFlowCommentVo convertToVo(WorkFlowComment comment, Map<Long, SysUser> userMap) {
private WorkFlowCommentVo convertToVo(WorkFlowComment comment,
Map<Long, SysUser> userMap,
Map<Long, Integer> likeStatusMap) {
SysUser user = userMap.get(comment.getUserId());
if (user == null) {
user = createDefaultUser(); // 处理用户信息缺失
@ -176,6 +212,7 @@ public class WorkFlowCommentServiceImpl extends ServiceImpl<WorkFlowCommentMappe
vo.setContent(comment.getContent());
vo.setReplyUserId(comment.getReplyUserId());
vo.setLikeNum(comment.getLikeNum());
vo.setIsLike(likeStatusMap.getOrDefault(comment.getId(), 0));
vo.setCreateTime(comment.getCreateTime());
return vo;
}