优化和完善批量删除动态功能

实现动态软删除,被删除的动态的状态将改为3,将动态关联的评论,点赞,收藏硬删除
This commit is contained in:
KilLze
2026-01-03 20:57:37 +08:00
parent 2ce8116126
commit 413bafa275
11 changed files with 103 additions and 33 deletions

View File

@@ -61,7 +61,7 @@ public class PostController {
@PostMapping("/deletePost")
public Result<String> deleteById(@RequestBody List<Long> postIds){
int deletedCount = postService.deletePostById(postIds);
return Result.success(ResultCode.SUCCESS_DELETE, deletedCount > 0 ? "成功删除" : "删除失败,该动态不存在", null);
return Result.success(ResultCode.SUCCESS_DELETE, "成功删除" + deletedCount + "条动态", null);
}
/**

View File

@@ -18,4 +18,11 @@ public interface CommentsMapper {
// 根据动态ID查询评论列表
@Select("SELECT * FROM comments WHERE post_id = #{post_id} ORDER BY created_at DESC")
List<Comments> getCommentsByPostId(@Param("post_id") Long post_id);
/**
* 根据动态ID批量删除评论
* @param postIds
* @return
*/
int deleteCommentsByPostIds(@Param("postIds") List<Long> postIds);
}

View File

@@ -12,4 +12,12 @@ public interface PostFavoriteMapper {
List<Long> selectUserIDByPostID(@Param("postId") Long postId);
int addPostFavorite(PostFavorite postFavorite);
int deletePostFavorite(@Param("postId") Long postId);
/**
* 批量删除收藏
* @param postIds
* @return
*/
int deleteFavoritesByPostIds(@Param("postIds") List<Long> postIds);
}

View File

@@ -4,6 +4,8 @@ import com.bao.dating.pojo.entity.PostLike;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import java.util.List;
@Mapper
public interface PostLikeMapper {
/**
@@ -31,4 +33,12 @@ public interface PostLikeMapper {
* @return
*/
int deleteByPostIdAndUserId(@Param("postId") Long postId, @Param("userId") Long userId);
/**
* 批量删除点赞记录
*
* @param postIds
* @return
*/
int deleteLikesByPostIds(@Param("postIds") List<Long> postIds);
}

View File

