feat: 项目管理添加评论功能

This commit is contained in:
caiyuchao
2025-07-15 16:22:39 +08:00
parent eaf24f21ad
commit dbde5021a6
11 changed files with 632 additions and 0 deletions

View File

@@ -16,4 +16,11 @@ public interface ErrorCodeConstants {
ErrorCode PROJECT_CODE_DUPLICATE = new ErrorCode(1_100_001_003, "项目编号`{}`已存在"); ErrorCode PROJECT_CODE_DUPLICATE = new ErrorCode(1_100_001_003, "项目编号`{}`已存在");
ErrorCode LICENSE_NOT_EXISTS = new ErrorCode(1_100_002_001, "License不存在"); ErrorCode LICENSE_NOT_EXISTS = new ErrorCode(1_100_002_001, "License不存在");
ErrorCode LICENSE_SN_DUPLICATE = new ErrorCode(1_100_001_002, "License SN`{}`已存在"); ErrorCode LICENSE_SN_DUPLICATE = new ErrorCode(1_100_001_002, "License SN`{}`已存在");
ErrorCode COMMENT_NOT_EXISTS = new ErrorCode(1_100_003_001, "评论不存在");
ErrorCode COMMENT_EXITS_CHILDREN = new ErrorCode(1_100_003_002, "存在子评论,无法删除");
ErrorCode COMMENT_PARENT_NOT_EXITS = new ErrorCode(1_100_003_003,"父级评论不存在");
ErrorCode COMMENT_PARENT_ERROR = new ErrorCode(1_100_003_004, "不能设置自己为父评论");
ErrorCode COMMENT_CONTENT_DUPLICATE = new ErrorCode(1_100_003_005, "已经存在该内容的评论");
ErrorCode COMMENT_PARENT_IS_CHILD = new ErrorCode(1_100_003_006, "不能设置自己的子Comment为父Comment");
} }

View File

@@ -0,0 +1,105 @@
package org.agt.module.license.controller.admin.comment;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.Valid;
import org.agt.framework.apilog.core.annotation.ApiAccessLog;
import org.agt.framework.common.pojo.CommonResult;
import org.agt.framework.common.util.object.BeanUtils;
import org.agt.framework.excel.core.util.ExcelUtils;
import org.agt.module.license.controller.admin.comment.vo.CommentListReqVO;
import org.agt.module.license.controller.admin.comment.vo.CommentRespVO;
import org.agt.module.license.controller.admin.comment.vo.CommentSaveReqVO;
import org.agt.module.license.controller.admin.comment.vo.CommentTreeRespVO;
import org.agt.module.license.dal.dataobject.comment.CommentDO;
import org.agt.module.license.service.comment.CommentService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import java.io.IOException;
import java.util.List;
import static org.agt.framework.apilog.core.enums.OperateTypeEnum.EXPORT;
import static org.agt.framework.common.pojo.CommonResult.success;
@Tag(name = "管理后台 - 评论")
@RestController
@RequestMapping("/license/comment")
@Validated
public class CommentController {
@Resource
private CommentService commentService;
@PostMapping("/create")
@Operation(summary = "创建评论")
// @PreAuthorize("@ss.hasPermission('license:comment:create')")
public CommonResult<Long> createComment(@Valid @RequestBody CommentSaveReqVO createReqVO) {
return success(commentService.createComment(createReqVO));
}
@PutMapping("/update")
@Operation(summary = "更新评论")
// @PreAuthorize("@ss.hasPermission('license:comment:update')")
public CommonResult<Boolean> updateComment(@Valid @RequestBody CommentSaveReqVO updateReqVO) {
commentService.updateComment(updateReqVO);
return success(true);
}
@DeleteMapping("/delete")
@Operation(summary = "删除评论")
@Parameter(name = "id", description = "编号", required = true)
// @PreAuthorize("@ss.hasPermission('license:comment:delete')")
public CommonResult<Boolean> deleteComment(@RequestParam("id") Long id) {
commentService.deleteComment(id);
return success(true);
}
@GetMapping("/get")
@Operation(summary = "获得评论")
@Parameter(name = "id", description = "编号", required = true, example = "1024")
@PreAuthorize("@ss.hasPermission('license:comment:query')")
public CommonResult<CommentRespVO> getComment(@RequestParam("id") Long id) {
CommentDO comment = commentService.getComment(id);
return success(BeanUtils.toBean(comment, CommentRespVO.class));
}
@GetMapping("/list")
@Operation(summary = "获得评论列表")
@PreAuthorize("@ss.hasPermission('license:comment:query')")
public CommonResult<List<CommentRespVO>> getCommentList(@Valid CommentListReqVO listReqVO) {
List<CommentDO> list = commentService.getCommentList(listReqVO);
return success(BeanUtils.toBean(list, CommentRespVO.class));
}
@GetMapping("/tree")
@Operation(summary = "获得评论树形列表")
public CommonResult<List<CommentTreeRespVO>> getCommentTree(@RequestParam("projectId") Long projectId, @RequestParam(value = "sort", required = false) Boolean sort) {
List<CommentTreeRespVO> list = commentService.getCommentTree(projectId, sort);
return success(list);
}
@GetMapping("/export-excel")
@Operation(summary = "导出评论 Excel")
@PreAuthorize("@ss.hasPermission('license:comment:export')")
@ApiAccessLog(operateType = EXPORT)
public void exportCommentExcel(@Valid CommentListReqVO listReqVO,
HttpServletResponse response) throws IOException {
List<CommentDO> list = commentService.getCommentList(listReqVO);
// 导出 Excel
ExcelUtils.write(response, "评论.xls", "数据", CommentRespVO.class,
BeanUtils.toBean(list, CommentRespVO.class));
}
}

