feat(resource): 调整评论和回复
parent
e679fd847e
commit
570c7bb656
|
@ -43,8 +43,8 @@ public class WorkFlowCommentController {
|
||||||
* 工作流点赞/取消
|
* 工作流点赞/取消
|
||||||
*/
|
*/
|
||||||
@ApiOperation(value = "工作流点赞/取消")
|
@ApiOperation(value = "工作流点赞/取消")
|
||||||
@GetMapping("/like/{workFlowId}")
|
@GetMapping("/like")
|
||||||
public AjaxResult like(@PathVariable Long workFlowId) {
|
public AjaxResult like(@Valid @NotNull(message = "模型id不能为空") Long workFlowId) {
|
||||||
workFlowLikeService.like(workFlowId);
|
workFlowLikeService.like(workFlowId);
|
||||||
return AjaxResult.success();
|
return AjaxResult.success();
|
||||||
}
|
}
|
||||||
|
|
|
@ -72,6 +72,11 @@ public class ModelCommentVo {
|
||||||
@ApiModelProperty(value = "评论点赞数")
|
@ApiModelProperty(value = "评论点赞数")
|
||||||
private Integer likeNum;
|
private Integer likeNum;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 是否点赞
|
||||||
|
*/
|
||||||
|
private Integer isLike;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 评论时间
|
* 评论时间
|
||||||
*/
|
*/
|
||||||
|
|
|
@ -72,6 +72,11 @@ public class ModelImageCommentVo {
|
||||||
@ApiModelProperty(value = "评论点赞数")
|
@ApiModelProperty(value = "评论点赞数")
|
||||||
private Integer likeNum;
|
private Integer likeNum;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 是否点赞
|
||||||
|
*/
|
||||||
|
private Integer isLike;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 评论时间
|
* 评论时间
|
||||||
*/
|
*/
|
||||||
|
|
|
@ -72,6 +72,11 @@ public class WorkFlowCommentVo {
|
||||||
@ApiModelProperty(value = "评论点赞数")
|
@ApiModelProperty(value = "评论点赞数")
|
||||||
private Integer likeNum;
|
private Integer likeNum;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 是否点赞
|
||||||
|
*/
|
||||||
|
private Integer isLike;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 评论时间
|
* 评论时间
|
||||||
*/
|
*/
|
||||||
|
|
|
@ -6,6 +6,7 @@ import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
||||||
import com.mcwl.common.core.domain.entity.SysUser;
|
import com.mcwl.common.core.domain.entity.SysUser;
|
||||||
import com.mcwl.common.utils.SecurityUtils;
|
import com.mcwl.common.utils.SecurityUtils;
|
||||||
import com.mcwl.resource.domain.ModelComment;
|
import com.mcwl.resource.domain.ModelComment;
|
||||||
|
import com.mcwl.resource.domain.ModelCommentLike;
|
||||||
import com.mcwl.resource.domain.ModelImageComment;
|
import com.mcwl.resource.domain.ModelImageComment;
|
||||||
import com.mcwl.resource.domain.dto.ModelCommentRes;
|
import com.mcwl.resource.domain.dto.ModelCommentRes;
|
||||||
import com.mcwl.resource.domain.dto.ModelImageCommentRes;
|
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.ModelCommentMapper;
|
||||||
import com.mcwl.resource.mapper.ModelImageCommentMapper;
|
import com.mcwl.resource.mapper.ModelImageCommentMapper;
|
||||||
import com.mcwl.resource.mapper.ModelImageMapper;
|
import com.mcwl.resource.mapper.ModelImageMapper;
|
||||||
|
import com.mcwl.resource.service.ModelCommentLikeService;
|
||||||
import com.mcwl.resource.service.ModelCommentService;
|
import com.mcwl.resource.service.ModelCommentService;
|
||||||
import com.mcwl.system.service.ISysUserService;
|
import com.mcwl.system.service.ISysUserService;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
@ -32,9 +34,13 @@ public class ModelCommentServiceImpl extends ServiceImpl<ModelCommentMapper, Mod
|
||||||
|
|
||||||
@Autowired
|
@Autowired
|
||||||
private ISysUserService sysUserService;
|
private ISysUserService sysUserService;
|
||||||
|
|
||||||
@Autowired
|
@Autowired
|
||||||
private ModelCommentMapper modelCommentMapper;
|
private ModelCommentMapper modelCommentMapper;
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private ModelCommentLikeService modelCommentLikeService;
|
||||||
|
|
||||||
@Autowired
|
@Autowired
|
||||||
private ModelImageMapper modelImageMapper;
|
private ModelImageMapper modelImageMapper;
|
||||||
|
|
||||||
|
@ -54,6 +60,7 @@ public class ModelCommentServiceImpl extends ServiceImpl<ModelCommentMapper, Mod
|
||||||
modelCommentMapper.insert(modelComment);
|
modelCommentMapper.insert(modelComment);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 获取评论
|
* 获取评论
|
||||||
*
|
*
|
||||||
|
@ -89,8 +96,41 @@ public class ModelCommentServiceImpl extends ServiceImpl<ModelCommentMapper, Mod
|
||||||
sysUserService.listByIds(userIds).stream()
|
sysUserService.listByIds(userIds).stream()
|
||||||
.collect(Collectors.toMap(SysUser::getUserId, Function.identity()));
|
.collect(Collectors.toMap(SysUser::getUserId, Function.identity()));
|
||||||
|
|
||||||
// 4. 构建评论树结构
|
// 4. 收集所有评论ID(父评论+子评论)
|
||||||
return buildCommentTree(parentComments, childComments, userMap);
|
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,
|
private List<ModelCommentVo> buildCommentTree(List<ModelComment> parentComments,
|
||||||
List<ModelComment> childComments,
|
List<ModelComment> childComments,
|
||||||
Map<Long, SysUser> userMap) {
|
Map<Long, SysUser> userMap,
|
||||||
// 按父ID分组子评论
|
Map<Long, Integer> likeStatusMap) {
|
||||||
Map<Long, List<ModelComment>> childMap = new HashMap<>();
|
Map<Long, List<ModelComment>> childMap = new HashMap<>();
|
||||||
for (ModelComment child : childComments) {
|
for (ModelComment child : childComments) {
|
||||||
Long parentId = child.getParentId();
|
childMap.computeIfAbsent(child.getParentId(), k -> new ArrayList<>()).add(child);
|
||||||
childMap.computeIfAbsent(parentId, k -> new ArrayList<>()).add(child);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// 构建VO树
|
|
||||||
List<ModelCommentVo> result = new ArrayList<>();
|
List<ModelCommentVo> result = new ArrayList<>();
|
||||||
for (ModelComment parent : parentComments) {
|
for (ModelComment parent : parentComments) {
|
||||||
// 转换父评论VO
|
ModelCommentVo parentVo = convertToVo(parent, userMap, likeStatusMap);
|
||||||
ModelCommentVo parentVo = convertToVo(parent, userMap);
|
|
||||||
// 获取子评论VO列表
|
|
||||||
List<ModelComment> children = childMap.getOrDefault(parent.getId(), new ArrayList<>());
|
List<ModelComment> children = childMap.getOrDefault(parent.getId(), new ArrayList<>());
|
||||||
List<ModelCommentVo> childVos = new ArrayList<>();
|
List<ModelCommentVo> childVos = new ArrayList<>();
|
||||||
for (ModelComment child : children) {
|
for (ModelComment child : children) {
|
||||||
childVos.add(convertToVo(child, userMap));
|
childVos.add(convertToVo(child, userMap, likeStatusMap));
|
||||||
}
|
}
|
||||||
parentVo.setContentList(childVos);
|
parentVo.setContentList(childVos);
|
||||||
result.add(parentVo);
|
result.add(parentVo);
|
||||||
|
@ -160,15 +196,13 @@ public class ModelCommentServiceImpl extends ServiceImpl<ModelCommentMapper, Mod
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
private ModelCommentVo convertToVo(ModelComment comment,
|
||||||
* 转换单个评论为VO对象
|
Map<Long, SysUser> userMap,
|
||||||
*/
|
Map<Long, Integer> likeStatusMap) {
|
||||||
private ModelCommentVo convertToVo(ModelComment comment, Map<Long, SysUser> userMap) {
|
|
||||||
SysUser user = userMap.get(comment.getUserId());
|
SysUser user = userMap.get(comment.getUserId());
|
||||||
if (user == null) {
|
if (user == null) {
|
||||||
user = createDefaultUser(); // 处理用户信息缺失
|
user = createDefaultUser();
|
||||||
}
|
}
|
||||||
|
|
||||||
ModelCommentVo vo = new ModelCommentVo();
|
ModelCommentVo vo = new ModelCommentVo();
|
||||||
vo.setUserId(comment.getUserId());
|
vo.setUserId(comment.getUserId());
|
||||||
vo.setUserName(user.getUserName());
|
vo.setUserName(user.getUserName());
|
||||||
|
@ -177,6 +211,7 @@ public class ModelCommentServiceImpl extends ServiceImpl<ModelCommentMapper, Mod
|
||||||
vo.setContent(comment.getContent());
|
vo.setContent(comment.getContent());
|
||||||
vo.setReplyUserId(comment.getReplyUserId());
|
vo.setReplyUserId(comment.getReplyUserId());
|
||||||
vo.setLikeNum(comment.getLikeNum());
|
vo.setLikeNum(comment.getLikeNum());
|
||||||
|
vo.setIsLike(likeStatusMap.getOrDefault(comment.getId(), 0));
|
||||||
vo.setCreateTime(comment.getCreateTime());
|
vo.setCreateTime(comment.getCreateTime());
|
||||||
return vo;
|
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;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -5,12 +5,13 @@ import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
||||||
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
||||||
import com.mcwl.common.core.domain.entity.SysUser;
|
import com.mcwl.common.core.domain.entity.SysUser;
|
||||||
import com.mcwl.common.utils.SecurityUtils;
|
import com.mcwl.common.utils.SecurityUtils;
|
||||||
import com.mcwl.resource.domain.ModelImage;
|
import com.mcwl.resource.domain.*;
|
||||||
import com.mcwl.resource.domain.ModelImageComment;
|
|
||||||
import com.mcwl.resource.domain.dto.ModelImageCommentRes;
|
import com.mcwl.resource.domain.dto.ModelImageCommentRes;
|
||||||
import com.mcwl.resource.domain.vo.ModelImageCommentVo;
|
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.ModelImageCommentMapper;
|
||||||
import com.mcwl.resource.mapper.ModelImageMapper;
|
import com.mcwl.resource.mapper.ModelImageMapper;
|
||||||
|
import com.mcwl.resource.service.ModelImageCommentLikeService;
|
||||||
import com.mcwl.resource.service.ModelImageCommentService;
|
import com.mcwl.resource.service.ModelImageCommentService;
|
||||||
import com.mcwl.resource.service.ModelImageService;
|
import com.mcwl.resource.service.ModelImageService;
|
||||||
import com.mcwl.system.service.ISysUserService;
|
import com.mcwl.system.service.ISysUserService;
|
||||||
|
@ -31,6 +32,8 @@ public class ModelImageCommentServiceImpl extends ServiceImpl<ModelImageCommentM
|
||||||
|
|
||||||
private final ISysUserService sysUserService;
|
private final ISysUserService sysUserService;
|
||||||
|
|
||||||
|
private final ModelImageCommentLikeService modelImageCommentLikeService;
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void comment(ModelImageCommentRes modelImageCommentRes) {
|
public void comment(ModelImageCommentRes modelImageCommentRes) {
|
||||||
Long parentId = modelImageCommentRes.getParentId();
|
Long parentId = modelImageCommentRes.getParentId();
|
||||||
|
@ -83,8 +86,42 @@ public class ModelImageCommentServiceImpl extends ServiceImpl<ModelImageCommentM
|
||||||
sysUserService.listByIds(userIds).stream()
|
sysUserService.listByIds(userIds).stream()
|
||||||
.collect(Collectors.toMap(SysUser::getUserId, Function.identity()));
|
.collect(Collectors.toMap(SysUser::getUserId, Function.identity()));
|
||||||
|
|
||||||
// 4. 构建评论树结构
|
// 4. 收集所有评论ID(父评论+子评论)
|
||||||
return buildCommentTree(parentComments, childComments, userMap);
|
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,
|
private List<ModelImageCommentVo> buildCommentTree(List<ModelImageComment> parentComments,
|
||||||
List<ModelImageComment> childComments,
|
List<ModelImageComment> childComments,
|
||||||
Map<Long, SysUser> userMap) {
|
Map<Long, SysUser> userMap,
|
||||||
// 按父ID分组子评论
|
Map<Long, Integer> likeStatusMap) {
|
||||||
Map<Long, List<ModelImageComment>> childMap = new HashMap<>();
|
Map<Long, List<ModelImageComment>> childMap = new HashMap<>();
|
||||||
for (ModelImageComment child : childComments) {
|
for (ModelImageComment child : childComments) {
|
||||||
Long parentId = child.getParentId();
|
childMap.computeIfAbsent(child.getParentId(), k -> new ArrayList<>()).add(child);
|
||||||
childMap.computeIfAbsent(parentId, k -> new ArrayList<>()).add(child);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// 构建VO树
|
|
||||||
List<ModelImageCommentVo> result = new ArrayList<>();
|
List<ModelImageCommentVo> result = new ArrayList<>();
|
||||||
for (ModelImageComment parent : parentComments) {
|
for (ModelImageComment parent : parentComments) {
|
||||||
// 转换父评论VO
|
ModelImageCommentVo parentVo = convertToVo(parent, userMap, likeStatusMap);
|
||||||
ModelImageCommentVo parentVo = convertToVo(parent, userMap);
|
|
||||||
// 获取子评论VO列表
|
|
||||||
List<ModelImageComment> children = childMap.getOrDefault(parent.getId(), new ArrayList<>());
|
List<ModelImageComment> children = childMap.getOrDefault(parent.getId(), new ArrayList<>());
|
||||||
List<ModelImageCommentVo> childVos = new ArrayList<>();
|
List<ModelImageCommentVo> childVos = new ArrayList<>();
|
||||||
for (ModelImageComment child : children) {
|
for (ModelImageComment child : children) {
|
||||||
childVos.add(convertToVo(child, userMap));
|
childVos.add(convertToVo(child, userMap, likeStatusMap));
|
||||||
}
|
}
|
||||||
parentVo.setContentList(childVos);
|
parentVo.setContentList(childVos);
|
||||||
result.add(parentVo);
|
result.add(parentVo);
|
||||||
|
@ -157,7 +190,9 @@ public class ModelImageCommentServiceImpl extends ServiceImpl<ModelImageCommentM
|
||||||
/**
|
/**
|
||||||
* 转换单个评论为VO对象
|
* 转换单个评论为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());
|
SysUser user = userMap.get(comment.getUserId());
|
||||||
if (user == null) {
|
if (user == null) {
|
||||||
user = createDefaultUser(); // 处理用户信息缺失
|
user = createDefaultUser(); // 处理用户信息缺失
|
||||||
|
@ -171,6 +206,7 @@ public class ModelImageCommentServiceImpl extends ServiceImpl<ModelImageCommentM
|
||||||
vo.setContent(comment.getContent());
|
vo.setContent(comment.getContent());
|
||||||
vo.setReplyUserId(comment.getReplyUserId());
|
vo.setReplyUserId(comment.getReplyUserId());
|
||||||
vo.setLikeNum(comment.getLikeNum());
|
vo.setLikeNum(comment.getLikeNum());
|
||||||
|
vo.setIsLike(likeStatusMap.getOrDefault(comment.getId(), 0));
|
||||||
vo.setCreateTime(comment.getCreateTime());
|
vo.setCreateTime(comment.getCreateTime());
|
||||||
return vo;
|
return vo;
|
||||||
}
|
}
|
||||||
|
|
|
@ -5,12 +5,13 @@ import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
||||||
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
||||||
import com.mcwl.common.core.domain.entity.SysUser;
|
import com.mcwl.common.core.domain.entity.SysUser;
|
||||||
import com.mcwl.common.utils.SecurityUtils;
|
import com.mcwl.common.utils.SecurityUtils;
|
||||||
import com.mcwl.resource.domain.ModelImageComment;
|
import com.mcwl.resource.domain.*;
|
||||||
import com.mcwl.resource.domain.WorkFlowComment;
|
|
||||||
import com.mcwl.resource.domain.dto.WorkFlowCommentRes;
|
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.ModelImageCommentVo;
|
||||||
import com.mcwl.resource.domain.vo.WorkFlowCommentVo;
|
import com.mcwl.resource.domain.vo.WorkFlowCommentVo;
|
||||||
import com.mcwl.resource.mapper.WorkFlowCommentMapper;
|
import com.mcwl.resource.mapper.WorkFlowCommentMapper;
|
||||||
|
import com.mcwl.resource.service.WorkFlowCommentLikeService;
|
||||||
import com.mcwl.resource.service.WorkFlowCommentService;
|
import com.mcwl.resource.service.WorkFlowCommentService;
|
||||||
import com.mcwl.system.service.ISysUserService;
|
import com.mcwl.system.service.ISysUserService;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
@ -40,6 +41,9 @@ public class WorkFlowCommentServiceImpl extends ServiceImpl<WorkFlowCommentMappe
|
||||||
@Autowired
|
@Autowired
|
||||||
private WorkFlowCommentMapper workFlowCommentMapper;
|
private WorkFlowCommentMapper workFlowCommentMapper;
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private WorkFlowCommentLikeService workFlowCommentLikeService;
|
||||||
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void comment(WorkFlowCommentRes workFlowCommentRes) {
|
public void comment(WorkFlowCommentRes workFlowCommentRes) {
|
||||||
|
@ -88,8 +92,42 @@ public class WorkFlowCommentServiceImpl extends ServiceImpl<WorkFlowCommentMappe
|
||||||
.collect(Collectors.toMap(SysUser::getUserId, Function.identity()));
|
.collect(Collectors.toMap(SysUser::getUserId, Function.identity()));
|
||||||
|
|
||||||
|
|
||||||
// 4. 构建评论树结构
|
// 4. 收集所有评论ID(父评论+子评论)
|
||||||
return buildCommentTree(parentComments, childComments, userMap);
|
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,
|
private List<WorkFlowCommentVo> buildCommentTree(List<WorkFlowComment> parentComments,
|
||||||
List<WorkFlowComment> childComments,
|
List<WorkFlowComment> childComments,
|
||||||
Map<Long, SysUser> userMap) {
|
Map<Long, SysUser> userMap,
|
||||||
// 按父ID分组子评论
|
Map<Long, Integer> likeStatusMap) {
|
||||||
Map<Long, List<WorkFlowComment>> childMap = new HashMap<>();
|
Map<Long, List<WorkFlowComment>> childMap = new HashMap<>();
|
||||||
for (WorkFlowComment child : childComments) {
|
for (WorkFlowComment child : childComments) {
|
||||||
Long parentId = child.getParentId();
|
childMap.computeIfAbsent(child.getParentId(), k -> new ArrayList<>()).add(child);
|
||||||
childMap.computeIfAbsent(parentId, k -> new ArrayList<>()).add(child);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// 构建VO树
|
|
||||||
List<WorkFlowCommentVo> result = new ArrayList<>();
|
List<WorkFlowCommentVo> result = new ArrayList<>();
|
||||||
for (WorkFlowComment parent : parentComments) {
|
for (WorkFlowComment parent : parentComments) {
|
||||||
// 转换父评论VO
|
WorkFlowCommentVo parentVo = convertToVo(parent, userMap, likeStatusMap);
|
||||||
WorkFlowCommentVo parentVo = convertToVo(parent, userMap);
|
|
||||||
// 获取子评论VO列表
|
|
||||||
List<WorkFlowComment> children = childMap.getOrDefault(parent.getId(), new ArrayList<>());
|
List<WorkFlowComment> children = childMap.getOrDefault(parent.getId(), new ArrayList<>());
|
||||||
List<WorkFlowCommentVo> childVos = new ArrayList<>();
|
List<WorkFlowCommentVo> childVos = new ArrayList<>();
|
||||||
for (WorkFlowComment child : children) {
|
for (WorkFlowComment child : children) {
|
||||||
childVos.add(convertToVo(child, userMap));
|
childVos.add(convertToVo(child, userMap, likeStatusMap));
|
||||||
}
|
}
|
||||||
parentVo.setContentList(childVos);
|
parentVo.setContentList(childVos);
|
||||||
result.add(parentVo);
|
result.add(parentVo);
|
||||||
|
@ -162,7 +196,9 @@ public class WorkFlowCommentServiceImpl extends ServiceImpl<WorkFlowCommentMappe
|
||||||
/**
|
/**
|
||||||
* 转换单个评论为VO对象
|
* 转换单个评论为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());
|
SysUser user = userMap.get(comment.getUserId());
|
||||||
if (user == null) {
|
if (user == null) {
|
||||||
user = createDefaultUser(); // 处理用户信息缺失
|
user = createDefaultUser(); // 处理用户信息缺失
|
||||||
|
@ -176,6 +212,7 @@ public class WorkFlowCommentServiceImpl extends ServiceImpl<WorkFlowCommentMappe
|
||||||
vo.setContent(comment.getContent());
|
vo.setContent(comment.getContent());
|
||||||
vo.setReplyUserId(comment.getReplyUserId());
|
vo.setReplyUserId(comment.getReplyUserId());
|
||||||
vo.setLikeNum(comment.getLikeNum());
|
vo.setLikeNum(comment.getLikeNum());
|
||||||
|
vo.setIsLike(likeStatusMap.getOrDefault(comment.getId(), 0));
|
||||||
vo.setCreateTime(comment.getCreateTime());
|
vo.setCreateTime(comment.getCreateTime());
|
||||||
return vo;
|
return vo;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue