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.core.domain.AjaxResult;
import com.mcwl.resource.domain.ModelComment;
import com.mcwl.resource.domain.dto.ModelCommentRes;
import com.mcwl.resource.domain.vo.ModelCommentVo;
import com.mcwl.resource.service.ModelCommentLikeService;
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.web.bind.annotation.*;
import javax.validation.Valid;
import javax.validation.constraints.NotNull;
import java.util.List;
@ -42,8 +44,8 @@ public class ModelCommentController {
*/
@ApiOperation(value = "模型点赞/取消")
@RepeatSubmit
@GetMapping("/modelLike/{modelId}")
public AjaxResult like(@PathVariable Long modelId) {
@GetMapping("/modelLike")
public AjaxResult like(@Valid @NotNull(message = "模型id不能为空") Long modelId) {
modelLikeService.like(modelId);
return AjaxResult.success();
}
@ -54,8 +56,8 @@ public class ModelCommentController {
*/
@ApiOperation(value = "模型评论发布")
@PostMapping("/comment")
public AjaxResult comment(@RequestBody ModelComment modelComment) {
modelCommentService.comment(modelComment);
public AjaxResult comment(@Valid @RequestBody ModelCommentRes modelCommentRes) {
modelCommentService.comment(modelCommentRes);
return AjaxResult.success();
}
@ -64,8 +66,8 @@ public class ModelCommentController {
*/
@ApiOperation(value = "模型评论点赞/取消")
@RepeatSubmit
@GetMapping("/commentLike/{commentId}")
public AjaxResult commentLike(@PathVariable Long commentId) {
@GetMapping("/commentLike")
public AjaxResult commentLike(@Valid @NotNull(message = "评论id不能为空") Long commentId) {
modelCommentLikeService.like(commentId);
return AjaxResult.error();
}
@ -76,8 +78,8 @@ public class ModelCommentController {
*
*/
@ApiOperation(value = "获取模型评论")
@GetMapping("/comment/{modelId}")
public AjaxResult getComment(@PathVariable @NotNull(message = "模型id不能为空") Long modelId) {
@GetMapping("/comment")
public AjaxResult getComment(@Valid @NotNull(message = "模型id不能为空") Long modelId) {
List<ModelCommentVo> modelCommentList = modelCommentService.getComment(modelId);
return AjaxResult.success(modelCommentList);
}
@ -87,8 +89,8 @@ public class ModelCommentController {
*
*/
@ApiOperation(value = "删除模型评论")
@GetMapping("/commentDelete/{commentId}")
public AjaxResult commentDelete(@PathVariable @NotNull(message = "评论id不能为空") Long commentId) {
@GetMapping("/commentDelete")
public AjaxResult commentDelete(@Valid @NotNull(message = "评论id不能为空") Long commentId) {
modelCommentService.removeById(commentId);
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.resource.domain.WorkFlowComment;
import com.mcwl.resource.domain.dto.WorkFlowCommentRes;
import com.mcwl.resource.domain.vo.WorkFlowCommentVo;
import com.mcwl.resource.service.WorkFlowCommentLikeService;
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.web.bind.annotation.*;
import javax.validation.Valid;
import javax.validation.constraints.NotNull;
import java.util.List;
@ -53,8 +55,8 @@ public class WorkFlowCommentController {
*/
@ApiOperation(value = "工作流评论发布")
@PostMapping("/comment")
public AjaxResult comment(@RequestBody WorkFlowComment modelComment) {
workFlowCommentService.comment(modelComment);
public AjaxResult comment(@RequestBody WorkFlowCommentRes workFlowCommentRes) {
workFlowCommentService.comment(workFlowCommentRes);
return AjaxResult.success();
}
@ -62,8 +64,8 @@ public class WorkFlowCommentController {
* /
*/
@ApiOperation(value = "工作流评论点赞/取消")
@GetMapping("/commentLike/{commentId}")
public AjaxResult commentLike(@PathVariable Long commentId) {
@GetMapping("/commentLike")
public AjaxResult commentLike(@Valid @NotNull(message = "评论id不能为空") Long commentId) {
workFlowCommentLikeService.like(commentId);
return AjaxResult.error();
}
@ -74,9 +76,9 @@ public class WorkFlowCommentController {
*
*/
@ApiOperation(value = "获取工作流评论")
@GetMapping("/comment/{modelId}")
public AjaxResult getComment(@PathVariable @NotNull(message = "模型id不能为空") Long modelId) {
List<WorkFlowCommentVo> modelCommentList = workFlowCommentService.getComment(modelId);
@GetMapping("/comment")
public AjaxResult getComment(@Valid @NotNull(message = "模型id不能为空") Long commentId) {
List<WorkFlowCommentVo> modelCommentList = workFlowCommentService.getComment(commentId);
return AjaxResult.success(modelCommentList);
}
@ -85,8 +87,8 @@ public class WorkFlowCommentController {
*
*/
@ApiOperation(value = "删除工作流评论")
@GetMapping("/commentDelete/{commentId}")
public AjaxResult commentDelete(@PathVariable @NotNull(message = "评论id不能为空") Long commentId) {
@GetMapping("/commentDelete")
public AjaxResult commentDelete(@Valid @NotNull(message = "评论id不能为空") Long commentId) {
workFlowCommentService.removeById(commentId);
return AjaxResult.success();
}

View File

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

View File

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

View File

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

View File

@ -47,6 +47,12 @@ public class WorkFlowComment extends BaseEntity {
@ApiModelProperty ("父评论id")
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")
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 = "评论内容")
private String content;
/**
*
*/
@ApiModelProperty(value = "回复人")
private Long replyUserId;
/**
*
*/

View File

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

View File

@ -54,6 +54,12 @@ public class WorkFlowCommentVo {
@ApiModelProperty(value = "评论内容")
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.mcwl.resource.domain.ModelComment;
import com.mcwl.resource.domain.dto.ModelCommentRes;
import com.mcwl.resource.domain.vo.ModelCommentVo;
import com.mcwl.resource.domain.vo.ModelImageCommentVo;
@ -17,7 +18,7 @@ import java.util.List;
* @Date2025/1/12 11:58
*/
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.mcwl.resource.domain.WorkFlowComment;
import com.mcwl.resource.domain.dto.WorkFlowCommentRes;
import com.mcwl.resource.domain.vo.WorkFlowCommentVo;
import java.util.List;
@ -16,7 +17,7 @@ import java.util.List;
* @Date2025/1/12 11:58
*/
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.resource.domain.ModelComment;
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.vo.ModelCommentVo;
import com.mcwl.resource.domain.vo.ModelImageCommentVo;
@ -19,10 +20,9 @@ import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import javax.validation.constraints.NotNull;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Objects;
import java.util.*;
import java.util.function.Function;
import java.util.stream.Collectors;
/**
*
@ -39,8 +39,8 @@ public class ModelCommentServiceImpl extends ServiceImpl<ModelCommentMapper, Mod
private ModelImageMapper modelImageMapper;
@Override
public void comment(ModelComment modelComment) {
Long parentId = modelComment.getParentId();
public void comment(ModelCommentRes modelCommentRes) {
Long parentId = modelCommentRes.getParentId();
if (Objects.nonNull(parentId)) {
ModelComment mic = modelCommentMapper.selectById(parentId);
@ -48,11 +48,9 @@ public class ModelCommentServiceImpl extends ServiceImpl<ModelCommentMapper, Mod
return;
}
}
ModelComment modelComment = new ModelComment();
BeanUtil.copyProperties(modelCommentRes, modelComment);
modelComment.setUserId(SecurityUtils.getUserId());
modelComment.setCreateBy(SecurityUtils.getUsername());
modelComment.setCreateTime(new Date());
modelComment.setUpdateBy(SecurityUtils.getUsername());
modelComment.setUpdateTime(new Date());
modelCommentMapper.insert(modelComment);
}
@ -64,21 +62,133 @@ public class ModelCommentServiceImpl extends ServiceImpl<ModelCommentMapper, Mod
*/
@Override
public List<ModelCommentVo> getComment(Long imageId) {
List<ModelCommentVo> modelCommentVoList = new ArrayList<>();
// 查询所有父评论
LambdaQueryWrapper<ModelComment> lqw = new LambdaQueryWrapper<>();
lqw.eq(ModelComment::getModelId, imageId)
// 1. 查询所有父评论
List<ModelComment> parentComments = baseMapper.selectList(
new LambdaQueryWrapper<ModelComment>()
.eq(ModelComment::getModelId, imageId)
.isNull(ModelComment::getParentId)
.orderByDesc(ModelComment::getCreateTime);
// 添加父评论
List<ModelComment> modelCommentList = modelCommentMapper.selectList(lqw);
for (ModelComment modelComment : modelCommentList) {
ModelCommentVo modelCommentVo = getModelCommentVo(modelComment);
modelCommentVoList.add(modelCommentVo);
.orderByAsc(ModelComment::getCreateTime)
);
// 无评论直接返回空列表
if (parentComments.isEmpty()) {
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 javax.validation.constraints.NotNull;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.*;
import java.util.function.Function;
import java.util.stream.Collectors;
/**
*
@ -57,88 +57,133 @@ public class ModelImageCommentServiceImpl extends ServiceImpl<ModelImageCommentM
*/
@Override
public List<ModelImageCommentVo> getComment(Long imageId) {
List<ModelImageCommentVo> modelImageCommentVoList = new ArrayList<>();
// 查询所有父评论
LambdaQueryWrapper<ModelImageComment> lqw = new LambdaQueryWrapper<>();
lqw.eq(ModelImageComment::getModelImageId, imageId)
// 1. 查询所有父评论
List<ModelImageComment> parentComments = baseMapper.selectList(
new LambdaQueryWrapper<ModelImageComment>()
.eq(ModelImageComment::getModelImageId, imageId)
.isNull(ModelImageComment::getParentId)
.orderByDesc(ModelImageComment::getCreateTime);
// 添加父评论
List<ModelImageComment> modelImageCommentList = baseMapper.selectList(lqw);
for (ModelImageComment modelImageComment : modelImageCommentList) {
ModelImageCommentVo modelImageCommentVo = getModelImageCommentVo(modelImageComment);
modelImageCommentVoList.add(modelImageCommentVo);
.orderByAsc(ModelImageComment::getCreateTime)
);
// 无评论直接返回空列表
if (parentComments.isEmpty()) {
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
* @return ModelImageCommentVo
* @param parentComments
* @return ID
*/
@NotNull
private ModelImageCommentVo getModelImageCommentVo(ModelImageComment modelImageComment) {
Long userId = modelImageComment.getUserId();
SysUser sysUser = sysUserService.selectUserById(userId);
// 构建ModelImageCommentVo对象
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;
private List<Long> collectUserIds(List<ModelImageComment> parentComments) {
List<Long> userIds = new ArrayList<>();
for (ModelImageComment comment : parentComments) {
userIds.add(comment.getUserId());
}
return userIds;
}
/**
*
*
*
* @param modelImageCommentId id
* @return List<ModelImageCommentVo>
* @param parentComments
* @return
*/
private List<ModelImageCommentVo> getContentList(Long modelImageCommentId) {
List<ModelImageCommentVo> modelImageCommentVoList = new ArrayList<>();
if (Objects.isNull(modelImageCommentId)) {
return modelImageCommentVoList;
private List<ModelImageComment> getChildComments(List<ModelImageComment> parentComments) {
if (parentComments.isEmpty()) {
return new ArrayList<>();
}
// 提取父评论ID集合
List<Long> parentIds = parentComments.stream()
.map(ModelImageComment::getId)
.collect(Collectors.toList());
// 查询子评论
LambdaQueryWrapper<ModelImageComment> lqw = new LambdaQueryWrapper<ModelImageComment>()
.eq(ModelImageComment::getParentId, modelImageCommentId)
.orderByDesc(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;
return baseMapper.selectList(
new LambdaQueryWrapper<ModelImageComment>()
.in(ModelImageComment::getParentId, parentIds)
.orderByAsc(ModelImageComment::getCreateTime)
);
}
/**
* 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;
import cn.hutool.core.bean.BeanUtil;
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.dto.WorkFlowCommentRes;
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.WorkFlowCommentService;
@ -13,13 +17,13 @@ import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import javax.validation.constraints.NotNull;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Objects;
import java.util.*;
import java.util.function.Function;
import java.util.stream.Collectors;
/**
*
*
* @AuthorChenYan
* @ProjectMcWl
* @Packagecom.mcwl.resource.service.impl
@ -38,105 +42,154 @@ public class WorkFlowCommentServiceImpl extends ServiceImpl<WorkFlowCommentMappe
@Override
public void comment(WorkFlowComment workFlowComment) {
Long parentId = workFlowComment.getParentId();
if (parentId != null){
public void comment(WorkFlowCommentRes workFlowCommentRes) {
Long parentId = workFlowCommentRes.getParentId();
if (parentId != null) {
WorkFlowComment mic = workFlowCommentMapper.selectById(parentId);
if (Objects.nonNull(parentId) && Objects.isNull(mic)) {
if (Objects.isNull(mic)) {
return;
}
}
WorkFlowComment workFlowComment = new WorkFlowComment();
BeanUtil.copyProperties(workFlowCommentRes, workFlowComment);
workFlowComment.setUserId(SecurityUtils.getUserId());
workFlowComment.setCreateBy(SecurityUtils.getUsername());
workFlowComment.setUpdateBy(SecurityUtils.getUsername());
workFlowComment.setUpdateTime(new Date());
workFlowCommentMapper.insert(workFlowComment);
}
@Override
public List<WorkFlowCommentVo> getComment(Long imageId) {
List<WorkFlowCommentVo> workFlowCommentVos = new ArrayList<>();
// 查询所有父评论
LambdaQueryWrapper<WorkFlowComment> lqw = new LambdaQueryWrapper<>();
lqw.eq(WorkFlowComment::getWorkFlowId, imageId)
List<WorkFlowComment> parentComments = baseMapper.selectList(
new LambdaQueryWrapper<WorkFlowComment>()
.eq(WorkFlowComment::getWorkFlowId, imageId)
.isNull(WorkFlowComment::getParentId)
.orderByDesc(WorkFlowComment::getCreateTime);
// 添加父评论
List<WorkFlowComment> workFlowComments = workFlowCommentMapper.selectList(lqw);
for (WorkFlowComment workFlowComment : workFlowComments) {
WorkFlowCommentVo workFlowCommentVo = getModelCommentVo(workFlowComment);
workFlowCommentVos.add(workFlowCommentVo);
.orderByAsc(WorkFlowComment::getCreateTime)
);
// 无评论直接返回空列表
if (parentComments.isEmpty()) {
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
* @return WorkFlowCommentVo
* @param parentComments
* @return ID
*/
@NotNull
private WorkFlowCommentVo getModelCommentVo(WorkFlowComment workFlowComment) {
Long userId = workFlowComment.getUserId();
SysUser sysUser = sysUserService.selectUserById(userId);
// 构建WorkFlowCommentVo对象
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;
private List<Long> collectUserIds(List<WorkFlowComment> parentComments) {
List<Long> userIds = new ArrayList<>();
for (WorkFlowComment comment : parentComments) {
userIds.add(comment.getUserId());
}
return userIds;
}
/**
*
*
*
* @param modelCommentId id
* @return List<ModelCommentVo>
* @param parentComments
* @return
*/
private List<WorkFlowCommentVo> getContentList(Long modelCommentId) {
List<WorkFlowCommentVo> modelCommentVoList = new ArrayList<>();
if (Objects.isNull(modelCommentId)) {
return modelCommentVoList;
private List<WorkFlowComment> getChildComments(List<WorkFlowComment> parentComments) {
if (parentComments.isEmpty()) {
return new ArrayList<>();
}
// 提取父评论ID集合
List<Long> parentIds = parentComments.stream()
.map(WorkFlowComment::getId)
.collect(Collectors.toList());
// 查询子评论
LambdaQueryWrapper<WorkFlowComment> lqw = new LambdaQueryWrapper<WorkFlowComment>()
.eq(WorkFlowComment::getParentId, modelCommentId)
.orderByDesc(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 baseMapper.selectList(
new LambdaQueryWrapper<WorkFlowComment>()
.in(WorkFlowComment::getParentId, parentIds)
.orderByAsc(WorkFlowComment::getCreateTime)
);
}
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;
}
}