View File

@@ -0,0 +1,32 @@
package org.agt.module.license.controller.admin.comment.vo;
import lombok.*;
import java.util.*;
import io.swagger.v3.oas.annotations.media.Schema;
import org.agt.framework.common.pojo.PageParam;
import java.time.LocalDateTime;
import org.springframework.format.annotation.DateTimeFormat;
import static org.agt.framework.common.util.date.DateUtils.FORMAT_YEAR_MONTH_DAY_HOUR_MINUTE_SECOND;
@Schema(description = "管理后台 - 评论列表 Request VO")
@Data
public class CommentListReqVO {
@Schema(description = "项目ID", example = "18334")
private Long projectId;
@Schema(description = "用户ID", example = "30248")
private Long userId;
@Schema(description = "父ID", example = "20143")
private Long parentId;
@Schema(description = "内容")
private String content;
@Schema(description = "创建时间")
@DateTimeFormat(pattern = FORMAT_YEAR_MONTH_DAY_HOUR_MINUTE_SECOND)
private LocalDateTime[] createTime;
}

View File

@@ -0,0 +1,39 @@
package org.agt.module.license.controller.admin.comment.vo;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.*;
import java.util.*;
import org.springframework.format.annotation.DateTimeFormat;
import java.time.LocalDateTime;
import com.alibaba.excel.annotation.*;
@Schema(description = "管理后台 - 评论 Response VO")
@Data
@ExcelIgnoreUnannotated
public class CommentRespVO {
@Schema(description = "主键", requiredMode = Schema.RequiredMode.REQUIRED, example = "4294")
@ExcelProperty("主键")
private Long id;
@Schema(description = "项目ID", example = "18334")
@ExcelProperty("项目ID")
private Long projectId;
@Schema(description = "用户ID", example = "30248")
@ExcelProperty("用户ID")
private Long userId;
@Schema(description = "父ID", example = "20143")
@ExcelProperty("父ID")
private Long parentId;
@Schema(description = "内容")
@ExcelProperty("内容")
private String content;
@Schema(description = "创建时间", requiredMode = Schema.RequiredMode.REQUIRED)
@ExcelProperty("创建时间")
private LocalDateTime createTime;
}

View File

@@ -0,0 +1,27 @@
package org.agt.module.license.controller.admin.comment.vo;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.*;
import java.util.*;
import jakarta.validation.constraints.*;
@Schema(description = "管理后台 - 评论新增/修改 Request VO")
@Data
public class CommentSaveReqVO {
@Schema(description = "主键", requiredMode = Schema.RequiredMode.REQUIRED, example = "4294")
private Long id;
@Schema(description = "项目ID", example = "18334")
private Long projectId;
@Schema(description = "用户ID", example = "30248")
private Long userId;
@Schema(description = "父ID", example = "20143")
private Long parentId;
@Schema(description = "内容")
private String content;
}

View File