@@ -21,11 +21,11 @@ public interface PostMapper {
void insert(Post post);
/**
* 根据ID删除动
* 根据ID修改动态状
*
* @param postIds 动态ID
*/
int deletePostByIds(List<Long> postIds);
int updatePublicById(@Param("postIds") List<Long> postIds, @Param("userId") Long userId);
/**
* 根据ID查询动态

View File

@@ -27,7 +27,7 @@ public interface PostService {
Post createPost(PostRequestDTO postRequestDTO);
/**
* 批量删除动态
* 批量删除动态(将动态状态改为已删除)
* @param postIds 动态ID
* @return 删除的动态对象
*/

View File

@@ -4,17 +4,22 @@ import com.bao.dating.common.aliyun.GreenImageScan;
import com.bao.dating.common.aliyun.GreenTextScan;
import com.bao.dating.common.result.GreenAuditResult;
import com.bao.dating.context.UserContext;
import com.bao.dating.mapper.CommentsMapper;
import com.bao.dating.mapper.PostFavoriteMapper;
import com.bao.dating.mapper.PostLikeMapper;
import com.bao.dating.mapper.PostMapper;
import com.bao.dating.pojo.dto.PostRequestDTO;
import com.bao.dating.pojo.entity.Post;
import com.bao.dating.pojo.vo.PostEditVO;
import com.bao.dating.service.PostService;
import com.bao.dating.common.aliyun.AliOssUtil;
import com.bao.dating.service.UserService;
import com.bao.dating.util.FileUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import org.springframework.web.multipart.MultipartFile;
import java.io.IOException;
@@ -43,6 +48,15 @@ public class PostServiceImpl implements PostService {
@Autowired
private PostMapper postMapper;
@Autowired
private PostLikeMapper postLikeMapper;
@Autowired
private PostFavoriteMapper postFavoriteMapper;
@Autowired
private CommentsMapper commentsMapper;
/**
* 上传媒体文件
* @param files 媒体文件数组
@@ -177,7 +191,7 @@ public class PostServiceImpl implements PostService {
}
/**
* 批量删除动态
* 批量删除动态(将动态状态改为已删除)
*
* @param postIds 动态ID
* @return 删除的动态对象
@@ -188,19 +202,21 @@ public class PostServiceImpl implements PostService {
// 判断用户权限
Long userId = UserContext.getUserId();
// 遍历所有要删除的帖子ID验证权限
for (Long postId : postIds) {
Post post = postMapper.selectById(postId);
if (post == null) {
throw new RuntimeException("动态不存在");
if (CollectionUtils.isEmpty(postIds)) {
return 0;
}
// 验证用户权限
if (post.getUserId() == null || !post.getUserId().equals(userId)) {
throw new RuntimeException("无权限删除此动态");
int affected = postMapper.updatePublicById(postIds, userId);
if (affected == 0) {
throw new RuntimeException("未删除任何动态,可能无权限或动态不存在");
}
}
// 批量删除动态
return postMapper.deletePostByIds(postIds);
// 删除动态下的评论、点赞、收藏
commentsMapper.deleteCommentsByPostIds(postIds);
postLikeMapper.deleteLikesByPostIds(postIds);
postFavoriteMapper.deleteFavoritesByPostIds(postIds);
return affected;
}
/**

View File

@@ -0,0 +1,15 @@
<?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="com.bao.dating.mapper.CommentsMapper">
<!-- 批量删除动态下的所有评论 -->
<delete id="deleteCommentsByPostIds">
DELETE FROM comments
WHERE post_id IN
<foreach collection="postIds" item="postId" open="(" close=")" separator=",">
#{postId}
</foreach>
</delete>
</mapper>

View File

@@ -14,4 +14,14 @@
<select id="selectUserIDByPostID" resultType="java.lang.Long">
SELECT user_id FROM post_favorite WHERE post_id = #{postId}
</select>
<!--批量删除动态收藏-->
<delete id="deleteFavoritesByPostIds">
DELETE FROM post_favorite
WHERE post_id IN
<foreach collection="postIds" item="postId" open="(" close=")" separator=",">
#{postId}
</foreach>
</delete>
</mapper>

View File

@@ -14,4 +14,14 @@
<delete id="deleteByPostIdAndUserId">
delete from dating.post_like where post_id = #{postId} and user_id = #{userId}
</delete>
<!--批量删除点赞记录-->
<delete id="deleteLikesByPostIds">
DELETE FROM post_like
WHERE post_id IN
<foreach collection="postIds" item="postId" open="(" close=")" separator=",">
#{postId}
</foreach>
</delete>
</mapper>

View File

@@ -28,25 +28,19 @@
#{isPublic}, 0, 0, #{createdAt}, #{updatedAt})
</insert>
<!--动态删除-->
<delete id="deletePostByIds">
DELETE FROM post WHERE post_id IN
<!--修改动态状态-->
<update id="updatePublicById">
UPDATE post
<set>
is_public = 3,
updated_at = NOW()
</set>
WHERE post_id IN
<foreach item="postId" index="index" collection="postIds" separator="," open="(" close=")">
#{postId}
</foreach>
</delete>
<!--删除收藏记录-->
<delete id="1">
DELETE FROM post_favorite WHERE post_id = #{postId}
</delete>
<!--删除点赞记录-->
<delete id="2">
DELETE FROM post_like WHERE post_id = #{postId}
</delete>
<!--动态评论删除-->
<delete id="3">
DELETE FROM comments WHERE post_id = #{postId}
</delete>
AND user_id = #{userId}
</update>
<!--动态查询-->
<resultMap id="PostResultMap" type="com.bao.dating.pojo.entity.Post">