22301138 | f682451 | 2025-06-04 02:03:13 +0800 | [diff] [blame] | 1 | package com.example.myproject.service; |
| 2 | |
| 3 | import com.example.myproject.entity.Likes; |
| 4 | import com.example.myproject.entity.Post; |
| 5 | import com.example.myproject.entity.Users; |
| 6 | import com.example.myproject.repository.CollectionsRepository; |
| 7 | import com.example.myproject.repository.LikesRepository; |
| 8 | import com.example.myproject.repository.PostRepository; |
| 9 | import com.example.myproject.repository.UserRepository; |
| 10 | import org.springframework.beans.factory.annotation.Autowired; |
| 11 | import org.springframework.stereotype.Service; |
| 12 | import org.springframework.web.multipart.MultipartFile; |
| 13 | import com.example.myproject.entity.Collections; |
| 14 | import java.io.IOException; |
| 15 | import java.nio.file.Files; |
| 16 | import java.nio.file.Path; |
| 17 | import java.nio.file.Paths; |
| 18 | import java.util.*; |
| 19 | |
| 20 | @Service |
| 21 | public class PostService { |
| 22 | |
| 23 | @Autowired |
| 24 | private PostRepository postRepository; |
| 25 | |
| 26 | @Autowired |
| 27 | private UserRepository userRepository; |
| 28 | @Autowired |
| 29 | private LikesRepository likesRepository; |
| 30 | |
| 31 | @Autowired |
| 32 | private CollectionsRepository collectionsRepository; |
| 33 | |
| 34 | private static final String IMAGE_DIR = "uploads/post/"; |
| 35 | |
| 36 | |
| 37 | //创建帖子 |
| 38 | public Map<String, Object> createPost(Long userId, String postContent, String title, MultipartFile[] imageFiles) { |
| 39 | // 查找用户信息 |
| 40 | Users user = userRepository.findById(Long.valueOf(userId)).orElseThrow(() -> new RuntimeException("User not found")); |
| 41 | Post post = new Post(); |
| 42 | post.setUser_id(user.getUserId()); |
| 43 | post.setPostTime(new Date()); |
| 44 | post.setPostLikeNum(0); |
| 45 | post.setPostCollectNum(0); |
| 46 | post.setPostContent(postContent); |
| 47 | post.setTitle(title); |
| 48 | |
| 49 | // 处理多张图片的上传 |
| 50 | StringBuilder imageUrlsBuilder = new StringBuilder(); |
| 51 | if (imageFiles != null && imageFiles.length > 0) { |
| 52 | for (int i = 0; i < imageFiles.length; i++) { |
| 53 | if (i > 0) { |
| 54 | imageUrlsBuilder.append(","); |
| 55 | } |
| 56 | try { |
| 57 | String imageUrl = saveImage(imageFiles[i]); |
| 58 | imageUrlsBuilder.append(imageUrl); |
| 59 | } catch (IOException e) { |
| 60 | throw new RuntimeException("Image upload failed: " + e.getMessage()); |
| 61 | } |
| 62 | } |
| 63 | } |
| 64 | post.setImageUrl(imageUrlsBuilder.toString()); |
| 65 | Post savedPost = postRepository.save(post); |
| 66 | Map<String, Object> response = new HashMap<>(); |
| 67 | response.put("postNo", savedPost.getPostNo()); |
| 68 | response.put("message", "帖子创建成功"); |
| 69 | return response; |
| 70 | } |
| 71 | |
| 72 | // 保存图片并返回图片的 URL |
| 73 | public String saveImage(MultipartFile imageFile) throws IOException { |
| 74 | String fileName = imageFile.getOriginalFilename(); |
| 75 | Path path = Paths.get(IMAGE_DIR + fileName); |
| 76 | Files.createDirectories(path.getParent()); |
| 77 | Files.write(path, imageFile.getBytes()); |
| 78 | return "/images/" + fileName; |
| 79 | } |
| 80 | |
| 81 | |
| 82 | //编辑帖子 |
| 83 | public void updatePost(Long postId, Post post) { |
| 84 | Post existingPost = postRepository.findById(postId).orElseThrow(() -> new RuntimeException("Post not found")); |
| 85 | if (post.getTitle() != null) { |
| 86 | existingPost.setTitle(post.getTitle()); |
| 87 | } |
| 88 | if (post.getPostContent() != null) { |
| 89 | existingPost.setPostContent(post.getPostContent()); |
| 90 | } |
| 91 | if (post.getImageUrl() != null) { |
| 92 | existingPost.setImageUrl(post.getImageUrl()); |
| 93 | } |
| 94 | existingPost.setPostTime(post.getPostTime()); |
| 95 | // 保存更新后的帖子 |
| 96 | postRepository.save(existingPost); |
| 97 | } |
| 98 | |
| 99 | //删除帖子 |
| 100 | public void deletePost(Long postId) { |
| 101 | // 查找指定 ID 的帖子,如果不存在则抛出异常 |
| 102 | if (!postRepository.existsById(postId)) { |
| 103 | throw new RuntimeException("Post not found"); |
| 104 | } |
| 105 | // 删除该帖子 |
| 106 | postRepository.deleteById(postId); |
| 107 | } |
| 108 | |
| 109 | //点赞帖子(已完成) |
| 110 | public void likePost(Long postId, Long userId) { |
| 111 | // 查找指定 ID 的帖子 |
| 112 | Post post = postRepository.findById(postId).orElseThrow(() -> new RuntimeException("Post not found")); |
| 113 | post.setPostLikeNum(post.getPostLikeNum() + 1); |
| 114 | |
| 115 | // 保存帖子 |
| 116 | postRepository.save(post); |
| 117 | Likes like = new Likes(); |
| 118 | like.setUserId(userId); |
| 119 | like.setPostNo(postId); |
| 120 | // 保存点赞记录 |
| 121 | likesRepository.save(like); |
| 122 | } |
| 123 | |
| 124 | // 取消点赞帖子(已完成) |
| 125 | public void unlikePost(Long postId, Long userId) { |
| 126 | // 查找指定 ID 的帖子 |
| 127 | Post post = postRepository.findById(postId).orElseThrow(() -> new RuntimeException("Post not found")); |
| 128 | |
| 129 | // 如果点赞数大于 0,则减少点赞数 |
| 130 | if (post.getPostLikeNum() > 0) { |
| 131 | post.setPostLikeNum(post.getPostLikeNum() - 1); |
| 132 | } |
| 133 | |
| 134 | // 删除点赞表中对应的记录 |
| 135 | likesRepository.deleteLikeByUserIdAndPostNo(userId, postId); |
| 136 | |
| 137 | // 保存更新后的帖子 |
| 138 | postRepository.save(post); |
| 139 | } |
| 140 | |
| 141 | |
| 142 | // 获取帖子列表(已完成) |
| 143 | public Map<String, Object> getAllPosts() { |
| 144 | List<Post> posts = postRepository.findAll(); |
| 145 | List<Map<String, Object>> postList = new ArrayList<>(); |
| 146 | |
| 147 | for (Post post : posts) { |
| 148 | Map<String, Object> postMap = new LinkedHashMap<>(); |
| 149 | postMap.put("postNo", post.getPostNo()); |
| 150 | postMap.put("user_id", post.getUser_id()); |
| 151 | postMap.put("postContent", post.getPostContent()); |
| 152 | postMap.put("imgUrl", post.getImageUrl()); |
| 153 | postMap.put("title", post.getTitle()); |
| 154 | postMap.put("createdAt", post.getPostTime().toString()); |
| 155 | postMap.put("likeCount", post.getPostLikeNum()); |
| 156 | postMap.put("collectCount", post.getPostCollectNum()); |
| 157 | |
| 158 | postList.add(postMap); |
| 159 | } |
| 160 | |
| 161 | Map<String, Object> response = new HashMap<>(); |
| 162 | // 统计帖子数量 |
| 163 | response.put("total", postList.size()); |
| 164 | response.put("posts", postList); |
| 165 | |
| 166 | return response; |
| 167 | } |
| 168 | |
| 169 | public Map<String, Object> getPostById(Long postId) { |
| 170 | // 获取帖子 |
| 171 | Optional<Post> postOptional = postRepository.findById(postId); |
| 172 | |
| 173 | // 如果帖子存在 |
| 174 | if (postOptional.isPresent()) { |
| 175 | Post post = postOptional.get(); |
| 176 | Map<String, Object> postData = new LinkedHashMap<>(); |
| 177 | postData.put("postNo", post.getPostNo()); |
| 178 | postData.put("user_id", post.getUser_id()); |
| 179 | Long userId = post.getUser_id(); |
| 180 | Users user = userRepository.findById(userId).orElse(null); |
| 181 | |
| 182 | if (user != null) { |
| 183 | postData.put("username", user.getUsername()); |
| 184 | postData.put("avatar_url", user.getAvatarUrl()); |
| 185 | } else { |
| 186 | postData.put("username", null); |
| 187 | postData.put("avatar_url", null); |
| 188 | } |
| 189 | |
| 190 | postData.put("postContent", post.getPostContent()); |
| 191 | postData.put("imageUrl", post.getImageUrl()); |
| 192 | postData.put("postTime", post.getPostTime()); |
| 193 | postData.put("postLikeNum", post.getPostLikeNum()); |
| 194 | postData.put("postCollectNum", post.getPostCollectNum()); |
| 195 | postData.put("title", post.getTitle()); |
| 196 | |
| 197 | return postData; |
| 198 | } else { |
| 199 | throw new RuntimeException("Post not found with id: " + postId); |
| 200 | } |
| 201 | } |
| 202 | |
| 203 | |
| 204 | //收藏 |
| 205 | public void collectPost(Long postId, Long userId) { |
| 206 | Post post = postRepository.findById(postId).orElseThrow(() -> new RuntimeException("Post not found")); |
| 207 | post.setPostCollectNum(post.getPostCollectNum() + 1); |
| 208 | postRepository.save(post); |
| 209 | |
| 210 | // 添加到收藏表 |
| 211 | Collections collection = new Collections(); |
| 212 | collection.setUserId(userId); |
| 213 | collection.setPostNo(postId); |
| 214 | collectionsRepository.save(collection); |
| 215 | } |
| 216 | |
| 217 | //取消收藏 |
| 218 | public void uncollectPost(Long postId, Long userId) { |
| 219 | Post post = postRepository.findById(postId).orElseThrow(() -> new RuntimeException("Post not found")); |
| 220 | // 减少帖子收藏数 |
| 221 | if (post.getPostCollectNum() > 0) { |
| 222 | post.setPostCollectNum(post.getPostCollectNum() - 1); |
| 223 | postRepository.save(post); |
| 224 | } |
| 225 | |
| 226 | // 从收藏表中删除对应记录 |
| 227 | collectionsRepository.deleteLikeByUserIdAndPostNo(userId, postId); |
| 228 | } |
| 229 | |
| 230 | |
| 231 | |
| 232 | } |