@@ -0,0 +1,61 @@
package org.agt.module.license.controller.admin.comment.vo;
import com.alibaba.excel.annotation.ExcelIgnoreUnannotated;
import com.alibaba.excel.annotation.ExcelProperty;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;
import java.time.LocalDateTime;
import java.util.List;
@Schema(description = "管理后台 - 评论 树形Response VO")
@Data
@ExcelIgnoreUnannotated
public class CommentTreeRespVO {
@Schema(description = "主键", requiredMode = Schema.RequiredMode.REQUIRED, example = "4294")
@ExcelProperty("主键")
private Long id;
@Schema(description = "项目ID", example = "18334")
@ExcelProperty("项目ID")
private Long projectId;
@Schema(description = "用户ID", example = "30248")
@ExcelProperty("用户ID")
private Long userId;
@Schema(description = "父ID", example = "20143")
@ExcelProperty("父ID")
private Long parentId;
@Schema(description = "内容")
@ExcelProperty("内容")
private String content;
@Schema(description = "创建时间", requiredMode = Schema.RequiredMode.REQUIRED)
@ExcelProperty("创建时间")
private LocalDateTime createTime;
@Schema(description = "更新时间", requiredMode = Schema.RequiredMode.REQUIRED)
@ExcelProperty("更新时间")
private LocalDateTime updateTime;
@Schema(description = "评论用户")
private String author;
@Schema(description = "评论用户头像")
private String avatar;
@Schema(description = "回复用户ID", example = "30248")
private Long replyUserId;
@Schema(description = "回复用户")
private String replyUser;
@Schema(description = "深度")
private Integer depth;
@Schema(description = "子列表")
private List<CommentTreeRespVO> children;
}

View File

@@ -0,0 +1,53 @@
package org.agt.module.license.dal.dataobject.comment;
import com.baomidou.mybatisplus.annotation.KeySequence;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.NoArgsConstructor;
import lombok.ToString;
import org.agt.framework.mybatis.core.dataobject.BaseDO;
/**
* 评论 DO
*
* @author 管理员
*/
@TableName("lic_comment")
@KeySequence("lic_comment_seq") // 用于 Oracle、PostgreSQL、Kingbase、DB2、H2 数据库的主键自增。如果是 MySQL 等数据库,可不写。
@Data
@EqualsAndHashCode(callSuper = true)
@ToString(callSuper = true)
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class CommentDO extends BaseDO {
public static final Long PARENT_ID_ROOT = 0L;
/**
* 主键
*/
@TableId
private Long id;
/**
* 项目ID
*/
private Long projectId;
/**
* 用户ID
*/
private Long userId;
/**
* 父ID
*/
private Long parentId;
/**
* 内容
*/
private String content;
}

View File

@@ -0,0 +1,40 @@
package org.agt.module.license.dal.mysql.comment;
import org.agt.framework.mybatis.core.mapper.BaseMapperX;
import org.agt.framework.mybatis.core.query.LambdaQueryWrapperX;
import org.agt.module.license.controller.admin.comment.vo.CommentListReqVO;
import org.agt.module.license.controller.admin.comment.vo.CommentTreeRespVO;
import org.agt.module.license.dal.dataobject.comment.CommentDO;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import java.util.List;
/**
* 评论 Mapper
*
* @author 管理员
*/
@Mapper
public interface CommentMapper extends BaseMapperX<CommentDO> {
default List<CommentDO> selectList(CommentListReqVO reqVO) {
return selectList(new LambdaQueryWrapperX<CommentDO>()
.eqIfPresent(CommentDO::getProjectId, reqVO.getProjectId())
.eqIfPresent(CommentDO::getUserId, reqVO.getUserId())
.eqIfPresent(CommentDO::getParentId, reqVO.getParentId())
.eqIfPresent(CommentDO::getContent, reqVO.getContent())
.betweenIfPresent(CommentDO::getCreateTime, reqVO.getCreateTime())
.orderByDesc(CommentDO::getId));
}
default CommentDO selectByParentIdAndContent(Long parentId, String content) {
return selectOne(CommentDO::getParentId, parentId, CommentDO::getContent, content);
}
default Long selectCountByParentId(Long parentId) {
return selectCount(CommentDO::getParentId, parentId);
}
List<CommentTreeRespVO> getCommentList(@Param("projectId") Long projectId);
}

View File

