apply .gitignore clean-up
Change-Id: Idd0b57a7340f0e62c85090d84c4fdb8cb5d6fe00
diff --git a/src/main/java/com/example/myproject/service/GroupService.java b/src/main/java/com/example/myproject/service/GroupService.java
index 016d7e0..d3ba029 100644
--- a/src/main/java/com/example/myproject/service/GroupService.java
+++ b/src/main/java/com/example/myproject/service/GroupService.java
@@ -1,3 +1,386 @@
+//package com.example.myproject.service;
+//
+//import com.example.myproject.entity.*;
+//
+//import com.example.myproject.repository.*;
+//import jakarta.transaction.Transactional;
+//import org.springframework.beans.factory.annotation.Autowired;
+//import org.springframework.data.domain.Page;
+//import org.springframework.data.domain.PageRequest;
+//import org.springframework.data.domain.Pageable;
+//import org.springframework.data.domain.Sort;
+//import org.springframework.http.ResponseEntity;
+//import org.springframework.stereotype.Service;
+//import org.springframework.web.multipart.MultipartFile;
+//import java.io.IOException;
+//import java.nio.file.Path;
+//import java.nio.file.Files;
+//
+//import java.util.*;
+//
+//@Service
+//public class GroupService {
+//
+// @Autowired
+// private GroupRepository groupRepository;
+//
+// @Autowired
+// private GroupMembersRepository groupMembersRepository;
+//
+//
+// @Autowired
+// private UserRepository userRepository;
+//
+// @Autowired
+// private GroupPostRepository groupPostRepository;
+//
+// @Autowired
+// private GroupCommentsRepository groupCommentsRepository;
+//
+// private static final String IMAGE_DIR = "uploads/groupPost/";
+//
+// @Transactional
+// // 创建小组的方法
+// public ResponseEntity<Map<String, Object>> createGroup(Group groupRequest) {
+// try {
+// // 创建新的小组对象
+// Group newGroup = new Group();
+// newGroup.setGroupName(groupRequest.getGroupName());
+// newGroup.setDescription(groupRequest.getDescription());
+// newGroup.setCreateTime(new Date());
+// newGroup.setUserId(groupRequest.getUserId());
+// newGroup.setMemberCount(1);
+// newGroup.setCategory(groupRequest.getCategory());
+// newGroup.setCoverImage(groupRequest.getCoverImage());
+// Group createdGroup = groupRepository.save(newGroup);
+//
+// // 返回成功响应
+// Map<String, Object> response = new HashMap<>();
+// response.put("status", "success");
+// response.put("message", "兴趣小组创建成功");
+// response.put("group_id", createdGroup.getGroupId());
+//
+// return ResponseEntity.ok(response);
+// } catch (Exception e) {
+// // 返回失败响应
+// Map<String, Object> errorResponse = new HashMap<>();
+// errorResponse.put("status", "error");
+// errorResponse.put("message", "小组创建失败");
+// errorResponse.put("group_id", null);
+//
+// return ResponseEntity.status(400).body(errorResponse);
+// }
+// }
+//
+//
+// // 加入小组
+// @Transactional
+// public ResponseEntity<Map<String, Object>> joinGroup(Long groupId, Long userId) {
+// // 查找小组
+// Optional<Group> groupOptional = groupRepository.findById(groupId);
+// if (!groupOptional.isPresent()) {
+// return ResponseEntity.status(400).body(
+// Map.of("status", "failure", "message", "小组不存在")
+// );
+// }
+//
+// Group group = groupOptional.get();
+//
+// // 检查用户是否已经是该小组成员
+// if (groupMembersRepository.existsByGroupIdAndUserId(groupId, userId)) {
+// return ResponseEntity.status(400).body(
+// Map.of("status", "failure", "message", "用户已经是该小组的成员")
+// );
+// }
+//
+// // 插入用户和小组的关联记录
+// GroupMembers groupMembers = new GroupMembers();
+// groupMembers.setGroupId(groupId);
+// groupMembers.setUserId(userId);
+// groupMembersRepository.save(groupMembers);
+//
+// // 更新小组的成员数
+// group.setMemberCount(group.getMemberCount() + 1);
+// groupRepository.save(group);
+//
+// return ResponseEntity.ok().body(
+// Map.of("status", "success", "message", "成功加入小组")
+// );
+// }
+//
+// // 退出小组
+// @Transactional
+// public ResponseEntity<Map<String, Object>> leaveGroup(Long groupId, Long userId) {
+// // 查找小组
+// Optional<Group> groupOptional = groupRepository.findById(groupId);
+// if (!groupOptional.isPresent()) {
+// return ResponseEntity.status(400).body(
+// Map.of("status", "failure", "message", "小组不存在")
+// );
+// }
+//
+// Group group = groupOptional.get();
+//
+// // 检查用户是否是该小组成员
+// if (!groupMembersRepository.existsByGroupIdAndUserId(groupId, userId)) {
+// return ResponseEntity.status(400).body(
+// Map.of("status", "failure", "message", "用户不是该小组的成员")
+// );
+// }
+//
+// // 删除用户和小组的关联记录
+// groupMembersRepository.deleteByGroupIdAndUserId(groupId, userId);
+//
+// // 更新小组的成员数
+// group.setMemberCount(group.getMemberCount() - 1);
+// groupRepository.save(group);
+//
+// return ResponseEntity.ok().body(
+// Map.of("status", "success", "message", "成功退出小组")
+// );
+// }
+//
+// // 获取所有成员
+// @Transactional
+// public ResponseEntity<Map<String, Object>> getGroupMembers(Long groupId) {
+// // 查找小组
+// Optional<Group> groupOptional = groupRepository.findById(groupId);
+// if (!groupOptional.isPresent()) {
+// return ResponseEntity.status(400).body(
+// Map.of("status", "failure", "message", "小组不存在")
+// );
+// }
+//
+// Group group = groupOptional.get();
+//
+// // 获取所有小组成员
+// List<GroupMembers> groupMembersList = groupMembersRepository.findByGroupId(groupId);
+//
+// // 创建成员返回列表
+// List<Map<String, Object>> members = new ArrayList<>();
+// for (GroupMembers groupMember : groupMembersList) {
+// Optional<Users> userOptional = userRepository.findById(groupMember.getUserId());
+// if (userOptional.isPresent()) {
+// Users user = userOptional.get();
+// members.add(Map.of(
+// "user_id", user.getUserId(),
+// "username", user.getUsername(),
+// "avatar_url", user.getAvatarUrl()
+// ));
+// }
+// }
+//
+// return ResponseEntity.ok().body(
+// Map.of("status", "success", "members", members)
+// );
+// }
+//
+//
+// //发布帖子
+// public Map<String, Object> createPost(Long groupId, Long userId, String postContent, String title, MultipartFile[] imageFiles) {
+// // 创建一个新的帖子对象
+// GroupPost post = new GroupPost();
+// post.setGroupId(groupId);
+// post.setUserId(userId);
+// post.setContent(postContent);
+// post.setTitle(title);
+// post.setTime(new Date());
+// post.setLikeCount(0);
+// post.setCommentCount(0);
+//
+// // 处理图片上传,如果没有图片,返回空字符串
+// String imageUrls = handleImages(imageFiles);
+// post.setImage(imageUrls);
+//
+// // 保存帖子到数据库
+// GroupPost savedPost = groupPostRepository.save(post);
+//
+// // 返回结果
+// Map<String, Object> response = new LinkedHashMap<>();
+// response.put("post_id", savedPost.getGroupPostId());
+// response.put("message", "帖子创建成功");
+// return response;
+// }
+//
+// // 处理图片上传的方法,返回图片的 URL 字符串
+// private String handleImages(MultipartFile[] imageFiles) {
+// if (imageFiles == null || imageFiles.length == 0) {
+// return "";
+// }
+//
+// StringBuilder imageUrlsBuilder = new StringBuilder();
+//
+// for (int i = 0; i < imageFiles.length; i++) {
+// if (i > 0) {
+// imageUrlsBuilder.append(",");
+// }
+// try {
+// String imageUrl = saveImage(imageFiles[i]);
+// imageUrlsBuilder.append(imageUrl);
+// } catch (IOException e) {
+// throw new RuntimeException("图片上传失败: " + e.getMessage());
+// }
+// }
+// return imageUrlsBuilder.toString();
+// }
+//
+// // 保存图片并返回图片的 URL
+// private String saveImage(MultipartFile imageFile) throws IOException {
+// if (imageFile == null) {
+// throw new IOException("图片文件为空");
+// }
+//
+// String fileName = imageFile.getOriginalFilename();
+// Path path = Path.of(IMAGE_DIR + fileName);
+// Files.createDirectories(path.getParent());
+// Files.write(path, imageFile.getBytes());
+// return "/images/" + fileName;
+// }
+//
+// // 获取小组内的所有帖子
+// public Map<String, Object> getAllPosts(Long groupId) {
+// List<GroupPost> posts = groupPostRepository.findByGroupId(groupId);
+//
+// List<Map<String, Object>> postList = new ArrayList<>();
+//
+// for (GroupPost post : posts) {
+// Map<String, Object> postData = new HashMap<>();
+// Users user = userRepository.findById(post.getUserId()).orElseThrow(() -> new RuntimeException("User not found"));
+// postData.put("group_post_id", post.getGroupPostId());
+// postData.put("user_id", post.getUserId());
+// postData.put("username", user.getUsername());
+// postData.put("title", post.getTitle());
+// postData.put("content", post.getContent());
+// postData.put("time", post.getTime());
+//
+// postList.add(postData);
+// }
+//
+// // 返回响应数据
+// Map<String, Object> response = new LinkedHashMap<>();
+// response.put("status", "success");
+// response.put("posts", postList);
+// return response;
+// }
+//
+//
+// // 获取小组列表接口
+// public Map<String, Object> searchGroups(Map<String, Object> params) {
+// String name = (String) params.get("name");
+// String category = (String) params.get("category");
+// Integer page = (Integer) params.getOrDefault("page", 1);
+// Integer size = (Integer) params.getOrDefault("size", 10);
+// String sortBy = (String) params.getOrDefault("sort_by", "memberCount");
+//
+// // 构建分页对象
+// Pageable pageable;
+//
+// // 根据 `sortBy` 判断排序方式
+// if ("createTime".equals(sortBy)) {
+// pageable = PageRequest.of(page - 1, size, Sort.by(Sort.Order.asc("createTime")));
+// } else if ("groupName".equals(sortBy)) {
+// pageable = PageRequest.of(page - 1, size, Sort.by(Sort.Order.asc("groupName")));
+// } else {
+// pageable = PageRequest.of(page - 1, size, Sort.by(Sort.Order.desc("memberCount")));
+// }
+//
+// Page<Group> groups;
+// if (name != null && !name.isEmpty()) {
+// // 小组名称模糊查询
+// groups = groupRepository.searchGroups(name, category, pageable);
+// } else {
+// groups = groupRepository.searchWithFilters(category, pageable);
+// }
+//
+// return buildResponse(groups);
+// }
+//
+// // 构建返回的响应数据
+// private Map<String, Object> buildResponse(Page<Group> groups) {
+// Map<String, Object> response = new LinkedHashMap<>();
+// response.put("status", "success");
+// response.put("total", groups.getTotalElements());
+// response.put("total_pages", groups.getTotalPages());
+// response.put("current_page", groups.getNumber() + 1);
+// response.put("items", groups.getContent());
+//
+// return response;
+// }
+//
+// // 点赞帖子
+// public Map<String, Object> likePost(Long GroupPostId) {
+// // 查找帖子
+// Optional<GroupPost> postOpt = groupPostRepository.findById(GroupPostId);
+// if (!postOpt.isPresent()) {
+// throw new RuntimeException("Post not found");
+// }
+//
+// GroupPost post = postOpt.get();
+//
+// // 更新点赞数
+// post.setLikeCount(post.getLikeCount() + 1);
+// groupPostRepository.save(post);
+//
+// // 返回成功信息
+// Map<String, Object> response = new HashMap<>();
+// response.put("status", "success");
+// response.put("message", "点赞成功");
+// return response;
+// }
+//
+// // 取消点赞
+// public Map<String, Object> unlikePost(Long GroupPostId) {
+// // 查找帖子
+// Optional<GroupPost> postOpt = groupPostRepository.findById(GroupPostId);
+// if (!postOpt.isPresent()) {
+// throw new RuntimeException("Post not found");
+// }
+//
+// GroupPost post = postOpt.get();
+//
+// // 更新点赞数
+// if (post.getLikeCount() > 0) {
+// post.setLikeCount(post.getLikeCount() - 1);
+// groupPostRepository.save(post);
+//
+// // 返回成功信息
+// Map<String, Object> response = new HashMap<>();
+// response.put("status", "success");
+// response.put("message", "取消点赞成功");
+// return response;
+// } else {
+// // 如果点赞数已经是0,则返回相应消息
+// Map<String, Object> response = new HashMap<>();
+// response.put("status", "failure");
+// response.put("message", "无法取消点赞,点赞数已经为0");
+// return response;
+// }
+// }
+//
+// // 添加评论并更新帖子的评论数
+// public Map<String, Object> addComment(Long postId, GroupComments comment) {
+// // 查找帖子
+// Optional<GroupPost> postOpt = groupPostRepository.findById(postId);
+// if (!postOpt.isPresent()) {
+// throw new RuntimeException("Post not found");
+// }
+//
+// GroupPost post = postOpt.get();
+//
+// comment.setGroupPostId(postId);
+// comment.setTime(new java.util.Date());
+// groupCommentsRepository.save(comment);
+// post.setCommentCount(post.getCommentCount() + 1);
+// groupPostRepository.save(post);
+// Map<String, Object> response = new HashMap<>();
+// response.put("status", "success");
+// response.put("message", "评论成功");
+// return response;
+// }
+//
+//
+//}
+
package com.example.myproject.service;
import com.example.myproject.entity.*;
@@ -39,6 +422,7 @@
private static final String IMAGE_DIR = "uploads/groupPost/";
+
@Transactional
// 创建小组的方法
public ResponseEntity<Map<String, Object>> createGroup(Group groupRequest) {
@@ -129,8 +513,8 @@
}
// 删除用户和小组的关联记录
- groupMembersRepository.deleteByGroupIdAndUserId(groupId, userId);
-
+ int leaveCount = groupMembersRepository.deleteByGroupIdAndUserId(groupId, userId);
+ System.out.println("删除行数"+leaveCount);
// 更新小组的成员数
group.setMemberCount(group.getMemberCount() - 1);
groupRepository.save(group);
diff --git a/src/main/java/com/example/myproject/service/PopularSeedService.java b/src/main/java/com/example/myproject/service/PopularSeedService.java
new file mode 100644
index 0000000..7928632
--- /dev/null
+++ b/src/main/java/com/example/myproject/service/PopularSeedService.java
@@ -0,0 +1,10 @@
+package com.example.myproject.service;
+
+import com.example.myproject.entity.TorrentEntity;
+
+import java.util.List;
+
+public interface PopularSeedService {
+ List<TorrentEntity> getPopularSeeds(int limit);
+ List<TorrentEntity> getRecommendedSeeds(Long userId); // 假设接口中是这个方法
+}
\ No newline at end of file
diff --git a/src/main/java/com/example/myproject/service/RecommendationService.java b/src/main/java/com/example/myproject/service/RecommendationService.java
new file mode 100644
index 0000000..4f78456
--- /dev/null
+++ b/src/main/java/com/example/myproject/service/RecommendationService.java
@@ -0,0 +1,9 @@
+package com.example.myproject.service;
+
+import com.example.myproject.entity.TorrentEntity;
+
+import java.util.List;
+
+public interface RecommendationService {
+ List<TorrentEntity> getRecommendedSeeds(Long userId);
+}
\ No newline at end of file
diff --git a/src/main/java/com/example/myproject/service/SeedRatingService.java b/src/main/java/com/example/myproject/service/SeedRatingService.java
new file mode 100644
index 0000000..26b0fb4
--- /dev/null
+++ b/src/main/java/com/example/myproject/service/SeedRatingService.java
@@ -0,0 +1,58 @@
+package com.example.myproject.service;
+
+import com.example.myproject.entity.SeedRating;
+import com.example.myproject.repository.SeedRatingRepository;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.stereotype.Service;
+
+import java.util.HashMap;
+import java.util.Map;
+import java.util.Optional;
+
+@Service
+public class SeedRatingService {
+
+ @Autowired
+ private SeedRatingRepository seedRatingRepository;
+
+ public Map<String, Object> rateSeed(Long userId, Long seedId, Integer score) {
+ validateScoreRange(score);
+
+ Map<String, Object> response = new HashMap<>();
+
+ try {
+ Optional<SeedRating> existingRating = seedRatingRepository.findByUserIdAndSeedId(userId, seedId);
+
+ SeedRating rating;
+ if (existingRating.isPresent()) {
+ rating = existingRating.get();
+ rating.setScore(score);
+ response.put("message", "评分更新成功");
+ } else {
+ rating = new SeedRating();
+ rating.setUserId(userId);
+ rating.setSeedId(seedId);
+ rating.setScore(score);
+ response.put("message", "新评分提交成功");
+ }
+
+ seedRatingRepository.save(rating);
+
+ response.put("status", "success");
+ response.put("rating_id", rating.getRatingId());
+ response.put("score", rating.getScore());
+
+ } catch (Exception e) {
+ response.put("status", "error");
+ response.put("message", "评分失败: " + e.getMessage());
+ }
+
+ return response;
+ }
+
+ private void validateScoreRange(Integer score) {
+ if (score == null || score < 0 || score > 10) {
+ throw new IllegalArgumentException("评分必须在0-10之间");
+ }
+ }
+}
\ No newline at end of file
diff --git a/src/main/java/com/example/myproject/service/TorrentLikesService.java b/src/main/java/com/example/myproject/service/TorrentLikesService.java
deleted file mode 100644
index 4b96310..0000000
--- a/src/main/java/com/example/myproject/service/TorrentLikesService.java
+++ /dev/null
@@ -1,61 +0,0 @@
-//package com.example.myproject.service;
-//
-//import com.example.myproject.entity.TorrentLikes;
-//import com.example.myproject.repository.TorrentLikesRepository;
-//import org.springframework.stereotype.Service;
-//import org.springframework.transaction.annotation.Transactional;
-//
-//import java.util.HashMap;
-//import java.util.Map;
-//import java.util.Optional;
-//
-//@Service
-//public class TorrentLikesService {
-//
-// private final TorrentLikesRepository repository;
-//
-//
-// public TorrentLikesService(TorrentLikesRepository repository) {
-// this.repository = repository;
-//
-// }
-//
-// /**
-// * 点赞接口核心逻辑
-// * @param userId 用户ID
-// * @param torrentId 种子ID
-// * @return 最终点赞状态(1=已点赞,0=未点赞)
-// */
-// @Transactional
-// public Map<String, Object> toggleLike(Integer userId, Long torrentId) {
-// // 校验torrentId是否存在
-// if (!repository.existsById(torrentId)) {
-// throw new IllegalArgumentException("无效的torrentId: " + torrentId);
-// }
-//
-// Optional<TorrentLikes> existingLike = repository.findByUserIdAndTorrentId(userId, torrentId);
-// Integer newStatus;
-//
-// if (existingLike.isPresent()) {
-// TorrentLikes likes = existingLike.get();
-// Integer currentStatus = likes.getIsLiked();
-// newStatus = currentStatus == 1 ? 0 : 1;
-// repository.updateIsLikedByUserIdAndTorrentId(userId, torrentId, newStatus);
-// } else {
-// TorrentLikes newLike = new TorrentLikes();
-// newLike.setUserId(userId);
-// newLike.setTorrentId(torrentId);
-// newLike.setIsLiked(1);
-// repository.save(newLike);
-// newStatus = 1;
-// }
-//
-// // 构建返回结果
-// Map<String, Object> result = new HashMap<>();
-// result.put("status", newStatus);
-// result.put("message", newStatus == 1 ? "点赞成功" : "取消点赞成功");
-// result.put("userId", userId);
-// result.put("torrentId", torrentId);
-// return result;
-// }
-//}
\ No newline at end of file
diff --git a/src/main/java/com/example/myproject/service/UserService.java b/src/main/java/com/example/myproject/service/UserService.java
index de6bf8b..4315c39 100644
--- a/src/main/java/com/example/myproject/service/UserService.java
+++ b/src/main/java/com/example/myproject/service/UserService.java
@@ -1,5 +1,334 @@
+//package com.example.myproject.service;
+//import cn.dev33.satoken.stp.StpUtil;
+//import com.example.myproject.entity.FriendRelation;
+//import com.example.myproject.entity.User;
+//import com.example.myproject.entity.Users;
+//import com.example.myproject.entity.UserInviteCode;
+//import com.example.myproject.repository.FriendRelationRepository;
+//import com.example.myproject.repository.UserRepository;
+//import com.example.myproject.repository.UserInviteCodeRepository;
+//import org.springframework.beans.factory.annotation.Autowired;
+//import org.springframework.stereotype.Service;
+//import org.springframework.web.multipart.MultipartFile;
+//
+//import javax.servlet.http.HttpServletRequest;
+//import java.io.File;
+//import java.io.IOException;
+//import java.nio.file.Files;
+//import java.nio.file.Path;
+//import java.nio.file.Paths;
+//import java.time.LocalDateTime;
+//import java.util.*;
+//
+//@Service
+//public class UserService {
+//
+// @Autowired
+// private UserRepository userRepository;
+//
+// @Autowired
+// private UserInviteCodeRepository userInviteCodeRepository;
+//
+// @Autowired
+// private FriendRelationRepository friendRelationRepository;
+//
+// // 生成邀请码
+// public Map<String, Object> generateInviteCode(Long userId) {
+// // 获取用户信息
+// Users user = userRepository.findById(userId).orElse(null);
+//
+// // 如果用户不存在,返回错误
+// if (user == null) {
+// Map<String, Object> errorResponse = new HashMap<>();
+// errorResponse.put("status", "failure");
+// errorResponse.put("message", "用户不存在");
+// return errorResponse;
+// }
+//
+// // 检查用户的邀请数量
+// if (user.getInviteCount() <= 0) {
+// Map<String, Object> errorResponse = new HashMap<>();
+// errorResponse.put("status", "failure");
+// errorResponse.put("message", "没有剩余的邀请码");
+// return errorResponse;
+// }
+//
+// // 生成唯一的邀请码
+// String inviteCode = generateUniqueInviteCode();
+//
+// // 将邀请码保存到 `user_invite_code` 表
+// UserInviteCode userInviteCode = new UserInviteCode();
+// userInviteCode.setUserId(userId);
+// userInviteCode.setInviteCode(inviteCode);
+// userInviteCode.setCreatedAt(java.time.LocalDateTime.now());
+//
+// userInviteCodeRepository.save(userInviteCode);
+//
+// // 更新用户的 `invite_count`,减少1
+// user.setInviteCount(user.getInviteCount() - 1);
+// userRepository.save(user);
+//
+// // 返回成功信息
+// Map<String, Object> response = new HashMap<>();
+// response.put("status", "success");
+// response.put("message", "邀请码生成成功");
+// response.put("invite_code", inviteCode);
+//
+// return response;
+// }
+//
+// // 生成唯一的邀请码,使用26个字母(大小写)
+// private String generateUniqueInviteCode() {
+// String characters = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
+// StringBuilder inviteCode = new StringBuilder();
+//
+// Random random = new Random();
+// for (int i = 0; i < 10; i++) {
+// inviteCode.append(characters.charAt(random.nextInt(characters.length())));
+// }
+//
+// return inviteCode.toString();
+// }
+//
+// public String registerUser(String username, String email, String password, String role, String inviteCode) {
+// // 检查邮箱是否已经注册
+// Optional<Users> existingEmailUser = userRepository.findByEmail(email);
+// if (existingEmailUser.isPresent()) {
+// return "该邮箱已被注册";
+// }
+//
+// // 检查用户名是否已经存在
+// Optional<Users> existingUsernameUser = userRepository.findByUsername(username); // 需要根据用户名查询
+// if (existingUsernameUser.isPresent()) {
+// return "该用户名已被注册";
+// }
+//
+// // 检查邀请码是否有效
+// if (inviteCode == null || inviteCode.isEmpty()) {
+// return "邀请码不能为空";
+// }
+//
+// Optional<UserInviteCode> invite = userInviteCodeRepository.findByInviteCode(inviteCode);
+// if (invite.isEmpty() || invite.get().getIsUsed()) {
+// return "邀请码无效或已被使用";
+// }
+//
+// // 设置默认等级为2(由于邀请码有效)
+// Long level = 2L;
+//
+// // 设置默认头像 URL
+// String avatarUrl = "https://example.com/default-avatar.jpg"; // 默认头像
+//
+// // 获取邀请码对应的用户ID
+// UserInviteCode inviteEntity = invite.get();
+// Long inviteUserId = inviteEntity.getUserId();
+//
+// // 创建新用户
+// Users newUser = new Users();
+// newUser.setUsername(username);
+// newUser.setEmail(email);
+// newUser.setPassword(password);
+// newUser.setRole(role);
+// newUser.setInviteCount(0); // 初始邀请码数量为 0
+// newUser.setLevel(level);
+// newUser.setAvatarUrl(avatarUrl); // 设置默认头像
+// newUser.setRegistrationDate(new java.util.Date()); // 设置注册日期
+// newUser.setCurrentExperience(0); // 默认经验为 0
+// newUser.setCurrentSeedingHours(0f); // 默认做种时长为 0
+// newUser.setRegistrationTime(new java.util.Date()); // 设置注册时间为当前时间
+//
+// // 保存用户信息
+// userRepository.save(newUser);
+//
+// FriendRelation newFriendRelation = new FriendRelation();
+// newFriendRelation.setUserId(newUser.getUserId());
+// newFriendRelation.setCreateTime(new Date());
+// newFriendRelation.setFriendId(inviteUserId);
+//
+// FriendRelation newFriendRelations = new FriendRelation();
+// newFriendRelations.setUserId(inviteUserId);
+// newFriendRelations.setCreateTime(new Date());
+// newFriendRelations.setFriendId(newUser.getUserId());
+// friendRelationRepository.save(newFriendRelation);
+// friendRelationRepository.save(newFriendRelations);
+// // 更新邀请码的使用状态
+// inviteEntity.setIsUsed(true);
+// userInviteCodeRepository.save(inviteEntity);
+//
+// return "用户注册成功";
+// }
+//
+//
+// public String loginUser(String username, String password) {
+// // 检查用户是否存在
+// Optional<Users> userOptional = userRepository.findByUsername(username);
+// if (userOptional.isEmpty()) {
+// return "用户名不存在";
+// }
+//
+// Users user = userOptional.get();
+//
+// // 检查密码是否正确
+// if (!user.getPassword().equals(password)) {
+// return "密码错误";
+// }
+// StpUtil.login(user.getUserId());
+//
+// // 登录成功
+// return "登录成功";
+// }
+//
+// public String changePassword(Long userId, String oldPassword, String newPassword, String confirmPassword) {
+// // 查找用户
+// Users user = userRepository.findById(userId).orElse(null);
+//
+// if (user == null) {
+// return "用户不存在";
+// }
+//
+// // 检查旧密码是否正确
+// if (!user.getPassword().equals(oldPassword)) {
+// return "旧密码错误";
+// }
+//
+// // 检查新密码和确认密码是否一致
+// if (!newPassword.equals(confirmPassword)) {
+// return "新密码与确认密码不一致";
+// }
+//
+// // 更新密码
+// user.setPassword(newPassword);
+// userRepository.save(user);
+//
+// return "密码修改成功";
+// }
+//
+// // 获取用户个人资料
+// public Map<String, Object> getProfile(Long userId) {
+// Optional<Users> userOptional = userRepository.findById(userId);
+//
+// // 如果用户不存在,返回null或者可以抛出异常
+// if (userOptional.isEmpty()) {
+// return null; // 可以返回 null 或者根据需要返回错误信息
+// }
+//
+// Users user = userOptional.get();
+//
+// // 将需要的字段放入 Map 中
+// Map<String, Object> profile = new LinkedHashMap<>();
+// profile.put("avatarUrl", user.getAvatarUrl());
+// profile.put("username", user.getUsername());
+// profile.put("email", user.getEmail());
+// profile.put("gender", user.getGender());
+// profile.put("description", user.getDescription());
+// profile.put("hobbies", user.getHobbies());
+// profile.put("level", user.getLevel());
+// profile.put("Experience", user.getCurrentExperience());
+// profile.put("uploadCount", user.getUploadCount());
+// profile.put("downloadCount", user.getDownloadCount());
+// profile.put("shareRate", user.getShareRate());
+// profile.put("registrationTime", user.getRegistrationTime());
+//
+// return profile;
+// }
+//
+// // 修改用户个人资料
+// public boolean editProfile(Long userId, String nickname, String gender, String description, String hobbies) {
+// Optional<Users> userOptional = userRepository.findById(userId);
+//
+// // 如果用户不存在,返回false
+// if (userOptional.isEmpty()) {
+// return false; // 用户不存在
+// }
+//
+// Users user = userOptional.get();
+//
+// // 更新用户资料,只有传入值才会更新对应字段
+// if (nickname != null) {
+// user.setUsername(nickname);
+// }
+// if (gender != null) {
+// user.setGender(gender);
+// }
+// if (description != null) {
+// user.setDescription(description);
+// }
+// if (hobbies != null) {
+// user.setHobbies(hobbies);
+// }
+//
+// // 保存更新后的用户信息
+// userRepository.save(user);
+//
+// return true; // 更新成功
+// }
+//
+// public Map<String, Object> calculateShareRate(Long userId) {
+// // 查找用户
+// Users user = userRepository.findById(userId).orElse(null);
+// if (user == null) {
+// return Map.of("status", "error", "message", "用户不存在");
+// }
+//
+// // 获取上传量和下载量
+// Float uploadCount = user.getUploadCount();
+// Float downloadCount = user.getDownloadCount();
+//
+// // 计算分享率
+// Float shareRate = 0f; // 默认分享率为0
+// if (downloadCount != 0) {
+// shareRate = uploadCount / downloadCount; // 分享率 = 上传量 / 下载量
+// }
+//
+// // 更新用户的分享率
+// user.setShareRate(shareRate);
+// userRepository.save(user);
+//
+// // 返回结果
+// return Map.of("status", "success", "message", "分享率计算成功", "shareRate", shareRate);
+// }
+//
+//
+// private static final String AVATAR_DIR = "uploads/avatarUrl/";
+//
+// public Map<String, Object> uploadUserAvatar(Long userId, MultipartFile file) {
+// Users user = userRepository.findById(userId)
+// .orElseThrow(() -> new RuntimeException("用户不存在"));
+//
+// try {
+// String avatarUrl = saveAvatar(file, userId);
+// user.setAvatarUrl(avatarUrl);
+// userRepository.save(user);
+//
+// Map<String, Object> response = new HashMap<>();
+// response.put("status", "success");
+// response.put("message", "头像上传成功");
+// response.put("userId", user.getUserId());
+// response.put("avatarUrl", avatarUrl);
+// return response;
+//
+// } catch (IOException e) {
+// throw new RuntimeException("头像上传失败: " + e.getMessage());
+// }
+// }
+//
+// // 保存头像文件并返回可访问 URL
+// public String saveAvatar(MultipartFile file, Long userId) throws IOException {
+// String originalFilename = file.getOriginalFilename();
+// String extension = originalFilename.substring(originalFilename.lastIndexOf('.'));
+// String fileName = userId + extension; // 以用户ID作为文件名
+// Path path = Paths.get(AVATAR_DIR + fileName);
+// Files.createDirectories(path.getParent());
+// Files.write(path, file.getBytes());
+// return "/" + AVATAR_DIR + fileName; // 返回相对URL路径
+// }
+//
+//
+//
+//}
+
package com.example.myproject.service;
-import cn.dev33.satoken.stp.StpUtil;
+
import com.example.myproject.entity.FriendRelation;
import com.example.myproject.entity.User;
import com.example.myproject.entity.Users;
@@ -7,6 +336,7 @@
import com.example.myproject.repository.FriendRelationRepository;
import com.example.myproject.repository.UserRepository;
import com.example.myproject.repository.UserInviteCodeRepository;
+import jakarta.transaction.Transactional;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
@@ -17,7 +347,9 @@
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
+import java.time.LocalDate;
import java.time.LocalDateTime;
+import java.time.ZoneId;
import java.util.*;
@Service
@@ -172,8 +504,7 @@
if (!user.getPassword().equals(password)) {
return "密码错误";
}
- StpUtil.login(user.getUserId());
-
+
// 登录成功
return "登录成功";
}
@@ -282,6 +613,8 @@
// 更新用户的分享率
user.setShareRate(shareRate);
+ user.setLastupdatetime(new Date());
+
userRepository.save(user);
// 返回结果
@@ -324,5 +657,63 @@
}
+ @Transactional
+ public String checkUserShareRate(Long userId) {
+ Optional<Users> optionalUser = userRepository.findById(userId);
+
+ if (optionalUser.isEmpty()) {
+ return "用户不存在";
+ }
+
+ Users user = optionalUser.get();
+
+ // 检查用户是否注册超过3天
+ LocalDate registrationDate = user.getRegistrationTime().toInstant()
+ .atZone(ZoneId.systemDefault())
+ .toLocalDate();
+ LocalDate today = LocalDate.now();
+
+ if (today.minusDays(3).isAfter(registrationDate)) {
+ // 用户注册超过3天,检查分享率
+
+ // 假设我们有一个方法可以获取用户最近3天的分享率历史
+ boolean hasLowShareRateForThreeDays = checkShareRateHistory(userId);
+
+ if (hasLowShareRateForThreeDays) {
+ // 分享率连续3天低于0.5
+
+ // 检查是否已发送过警告且超过24小时
+ Date lastUpdateTime = user.getLastupdatetime();
+ if (lastUpdateTime != null) {
+ LocalDate warningDate = lastUpdateTime.toInstant()
+ .atZone(ZoneId.systemDefault())
+ .toLocalDate();
+
+ if (today.minusDays(1).isAfter(warningDate)) {
+ // 已警告超过24小时,执行账号注销
+ userRepository.delete(user);
+ return "用户因分享率长期低于标准,账号已被注销";
+ }
+ }
+
+ // 首次发现或未超过24小时,发送警告
+ user.setLastupdatetime(new Date());
+ userRepository.save(user);
+ return "警告:您的分享率已连续3天低于0.5,请尽快提升,否则账号将被注销";
+ }
+ }
+
+ return "用户状态正常";
+ }
+
+ private boolean checkShareRateHistory(Long userId) {
+ Users user = userRepository.findById(userId).orElse(null);
+ if (user != null && user.getShareRate() < 0.5) {
+ // 实际应用中应查询历史记录,这里简化处理
+ return true;
+ }
+ return false;
+ }
+
}
diff --git a/src/main/java/com/example/myproject/service/serviceImpl/PopularSeedServiceImpl.java b/src/main/java/com/example/myproject/service/serviceImpl/PopularSeedServiceImpl.java
new file mode 100644
index 0000000..5a44417
--- /dev/null
+++ b/src/main/java/com/example/myproject/service/serviceImpl/PopularSeedServiceImpl.java
@@ -0,0 +1,104 @@
+package com.example.myproject.service.serviceImpl;
+
+import com.example.myproject.entity.SeedRating;
+import com.example.myproject.entity.TorrentEntity;
+import com.example.myproject.mapper.SeedRatingMapper;
+import com.example.myproject.mapper.TorrentMapper;
+import com.example.myproject.service.PopularSeedService;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.stereotype.Service;
+
+import java.util.*;
+import java.util.stream.Collectors;
+
+@Service
+public class PopularSeedServiceImpl implements PopularSeedService {
+
+ @Override
+ public List<TorrentEntity> getRecommendedSeeds(Long userId) { // 必须与接口方法一致
+ // 实现推荐逻辑(例如调用热门推荐或个性化推荐)
+ return getPopularSeeds(16); // 示例:调用内部方法获取前16个
+ }
+
+ @Autowired
+ private TorrentMapper torrentMapper;
+
+ @Autowired
+ private SeedRatingMapper seedRatingMapper;
+
+
+ public List<TorrentEntity> getPopularSeeds(int limit) {
+ // 获取所有种子
+ List<TorrentEntity> allSeeds = torrentMapper.selectList(null);
+
+ // 统计每个种子的评分数量
+ Map<Long, Integer> seedRatingCounts = calculateSeedRatingCounts(allSeeds);
+
+ // 计算每个种子的热度分数
+ List<ScoredSeed> scoredSeeds = allSeeds.stream()
+ .filter(seed -> !seed.getIsDeleted()) // 过滤已删除的种子
+ .map(seed -> {
+ double score = calculatePopularityScore(
+ seed.getSeeders(), // 做种数
+ seed.getLeechers(), // 下载量
+ seedRatingCounts.getOrDefault(seed.getId(), 0) // 评分数量
+ );
+ return new ScoredSeed(seed, score);
+ })
+ .sorted(Comparator.comparingDouble(ScoredSeed::getScore).reversed())
+ .limit(limit)
+ .collect(Collectors.toList());
+
+ // 4. 返回热门种子列表
+ return scoredSeeds.stream()
+ .map(ScoredSeed::getSeed)
+ .collect(Collectors.toList());
+ }
+
+ // 统计种子的评分数量
+ private Map<Long, Integer> calculateSeedRatingCounts(List<TorrentEntity> seeds) {
+ if (seeds.isEmpty()) return Collections.emptyMap();
+
+ // 获取所有种子ID
+ List<Long> seedIds = seeds.stream()
+ .map(TorrentEntity::getId)
+ .collect(Collectors.toList());
+
+ // 查询所有评分记录
+ List<SeedRating> ratings = seedRatingMapper.selectBySeedIds(seedIds);
+
+ // 按种子ID分组统计评分数量
+ return ratings.stream()
+ .collect(Collectors.groupingBy(
+ SeedRating::getSeedId,
+ Collectors.summingInt(r -> 1)
+ ));
+ }
+
+ // 计算热度分数
+ private double calculatePopularityScore(int seeders, int leechers, int ratingCount) {
+ // 综合得分公式
+ return 0.4 * Math.log(seeders + 1) + // 做种数权重40%
+ 0.3 * Math.log(leechers + 1) + // 下载量权重30%
+ 0.3 * Math.log(ratingCount + 1); // 评分数量权重30%
+ }
+
+ // 带分数的种子
+ private static class ScoredSeed {
+ private final TorrentEntity seed;
+ private final double score;
+
+ public ScoredSeed(TorrentEntity seed, double score) {
+ this.seed = seed;
+ this.score = score;
+ }
+
+ public TorrentEntity getSeed() {
+ return seed;
+ }
+
+ public double getScore() {
+ return score;
+ }
+ }
+}
\ No newline at end of file
diff --git a/src/main/java/com/example/myproject/service/serviceImpl/RecommendationServiceImpl.java b/src/main/java/com/example/myproject/service/serviceImpl/RecommendationServiceImpl.java
new file mode 100644
index 0000000..a52817c
--- /dev/null
+++ b/src/main/java/com/example/myproject/service/serviceImpl/RecommendationServiceImpl.java
@@ -0,0 +1,327 @@
+package com.example.myproject.service.serviceImpl;
+
+import com.example.myproject.entity.FavoriteEntity;
+import com.example.myproject.entity.SeedRating;
+import com.example.myproject.entity.TorrentEntity;
+import com.example.myproject.mapper.FavoriteMapper;
+import com.example.myproject.mapper.SeedRatingMapper;
+import com.example.myproject.mapper.TorrentMapper;
+import com.example.myproject.service.RecommendationService;
+import com.google.common.cache.CacheBuilder;
+import com.google.common.cache.CacheLoader;
+import com.google.common.cache.LoadingCache;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.stereotype.Service;
+
+import java.util.*;
+import java.util.concurrent.ExecutionException;
+import java.util.concurrent.TimeUnit;
+import java.util.stream.Collectors;
+
+@Service
+public class RecommendationServiceImpl implements RecommendationService {
+
+ @Autowired
+ private FavoriteMapper favoriteMapper;
+
+ @Autowired
+ private SeedRatingMapper seedRatingMapper;
+
+ @Autowired
+ private TorrentMapper torrentMapper;
+
+ // 标签预处理工具
+ private final Map<String, String> synonymMap = new HashMap<>();
+ private final Set<String> stopWords = new HashSet<>();
+
+ // 用户兴趣缓存
+ private LoadingCache<Long, UserInterestModel> userInterestCache;
+
+ // 全局标签频率缓存
+ private LoadingCache<String, Map<String, Integer>> globalTagFrequencyCache;
+
+ public RecommendationServiceImpl() {
+
+ // 初始化用户兴趣缓存
+ userInterestCache = CacheBuilder.newBuilder()
+ .maximumSize(1000)
+ .expireAfterWrite(1, TimeUnit.HOURS)
+ .build(new CacheLoader<Long, UserInterestModel>() {
+ @Override
+ public UserInterestModel load(Long userId) {
+ return buildUserInterestModel(userId);
+ }
+ });
+
+ // 初始化全局标签频率缓存
+ globalTagFrequencyCache = CacheBuilder.newBuilder()
+ .maximumSize(1)
+ .expireAfterWrite(24, TimeUnit.HOURS)
+ .build(new CacheLoader<String, Map<String, Integer>>() {
+ @Override
+ public Map<String, Integer> load(String key) {
+ return calculateGlobalTagFrequency();
+ }
+ });
+ }
+
+ @Override
+ public List<TorrentEntity> getRecommendedSeeds(Long userId) {
+ try {
+ // 从缓存获取用户兴趣模型
+ UserInterestModel interestModel = userInterestCache.get(userId);
+
+ // 获取所有种子(实际应用中应优化为分页或条件查询)
+ List<TorrentEntity> allSeeds = torrentMapper.selectList(null);
+ int totalSeeds = allSeeds.size();
+
+ // 计算全局标签频率
+ Map<String, Integer> globalTagFrequency = globalTagFrequencyCache.get("global");
+
+ // 计算推荐分数
+ List<ScoredSeed> scoredSeeds = new ArrayList<>();
+ for (TorrentEntity seed : allSeeds) {
+ // 跳过用户已交互的种子
+ if (interestModel.getUserInteractedSeedIds().contains(seed.getId())) {
+ continue;
+ }
+
+ // 计算余弦相似度
+ double similarityScore = calculateCosineSimilarity(
+ seed, interestModel, totalSeeds, globalTagFrequency);
+
+ if (similarityScore > 0) {
+ scoredSeeds.add(new ScoredSeed(seed, similarityScore));
+ }
+ }
+
+ // 排序并取前16个
+ scoredSeeds.sort(Comparator.comparingDouble(ScoredSeed::getScore).reversed());
+ return scoredSeeds.stream()
+ .limit(16)
+ .map(ScoredSeed::getSeed)
+ .collect(Collectors.toList());
+
+ } catch (ExecutionException e) {
+ // 缓存加载异常处理
+ throw new RuntimeException("Failed to load user interest model", e);
+ }
+ }
+
+ // 构建用户兴趣模型
+ private UserInterestModel buildUserInterestModel(Long userId) {
+ // 获取用户收藏的种子ID
+ List<FavoriteEntity> favorites = favoriteMapper.selectByUserId(userId);
+ List<Long> favoriteSeedIds = favorites.stream()
+ .map(FavoriteEntity::getSeedId)
+ .collect(Collectors.toList());
+
+ // 获取用户评分>=7的种子ID,并应用评分权重
+ List<SeedRating> highRatings = seedRatingMapper.selectHighRatingsByUserId(userId);
+ Map<Long, Double> ratingWeightMap = new HashMap<>();
+ List<Long> highRatingSeedIds = new ArrayList<>();
+
+ for (SeedRating rating : highRatings) {
+ // 评分权重:评分越高权重越大(线性缩放至1-3倍)
+ double ratingWeight = 1.0 + (rating.getScore() - 7) * 0.5;
+ ratingWeightMap.put(rating.getSeedId(), ratingWeight);
+ highRatingSeedIds.add(rating.getSeedId());
+ }
+
+ // 合并用户感兴趣的种子ID
+ Set<Long> userInterestedSeedIds = new HashSet<>();
+ userInterestedSeedIds.addAll(favoriteSeedIds);
+ userInterestedSeedIds.addAll(highRatingSeedIds);
+
+
+ // 获取用户感兴趣种子的元数据
+ List<TorrentEntity> userInterestedSeeds = torrentMapper.selectBatchIds(userInterestedSeedIds);
+
+
+ // 计算分类和标签,标题权重
+ Map<String, Double> categoryWeight = new HashMap<>();
+ Map<String, Double> tagWeight = new HashMap<>();
+ Set<String> userInterestedTitleKeywords = new HashSet<>();
+
+ for (TorrentEntity seed : userInterestedSeeds) {
+ // 获取评分权重(默认为1.0)
+ double weightFactor = ratingWeightMap.getOrDefault(seed.getId(), 1.0);
+
+ // 分类权重计算
+ String category = seed.getCategory();
+ categoryWeight.put(category, categoryWeight.getOrDefault(category, 0.0) + weightFactor);
+
+ // 标签权重计算
+ List<String> tags = processTags(seed.getTags());
+ for (String tag : tags) {
+ tagWeight.put(tag, tagWeight.getOrDefault(tag, 0.0) + weightFactor);
+ }
+
+ // 提取标题关键词
+ List<String> titleKeywords = extractTitleKeywords(seed.getTitle());
+ userInterestedTitleKeywords.addAll(titleKeywords);
+ }
+
+ return new UserInterestModel(
+ userInterestedSeedIds,
+ categoryWeight,
+ tagWeight,
+ userInterestedTitleKeywords
+ );
+ }
+
+ // 标签预处理与标准化
+ private List<String> processTags(String tags) {
+ if (tags == null || tags.isEmpty()) return Collections.emptyList();
+
+ return Arrays.stream(tags.replaceAll("[\\[\\]\"']", "").split(","))
+ .map(String::trim)
+ .filter(tag -> !tag.isEmpty() && !stopWords.contains(tag.toLowerCase()))
+ .map(tag -> synonymMap.getOrDefault(tag.toLowerCase(), tag))
+ .collect(Collectors.toList());
+ }
+
+ // 从标题中提取关键词
+ private List<String> extractTitleKeywords(String title) {
+ if (title == null || title.isEmpty()) return Collections.emptyList();
+
+ return Arrays.stream(title.replaceAll("[^\\w\\s]", "").split("\\s+"))
+ .map(String::trim)
+ .filter(word -> !word.isEmpty() && !stopWords.contains(word.toLowerCase()))
+ .map(word -> synonymMap.getOrDefault(word.toLowerCase(), word))
+ .collect(Collectors.toList());
+ }
+
+ // 计算全局标签频率
+ private Map<String, Integer> calculateGlobalTagFrequency() {
+ Map<String, Integer> globalTagCount = new HashMap<>();
+ List<TorrentEntity> allSeeds = torrentMapper.selectList(null);
+
+ for (TorrentEntity seed : allSeeds) {
+ List<String> tags = processTags(seed.getTags());
+ Set<String> uniqueTags = new HashSet<>(tags);
+ for (String tag : uniqueTags) {
+ globalTagCount.put(tag, globalTagCount.getOrDefault(tag, 0) + 1);
+ }
+ }
+ return globalTagCount;
+ }
+
+ // 计算标签的IDF值
+ private double calculateTagIDF(String tag, int totalSeeds, Map<String, Integer> globalTagCount) {
+ int tagCount = globalTagCount.getOrDefault(tag, 1); // 避免除零
+ return Math.log((double) totalSeeds / tagCount);
+ }
+
+ // 计算余弦相似度
+ private double calculateCosineSimilarity(TorrentEntity seed,
+ UserInterestModel interestModel,
+ int totalSeeds,
+ Map<String, Integer> globalTagCount) {
+ double dotProduct = 0.0;
+ double userVectorNorm = 0.0;
+ double seedVectorNorm = 0.0;
+
+ // 处理分类相似度
+ String seedCategory = seed.getCategory();
+ if (interestModel.getCategoryWeight().containsKey(seedCategory)) {
+ double userCategoryWeight = interestModel.getCategoryWeight().get(seedCategory);
+ double seedCategoryWeight = 1.0; // 种子的分类权重为1
+ dotProduct += userCategoryWeight * seedCategoryWeight;
+ userVectorNorm += Math.pow(userCategoryWeight, 2);
+ seedVectorNorm += Math.pow(seedCategoryWeight, 2);
+ }
+
+ // 处理标签相似度(应用TF-IDF)
+ List<String> seedTags = processTags(seed.getTags());
+ for (String tag : seedTags) {
+ if (interestModel.getTagWeight().containsKey(tag)) {
+ // 用户兴趣标签权重 × IDF
+ double userTagWeight = interestModel.getTagWeight().get(tag) *
+ calculateTagIDF(tag, totalSeeds, globalTagCount);
+ double seedTagWeight = 1.0; // 种子的标签权重为1
+ dotProduct += userTagWeight * seedTagWeight;
+ userVectorNorm += Math.pow(userTagWeight, 2);
+ seedVectorNorm += Math.pow(seedTagWeight, 2);
+ }
+ }
+
+ // 处理标题相似度
+ double titleMatchScore = calculateTitleMatchScore(seed.getTitle(), interestModel.getUserInterestedTitleKeywords());
+ dotProduct += titleMatchScore ; // 标题匹配权重(可调整)
+ userVectorNorm += Math.pow(0.3, 2);
+ seedVectorNorm += Math.pow(titleMatchScore, 2);
+
+ if (userVectorNorm == 0 || seedVectorNorm == 0) return 0.0;
+ return dotProduct / (Math.sqrt(userVectorNorm) * Math.sqrt(seedVectorNorm));
+ }
+
+ // 计算标题匹配分数
+ private double calculateTitleMatchScore(String seedTitle, Set<String> userInterestedTitleKeywords) {
+ if (seedTitle == null || seedTitle.isEmpty() || userInterestedTitleKeywords.isEmpty()) {
+ return 0.0;
+ }
+
+ List<String> seedTitleKeywords = extractTitleKeywords(seedTitle);
+ if (seedTitleKeywords.isEmpty()) return 0.0;
+
+ long matchCount = seedTitleKeywords.stream()
+ .filter(userInterestedTitleKeywords::contains)
+ .count();
+
+ return (double) matchCount / seedTitleKeywords.size();
+ }
+
+ // 用户兴趣模型
+ private static class UserInterestModel {
+ private final Set<Long> userInteractedSeedIds;
+ private final Map<String, Double> categoryWeight;
+ private final Map<String, Double> tagWeight;
+ private final Set<String> userInterestedTitleKeywords;
+
+ public UserInterestModel(Set<Long> userInteractedSeedIds,
+ Map<String, Double> categoryWeight,
+ Map<String, Double> tagWeight,
+ Set<String> userInterestedTitleKeywords) {
+ this.userInteractedSeedIds = userInteractedSeedIds;
+ this.categoryWeight = categoryWeight;
+ this.tagWeight = tagWeight;
+ this.userInterestedTitleKeywords = userInterestedTitleKeywords;
+ }
+
+ public Set<Long> getUserInteractedSeedIds() {
+ return userInteractedSeedIds;
+ }
+
+ public Map<String, Double> getCategoryWeight() {
+ return categoryWeight;
+ }
+
+ public Map<String, Double> getTagWeight() {
+ return tagWeight;
+ }
+
+ public Set<String> getUserInterestedTitleKeywords() {
+ return userInterestedTitleKeywords;
+ }
+ }
+
+ // 带分数的种子
+ private static class ScoredSeed {
+ private final TorrentEntity seed;
+ private final double score;
+
+ public ScoredSeed(TorrentEntity seed, double score) {
+ this.seed = seed;
+ this.score = score;
+ }
+
+ public TorrentEntity getSeed() {
+ return seed;
+ }
+
+ public double getScore() {
+ return score;
+ }
+ }
+}
\ No newline at end of file