refactor(resource): 调整评论和点赞功能

master
yang 2025-02-13 10:37:15 +08:00
parent dd16eb0106
commit 89ff879055
21 changed files with 125 additions and 74 deletions

View File

@ -76,7 +76,7 @@ public class ModelCommentController {
@ApiParam(value = "评论id", required = true)
Long commentId) {
modelCommentLikeService.like(commentId);
return AjaxResult.error();
return AjaxResult.success();
}
@ -86,11 +86,13 @@ public class ModelCommentController {
@ApiOperation(value = "获取模型评论")
@GetMapping("/comment")
@Valid
public AjaxResult getComment(@NotNull(message = "模型id不能为空")
public AjaxResult getComment(@Valid
@NotNull(message = "模型id不能为空")
@ApiParam(value = "模型id", required = true)
Long modelId,
@Valid
@NotNull(message = "排序方式不能为空")
@ApiParam(value = "排序方式", required = true)
@ApiParam(value = "排序方式 0最热 1最新", required = true)
Integer sortType) {
List<ModelCommentVo> modelCommentList = modelCommentService.getComment(modelId, sortType);
return AjaxResult.success(modelCommentList);

View File

@ -75,11 +75,13 @@ public class ModelImageCommentController {
@ApiOperation(value = "获取图片评论")
@GetMapping("/comment")
@Valid
public AjaxResult getComment(@NotNull(message = "图片id不能为空")
public AjaxResult getComment(@Valid
@NotNull(message = "图片id不能为空")
@ApiParam(value = "评论id", required = true)
Long imageId,
@Valid
@NotNull(message = "排序方式不能为空")
@ApiParam(value = "排序方式", required = true)
@ApiParam(value = "排序方式 0最热 1最新", required = true)
Integer sortType) {
List<ModelImageCommentVo> modelImageCommentVoList = modelImageCommentService.getComment(imageId, sortType);
return AjaxResult.success(modelImageCommentVoList);

View File

@ -75,7 +75,7 @@ public class WorkFlowCommentController {
@ApiParam(value = "评论id", required = true)
Long commentId) {
workFlowCommentLikeService.like(commentId);
return AjaxResult.error();
return AjaxResult.success();
}
@ -84,12 +84,13 @@ public class WorkFlowCommentController {
*/
@ApiOperation(value = "获取工作流评论")
@GetMapping("/comment")
@Valid
public AjaxResult getComment(@NotNull(message = "模型id不能为空")
public AjaxResult getComment(@Valid
@NotNull(message = "模型id不能为空")
@ApiParam(value = "模型id", required = true)
Long commentId,
@Valid
@NotNull(message = "排序方式不能为空")
@ApiParam(value = "排序方式", required = true)
@ApiParam(value = "排序方式 0最热 1最新", required = true)
Integer sortType) {
List<WorkFlowCommentVo> modelCommentList = workFlowCommentService.getComment(commentId, sortType);
return AjaxResult.success(modelCommentList);
@ -117,8 +118,8 @@ public class WorkFlowCommentController {
public AjaxResult getCommentCount(@Valid
@NotNull(message = "工作流id不能为空")
@ApiParam(value = "工作流id", required = true)
Long imageId) {
Integer commentCount = workFlowCommentService.getCommentCount(imageId);
Long workFlowId) {
Integer commentCount = workFlowCommentService.getCommentCount(workFlowId);
return AjaxResult.success(commentCount);
}

View File

@ -17,4 +17,5 @@ import org.apache.ibatis.annotations.Param;
public interface ModelCommentLikeMapper extends BaseMapper<ModelCommentLike> {
ModelCommentLike getLikeComment(@Param("userId") Long userId, @Param("commentId") Long commentId);
void updateDelFlagById(ModelCommentLike modelCommentLike);
}

View File

@ -10,5 +10,5 @@ import org.apache.ibatis.annotations.Param;
public interface ModelImageCommentLikeMapper extends BaseMapper<ModelImageCommentLike> {
ModelImageCommentLike getLikeImageComment(@Param("userId") Long userId, @Param("commentId") Long commentId);
void updateDelFlagById(@Param("userId") Long userId, @Param("commentId") Long commentId);
void updateDelFlagById(ModelImageCommentLike modelImageCommentLike);
}

View File

@ -17,4 +17,5 @@ import org.apache.ibatis.annotations.Param;
public interface WorkFlowCommentLikeMapper extends BaseMapper<WorkFlowCommentLike> {
WorkFlowCommentLike getLikeComment(@Param("userId") Long userId, @Param("commentId") Long commentId);
void updateDelFlagById(WorkFlowCommentLike workFlowCommentLike);
}

View File

@ -20,10 +20,10 @@ public interface ModelCommentService extends IService<ModelComment> {
/**
*
* @param imageId id
* @param modelId id
* @return
*/
List<ModelCommentVo> getComment(Long imageId, Integer sortType);
List<ModelCommentVo> getComment(Long modelId, Integer sortType);
/**
*

View File

@ -23,10 +23,10 @@ public interface WorkFlowCommentService extends IService<WorkFlowComment> {
/**
*
*
* @param imageId id
* @param workFLowId id
* @return
*/
List<WorkFlowCommentVo> getComment(Long imageId, Integer sortType);
List<WorkFlowCommentVo> getComment(Long workFLowId, Integer sortType);
/**

View File

@ -43,14 +43,16 @@ public class ModelCommentLikeServiceImpl extends ServiceImpl<ModelCommentLikeMap
ModelCommentLike modelCommentLike = baseMapper.getLikeComment(userId, commentId);
if (Objects.nonNull(modelCommentLike)) {
if (Objects.equals(modelCommentLike.getDelFlag(), "0")) {
modelCommentLike.setDelFlag("1");
modelComment.setLikeNum(modelComment.getLikeNum() - 1);
modelCommentLike.setDelFlag("2");
int likeNum = modelComment.getLikeNum() - 1;
likeNum = Math.max(likeNum, 0);
modelComment.setLikeNum(likeNum);
} else {
modelCommentLike.setDelFlag("0");
modelComment.setLikeNum(modelComment.getLikeNum() + 1);
}
// 更新点赞记录
baseMapper.updateById(modelCommentLike);
baseMapper.updateDelFlagById(modelCommentLike);
// 更新图片评论点赞数
modelCommentMapper.updateById(modelComment);
return;

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.ModelCommentLike;
import com.mcwl.resource.domain.WorkFlowComment;
import com.mcwl.resource.domain.dto.ModelCommentRes;
import com.mcwl.resource.domain.vo.ModelCommentVo;
import com.mcwl.resource.mapper.ModelCommentMapper;
@ -59,20 +60,24 @@ public class ModelCommentServiceImpl extends ServiceImpl<ModelCommentMapper, Mod
/**
*
*
* @param imageId id
* @param modelId id
* @return
*/
@Override
public List<ModelCommentVo> getComment(Long imageId, Integer sortType) {
public List<ModelCommentVo> getComment(Long modelId, Integer sortType) {
// 1. 查询所有父评论
List<ModelComment> parentComments = baseMapper.selectList(
new LambdaQueryWrapper<ModelComment>()
.eq(ModelComment::getModelId, imageId)
.isNull(ModelComment::getParentId)
.orderByDesc(sortType != 1, ModelComment::getLikeNum)
.orderByAsc(ModelComment::getCreateTime)
);
LambdaQueryWrapper<ModelComment> lqw = new LambdaQueryWrapper<ModelComment>()
.eq(ModelComment::getModelId, modelId)
.isNull(ModelComment::getParentId);
if (sortType == 0) {
// 按点赞数降序,创建时间升序
lqw.orderByAsc(ModelComment::getLikeNum)
.orderByAsc(ModelComment::getCreateTime);
} else {
lqw.orderByDesc(ModelComment::getCreateTime);
}
List<ModelComment> parentComments = baseMapper.selectList(lqw);
// 无评论直接返回空列表
if (parentComments.isEmpty()) {
@ -124,8 +129,11 @@ public class ModelCommentServiceImpl extends ServiceImpl<ModelCommentMapper, Mod
.collect(Collectors.toList());
// 获取所有子评论
List<ModelComment> childCommentList = baseMapper.selectList(new LambdaQueryWrapper<ModelComment>()
.in(ModelComment::getParentId, parentIds));
List<ModelComment> childCommentList = new ArrayList<>();
if (!parentIds.isEmpty()) {
childCommentList = baseMapper.selectList(new LambdaQueryWrapper<ModelComment>()
.in(ModelComment::getParentId, parentIds));
}
return parentIds.size() + childCommentList.size();

View File

@ -39,12 +39,16 @@ public class ModelImageCommentLikeServiceImpl extends ServiceImpl<ModelImageComm
ModelImageCommentLike modelImageCommentLike = baseMapper.getLikeImageComment(userId, commentId);
if (Objects.nonNull(modelImageCommentLike)) {
if (Objects.equals(modelImageCommentLike.getDelFlag(), "0")) {
modelImageComment.setLikeNum(modelImageComment.getLikeNum() - 1);
modelImageCommentLike.setDelFlag("2");
int likeNum = modelImageComment.getLikeNum() - 1;
likeNum = Math.max(likeNum, 0);
modelImageComment.setLikeNum(likeNum);
} else {
modelImageCommentLike.setDelFlag("0");
modelImageComment.setLikeNum(modelImageComment.getLikeNum() + 1);
}
// 更新点赞记录
baseMapper.updateDelFlagById(userId, commentId);
baseMapper.updateDelFlagById(modelImageCommentLike);
// 更新图片评论点赞数
modelImageCommentMapper.updateById(modelImageComment);
return;

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.ModelImageComment;
import com.mcwl.resource.domain.ModelImageCommentLike;
import com.mcwl.resource.domain.WorkFlowComment;
import com.mcwl.resource.domain.dto.ModelImageCommentRes;
import com.mcwl.resource.domain.vo.ModelImageCommentVo;
import com.mcwl.resource.mapper.ModelImageCommentMapper;
@ -58,13 +59,17 @@ public class ModelImageCommentServiceImpl extends ServiceImpl<ModelImageCommentM
@Override
public List<ModelImageCommentVo> getComment(Long imageId, Integer sortType) {
// 1. 查询所有父评论
List<ModelImageComment> parentComments = baseMapper.selectList(
new LambdaQueryWrapper<ModelImageComment>()
.eq(ModelImageComment::getModelImageId, imageId)
.isNull(ModelImageComment::getParentId)
.orderByDesc(sortType != 1, ModelImageComment::getLikeNum)
.orderByAsc(ModelImageComment::getCreateTime)
);
LambdaQueryWrapper<ModelImageComment> lqw = new LambdaQueryWrapper<ModelImageComment>()
.eq(ModelImageComment::getModelImageId, imageId)
.isNull(ModelImageComment::getParentId);
if (sortType == 0) {
// 按点赞数降序,创建时间升序
lqw.orderByAsc(ModelImageComment::getLikeNum)
.orderByAsc(ModelImageComment::getCreateTime);
} else {
lqw.orderByDesc(ModelImageComment::getCreateTime);
}
List<ModelImageComment> parentComments = baseMapper.selectList(lqw);
// 无评论直接返回空列表
if (parentComments.isEmpty()) {
@ -111,8 +116,11 @@ public class ModelImageCommentServiceImpl extends ServiceImpl<ModelImageCommentM
.collect(Collectors.toList());
// 获取所有子评论
List<ModelImageComment> childCommentList = baseMapper.selectList(new LambdaQueryWrapper<ModelImageComment>()
.in(ModelImageComment::getParentId, parentIds));
List<ModelImageComment> childCommentList = new ArrayList<>();
if (!parentIds.isEmpty()) {
childCommentList = baseMapper.selectList(new LambdaQueryWrapper<ModelImageComment>()
.in(ModelImageComment::getParentId, parentIds));
}
return parentIds.size() + childCommentList.size();

View File

@ -49,8 +49,12 @@ public class ModelImageLikeServiceImpl extends ServiceImpl<ModelImageLikeMapper,
ModelImageLike modelImageLike = baseMapper.getLikeImage(userId, imageId);
if (Objects.nonNull(modelImageLike)) {
if (Objects.equals(modelImageLike.getDelFlag(), "0")) {
modelImage.setLikeNum(modelImage.getLikeNum() - 1);
modelImageLike.setDelFlag("2");
int likeNum = modelImage.getLikeNum() - 1;
likeNum = Math.max(likeNum, 0);
modelImage.setLikeNum(likeNum);
} else {
modelImageLike.setDelFlag("0");
modelImage.setLikeNum(modelImage.getLikeNum() + 1);
}
// 更新点赞记录

View File

@ -52,7 +52,9 @@ public class ModelLikeServiceImpl extends ServiceImpl<ModelLikeMapper, ModelLike
if (Objects.nonNull(modelLike)) {
if (Objects.equals(modelLike.getDelFlag(), "0")) {
modelLike.setDelFlag("2");
model.setLikeNum(model.getLikeNum() - 1);
int likeNum = model.getLikeNum() - 1;
likeNum = Math.max(likeNum, 0);
model.setLikeNum(likeNum);
} else {
modelLike.setDelFlag("0");
model.setLikeNum(model.getLikeNum() + 1);

View File

@ -40,14 +40,16 @@ public class WorkFlowCommentLikeServiceImpl extends ServiceImpl<WorkFlowCommentL
WorkFlowCommentLike workFlowCommentLike = baseMapper.getLikeComment(userId, commentId);
if (Objects.nonNull(workFlowCommentLike)) {
if (Objects.equals(workFlowComment.getDelFlag(), "0")) {
workFlowCommentLike.setDelFlag("1");
workFlowComment.setLikeNum(workFlowComment.getLikeNum() - 1);
workFlowCommentLike.setDelFlag("2");
int likeNum = workFlowComment.getLikeNum() - 1;
likeNum = Math.max(likeNum, 0);
workFlowComment.setLikeNum(likeNum);
} else {
workFlowCommentLike.setDelFlag("0");
workFlowComment.setLikeNum(workFlowComment.getLikeNum() + 1);
}
// 更新点赞记录
baseMapper.updateById(workFlowCommentLike);
baseMapper.updateDelFlagById(workFlowCommentLike);
// 更新图片评论点赞数
workFlowCommentMapper.updateById(workFlowComment);
return;

View File

@ -61,16 +61,20 @@ public class WorkFlowCommentServiceImpl extends ServiceImpl<WorkFlowCommentMappe
}
@Override
public List<WorkFlowCommentVo> getComment(Long imageId, Integer sortType) {
public List<WorkFlowCommentVo> getComment(Long workFLowId, Integer sortType) {
// 查询所有父评论
List<WorkFlowComment> parentComments = baseMapper.selectList(
new LambdaQueryWrapper<WorkFlowComment>()
.eq(WorkFlowComment::getWorkFlowId, imageId)
.isNull(WorkFlowComment::getParentId)
.orderByDesc(sortType != 1, WorkFlowComment::getLikeNum)
.orderByAsc(WorkFlowComment::getCreateTime)
);
LambdaQueryWrapper<WorkFlowComment> lqw = new LambdaQueryWrapper<WorkFlowComment>()
.eq(WorkFlowComment::getWorkFlowId, workFLowId)
.isNull(WorkFlowComment::getParentId);
if (sortType == 0) {
// 按点赞数降序,创建时间升序
lqw.orderByAsc(WorkFlowComment::getLikeNum)
.orderByAsc(WorkFlowComment::getCreateTime);
} else {
lqw.orderByDesc(WorkFlowComment::getCreateTime);
}
List<WorkFlowComment> parentComments = baseMapper.selectList(lqw);
// 无评论直接返回空列表
if (parentComments.isEmpty()) {
@ -116,8 +120,11 @@ public class WorkFlowCommentServiceImpl extends ServiceImpl<WorkFlowCommentMappe
.collect(Collectors.toList());
// 获取所有子评论
List<WorkFlowComment> childCommentList = baseMapper.selectList(new LambdaQueryWrapper<WorkFlowComment>()
.in(WorkFlowComment::getParentId, parentIds));
List<WorkFlowComment> childCommentList = new ArrayList<>();
if (!parentIds.isEmpty()) {
childCommentList = baseMapper.selectList(new LambdaQueryWrapper<WorkFlowComment>()
.in(WorkFlowComment::getParentId, parentIds));
}
return parentIds.size() + childCommentList.size();
@ -189,9 +196,9 @@ public class WorkFlowCommentServiceImpl extends ServiceImpl<WorkFlowCommentMappe
* VO
*/
private List<WorkFlowCommentVo> buildCommentTree(List<WorkFlowComment> parentComments,
List<WorkFlowComment> childComments,
Map<Long, SysUser> userMap,
Map<Long, Integer> likeStatusMap) {
List<WorkFlowComment> childComments,
Map<Long, SysUser> userMap,
Map<Long, Integer> likeStatusMap) {
Map<Long, List<WorkFlowComment>> childMap = new HashMap<>();
for (WorkFlowComment child : childComments) {
childMap.computeIfAbsent(child.getParentId(), k -> new ArrayList<>()).add(child);
@ -246,5 +253,4 @@ public class WorkFlowCommentServiceImpl extends ServiceImpl<WorkFlowCommentMappe
}
}

View File

@ -60,8 +60,10 @@ public class WorkFlowLikeServiceImpl extends ServiceImpl<WorkFlowLikeMapper, Wor
WorkFlowLike workFlowLike = baseMapper.getLike(userId, modelId);
if (Objects.nonNull(workFlowLike)) {
if (Objects.equals(workFlowLike.getDelFlag(), "0")) {
workFlowLike.setDelFlag("1");
workFlow.setLikeCount(workFlow.getLikeCount() - 1);
workFlowLike.setDelFlag("2");
int likeCount = workFlow.getLikeCount() - 1;
likeCount = Math.max(likeCount, 0);
workFlow.setLikeCount(likeCount);
} else {
workFlowLike.setDelFlag("0");
workFlow.setLikeCount(workFlow.getLikeCount() + 1);

View File

@ -3,6 +3,11 @@
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.mcwl.resource.mapper.ModelCommentLikeMapper">
<update id="updateDelFlagById">
update model_comment_like
set del_flag = #{delFlag}
where user_id = #{userId} and model_comment_id = #{modelCommentId}
</update>
<select id="getLikeComment" resultType="com.mcwl.resource.domain.ModelCommentLike">
@ -14,6 +19,8 @@
update_by,
update_time,
del_flag,
remark where user_id = #{userId} and model_comment_id = #{commentId}
remark
from model_comment_like
where user_id = #{userId} and model_comment_id = #{commentId}
</select>
</mapper>

View File

@ -30,9 +30,9 @@
from model_image_comment_like
</sql>
<update id="updateDelFlagById">
update model_image_comment_like
set del_flag = !del_flag
where user_id = #{userId} and model_image_comment_id = #{commentId}
UPDATE model_image_comment_like
SET del_flag = #{delFlag}
WHERE user_id = #{userId} and model_image_comment_id = #{modelImageCommentId}
</update>

View File

@ -3,6 +3,11 @@
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.mcwl.resource.mapper.WorkFlowCommentLikeMapper">
<update id="updateDelFlagById">
update work_flow_comment_like
set del_flag = #{delFlag}
where user_id = #{userId} and work_flow_comment_id = #{workFlowCommentId}
</update>
<select id="getLikeComment" resultType="com.mcwl.resource.domain.WorkFlowCommentLike">

View File

@ -6,14 +6,8 @@
<update id="updateStatus">
UPDATE work_flow_like
SET user_id = #{userId},
work_flow_id = #{workFlowId},
create_by = #{createBy},
create_time = #{createTime},
update_by = #{updateBy},
update_time = #{updateTime},
del_flag = #{delFlag}
WHERE id = #{id}
SET del_flag = #{delFlag}
WHERE user_id = #{userId} and work_flow_id = #{workFlowId}
</update>