@@ -0,0 +1,57 @@
package org.agt.module.license.service.comment;
import jakarta.validation.Valid;
import org.agt.module.license.controller.admin.comment.vo.CommentListReqVO;
import org.agt.module.license.controller.admin.comment.vo.CommentSaveReqVO;
import org.agt.module.license.controller.admin.comment.vo.CommentTreeRespVO;
import org.agt.module.license.dal.dataobject.comment.CommentDO;
import java.util.List;
/**
* 评论 Service 接口
*
* @author 管理员
*/
public interface CommentService {
/**
* 创建评论
*
* @param createReqVO 创建信息
* @return 编号
*/
Long createComment(@Valid CommentSaveReqVO createReqVO);
/**
* 更新评论
*
* @param updateReqVO 更新信息
*/
void updateComment(@Valid CommentSaveReqVO updateReqVO);
/**
* 删除评论
*
* @param id 编号
*/
void deleteComment(Long id);
/**
* 获得评论
*
* @param id 编号
* @return 评论
*/
CommentDO getComment(Long id);
/**
* 获得评论列表
*
* @param listReqVO 查询条件
* @return 评论列表
*/
List<CommentDO> getCommentList(CommentListReqVO listReqVO);
List<CommentTreeRespVO> getCommentTree(Long projectId, Boolean sort);
}

View File

