diff --git a/mcwl-admin/src/main/java/com/mcwl/web/controller/resource/WorkFlowCommentController.java b/mcwl-admin/src/main/java/com/mcwl/web/controller/resource/WorkFlowCommentController.java index 61c0a06..ce05862 100644 --- a/mcwl-admin/src/main/java/com/mcwl/web/controller/resource/WorkFlowCommentController.java +++ b/mcwl-admin/src/main/java/com/mcwl/web/controller/resource/WorkFlowCommentController.java @@ -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(); } diff --git a/mcwl-resource/src/main/java/com/mcwl/resource/domain/vo/ModelCommentVo.java b/mcwl-resource/src/main/java/com/mcwl/resource/domain/vo/ModelCommentVo.java index 04cc35f..f0fe3f0 100644 --- a/mcwl-resource/src/main/java/com/mcwl/resource/domain/vo/ModelCommentVo.java +++ b/mcwl-resource/src/main/java/com/mcwl/resource/domain/vo/ModelCommentVo.java @@ -72,6 +72,11 @@ public class ModelCommentVo { @ApiModelProperty(value = "评论点赞数") private Integer likeNum; + /** + * 是否点赞 + */ + private Integer isLike; + /** * 评论时间 */ diff --git a/mcwl-resource/src/main/java/com/mcwl/resource/domain/vo/ModelImageCommentVo.java b/mcwl-resource/src/main/java/com/mcwl/resource/domain/vo/ModelImageCommentVo.java index 6920f7c..3bfba88 100644 --- a/mcwl-resource/src/main/java/com/mcwl/resource/domain/vo/ModelImageCommentVo.java +++ b/mcwl-resource/src/main/java/com/mcwl/resource/domain/vo/ModelImageCommentVo.java @@ -72,6 +72,11 @@ public class ModelImageCommentVo { @ApiModelProperty(value = "评论点赞数") private Integer likeNum; + /** + * 是否点赞 + */ + private Integer isLike; + /** * 评论时间 */ diff --git a/mcwl-resource/src/main/java/com/mcwl/resource/domain/vo/WorkFlowCommentVo.java b/mcwl-resource/src/main/java/com/mcwl/resource/domain/vo/WorkFlowCommentVo.java index 2e65b5b..b874cb5 100644 --- a/mcwl-resource/src/main/java/com/mcwl/resource/domain/vo/WorkFlowCommentVo.java +++ b/mcwl-resource/src/main/java/com/mcwl/resource/domain/vo/WorkFlowCommentVo.java @@ -72,6 +72,11 @@ public class WorkFlowCommentVo { @ApiModelProperty(value = "评论点赞数") private Integer likeNum; + /** + * 是否点赞 + */ + private Integer isLike; + /** * 评论时间 */ diff --git a/mcwl-resource/src/main/java/com/mcwl/resource/service/impl/ModelCommentServiceImpl.java b/mcwl-resource/src/main/java/com/mcwl/resource/service/impl/ModelCommentServiceImpl.java index 056ea7e..afd854a 100644 --- a/mcwl-resource/src/main/java/com/mcwl/resource/service/impl/ModelCommentServiceImpl.java +++ b/mcwl-resource/src/main/java/com/mcwl/resource/service/impl/ModelCommentServiceImpl.java @@ -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; @@ -31,9 +33,13 @@ import java.util.stream.Collectors; public class ModelCommentServiceImpl extends ServiceImpl implements ModelCommentService { @Autowired - private ISysUserService sysUserService; + private ISysUserService sysUserService; + @Autowired - private ModelCommentMapper modelCommentMapper; + private ModelCommentMapper modelCommentMapper; + + @Autowired + private ModelCommentLikeService modelCommentLikeService; @Autowired private ModelImageMapper modelImageMapper; @@ -54,6 +60,7 @@ public class ModelCommentServiceImpl extends ServiceImpl allCommentIds = collectAllCommentIds(parentComments, childComments); + + // 5. 批量查询点赞状态(仅当用户已登录) + Map likeStatusMap = SecurityUtils.getUserId() != null ? + batchGetLikeStatus(allCommentIds, SecurityUtils.getUserId()) : + new HashMap<>(); + + // 6. 构建评论树结构(传递点赞状态) + return buildCommentTree(parentComments, childComments, userMap, likeStatusMap); + } + + /** + * 收集所有评论ID(父+子) + */ + private List collectAllCommentIds(List parentComments, List childComments) { + List commentIds = new ArrayList<>(); + parentComments.forEach(c -> commentIds.add(c.getId())); + childComments.forEach(c -> commentIds.add(c.getId())); + return commentIds; + } + + private Map batchGetLikeStatus(List commentIds, Long currentUserId) { + if (commentIds.isEmpty()) { + return Collections.emptyMap(); + } + + // 批量查询点赞状态 + List likes = modelCommentLikeService.list(new LambdaQueryWrapper() + .eq(ModelCommentLike::getUserId, currentUserId) + .in(ModelCommentLike::getModelCommentId, commentIds)); + + // 构建点赞状态映射 + return likes.stream() + .collect(Collectors.toMap(ModelCommentLike::getModelCommentId, like -> 1)); } /** @@ -134,25 +174,21 @@ public class ModelCommentServiceImpl extends ServiceImpl buildCommentTree(List parentComments, - List childComments, - Map userMap) { - // 按父ID分组子评论 + List childComments, + Map userMap, + Map likeStatusMap) { Map> 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 result = new ArrayList<>(); for (ModelComment parent : parentComments) { - // 转换父评论VO - ModelCommentVo parentVo = convertToVo(parent, userMap); - // 获取子评论VO列表 + ModelCommentVo parentVo = convertToVo(parent, userMap, likeStatusMap); List children = childMap.getOrDefault(parent.getId(), new ArrayList<>()); List 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 userMap) { + private ModelCommentVo convertToVo(ModelComment comment, + Map userMap, + Map 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 - */ - private List getContentList(Long modelCommentId) { - List modelCommentVoList = new ArrayList<>(); - if (Objects.isNull(modelCommentId)) { - return modelCommentVoList; - } - - // 查询子评论 - LambdaQueryWrapper lqw = new LambdaQueryWrapper() - .eq(ModelComment::getParentId, modelCommentId) - .orderByDesc(ModelComment::getCreateTime); - - List 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; - } - - } diff --git a/mcwl-resource/src/main/java/com/mcwl/resource/service/impl/ModelImageCommentServiceImpl.java b/mcwl-resource/src/main/java/com/mcwl/resource/service/impl/ModelImageCommentServiceImpl.java index 8889b2b..75200d3 100644 --- a/mcwl-resource/src/main/java/com/mcwl/resource/service/impl/ModelImageCommentServiceImpl.java +++ b/mcwl-resource/src/main/java/com/mcwl/resource/service/impl/ModelImageCommentServiceImpl.java @@ -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 allCommentIds = collectAllCommentIds(parentComments, childComments); + + // 5. 批量查询点赞状态(仅当用户已登录) + Map likeStatusMap = SecurityUtils.getUserId() != null ? + batchGetLikeStatus(allCommentIds, SecurityUtils.getUserId()) : + new HashMap<>(); + + // 6. 构建评论树结构(传递点赞状态) + return buildCommentTree(parentComments, childComments, userMap, likeStatusMap); + + } + + /** + * 收集所有评论ID(父+子) + */ + private List collectAllCommentIds(List parentComments, List childComments) { + List commentIds = new ArrayList<>(); + parentComments.forEach(c -> commentIds.add(c.getId())); + childComments.forEach(c -> commentIds.add(c.getId())); + return commentIds; + } + + private Map batchGetLikeStatus(List commentIds, Long currentUserId) { + if (commentIds.isEmpty()) { + return Collections.emptyMap(); + } + + // 批量查询点赞状态 + List likes = modelImageCommentLikeService.list(new LambdaQueryWrapper() + .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 buildCommentTree(List parentComments, List childComments, - Map userMap) { - // 按父ID分组子评论 + Map userMap, + Map likeStatusMap) { Map> 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 result = new ArrayList<>(); for (ModelImageComment parent : parentComments) { - // 转换父评论VO - ModelImageCommentVo parentVo = convertToVo(parent, userMap); - // 获取子评论VO列表 + ModelImageCommentVo parentVo = convertToVo(parent, userMap, likeStatusMap); List children = childMap.getOrDefault(parent.getId(), new ArrayList<>()); List 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 userMap) { + private ModelImageCommentVo convertToVo(ModelImageComment comment, + Map userMap, + Map likeStatusMap) { SysUser user = userMap.get(comment.getUserId()); if (user == null) { user = createDefaultUser(); // 处理用户信息缺失 @@ -171,6 +206,7 @@ public class ModelImageCommentServiceImpl extends ServiceImpl allCommentIds = collectAllCommentIds(parentComments, childComments); + + // 5. 批量查询点赞状态(仅当用户已登录) + Map likeStatusMap = SecurityUtils.getUserId() != null ? + batchGetLikeStatus(allCommentIds, SecurityUtils.getUserId()) : + new HashMap<>(); + + // 6. 构建评论树结构(传递点赞状态) + return buildCommentTree(parentComments, childComments, userMap, likeStatusMap); + } + + + /** + * 收集所有评论ID(父+子) + */ + private List collectAllCommentIds(List parentComments, List childComments) { + List commentIds = new ArrayList<>(); + parentComments.forEach(c -> commentIds.add(c.getId())); + childComments.forEach(c -> commentIds.add(c.getId())); + return commentIds; + } + + private Map batchGetLikeStatus(List commentIds, Long currentUserId) { + if (commentIds.isEmpty()) { + return Collections.emptyMap(); + } + + // 批量查询点赞状态 + List likes = workFlowCommentLikeService.list(new LambdaQueryWrapper() + .eq(WorkFlowCommentLike::getUserId, currentUserId) + .in(WorkFlowCommentLike::getWorkFlowCommentId, commentIds)); + + // 构建点赞状态映射 + return likes.stream() + .collect(Collectors.toMap(WorkFlowCommentLike::getWorkFlowCommentId, like -> 1)); } /** @@ -133,25 +171,21 @@ public class WorkFlowCommentServiceImpl extends ServiceImpl buildCommentTree(List parentComments, - List childComments, - Map userMap) { - // 按父ID分组子评论 + List childComments, + Map userMap, + Map likeStatusMap) { Map> 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 result = new ArrayList<>(); for (WorkFlowComment parent : parentComments) { - // 转换父评论VO - WorkFlowCommentVo parentVo = convertToVo(parent, userMap); - // 获取子评论VO列表 + WorkFlowCommentVo parentVo = convertToVo(parent, userMap, likeStatusMap); List children = childMap.getOrDefault(parent.getId(), new ArrayList<>()); List 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 userMap) { + private WorkFlowCommentVo convertToVo(WorkFlowComment comment, + Map userMap, + Map likeStatusMap) { SysUser user = userMap.get(comment.getUserId()); if (user == null) { user = createDefaultUser(); // 处理用户信息缺失 @@ -176,6 +212,7 @@ public class WorkFlowCommentServiceImpl extends ServiceImpl