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

master
yang 2025-02-12 16:59:30 +08:00
parent 7c3621ac2d
commit e679fd847e
17 changed files with 522 additions and 187 deletions

View File

@ -3,6 +3,7 @@ package com.mcwl.web.controller.resource;
import com.mcwl.common.annotation.RepeatSubmit; import com.mcwl.common.annotation.RepeatSubmit;
import com.mcwl.common.core.domain.AjaxResult; import com.mcwl.common.core.domain.AjaxResult;
import com.mcwl.resource.domain.ModelComment; import com.mcwl.resource.domain.ModelComment;
import com.mcwl.resource.domain.dto.ModelCommentRes;
import com.mcwl.resource.domain.vo.ModelCommentVo; import com.mcwl.resource.domain.vo.ModelCommentVo;
import com.mcwl.resource.service.ModelCommentLikeService; import com.mcwl.resource.service.ModelCommentLikeService;
import com.mcwl.resource.service.ModelCommentService; import com.mcwl.resource.service.ModelCommentService;
@ -12,6 +13,7 @@ import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*; import org.springframework.web.bind.annotation.*;
import javax.validation.Valid;
import javax.validation.constraints.NotNull; import javax.validation.constraints.NotNull;
import java.util.List; import java.util.List;
@ -42,8 +44,8 @@ public class ModelCommentController {
*/ */
@ApiOperation(value = "模型点赞/取消") @ApiOperation(value = "模型点赞/取消")
@RepeatSubmit @RepeatSubmit
@GetMapping("/modelLike/{modelId}") @GetMapping("/modelLike")
public AjaxResult like(@PathVariable Long modelId) { public AjaxResult like(@Valid @NotNull(message = "模型id不能为空") Long modelId) {
modelLikeService.like(modelId); modelLikeService.like(modelId);
return AjaxResult.success(); return AjaxResult.success();
} }
@ -54,8 +56,8 @@ public class ModelCommentController {
*/ */
@ApiOperation(value = "模型评论发布") @ApiOperation(value = "模型评论发布")
@PostMapping("/comment") @PostMapping("/comment")
public AjaxResult comment(@RequestBody ModelComment modelComment) { public AjaxResult comment(@Valid @RequestBody ModelCommentRes modelCommentRes) {
modelCommentService.comment(modelComment); modelCommentService.comment(modelCommentRes);
return AjaxResult.success(); return AjaxResult.success();
} }
@ -64,8 +66,8 @@ public class ModelCommentController {
*/ */
@ApiOperation(value = "模型评论点赞/取消") @ApiOperation(value = "模型评论点赞/取消")
@RepeatSubmit @RepeatSubmit
@GetMapping("/commentLike/{commentId}") @GetMapping("/commentLike")
public AjaxResult commentLike(@PathVariable Long commentId) { public AjaxResult commentLike(@Valid @NotNull(message = "评论id不能为空") Long commentId) {
modelCommentLikeService.like(commentId); modelCommentLikeService.like(commentId);
return AjaxResult.error(); return AjaxResult.error();
} }
@ -76,8 +78,8 @@ public class ModelCommentController {
* *
*/ */
@ApiOperation(value = "获取模型评论") @ApiOperation(value = "获取模型评论")
@GetMapping("/comment/{modelId}") @GetMapping("/comment")
public AjaxResult getComment(@PathVariable @NotNull(message = "模型id不能为空") Long modelId) { public AjaxResult getComment(@Valid @NotNull(message = "模型id不能为空") Long modelId) {
List<ModelCommentVo> modelCommentList = modelCommentService.getComment(modelId); List<ModelCommentVo> modelCommentList = modelCommentService.getComment(modelId);
return AjaxResult.success(modelCommentList); return AjaxResult.success(modelCommentList);
} }
@ -87,8 +89,8 @@ public class ModelCommentController {
* *
*/ */
@ApiOperation(value = "删除模型评论") @ApiOperation(value = "删除模型评论")
@GetMapping("/commentDelete/{commentId}") @GetMapping("/commentDelete")
public AjaxResult commentDelete(@PathVariable @NotNull(message = "评论id不能为空") Long commentId) { public AjaxResult commentDelete(@Valid @NotNull(message = "评论id不能为空") Long commentId) {
modelCommentService.removeById(commentId); modelCommentService.removeById(commentId);
return AjaxResult.success(); return AjaxResult.success();
} }

View File

@ -2,6 +2,7 @@ package com.mcwl.web.controller.resource;
import com.mcwl.common.core.domain.AjaxResult; import com.mcwl.common.core.domain.AjaxResult;
import com.mcwl.resource.domain.WorkFlowComment; import com.mcwl.resource.domain.WorkFlowComment;
import com.mcwl.resource.domain.dto.WorkFlowCommentRes;
import com.mcwl.resource.domain.vo.WorkFlowCommentVo; import com.mcwl.resource.domain.vo.WorkFlowCommentVo;
import com.mcwl.resource.service.WorkFlowCommentLikeService; import com.mcwl.resource.service.WorkFlowCommentLikeService;
import com.mcwl.resource.service.WorkFlowCommentService; import com.mcwl.resource.service.WorkFlowCommentService;
@ -11,6 +12,7 @@ import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*; import org.springframework.web.bind.annotation.*;
import javax.validation.Valid;
import javax.validation.constraints.NotNull; import javax.validation.constraints.NotNull;
import java.util.List; import java.util.List;
@ -53,8 +55,8 @@ public class WorkFlowCommentController {
*/ */
@ApiOperation(value = "工作流评论发布") @ApiOperation(value = "工作流评论发布")
@PostMapping("/comment") @PostMapping("/comment")
public AjaxResult comment(@RequestBody WorkFlowComment modelComment) { public AjaxResult comment(@RequestBody WorkFlowCommentRes workFlowCommentRes) {
workFlowCommentService.comment(modelComment); workFlowCommentService.comment(workFlowCommentRes);
return AjaxResult.success(); return AjaxResult.success();
} }
@ -62,8 +64,8 @@ public class WorkFlowCommentController {
* / * /
*/ */
@ApiOperation(value = "工作流评论点赞/取消") @ApiOperation(value = "工作流评论点赞/取消")
@GetMapping("/commentLike/{commentId}") @GetMapping("/commentLike")
public AjaxResult commentLike(@PathVariable Long commentId) { public AjaxResult commentLike(@Valid @NotNull(message = "评论id不能为空") Long commentId) {
workFlowCommentLikeService.like(commentId); workFlowCommentLikeService.like(commentId);
return AjaxResult.error(); return AjaxResult.error();
} }
@ -74,9 +76,9 @@ public class WorkFlowCommentController {
* *
*/ */
@ApiOperation(value = "获取工作流评论") @ApiOperation(value = "获取工作流评论")
@GetMapping("/comment/{modelId}") @GetMapping("/comment")
public AjaxResult getComment(@PathVariable @NotNull(message = "模型id不能为空") Long modelId) { public AjaxResult getComment(@Valid @NotNull(message = "模型id不能为空") Long commentId) {
List<WorkFlowCommentVo> modelCommentList = workFlowCommentService.getComment(modelId); List<WorkFlowCommentVo> modelCommentList = workFlowCommentService.getComment(commentId);
return AjaxResult.success(modelCommentList); return AjaxResult.success(modelCommentList);
} }
@ -85,8 +87,8 @@ public class WorkFlowCommentController {
* *
*/ */
@ApiOperation(value = "删除工作流评论") @ApiOperation(value = "删除工作流评论")
@GetMapping("/commentDelete/{commentId}") @GetMapping("/commentDelete")
public AjaxResult commentDelete(@PathVariable @NotNull(message = "评论id不能为空") Long commentId) { public AjaxResult commentDelete(@Valid @NotNull(message = "评论id不能为空") Long commentId) {
workFlowCommentService.removeById(commentId); workFlowCommentService.removeById(commentId);
return AjaxResult.success(); return AjaxResult.success();
} }

View File

@ -36,8 +36,7 @@ public class MyMetaObjectHandler implements MetaObjectHandler {
@Override @Override
public void updateFill(MetaObject metaObject) { public void updateFill(MetaObject metaObject) {
try { try {
String username = SecurityUtils.getUsername(); this.setFieldValByName("updateBy", SecurityUtils.getUsername(), metaObject);
this.setFieldValByName("updateBy", username, metaObject);
} catch (Exception e) { } catch (Exception e) {
this.setFieldValByName("updateBy", "", metaObject); this.setFieldValByName("updateBy", "", metaObject);
} }

View File

@ -48,6 +48,12 @@ public class ModelComment extends BaseEntity {
@ApiModelProperty(value = "父评论id") @ApiModelProperty(value = "父评论id")
private Long parentId; private Long parentId;
/**
*
*/
@ApiModelProperty(value = "回复人")
private Long replyUserId;
/** /**
* *
*/ */

View File

@ -45,6 +45,12 @@ public class ModelImageComment extends BaseEntity {
@ApiModelProperty(value = "父评论id") @ApiModelProperty(value = "父评论id")
private Long parentId; private Long parentId;
/**
*
*/
@ApiModelProperty(value = "回复人")
private Long replyUserId;
/** /**
* *
*/ */

View File

@ -47,6 +47,12 @@ public class WorkFlowComment extends BaseEntity {
@ApiModelProperty ("父评论id") @ApiModelProperty ("父评论id")
private Long parentId; private Long parentId;
/**
*
*/
@ApiModelProperty ("回复人")
private Long replyUserId;
/** /**
* *
*/ */

View File

@ -0,0 +1,39 @@
package com.mcwl.resource.domain.dto;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;
@ApiModel(description = "模型评论请求参数")
@Data
public class ModelCommentRes {
/**
* id
*/
@ApiModelProperty(value = "模型id", required = true)
@NotNull(message = "模型id不能为空")
private Long workFlowId;
/**
*
*/
@ApiModelProperty(value = "评论内容", required = true)
@NotBlank(message = "评论内容不能为空")
private String content;
/**
* id
*/
@ApiModelProperty(value = "父评论id")
private Long parentId;
/**
*
*/
@ApiModelProperty(value = "回复人")
private Long replyUserId;
}

View File

@ -29,4 +29,11 @@ public class ModelImageCommentRes {
@ApiModelProperty(value = "父评论id") @ApiModelProperty(value = "父评论id")
private Long parentId; private Long parentId;
/**
*
*/
@ApiModelProperty(value = "回复人")
private Long replyUserId;
} }

View File

@ -0,0 +1,39 @@
package com.mcwl.resource.domain.dto;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;
@ApiModel(description = "工作流评论请求参数")
@Data
public class WorkFlowCommentRes {
/**
* id
*/
@ApiModelProperty(value = "工作流id", required = true)
@NotNull(message = "工作流id不能为空")
private Long workFlowId;
/**
*
*/
@ApiModelProperty(value = "评论内容", required = true)
@NotBlank(message = "评论内容不能为空")
private String content;
/**
* id
*/
@ApiModelProperty(value = "父评论id")
private Long parentId;
/**
*
*/
@ApiModelProperty(value = "回复人")
private Long replyUserId;
}

View File

@ -54,6 +54,12 @@ public class ModelCommentVo {
@ApiModelProperty(value = "评论内容") @ApiModelProperty(value = "评论内容")
private String content; private String content;
/**
*
*/
@ApiModelProperty(value = "回复人")
private Long replyUserId;
/** /**
* *
*/ */

View File

@ -53,6 +53,13 @@ public class ModelImageCommentVo {
@ApiModelProperty(value = "评论内容") @ApiModelProperty(value = "评论内容")
private String content; private String content;
/**
*
*/
@ApiModelProperty(value = "回复人")
private Long replyUserId;
/** /**
* *
*/ */

View File

@ -54,6 +54,12 @@ public class WorkFlowCommentVo {
@ApiModelProperty(value = "评论内容") @ApiModelProperty(value = "评论内容")
private String content; private String content;
/**
*
*/
@ApiModelProperty(value = "回复人")
private Long replyUserId;
/** /**
* *
*/ */

View File

@ -2,6 +2,7 @@ package com.mcwl.resource.service;
import com.baomidou.mybatisplus.extension.service.IService; import com.baomidou.mybatisplus.extension.service.IService;
import com.mcwl.resource.domain.ModelComment; import com.mcwl.resource.domain.ModelComment;
import com.mcwl.resource.domain.dto.ModelCommentRes;
import com.mcwl.resource.domain.vo.ModelCommentVo; import com.mcwl.resource.domain.vo.ModelCommentVo;
import com.mcwl.resource.domain.vo.ModelImageCommentVo; import com.mcwl.resource.domain.vo.ModelImageCommentVo;
@ -17,7 +18,7 @@ import java.util.List;
* @Date2025/1/12 11:58 * @Date2025/1/12 11:58
*/ */
public interface ModelCommentService extends IService<ModelComment> { public interface ModelCommentService extends IService<ModelComment> {
void comment(ModelComment modelComment); void comment(ModelCommentRes modelCommentRes);
/** /**
* *

View File

@ -2,6 +2,7 @@ package com.mcwl.resource.service;
import com.baomidou.mybatisplus.extension.service.IService; import com.baomidou.mybatisplus.extension.service.IService;
import com.mcwl.resource.domain.WorkFlowComment; import com.mcwl.resource.domain.WorkFlowComment;
import com.mcwl.resource.domain.dto.WorkFlowCommentRes;
import com.mcwl.resource.domain.vo.WorkFlowCommentVo; import com.mcwl.resource.domain.vo.WorkFlowCommentVo;
import java.util.List; import java.util.List;
@ -16,7 +17,7 @@ import java.util.List;
* @Date2025/1/12 11:58 * @Date2025/1/12 11:58
*/ */
public interface WorkFlowCommentService extends IService<WorkFlowComment> { public interface WorkFlowCommentService extends IService<WorkFlowComment> {
void comment(WorkFlowComment modelComment); void comment(WorkFlowCommentRes workFlowCommentRes);
/** /**
* *

View File

@ -7,6 +7,7 @@ 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.ModelImageComment; import com.mcwl.resource.domain.ModelImageComment;
import com.mcwl.resource.domain.dto.ModelCommentRes;
import com.mcwl.resource.domain.dto.ModelImageCommentRes; import com.mcwl.resource.domain.dto.ModelImageCommentRes;
import com.mcwl.resource.domain.vo.ModelCommentVo; import com.mcwl.resource.domain.vo.ModelCommentVo;
import com.mcwl.resource.domain.vo.ModelImageCommentVo; import com.mcwl.resource.domain.vo.ModelImageCommentVo;
@ -19,10 +20,9 @@ import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import javax.validation.constraints.NotNull; import javax.validation.constraints.NotNull;
import java.util.ArrayList; import java.util.*;
import java.util.Date; import java.util.function.Function;
import java.util.List; import java.util.stream.Collectors;
import java.util.Objects;
/** /**
* *
@ -39,8 +39,8 @@ public class ModelCommentServiceImpl extends ServiceImpl<ModelCommentMapper, Mod
private ModelImageMapper modelImageMapper; private ModelImageMapper modelImageMapper;
@Override @Override
public void comment(ModelComment modelComment) { public void comment(ModelCommentRes modelCommentRes) {
Long parentId = modelComment.getParentId(); Long parentId = modelCommentRes.getParentId();
if (Objects.nonNull(parentId)) { if (Objects.nonNull(parentId)) {
ModelComment mic = modelCommentMapper.selectById(parentId); ModelComment mic = modelCommentMapper.selectById(parentId);
@ -48,11 +48,9 @@ public class ModelCommentServiceImpl extends ServiceImpl<ModelCommentMapper, Mod
return; return;
} }
} }
ModelComment modelComment = new ModelComment();
BeanUtil.copyProperties(modelCommentRes, modelComment);
modelComment.setUserId(SecurityUtils.getUserId()); modelComment.setUserId(SecurityUtils.getUserId());
modelComment.setCreateBy(SecurityUtils.getUsername());
modelComment.setCreateTime(new Date());
modelComment.setUpdateBy(SecurityUtils.getUsername());
modelComment.setUpdateTime(new Date());
modelCommentMapper.insert(modelComment); modelCommentMapper.insert(modelComment);
} }
@ -64,21 +62,133 @@ public class ModelCommentServiceImpl extends ServiceImpl<ModelCommentMapper, Mod
*/ */
@Override @Override
public List<ModelCommentVo> getComment(Long imageId) { public List<ModelCommentVo> getComment(Long imageId) {
List<ModelCommentVo> modelCommentVoList = new ArrayList<>();
// 查询所有父评论 // 1. 查询所有父评论
LambdaQueryWrapper<ModelComment> lqw = new LambdaQueryWrapper<>(); List<ModelComment> parentComments = baseMapper.selectList(
lqw.eq(ModelComment::getModelId, imageId) new LambdaQueryWrapper<ModelComment>()
.eq(ModelComment::getModelId, imageId)
.isNull(ModelComment::getParentId) .isNull(ModelComment::getParentId)
.orderByDesc(ModelComment::getCreateTime); .orderByAsc(ModelComment::getCreateTime)
// 添加父评论 );
List<ModelComment> modelCommentList = modelCommentMapper.selectList(lqw);
for (ModelComment modelComment : modelCommentList) { // 无评论直接返回空列表
ModelCommentVo modelCommentVo = getModelCommentVo(modelComment); if (parentComments.isEmpty()) {
modelCommentVoList.add(modelCommentVo); return new ArrayList<>();
} }
return modelCommentVoList; // 2. 收集所有用户ID父评论+子评论)
List<Long> userIds = collectUserIds(parentComments); // 收集父评论用户ID
List<ModelComment> childComments = getChildComments(parentComments); // 查询子评论
for (ModelComment child : childComments) {
userIds.add(child.getUserId()); // 收集子评论用户ID
}
// 3. 批量查询用户信息避免N+1查询
Map<Long, SysUser> userMap = userIds.isEmpty() ?
new HashMap<>() :
sysUserService.listByIds(userIds).stream()
.collect(Collectors.toMap(SysUser::getUserId, Function.identity()));
// 4. 构建评论树结构
return buildCommentTree(parentComments, childComments, userMap);
}
/**
* ID
*
* @param parentComments
* @return ID
*/
private List<Long> collectUserIds(List<ModelComment> parentComments) {
List<Long> userIds = new ArrayList<>();
for (ModelComment comment : parentComments) {
userIds.add(comment.getUserId());
}
return userIds;
}
/**
*
*
* @param parentComments
* @return
*/
private List<ModelComment> getChildComments(List<ModelComment> parentComments) {
if (parentComments.isEmpty()) {
return new ArrayList<>();
}
// 提取父评论ID集合
List<Long> parentIds = parentComments.stream()
.map(ModelComment::getId)
.collect(Collectors.toList());
// 查询子评论
return baseMapper.selectList(
new LambdaQueryWrapper<ModelComment>()
.in(ModelComment::getParentId, parentIds)
.orderByAsc(ModelComment::getCreateTime)
);
}
/**
* VO
*/
private List<ModelCommentVo> buildCommentTree(List<ModelComment> parentComments,
List<ModelComment> childComments,
Map<Long, SysUser> userMap) {
// 按父ID分组子评论
Map<Long, List<ModelComment>> childMap = new HashMap<>();
for (ModelComment child : childComments) {
Long parentId = child.getParentId();
childMap.computeIfAbsent(parentId, k -> new ArrayList<>()).add(child);
}
// 构建VO树
List<ModelCommentVo> result = new ArrayList<>();
for (ModelComment parent : parentComments) {
// 转换父评论VO
ModelCommentVo parentVo = convertToVo(parent, userMap);
// 获取子评论VO列表
List<ModelComment> children = childMap.getOrDefault(parent.getId(), new ArrayList<>());
List<ModelCommentVo> childVos = new ArrayList<>();
for (ModelComment child : children) {
childVos.add(convertToVo(child, userMap));
}
parentVo.setContentList(childVos);
result.add(parentVo);
}
return result;
}
/**
* VO
*/
private ModelCommentVo convertToVo(ModelComment comment, Map<Long, SysUser> userMap) {
SysUser user = userMap.get(comment.getUserId());
if (user == null) {
user = createDefaultUser(); // 处理用户信息缺失
}
ModelCommentVo vo = new ModelCommentVo();
vo.setUserId(comment.getUserId());
vo.setUserName(user.getUserName());
vo.setUserAvatar(user.getAvatar());
vo.setCommentId(comment.getId());
vo.setContent(comment.getContent());
vo.setReplyUserId(comment.getReplyUserId());
vo.setLikeNum(comment.getLikeNum());
vo.setCreateTime(comment.getCreateTime());
return vo;
}
/**
*
*/
private SysUser createDefaultUser() {
SysUser defaultUser = new SysUser();
defaultUser.setUserName("匿名用户");
defaultUser.setAvatar("/default-avatar.png");
return defaultUser;
} }

View File

@ -18,9 +18,9 @@ import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import javax.validation.constraints.NotNull; import javax.validation.constraints.NotNull;
import java.util.ArrayList; import java.util.*;
import java.util.List; import java.util.function.Function;
import java.util.Objects; import java.util.stream.Collectors;
/** /**
* *
@ -57,88 +57,133 @@ public class ModelImageCommentServiceImpl extends ServiceImpl<ModelImageCommentM
*/ */
@Override @Override
public List<ModelImageCommentVo> getComment(Long imageId) { public List<ModelImageCommentVo> getComment(Long imageId) {
// 1. 查询所有父评论
List<ModelImageCommentVo> modelImageCommentVoList = new ArrayList<>(); List<ModelImageComment> parentComments = baseMapper.selectList(
new LambdaQueryWrapper<ModelImageComment>()
// 查询所有父评论 .eq(ModelImageComment::getModelImageId, imageId)
LambdaQueryWrapper<ModelImageComment> lqw = new LambdaQueryWrapper<>();
lqw.eq(ModelImageComment::getModelImageId, imageId)
.isNull(ModelImageComment::getParentId) .isNull(ModelImageComment::getParentId)
.orderByDesc(ModelImageComment::getCreateTime); .orderByAsc(ModelImageComment::getCreateTime)
// 添加父评论 );
List<ModelImageComment> modelImageCommentList = baseMapper.selectList(lqw);
for (ModelImageComment modelImageComment : modelImageCommentList) { // 无评论直接返回空列表
ModelImageCommentVo modelImageCommentVo = getModelImageCommentVo(modelImageComment); if (parentComments.isEmpty()) {
modelImageCommentVoList.add(modelImageCommentVo); return new ArrayList<>();
} }
return modelImageCommentVoList; // 2. 收集所有用户ID父评论+子评论)
List<Long> userIds = collectUserIds(parentComments); // 收集父评论用户ID
List<ModelImageComment> childComments = getChildComments(parentComments); // 查询子评论
for (ModelImageComment child : childComments) {
userIds.add(child.getUserId()); // 收集子评论用户ID
} }
// 3. 批量查询用户信息避免N+1查询
Map<Long, SysUser> userMap = userIds.isEmpty() ?
new HashMap<>() :
sysUserService.listByIds(userIds).stream()
.collect(Collectors.toMap(SysUser::getUserId, Function.identity()));
// 4. 构建评论树结构
return buildCommentTree(parentComments, childComments, userMap);
}
/** /**
* ModelImageCommentVo * ID
* *
* @param modelImageComment * @param parentComments
* @return ModelImageCommentVo * @return ID
*/ */
@NotNull private List<Long> collectUserIds(List<ModelImageComment> parentComments) {
private ModelImageCommentVo getModelImageCommentVo(ModelImageComment modelImageComment) { List<Long> userIds = new ArrayList<>();
Long userId = modelImageComment.getUserId(); for (ModelImageComment comment : parentComments) {
SysUser sysUser = sysUserService.selectUserById(userId); userIds.add(comment.getUserId());
}
// 构建ModelImageCommentVo对象 return userIds;
ModelImageCommentVo modelImageCommentVo = new ModelImageCommentVo();
modelImageCommentVo.setUserId(userId);
modelImageCommentVo.setUserName(sysUser.getUserName());
modelImageCommentVo.setUserAvatar(sysUser.getAvatar());
modelImageCommentVo.setCommentId(modelImageComment.getId());
modelImageCommentVo.setContent(modelImageComment.getContent());
// 查询子评论
modelImageCommentVo.setContentList(getContentList(modelImageComment.getId()));
modelImageCommentVo.setLikeNum(modelImageComment.getLikeNum());
modelImageCommentVo.setCreateTime(modelImageComment.getCreateTime());
return modelImageCommentVo;
} }
/** /**
* *
* *
* @param modelImageCommentId id * @param parentComments
* @return List<ModelImageCommentVo> * @return
*/ */
private List<ModelImageCommentVo> getContentList(Long modelImageCommentId) { private List<ModelImageComment> getChildComments(List<ModelImageComment> parentComments) {
List<ModelImageCommentVo> modelImageCommentVoList = new ArrayList<>(); if (parentComments.isEmpty()) {
if (Objects.isNull(modelImageCommentId)) { return new ArrayList<>();
return modelImageCommentVoList;
} }
// 提取父评论ID集合
List<Long> parentIds = parentComments.stream()
.map(ModelImageComment::getId)
.collect(Collectors.toList());
// 查询子评论 // 查询子评论
LambdaQueryWrapper<ModelImageComment> lqw = new LambdaQueryWrapper<ModelImageComment>() return baseMapper.selectList(
.eq(ModelImageComment::getParentId, modelImageCommentId) new LambdaQueryWrapper<ModelImageComment>()
.orderByDesc(ModelImageComment::getCreateTime); .in(ModelImageComment::getParentId, parentIds)
.orderByAsc(ModelImageComment::getCreateTime)
List<ModelImageComment> modelImageCommentList = baseMapper.selectList(lqw); );
for (ModelImageComment modelImageComment : modelImageCommentList) {
Long userId = modelImageComment.getUserId();
SysUser sysUser = sysUserService.selectUserById(userId);
ModelImageCommentVo modelImageCommentVo = new ModelImageCommentVo();
modelImageCommentVo.setUserId(userId);
modelImageCommentVo.setUserName(sysUser.getUserName());
modelImageCommentVo.setUserAvatar(sysUser.getAvatar());
modelImageCommentVo.setCommentId(modelImageComment.getId());
modelImageCommentVo.setContent(modelImageComment.getContent());
modelImageCommentVo.setLikeNum(modelImageComment.getLikeNum());
modelImageCommentVo.setCreateTime(modelImageComment.getCreateTime());
modelImageCommentVoList.add(modelImageCommentVo);
}
return modelImageCommentVoList;
} }
/**
* VO
*/
private List<ModelImageCommentVo> buildCommentTree(List<ModelImageComment> parentComments,
List<ModelImageComment> childComments,
Map<Long, SysUser> userMap) {
// 按父ID分组子评论
Map<Long, List<ModelImageComment>> childMap = new HashMap<>();
for (ModelImageComment child : childComments) {
Long parentId = child.getParentId();
childMap.computeIfAbsent(parentId, k -> new ArrayList<>()).add(child);
}
// 构建VO树
List<ModelImageCommentVo> result = new ArrayList<>();
for (ModelImageComment parent : parentComments) {
// 转换父评论VO
ModelImageCommentVo parentVo = convertToVo(parent, userMap);
// 获取子评论VO列表
List<ModelImageComment> children = childMap.getOrDefault(parent.getId(), new ArrayList<>());
List<ModelImageCommentVo> childVos = new ArrayList<>();
for (ModelImageComment child : children) {
childVos.add(convertToVo(child, userMap));
}
parentVo.setContentList(childVos);
result.add(parentVo);
}
return result;
}
/**
* VO
*/
private ModelImageCommentVo convertToVo(ModelImageComment comment, Map<Long, SysUser> userMap) {
SysUser user = userMap.get(comment.getUserId());
if (user == null) {
user = createDefaultUser(); // 处理用户信息缺失
}
ModelImageCommentVo vo = new ModelImageCommentVo();
vo.setUserId(comment.getUserId());
vo.setUserName(user.getUserName());
vo.setUserAvatar(user.getAvatar());
vo.setCommentId(comment.getId());
vo.setContent(comment.getContent());
vo.setReplyUserId(comment.getReplyUserId());
vo.setLikeNum(comment.getLikeNum());
vo.setCreateTime(comment.getCreateTime());
return vo;
}
/**
*
*/
private SysUser createDefaultUser() {
SysUser defaultUser = new SysUser();
defaultUser.setUserName("匿名用户");
defaultUser.setAvatar("/default-avatar.png");
return defaultUser;
}
} }

View File

@ -1,10 +1,14 @@
package com.mcwl.resource.service.impl; package com.mcwl.resource.service.impl;
import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; 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.WorkFlowComment; import com.mcwl.resource.domain.WorkFlowComment;
import com.mcwl.resource.domain.dto.WorkFlowCommentRes;
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.WorkFlowCommentService; import com.mcwl.resource.service.WorkFlowCommentService;
@ -13,13 +17,13 @@ import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import javax.validation.constraints.NotNull; import javax.validation.constraints.NotNull;
import java.util.ArrayList; import java.util.*;
import java.util.Date; import java.util.function.Function;
import java.util.List; import java.util.stream.Collectors;
import java.util.Objects;
/** /**
* *
*
* @AuthorChenYan * @AuthorChenYan
* @ProjectMcWl * @ProjectMcWl
* @Packagecom.mcwl.resource.service.impl * @Packagecom.mcwl.resource.service.impl
@ -38,105 +42,154 @@ public class WorkFlowCommentServiceImpl extends ServiceImpl<WorkFlowCommentMappe
@Override @Override
public void comment(WorkFlowComment workFlowComment) { public void comment(WorkFlowCommentRes workFlowCommentRes) {
Long parentId = workFlowComment.getParentId(); Long parentId = workFlowCommentRes.getParentId();
if (parentId != null){ if (parentId != null) {
WorkFlowComment mic = workFlowCommentMapper.selectById(parentId); WorkFlowComment mic = workFlowCommentMapper.selectById(parentId);
if (Objects.nonNull(parentId) && Objects.isNull(mic)) { if (Objects.isNull(mic)) {
return; return;
} }
} }
WorkFlowComment workFlowComment = new WorkFlowComment();
BeanUtil.copyProperties(workFlowCommentRes, workFlowComment);
workFlowComment.setUserId(SecurityUtils.getUserId()); workFlowComment.setUserId(SecurityUtils.getUserId());
workFlowComment.setCreateBy(SecurityUtils.getUsername());
workFlowComment.setUpdateBy(SecurityUtils.getUsername());
workFlowComment.setUpdateTime(new Date());
workFlowCommentMapper.insert(workFlowComment); workFlowCommentMapper.insert(workFlowComment);
} }
@Override @Override
public List<WorkFlowCommentVo> getComment(Long imageId) { public List<WorkFlowCommentVo> getComment(Long imageId) {
List<WorkFlowCommentVo> workFlowCommentVos = new ArrayList<>();
// 查询所有父评论 // 查询所有父评论
LambdaQueryWrapper<WorkFlowComment> lqw = new LambdaQueryWrapper<>(); List<WorkFlowComment> parentComments = baseMapper.selectList(
lqw.eq(WorkFlowComment::getWorkFlowId, imageId) new LambdaQueryWrapper<WorkFlowComment>()
.eq(WorkFlowComment::getWorkFlowId, imageId)
.isNull(WorkFlowComment::getParentId) .isNull(WorkFlowComment::getParentId)
.orderByDesc(WorkFlowComment::getCreateTime); .orderByAsc(WorkFlowComment::getCreateTime)
// 添加父评论 );
List<WorkFlowComment> workFlowComments = workFlowCommentMapper.selectList(lqw);
for (WorkFlowComment workFlowComment : workFlowComments) { // 无评论直接返回空列表
WorkFlowCommentVo workFlowCommentVo = getModelCommentVo(workFlowComment); if (parentComments.isEmpty()) {
workFlowCommentVos.add(workFlowCommentVo); return new ArrayList<>();
} }
return workFlowCommentVos; // 2. 收集所有用户ID父评论+子评论)
List<Long> userIds = collectUserIds(parentComments); // 收集父评论用户ID
List<WorkFlowComment> childComments = getChildComments(parentComments); // 查询子评论
for (WorkFlowComment child : childComments) {
userIds.add(child.getUserId()); // 收集子评论用户ID
} }
// 3. 批量查询用户信息避免N+1查询
Map<Long, SysUser> userMap = userIds.isEmpty() ?
new HashMap<>() :
sysUserService.listByIds(userIds).stream()
.collect(Collectors.toMap(SysUser::getUserId, Function.identity()));
// 4. 构建评论树结构
return buildCommentTree(parentComments, childComments, userMap);
}
/** /**
* ModelCommentVo * ID
* *
* @param workFlowComment * @param parentComments
* @return WorkFlowCommentVo * @return ID
*/ */
@NotNull private List<Long> collectUserIds(List<WorkFlowComment> parentComments) {
private WorkFlowCommentVo getModelCommentVo(WorkFlowComment workFlowComment) { List<Long> userIds = new ArrayList<>();
Long userId = workFlowComment.getUserId(); for (WorkFlowComment comment : parentComments) {
SysUser sysUser = sysUserService.selectUserById(userId); userIds.add(comment.getUserId());
}
// 构建WorkFlowCommentVo对象 return userIds;
WorkFlowCommentVo workFlowCommentVo = new WorkFlowCommentVo();
workFlowCommentVo.setUserId(userId);
workFlowCommentVo.setUserName(sysUser.getUserName());
workFlowCommentVo.setUserAvatar(sysUser.getAvatar());
workFlowCommentVo.setCommentId(workFlowComment.getId());
workFlowCommentVo.setContent(workFlowComment.getContent());
// 查询子评论
workFlowCommentVo.setContentList(getContentList(workFlowComment.getId()));
workFlowCommentVo.setLikeNum(workFlowComment.getLikeNum());
workFlowCommentVo.setCreateTime(workFlowComment.getCreateTime());
return workFlowCommentVo;
} }
/** /**
* *
* *
* @param modelCommentId id * @param parentComments
* @return List<ModelCommentVo> * @return
*/ */
private List<WorkFlowCommentVo> getContentList(Long modelCommentId) { private List<WorkFlowComment> getChildComments(List<WorkFlowComment> parentComments) {
List<WorkFlowCommentVo> modelCommentVoList = new ArrayList<>(); if (parentComments.isEmpty()) {
if (Objects.isNull(modelCommentId)) { return new ArrayList<>();
return modelCommentVoList;
} }
// 提取父评论ID集合
List<Long> parentIds = parentComments.stream()
.map(WorkFlowComment::getId)
.collect(Collectors.toList());
// 查询子评论 // 查询子评论
LambdaQueryWrapper<WorkFlowComment> lqw = new LambdaQueryWrapper<WorkFlowComment>() return baseMapper.selectList(
.eq(WorkFlowComment::getParentId, modelCommentId) new LambdaQueryWrapper<WorkFlowComment>()
.orderByDesc(WorkFlowComment::getCreateTime); .in(WorkFlowComment::getParentId, parentIds)
.orderByAsc(WorkFlowComment::getCreateTime)
List<WorkFlowComment> modelCommentList = workFlowCommentMapper.selectList(lqw); );
for (WorkFlowComment modelComment : modelCommentList) {
Long userId = modelComment.getUserId();
SysUser sysUser = sysUserService.selectUserById(userId);
WorkFlowCommentVo modelCommentVo = new WorkFlowCommentVo();
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;
/**
* VO
*/
private List<WorkFlowCommentVo> buildCommentTree(List<WorkFlowComment> parentComments,
List<WorkFlowComment> childComments,
Map<Long, SysUser> userMap) {
// 按父ID分组子评论
Map<Long, List<WorkFlowComment>> childMap = new HashMap<>();
for (WorkFlowComment child : childComments) {
Long parentId = child.getParentId();
childMap.computeIfAbsent(parentId, k -> new ArrayList<>()).add(child);
} }
// 构建VO树
List<WorkFlowCommentVo> result = new ArrayList<>();
for (WorkFlowComment parent : parentComments) {
// 转换父评论VO
WorkFlowCommentVo parentVo = convertToVo(parent, userMap);
// 获取子评论VO列表
List<WorkFlowComment> children = childMap.getOrDefault(parent.getId(), new ArrayList<>());
List<WorkFlowCommentVo> childVos = new ArrayList<>();
for (WorkFlowComment child : children) {
childVos.add(convertToVo(child, userMap));
}
parentVo.setContentList(childVos);
result.add(parentVo);
}
return result;
}
/**
* VO
*/
private WorkFlowCommentVo convertToVo(WorkFlowComment comment, Map<Long, SysUser> userMap) {
SysUser user = userMap.get(comment.getUserId());
if (user == null) {
user = createDefaultUser(); // 处理用户信息缺失
}
WorkFlowCommentVo vo = new WorkFlowCommentVo();
vo.setUserId(comment.getUserId());
vo.setUserName(user.getUserName());
vo.setUserAvatar(user.getAvatar());
vo.setCommentId(comment.getId());
vo.setContent(comment.getContent());
vo.setReplyUserId(comment.getReplyUserId());
vo.setLikeNum(comment.getLikeNum());
vo.setCreateTime(comment.getCreateTime());
return vo;
}
/**
*
*/
private SysUser createDefaultUser() {
SysUser defaultUser = new SysUser();
defaultUser.setUserName("匿名用户");
defaultUser.setAvatar("/default-avatar.png");
return defaultUser;
}
} }