@@ -0,0 +1,183 @@
package org.agt.module.license.service.comment;
import jakarta.annotation.Resource;
import org.agt.framework.common.util.object.BeanUtils;
import org.agt.framework.web.core.util.WebFrameworkUtils;
import org.agt.module.license.controller.admin.comment.vo.CommentListReqVO;
import org.agt.module.license.controller.admin.comment.vo.CommentSaveReqVO;
import org.agt.module.license.controller.admin.comment.vo.CommentTreeRespVO;
import org.agt.module.license.dal.dataobject.comment.CommentDO;
import org.agt.module.license.dal.mysql.comment.CommentMapper;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;
import java.util.Comparator;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;
import static org.agt.framework.common.exception.util.ServiceExceptionUtil.exception;
import static org.agt.module.license.enums.ErrorCodeConstants.COMMENT_CONTENT_DUPLICATE;
import static org.agt.module.license.enums.ErrorCodeConstants.COMMENT_EXITS_CHILDREN;
import static org.agt.module.license.enums.ErrorCodeConstants.COMMENT_NOT_EXISTS;
import static org.agt.module.license.enums.ErrorCodeConstants.COMMENT_PARENT_ERROR;
import static org.agt.module.license.enums.ErrorCodeConstants.COMMENT_PARENT_IS_CHILD;
import static org.agt.module.license.enums.ErrorCodeConstants.COMMENT_PARENT_NOT_EXITS;
/**
* 评论 Service 实现类
*
* @author 管理员
*/
@Service
@Validated
public class CommentServiceImpl implements CommentService {
@Resource
private CommentMapper commentMapper;
@Override
public Long createComment(CommentSaveReqVO createReqVO) {
createReqVO.setUserId(WebFrameworkUtils.getLoginUserId());
if (createReqVO.getParentId() == null) {
createReqVO.setParentId(0L);
}
// 校验父ID的有效性
validateParentComment(null, createReqVO.getParentId());
// 校验内容的唯一性
validateCommentContentUnique(null, createReqVO.getParentId(), createReqVO.getContent());
// 插入
CommentDO comment = BeanUtils.toBean(createReqVO, CommentDO.class);
commentMapper.insert(comment);
// 返回
return comment.getId();
}
@Override
public void updateComment(CommentSaveReqVO updateReqVO) {
// 校验存在
validateCommentExists(updateReqVO.getId());
// 校验父ID的有效性
validateParentComment(updateReqVO.getId(), updateReqVO.getParentId());
// 校验内容的唯一性
validateCommentContentUnique(updateReqVO.getId(), updateReqVO.getParentId(), updateReqVO.getContent());
// 更新
CommentDO updateObj = BeanUtils.toBean(updateReqVO, CommentDO.class);
commentMapper.updateById(updateObj);
}
@Override
public void deleteComment(Long id) {
// 校验存在
validateCommentExists(id);
// 校验是否有子评论
if (commentMapper.selectCountByParentId(id) > 0) {
throw exception(COMMENT_EXITS_CHILDREN);
}
// 删除
commentMapper.deleteById(id);
}
private void validateCommentExists(Long id) {
if (commentMapper.selectById(id) == null) {
throw exception(COMMENT_NOT_EXISTS);
}
}
private void validateParentComment(Long id, Long parentId) {
if (parentId == null || CommentDO.PARENT_ID_ROOT.equals(parentId)) {
return;
}
// 1. 不能设置自己为父评论
if (Objects.equals(id, parentId)) {
throw exception(COMMENT_PARENT_ERROR);
}
// 2. 父评论不存在
CommentDO parentComment = commentMapper.selectById(parentId);
if (parentComment == null) {
throw exception(COMMENT_PARENT_NOT_EXITS);
}
// 3. 递归校验父评论,如果父评论是自己的子评论,则报错,避免形成环路
if (id == null) { // id 为空,说明新增,不需要考虑环路
return;
}
for (int i = 0; i < Short.MAX_VALUE; i++) {
// 3.1 校验环路
parentId = parentComment.getParentId();
if (Objects.equals(id, parentId)) {
throw exception(COMMENT_PARENT_IS_CHILD);
}
// 3.2 继续递归下一级父评论
if (parentId == null || CommentDO.PARENT_ID_ROOT.equals(parentId)) {
break;
}
parentComment = commentMapper.selectById(parentId);
if (parentComment == null) {
break;
}
}
}
private void validateCommentContentUnique(Long id, Long parentId, String content) {
CommentDO comment = commentMapper.selectByParentIdAndContent(parentId, content);
if (comment == null) {
return;
}
// 如果 id 为空,说明不用比较是否为相同 id 的评论
if (id == null) {
throw exception(COMMENT_CONTENT_DUPLICATE);
}
if (!Objects.equals(comment.getId(), id)) {
throw exception(COMMENT_CONTENT_DUPLICATE);
}
}
@Override
public CommentDO getComment(Long id) {
return commentMapper.selectById(id);
}
@Override
public List<CommentDO> getCommentList(CommentListReqVO listReqVO) {
return commentMapper.selectList(listReqVO);
}
@Override
public List<CommentTreeRespVO> getCommentTree(Long projectId, Boolean sort) {
List<CommentTreeRespVO> comments = commentMapper.getCommentList(projectId);
List<CommentTreeRespVO> commentTree = comments.stream().filter(c -> c.getParentId() == 0)
.peek(c -> {
c.setDepth(1);
c.setChildren(getChildren(c, comments, 1));
})
.sorted(sort != null && sort ? Comparator.comparing(CommentTreeRespVO::getUpdateTime): Comparator.comparing(CommentTreeRespVO::getUpdateTime).reversed())
.toList();
return commentTree.stream().filter(c -> c.getParentId() == 0)
.peek(c -> {
List<CommentTreeRespVO> childrenMerge = c.getChildren();
addChildrenMerge(c, comments, childrenMerge);
childrenMerge = childrenMerge.stream()
.sorted(sort != null && sort ? Comparator.comparing(CommentTreeRespVO::getUpdateTime): Comparator.comparing(CommentTreeRespVO::getUpdateTime).reversed())
.collect(Collectors.toList());
c.setChildren(childrenMerge);
}).toList();
}
public static List<CommentTreeRespVO> getChildren(CommentTreeRespVO root, List<CommentTreeRespVO> all, int i) {
return all.stream().filter(c -> Objects.equals(c.getParentId(), root.getId()))
.peek(c -> {
c.setDepth(i+1);
c.setChildren(getChildren(c, all, i+1));
}).collect(Collectors.toList());
}
public static void addChildrenMerge(CommentTreeRespVO root, List<CommentTreeRespVO> all, List<CommentTreeRespVO> children) {
all.stream().filter(c -> Objects.equals(c.getParentId(), root.getId()))
.forEach(c -> {
children.addAll(c.getChildren());
addChildrenMerge(c, all, children);
});
}
}

View File

@@ -0,0 +1,28 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="org.agt.module.license.dal.mysql.comment.CommentMapper">
<select id="getCommentList" resultType="org.agt.module.license.controller.admin.comment.vo.CommentTreeRespVO">
SELECT
c.*,
u.nickname author,
u.avatar,
pc.user_id reply_user_id,
pu.nickname reply_user
FROM
lic_comment c
LEFT JOIN system_users u ON c.user_id = u.id
AND u.deleted = 0
LEFT JOIN lic_comment pc ON c.parent_id = pc.id
AND pc.deleted = 0
LEFT JOIN system_users pu ON pc.user_id = pu.id
AND pu.deleted = 0
WHERE
c.deleted = 0
AND c.project_id = #{projectId}
ORDER BY
c.update_time DESC
</select>
</mapper>