feat(resource): 调整评论和回复
parent
7c3621ac2d
commit
e679fd847e
|
@ -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();
|
||||||
}
|
}
|
||||||
|
|
|
@ -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();
|
||||||
}
|
}
|
||||||
|
|
|
@ -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);
|
||||||
}
|
}
|
||||||
|
|
|
@ -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;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 点赞数
|
* 点赞数
|
||||||
*/
|
*/
|
||||||
|
|
|
@ -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;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 点赞数
|
* 点赞数
|
||||||
*/
|
*/
|
||||||
|
|
|
@ -47,6 +47,12 @@ public class WorkFlowComment extends BaseEntity {
|
||||||
@ApiModelProperty ("父评论id")
|
@ApiModelProperty ("父评论id")
|
||||||
private Long parentId;
|
private Long parentId;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 回复人
|
||||||
|
*/
|
||||||
|
@ApiModelProperty ("回复人")
|
||||||
|
private Long replyUserId;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 点赞数
|
* 点赞数
|
||||||
*/
|
*/
|
||||||
|
|
|
@ -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;
|
||||||
|
|
||||||
|
}
|
|
@ -29,4 +29,11 @@ public class ModelImageCommentRes {
|
||||||
@ApiModelProperty(value = "父评论id")
|
@ApiModelProperty(value = "父评论id")
|
||||||
private Long parentId;
|
private Long parentId;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 回复人
|
||||||
|
*/
|
||||||
|
@ApiModelProperty(value = "回复人")
|
||||||
|
private Long replyUserId;
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -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;
|
||||||
|
|
||||||
|
}
|
|
@ -54,6 +54,12 @@ public class ModelCommentVo {
|
||||||
@ApiModelProperty(value = "评论内容")
|
@ApiModelProperty(value = "评论内容")
|
||||||
private String content;
|
private String content;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 回复人
|
||||||
|
*/
|
||||||
|
@ApiModelProperty(value = "回复人")
|
||||||
|
private Long replyUserId;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 子评论
|
* 子评论
|
||||||
*/
|
*/
|
||||||
|
|
|
@ -53,6 +53,13 @@ public class ModelImageCommentVo {
|
||||||
@ApiModelProperty(value = "评论内容")
|
@ApiModelProperty(value = "评论内容")
|
||||||
private String content;
|
private String content;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 回复人
|
||||||
|
*/
|
||||||
|
@ApiModelProperty(value = "回复人")
|
||||||
|
private Long replyUserId;
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 子评论
|
* 子评论
|
||||||
*/
|
*/
|
||||||
|
|
|
@ -54,6 +54,12 @@ public class WorkFlowCommentVo {
|
||||||
@ApiModelProperty(value = "评论内容")
|
@ApiModelProperty(value = "评论内容")
|
||||||
private String content;
|
private String content;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 回复人
|
||||||
|
*/
|
||||||
|
@ApiModelProperty(value = "回复人")
|
||||||
|
private Long replyUserId;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 子评论
|
* 子评论
|
||||||
*/
|
*/
|
||||||
|
|
|
@ -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;
|
||||||
* @Date:2025/1/12 11:58
|
* @Date:2025/1/12 11:58
|
||||||
*/
|
*/
|
||||||
public interface ModelCommentService extends IService<ModelComment> {
|
public interface ModelCommentService extends IService<ModelComment> {
|
||||||
void comment(ModelComment modelComment);
|
void comment(ModelCommentRes modelCommentRes);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 获取评论
|
* 获取评论
|
||||||
|
|
|
@ -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;
|
||||||
* @Date:2025/1/12 11:58
|
* @Date:2025/1/12 11:58
|
||||||
*/
|
*/
|
||||||
public interface WorkFlowCommentService extends IService<WorkFlowComment> {
|
public interface WorkFlowCommentService extends IService<WorkFlowComment> {
|
||||||
void comment(WorkFlowComment modelComment);
|
void comment(WorkFlowCommentRes workFlowCommentRes);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 获取评论
|
* 获取评论
|
||||||
|
|
|
@ -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>()
|
||||||
.isNull(ModelComment::getParentId)
|
.eq(ModelComment::getModelId, imageId)
|
||||||
.orderByDesc(ModelComment::getCreateTime);
|
.isNull(ModelComment::getParentId)
|
||||||
// 添加父评论
|
.orderByAsc(ModelComment::getCreateTime)
|
||||||
List<ModelComment> modelCommentList = modelCommentMapper.selectList(lqw);
|
);
|
||||||
for (ModelComment modelComment : modelCommentList) {
|
|
||||||
ModelCommentVo modelCommentVo = getModelCommentVo(modelComment);
|
// 无评论直接返回空列表
|
||||||
modelCommentVoList.add(modelCommentVo);
|
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;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -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<ModelImageComment> parentComments = baseMapper.selectList(
|
||||||
|
new LambdaQueryWrapper<ModelImageComment>()
|
||||||
|
.eq(ModelImageComment::getModelImageId, imageId)
|
||||||
|
.isNull(ModelImageComment::getParentId)
|
||||||
|
.orderByAsc(ModelImageComment::getCreateTime)
|
||||||
|
);
|
||||||
|
|
||||||
List<ModelImageCommentVo> modelImageCommentVoList = new ArrayList<>();
|
// 无评论直接返回空列表
|
||||||
|
if (parentComments.isEmpty()) {
|
||||||
// 查询所有父评论
|
return new ArrayList<>();
|
||||||
LambdaQueryWrapper<ModelImageComment> lqw = new LambdaQueryWrapper<>();
|
|
||||||
lqw.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);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
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;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -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;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 工作流评论 实现层
|
* 工作流评论 实现层
|
||||||
|
*
|
||||||
* @Author:ChenYan
|
* @Author:ChenYan
|
||||||
* @Project:McWl
|
* @Project:McWl
|
||||||
* @Package:com.mcwl.resource.service.impl
|
* @Package:com.mcwl.resource.service.impl
|
||||||
|
@ -31,112 +35,161 @@ import java.util.Objects;
|
||||||
public class WorkFlowCommentServiceImpl extends ServiceImpl<WorkFlowCommentMapper, WorkFlowComment> implements WorkFlowCommentService {
|
public class WorkFlowCommentServiceImpl extends ServiceImpl<WorkFlowCommentMapper, WorkFlowComment> implements WorkFlowCommentService {
|
||||||
|
|
||||||
@Autowired
|
@Autowired
|
||||||
private ISysUserService sysUserService;
|
private ISysUserService sysUserService;
|
||||||
|
|
||||||
@Autowired
|
@Autowired
|
||||||
private WorkFlowCommentMapper workFlowCommentMapper;
|
private WorkFlowCommentMapper workFlowCommentMapper;
|
||||||
|
|
||||||
|
|
||||||
@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>()
|
||||||
.isNull(WorkFlowComment::getParentId)
|
.eq(WorkFlowComment::getWorkFlowId, imageId)
|
||||||
.orderByDesc(WorkFlowComment::getCreateTime);
|
.isNull(WorkFlowComment::getParentId)
|
||||||
// 添加父评论
|
.orderByAsc(WorkFlowComment::getCreateTime)
|
||||||
List<WorkFlowComment> workFlowComments = workFlowCommentMapper.selectList(lqw);
|
);
|
||||||
for (WorkFlowComment workFlowComment : workFlowComments) {
|
|
||||||
WorkFlowCommentVo workFlowCommentVo = getModelCommentVo(workFlowComment);
|
// 无评论直接返回空列表
|
||||||
workFlowCommentVos.add(workFlowCommentVo);
|
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 父评论对象
|
* @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;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue