apply .gitignore clean-up
Change-Id: Idd0b57a7340f0e62c85090d84c4fdb8cb5d6fe00
diff --git a/src/main/java/com/example/myproject/controller/RecommendationController.java b/src/main/java/com/example/myproject/controller/RecommendationController.java
new file mode 100644
index 0000000..d99c4d0
--- /dev/null
+++ b/src/main/java/com/example/myproject/controller/RecommendationController.java
@@ -0,0 +1,39 @@
+package com.example.myproject.controller;
+
+import com.example.myproject.entity.TorrentEntity;
+import com.example.myproject.service.PopularSeedService;
+import com.example.myproject.service.RecommendationService;
+import io.swagger.annotations.Api;
+import io.swagger.annotations.ApiOperation;
+import io.swagger.annotations.ApiParam;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.web.bind.annotation.*;
+
+import java.util.List;
+
+@RestController
+@RequestMapping("/echo/recommendation")
+@Api("推荐接口")
+public class RecommendationController {
+
+ @Autowired
+ private RecommendationService recommendationService;
+
+ @Autowired
+ private PopularSeedService popularSeedService;
+
+ @GetMapping("/seeds/{userId}")
+ @ApiOperation("获取基于内容的种子推荐")
+ public List<TorrentEntity> getRecommendedSeeds(@PathVariable Long userId) {
+ return recommendationService.getRecommendedSeeds(userId);
+ }
+
+ @GetMapping("/popular")
+ @ApiOperation("获取热门种子推荐")
+ public List<TorrentEntity> getPopularSeeds(
+ @ApiParam(value = "返回数量", defaultValue = "16")
+ @RequestParam(defaultValue = "16") int limit
+ ) {
+ return popularSeedService.getPopularSeeds(limit);
+ }
+}
\ No newline at end of file
diff --git a/src/main/java/com/example/myproject/controller/SeedRatingController.java b/src/main/java/com/example/myproject/controller/SeedRatingController.java
new file mode 100644
index 0000000..16e0b22
--- /dev/null
+++ b/src/main/java/com/example/myproject/controller/SeedRatingController.java
@@ -0,0 +1,26 @@
+package com.example.myproject.controller;
+
+import com.example.myproject.service.SeedRatingService;
+import org.springframework.web.bind.annotation.*;
+
+import java.util.Map;
+
+@RestController
+@RequestMapping("/echo/ratings")
+public class SeedRatingController {
+
+ private final SeedRatingService ratingService;
+
+ public SeedRatingController(SeedRatingService ratingService) {
+ this.ratingService = ratingService;
+ }
+
+ @PostMapping
+ public Map<String, Object> submitRating(
+ @RequestParam("userId") Long userId,
+ @RequestParam("seedId") Long seedId,
+ @RequestParam("score") Integer score
+ ) {
+ return ratingService.rateSeed(userId, seedId, score);
+ }
+}
\ No newline at end of file
diff --git a/src/main/java/com/example/myproject/controller/TorrentLikesController.java b/src/main/java/com/example/myproject/controller/TorrentLikesController.java
deleted file mode 100644
index 7419faf..0000000
--- a/src/main/java/com/example/myproject/controller/TorrentLikesController.java
+++ /dev/null
@@ -1,32 +0,0 @@
-//package com.example.myproject.controller;
-//
-//import com.example.myproject.service.TorrentLikesService;
-//import org.springframework.web.bind.annotation.*;
-//
-//import java.util.Map;
-//
-//@RestController
-//@RequestMapping("/echo/torrent")
-//public class TorrentLikesController {
-//
-// private final TorrentLikesService service;
-//
-// public TorrentLikesController(TorrentLikesService service) {
-// this.service = service;
-// }
-//
-// /**
-// * 点赞接口
-// * @param userId 用户ID(请求参数:user_id)
-// * @param torrentId 种子ID(请求参数:torrent_id)
-// * @return 操作结果:{"status": 1/0}
-// */
-// @PostMapping("/{userId}/{torrentId}")
-// public Map<String, Object> toggleLike(
-// @PathVariable Integer userId,
-// @PathVariable Long torrentId) {
-// return service.toggleLike(userId, torrentId);
-// }
-//
-//
-//}
\ No newline at end of file
diff --git a/src/main/java/com/example/myproject/controller/UserController.java b/src/main/java/com/example/myproject/controller/UserController.java
index 41a3350..dc4073b 100644
--- a/src/main/java/com/example/myproject/controller/UserController.java
+++ b/src/main/java/com/example/myproject/controller/UserController.java
@@ -1,3 +1,362 @@
+//// package com.example.myproject.controller;
+//
+//// import com.example.myproject.entity.Users;
+//// import com.example.myproject.repository.UserRepository;
+//// import com.example.myproject.service.DynamicService;
+//// import com.example.myproject.service.TaskService;
+//// import com.example.myproject.service.UserService;
+//// import org.springframework.beans.factory.annotation.Autowired;
+//// import org.springframework.http.ResponseEntity;
+//// import org.springframework.web.bind.annotation.*;
+//// import org.springframework.web.multipart.MultipartFile;
+//
+//// import javax.servlet.http.HttpServletRequest;
+//// import java.util.Map;
+//// import java.util.Optional;
+//
+//// import java.util.List;
+//// import java.util.ArrayList;
+//
+//
+//// @RestController
+//// @RequestMapping("/echo/user")
+//// public class UserController {
+//
+//// @Autowired
+//// private UserService userService;
+//
+//// @Autowired
+//// private UserRepository userRepository;
+//
+//// @Autowired
+//// private DynamicService dynamicService;
+//
+//// // 接口:生成邀请码
+//// @PostMapping("/getInviteCode")
+//// public Map<String, Object> generateInviteCode(@RequestBody Map<String, Object> request) {
+//// Long userId = Long.parseLong(request.get("user_id").toString());
+//// return userService.generateInviteCode(userId);
+//// }
+//
+//// //注册
+//// @PostMapping("/register")
+//// public Map<String, Object> register(@RequestBody Map<String, Object> request) {
+//// String username = (String) request.get("username");
+//// String email = (String) request.get("email");
+//// String password = (String) request.get("password");
+//// String role = (String) request.get("role");
+//// String inviteCode = (String) request.get("inviteCode");
+//
+//// // 调用服务层的注册方法
+//// String resultMessage = userService.registerUser(username, email, password, role, inviteCode);
+//
+//// // 返回注册结果
+//// return Map.of("msg", resultMessage);
+//// }
+//
+//// //登录
+//// @PostMapping("/login")
+//// public Map<String, Object> login(@RequestBody Map<String, Object> request) {
+//// String username = (String) request.get("username");
+//// String password = (String) request.get("password");
+//
+//// // 调用服务层的登录方法
+//// String resultMessage = userService.loginUser(username, password);
+//
+//// // 根据登录结果返回不同的响应
+//// if (resultMessage.equals("登录成功")) {
+//// // 查询用户信息
+//// Optional<Users> user = userRepository.findByUsername(username);
+//// if (user.isPresent()) {
+//// // 将用户的所有信息作为返回值
+//// return Map.of("msg", resultMessage, "user", user.get());
+//// } else {
+//// return Map.of("msg", "用户信息查询失败");
+//// }
+//// } else {
+//// return Map.of("msg", resultMessage);
+//// }
+//// }
+//
+//// //修改密码
+//// @PostMapping("/password")
+//// public Map<String, Object> changePassword(@RequestBody Map<String, Object> request) {
+//// Long userId = Long.parseLong(request.get("user_id").toString());
+//// String oldPassword = (String) request.get("old_password");
+//// String newPassword = (String) request.get("new_password");
+//// String confirmPassword = (String) request.get("confirm_password");
+//
+//// // 调用服务层的修改密码方法
+//// String resultMessage = userService.changePassword(userId, oldPassword, newPassword, confirmPassword);
+//
+//// // 返回修改结果
+//// return Map.of("message", resultMessage, "status", resultMessage.equals("密码修改成功") ? "success" : "error");
+//// }
+//
+//// // 获取用户个人资料
+//// @GetMapping("/{userId}/getProfile")
+//// public Map<String, Object> getProfile(@PathVariable("userId") Long userId) {
+//// return userService.getProfile(userId);
+//// }
+//
+//// // 修改用户个人资料
+//// @PutMapping("/{userId}/editProfile")
+//// public Map<String, String> editProfile(
+//// @PathVariable("userId") Long userId,
+//// @RequestBody Map<String, Object> profileData) {
+//
+//// // 获取请求体中的修改数据
+//// String nickname = (String) profileData.get("nickname");
+//// String gender = (String) profileData.get("gender");
+//// String description = (String) profileData.get("description");
+//// String hobbies = (String) profileData.get("hobbies");
+//
+//// // 调用服务层方法进行修改
+//// boolean updated = userService.editProfile(userId, nickname, gender, description, hobbies);
+//
+//// // 返回操作结果消息
+//// if (updated) {
+//// return Map.of("message", "用户资料更新成功");
+//// } else {
+//// return Map.of("message", "用户不存在");
+//// }
+//// }
+//
+//// // 计算分享率
+//// @GetMapping("/{user_id}/calculate-share-rate")
+//// public Map<String, Object> calculateShareRate(@PathVariable("user_id") Long userId) {
+//// return userService.calculateShareRate(userId);
+//// }
+//
+//// // 获取用户所有好友的基本信息
+//// @GetMapping("/{userId}/friends")
+//// public List<Map<String, Object>> getUserFriends(@PathVariable("userId") Long userId) {
+//// List<Long> friendIds = dynamicService.getAllFriendIds(userId); // 注意这里用的是实例对象
+//// List<Map<String, Object>> friends = new ArrayList<>();
+//
+//// for (Long friendId : friendIds) {
+//// Optional<Users> userOpt = userRepository.findById(friendId);
+//// if (userOpt.isPresent()) {
+//// Users user = userOpt.get();
+//// Map<String, Object> friendInfo = Map.of(
+//// "id", user.getUserId(),
+//// "avatar", user.getAvatarUrl() != null ? user.getAvatarUrl() : "https://example.com/default-avatar.jpg",
+//// "nickname", user.getUsername() != null ? user.getUsername() : "未知用户",
+//// "email", user.getEmail() != null ? user.getEmail() : "未填写"
+//// );
+//// friends.add(friendInfo);
+//// }
+//// }
+//
+//// return friends;
+//// }
+//
+//// @PostMapping("/{userId}/uploadAvatar")
+//// public Map<String, Object> uploadAvatar(
+//// @PathVariable Long userId,
+//// @RequestParam("file") MultipartFile file) {
+//// return userService.uploadUserAvatar(userId, file);
+//// }
+//
+//
+//
+//// }
+//
+//package com.example.myproject.controller;
+//
+//import com.example.myproject.entity.FriendRelation;
+//import com.example.myproject.entity.Users;
+//import com.example.myproject.repository.FriendRelationRepository;
+//import com.example.myproject.repository.UserRepository;
+//import com.example.myproject.service.DynamicService;
+//import com.example.myproject.service.UserService;
+//import org.springframework.beans.factory.annotation.Autowired;
+//import org.springframework.web.bind.annotation.*;
+//import org.springframework.web.multipart.MultipartFile;
+//
+//import java.util.*;
+//
+//
+//@RestController
+//@RequestMapping("/echo/user")
+//public class UserController {
+//
+// @Autowired
+// private UserService userService;
+//
+// @Autowired
+// private UserRepository userRepository;
+//
+// @Autowired
+// private DynamicService dynamicService;
+//
+// @Autowired
+// private FriendRelationRepository friendRelationRepository;
+//
+//
+// @PostMapping ("/addFriend")
+// public Map<String, Object> addFriend(@RequestBody Map<String, Object> request) {
+// //邮箱查用户
+// Optional<Users> userOptional = userRepository.findByEmail(request.get("email").toString());
+// // 如果用户不存在,返回null或者可以抛出异常
+// if (userOptional.isEmpty()) {
+// return Map.of("msg", "没有找到该用户"); // 可以返回 null 或者根据需要返回错误信息
+// }
+// Long userId = Long.parseLong(request.get("userid").toString()); //id查用户
+// Map<String, Object> users = userService.getProfile(userId);
+// // 如果用户不存在,返回null或者可以抛出异常
+// if (users.isEmpty()) {
+// return Map.of("msg", "当前登录账号异常"); // 可以返回 null 或者根据需要返回错误信息
+// }
+//
+// FriendRelation newFriendRelation = new FriendRelation();
+// newFriendRelation.setUserId(userId);
+// newFriendRelation.setCreateTime(new Date());
+// newFriendRelation.setFriendId(userOptional.get().getUserId());
+//
+// FriendRelation newFriendRelations = new FriendRelation();
+// newFriendRelations.setCreateTime(new Date());
+// newFriendRelations.setUserId(userOptional.get().getUserId());
+// newFriendRelations.setFriendId(userId);
+//
+// friendRelationRepository.save(newFriendRelation);
+// friendRelationRepository.save(newFriendRelations);
+//
+// return Map.of("msg", "添加成功");
+// }
+//
+// // 接口:生成邀请码
+// @PostMapping("/getInviteCode")
+// public Map<String, Object> generateInviteCode(@RequestBody Map<String, Object> request) {
+// Long userId = Long.parseLong(request.get("user_id").toString());
+// return userService.generateInviteCode(userId);
+// }
+//
+// //注册
+// @PostMapping("/register")
+// public Map<String, Object> register(@RequestBody Map<String, Object> request) {
+// String username = (String) request.get("username");
+// String email = (String) request.get("email");
+// String password = (String) request.get("password");
+// String role = (String) request.get("role");
+// String inviteCode = (String) request.get("inviteCode");
+//
+// // 调用服务层的注册方法
+// String resultMessage = userService.registerUser(username, email, password, role, inviteCode);
+//
+// // 返回注册结果
+// return Map.of("msg", resultMessage);
+// }
+//
+// //登录
+// @PostMapping("/login")
+// public Map<String, Object> login(@RequestBody Map<String, Object> request) {
+// String username = (String) request.get("username");
+// String password = (String) request.get("password");
+//
+// // 调用服务层的登录方法
+// String resultMessage = userService.loginUser(username, password);
+//
+// // 根据登录结果返回不同的响应
+// if (resultMessage.equals("登录成功")) {
+// // 查询用户信息
+// Optional<Users> user = userRepository.findByUsername(username);
+// if (user.isPresent()) {
+// // 将用户的所有信息作为返回值
+// return Map.of("msg", resultMessage, "user", user.get());
+// } else {
+// return Map.of("msg", "用户信息查询失败");
+// }
+// } else {
+// return Map.of("msg", resultMessage);
+// }
+// }
+//
+// //修改密码
+// @PostMapping("/password")
+// public Map<String, Object> changePassword(@RequestBody Map<String, Object> request) {
+// Long userId = Long.parseLong(request.get("user_id").toString());
+// String oldPassword = (String) request.get("old_password");
+// String newPassword = (String) request.get("new_password");
+// String confirmPassword = (String) request.get("confirm_password");
+//
+// // 调用服务层的修改密码方法
+// String resultMessage = userService.changePassword(userId, oldPassword, newPassword, confirmPassword);
+//
+// // 返回修改结果
+// return Map.of("message", resultMessage, "status", resultMessage.equals("密码修改成功") ? "success" : "error");
+// }
+//
+// // 获取用户个人资料
+// @GetMapping("/{userId}/getProfile")
+// public Map<String, Object> getProfile(@PathVariable("userId") Long userId) {
+// return userService.getProfile(userId);
+// }
+//
+// // 修改用户个人资料
+// @PutMapping("/{userId}/editProfile")
+// public Map<String, String> editProfile(
+// @PathVariable("userId") Long userId,
+// @RequestBody Map<String, Object> profileData) {
+//
+// // 获取请求体中的修改数据
+// String nickname = (String) profileData.get("nickname");
+// String gender = (String) profileData.get("gender");
+// String description = (String) profileData.get("description");
+// String hobbies = (String) profileData.get("hobbies");
+//
+// // 调用服务层方法进行修改
+// boolean updated = userService.editProfile(userId, nickname, gender, description, hobbies);
+//
+// // 返回操作结果消息
+// if (updated) {
+// return Map.of("message", "用户资料更新成功");
+// } else {
+// return Map.of("message", "用户不存在");
+// }
+// }
+//
+// // 计算分享率
+// @GetMapping("/{user_id}/calculate-share-rate")
+// public Map<String, Object> calculateShareRate(@PathVariable("user_id") Long userId) {
+// return userService.calculateShareRate(userId);
+// }
+//
+// // 获取用户所有好友的基本信息
+// @GetMapping("/{userId}/friends")
+// public List<Map<String, Object>> getUserFriends(@PathVariable("userId") Long userId) {
+// List<Long> friendIds = dynamicService.getAllFriendIds(userId); // 注意这里用的是实例对象
+// List<Map<String, Object>> friends = new ArrayList<>();
+//
+// for (Long friendId : friendIds) {
+// Optional<Users> userOpt = userRepository.findById(friendId);
+// if (userOpt.isPresent()) {
+// Users user = userOpt.get();
+// Map<String, Object> friendInfo = Map.of(
+// "id", user.getUserId(),
+// "avatar", user.getAvatarUrl() != null ? user.getAvatarUrl() : "https://example.com/default-avatar.jpg",
+// "nickname", user.getUsername() != null ? user.getUsername() : "未知用户",
+// "email", user.getEmail() != null ? user.getEmail() : "未填写"
+// );
+// friends.add(friendInfo);
+// }
+// }
+//
+// return friends;
+// }
+//
+// @PostMapping("/{userId}/uploadAvatar")
+// public Map<String, Object> uploadAvatar(
+// @PathVariable Long userId,
+// @RequestParam("file") MultipartFile file) {
+// return userService.uploadUserAvatar(userId, file);
+// }
+//
+//
+//
+//}
+//
+
// package com.example.myproject.controller;
// import com.example.myproject.entity.Users;
@@ -352,6 +711,10 @@
return userService.uploadUserAvatar(userId, file);
}
+ @GetMapping("/users/{userId}/share-rate")
+ public String checkUserShareRate(@PathVariable Long userId) {
+ return userService.checkUserShareRate(userId);
+ }
}
diff --git a/src/main/java/com/example/myproject/entity/SeedRating.java b/src/main/java/com/example/myproject/entity/SeedRating.java
new file mode 100644
index 0000000..268f64e
--- /dev/null
+++ b/src/main/java/com/example/myproject/entity/SeedRating.java
@@ -0,0 +1,59 @@
+package com.example.myproject.entity;
+
+import javax.persistence.*;
+import javax.validation.constraints.Max;
+import javax.validation.constraints.Min;
+
+@Entity
+@Table(name = "seed_rating", uniqueConstraints = @UniqueConstraint(columnNames = {"seed_id", "user_id"}))
+public class SeedRating {
+
+ @Id
+ @GeneratedValue(strategy = GenerationType.IDENTITY)
+ @Column(name = "rating_id")
+ private Long ratingId;
+
+ @Column(name = "seed_id", nullable = false)
+ private Long seedId;
+
+ @Column(name = "user_id", nullable = false)
+ private Long userId;
+
+ @Column(name = "score", nullable = false)
+ @Min(0)
+ @Max(10)
+ private Integer score;
+
+ // Getters and Setters
+ public Long getRatingId() {
+ return ratingId;
+ }
+
+ public void setRatingId(Long ratingId) {
+ this.ratingId = ratingId;
+ }
+
+ public Long getSeedId() {
+ return seedId;
+ }
+
+ public void setSeedId(Long seedId) {
+ this.seedId = seedId;
+ }
+
+ public Long getUserId() {
+ return userId;
+ }
+
+ public void setUserId(Long userId) {
+ this.userId = userId;
+ }
+
+ public Integer getScore() {
+ return score;
+ }
+
+ public void setScore(Integer score) {
+ this.score = score;
+ }
+}
\ No newline at end of file
diff --git a/src/main/java/com/example/myproject/entity/TorrentLikes.java b/src/main/java/com/example/myproject/entity/TorrentLikes.java
deleted file mode 100644
index e66ecd8..0000000
--- a/src/main/java/com/example/myproject/entity/TorrentLikes.java
+++ /dev/null
@@ -1,55 +0,0 @@
-//package com.example.myproject.entity;
-//
-//import javax.persistence.*;
-//
-//@Entity
-//@Table(name = "torrent_likes")
-//public class TorrentLikes {
-//
-// @Id
-// @GeneratedValue(strategy = GenerationType.IDENTITY)
-// @Column(name = "like_id")
-// private Long likeId;
-//
-// @Column(name = "user_id")
-// private Integer userId;
-//
-// @Column(name = "torrent_id")
-// private Long torrentId;
-//
-// @Column(name = "is_liked", columnDefinition = "TINYINT(1) DEFAULT 0")
-// private Integer isLiked;
-//
-// // Getters and Setters
-// public Long getLikeId() {
-// return likeId;
-// }
-//
-// public void setLikeId(Long likeId) {
-// this.likeId = likeId;
-// }
-//
-// public Integer getUserId() {
-// return userId;
-// }
-//
-// public void setUserId(Integer userId) {
-// this.userId = userId;
-// }
-//
-// public Long getTorrentId() {
-// return torrentId;
-// }
-//
-// public void setTorrentId(Long torrentId) {
-// this.torrentId = torrentId;
-// }
-//
-// public Integer getIsLiked() {
-// return isLiked;
-// }
-//
-// public void setIsLiked(Integer isLiked) {
-// this.isLiked = isLiked;
-// }
-//}
\ No newline at end of file
diff --git a/src/main/java/com/example/myproject/entity/Users.java b/src/main/java/com/example/myproject/entity/Users.java
index f1d0316..282ef79 100644
--- a/src/main/java/com/example/myproject/entity/Users.java
+++ b/src/main/java/com/example/myproject/entity/Users.java
@@ -1,246 +1,257 @@
- package com.example.myproject.entity;
+package com.example.myproject.entity;
- import javax.persistence.*;
- import java.util.Date;
+import javax.persistence.*;
+import java.util.Date;
- @Entity
- @Table(name = "user")
- public class Users {
+@Entity
+@Table(name = "user")
+public class Users {
- @Id
- @GeneratedValue(strategy = GenerationType.IDENTITY)
- @Column(name = "user_id")
- private Long userId;
+ @Id
+ @GeneratedValue(strategy = GenerationType.IDENTITY)
+ @Column(name = "user_id")
+ private Long userId;
- @Column(name = "username", nullable = false,unique = true)
- private String username;
+ @Column(name = "username", nullable = false,unique = true)
+ private String username;
- @Column(name = "avatar_url", nullable = true)
- private String avatarUrl;
+ @Column(name = "avatar_url", nullable = true)
+ private String avatarUrl;
- @Column(name = "email", nullable = false)
- private String email;
+ @Column(name = "email", nullable = false)
+ private String email;
- @Column(name = "password", nullable = false)
- private String password;
+ @Column(name = "password", nullable = false)
+ private String password;
- @Column(name = "role", nullable = false)
- private String role;
+ @Column(name = "role", nullable = false)
+ private String role;
- @Column(name = "invite_count")
- private Integer inviteCount;
+ @Column(name = "invite_count")
+ private Integer inviteCount;
- @Column(name = "level", nullable = false)
- private Long level;
+ @Column(name = "level", nullable = false)
+ private Long level;
- @Column(name = "upload_count")
- private Float uploadCount;
+ @Column(name = "upload_count")
+ private Float uploadCount;
- @Column(name = "download_count")
- private Float downloadCount;
+ @Column(name = "download_count")
+ private Float downloadCount;
- @Column(name = "share_rate")
- private Float shareRate;
+ @Column(name = "share_rate")
+ private Float shareRate;
- @Column(name = "registration_date", nullable = false)
- private Date registrationDate;
+ @Column(name = "registration_date", nullable = false)
+ private Date registrationDate;
- @Column(name = "last_login_time")
- private Date lastLoginTime;
+ @Column(name = "last_login_time")
+ private Date lastLoginTime;
- @Column(name = "user_points")
- private Integer userPoints;
+ @Column(name = "user_points")
+ private Integer userPoints;
- @Column(name = "is_promo")
- private Boolean isPromo;
+ @Column(name = "is_promo")
+ private Boolean isPromo;
- @Column(name = "current_experience", nullable = false)
- private Integer currentExperience; // 当前经验值
+ @Column(name = "current_experience", nullable = false)
+ private Integer currentExperience; // 当前经验值
- @Column(name = "current_seeding_hours", nullable = false)
- private Float currentSeedingHours; // 当前做种时长
+ @Column(name = "current_seeding_hours", nullable = false)
+ private Float currentSeedingHours; // 当前做种时长
+ @Column(name = "gender", nullable = true)
+ private String gender; // 性别
- @Column(name = "gender", nullable = true)
- private String gender; // 性别
+ @Column(name = "description", nullable = true)
+ private String description; // 个人描述
- @Column(name = "description", nullable = true)
- private String description; // 个人描述
+ @Column(name = "hobbies", nullable = true)
+ private String hobbies;
- @Column(name = "hobbies", nullable = true)
- private String hobbies;
+ @Column(name = "registration_time", nullable = false, updatable = false)
+ @Temporal(TemporalType.TIMESTAMP)
+ private Date registrationTime;
- @Column(name = "registration_time", nullable = false, updatable = false)
- @Temporal(TemporalType.TIMESTAMP)
- private Date registrationTime;
+ // 新增的lastupdatetime字段
+ @Column(name = "lastupdatetime")
+ private Date lastupdatetime;
- // Getters and Setters
- public Long getUserId() {
- return userId;
- }
+ // Getters and Setters
+ public Long getUserId() {
+ return userId;
+ }
- public void setUserId(Long userId) {
- this.userId = userId;
- }
+ public void setUserId(Long userId) {
+ this.userId = userId;
+ }
- public String getUsername() {
- return username;
- }
+ public String getUsername() {
+ return username;
+ }
- public void setUsername(String username) {
- this.username = username;
- }
+ public void setUsername(String username) {
+ this.username = username;
+ }
- public String getAvatarUrl() {
- return avatarUrl;
- }
+ public String getAvatarUrl() {
+ return avatarUrl;
+ }
- public void setAvatarUrl(String avatarUrl) {
- this.avatarUrl = avatarUrl;
- }
+ public void setAvatarUrl(String avatarUrl) {
+ this.avatarUrl = avatarUrl;
+ }
- public String getEmail() {
- return email;
- }
+ public String getEmail() {
+ return email;
+ }
- public void setEmail(String email) {
- this.email = email;
- }
+ public void setEmail(String email) {
+ this.email = email;
+ }
- public String getPassword() {
- return password;
- }
+ public String getPassword() {
+ return password;
+ }
- public void setPassword(String password) {
- this.password = password;
- }
+ public void setPassword(String password) {
+ this.password = password;
+ }
- public String getRole() {
- return role;
- }
+ public String getRole() {
+ return role;
+ }
- public void setRole(String role) {
- this.role = role;
- }
+ public void setRole(String role) {
+ this.role = role;
+ }
- public Integer getInviteCount() {
- return inviteCount;
- }
+ public Integer getInviteCount() {
+ return inviteCount;
+ }
+
+ public void setInviteCount(Integer inviteCount) {
+ this.inviteCount = inviteCount;
+ }
- public void setInviteCount(Integer inviteCount) {
- this.inviteCount = inviteCount;
- }
+ public Long getLevel() {
+ return level;
+ }
- public Long getLevel() {
- return level;
- }
+ public void setLevel(Long level) {
+ this.level = level;
+ }
- public void setLevel(Long level) {
- this.level = level;
- }
+ public Float getUploadCount() {
+ return uploadCount;
+ }
- public Float getUploadCount() {
- return uploadCount;
- }
+ public void setUploadCount(Float uploadCount) {
+ this.uploadCount = uploadCount;
+ }
- public void setUploadCount(Float uploadCount) {
- this.uploadCount = uploadCount;
- }
+ public Float getDownloadCount() {
+ return downloadCount;
+ }
- public Float getDownloadCount() {
- return downloadCount;
- }
+ public void setDownloadCount(Float downloadCount) {
+ this.downloadCount = downloadCount;
+ }
- public void setDownloadCount(Float downloadCount) {
- this.downloadCount = downloadCount;
- }
+ public Float getShareRate() {
+ return shareRate;
+ }
- public Float getShareRate() {
- return shareRate;
- }
+ public void setShareRate(Float shareRate) {
+ this.shareRate = shareRate;
+ }
- public void setShareRate(Float shareRate) {
- this.shareRate = shareRate;
- }
+ public Date getRegistrationDate() {
+ return registrationDate;
+ }
- public Date getRegistrationDate() {
- return registrationDate;
- }
+ public void setRegistrationDate(Date registrationDate) {
+ this.registrationDate = registrationDate;
+ }
- public void setRegistrationDate(Date registrationDate) {
- this.registrationDate = registrationDate;
- }
+ public Date getLastLoginTime() {
+ return lastLoginTime;
+ }
- public Date getLastLoginTime() {
- return lastLoginTime;
- }
+ public void setLastLoginTime(Date lastLoginTime) {
+ this.lastLoginTime = lastLoginTime;
+ }
- public void setLastLoginTime(Date lastLoginTime) {
- this.lastLoginTime = lastLoginTime;
- }
+ public Integer getUserPoints() {
+ return userPoints;
+ }
- public Integer getUserPoints() {
- return userPoints;
- }
+ public void setUserPoints(Integer userPoints) {
+ this.userPoints = userPoints;
+ }
- public void setUserPoints(Integer userPoints) {
- this.userPoints = userPoints;
- }
+ public Boolean getIsPromo() {
+ return isPromo;
+ }
- public Boolean getIsPromo() {
- return isPromo;
- }
+ public void setIsPromo(Boolean isPromo) {
+ this.isPromo = isPromo;
+ }
- public void setIsPromo(Boolean isPromo) {
- this.isPromo = isPromo;
- }
+ public Integer getCurrentExperience() {
+ return currentExperience;
+ }
- public Integer getCurrentExperience() {
- return currentExperience;
- }
+ public void setCurrentExperience(Integer currentExperience) {
+ this.currentExperience = currentExperience;
+ }
- public void setCurrentExperience(Integer currentExperience) {
- this.currentExperience = currentExperience;
- }
+ public Float getCurrentSeedingHours() {
+ return currentSeedingHours;
+ }
- public Float getCurrentSeedingHours() {
- return currentSeedingHours;
- }
+ public void setCurrentSeedingHours(Float currentSeedingHours) {
+ this.currentSeedingHours = currentSeedingHours;
+ }
- public void setCurrentSeedingHours(Float currentSeedingHours) {
- this.currentSeedingHours = currentSeedingHours;
- }
+ public String getGender() {
+ return gender;
+ }
- public String getGender() {
- return gender;
- }
+ public void setGender(String gender) {
+ this.gender = gender;
+ }
- public void setGender(String gender) {
- this.gender = gender;
- }
+ public String getDescription() {
+ return description;
+ }
- public String getDescription() {
- return description;
- }
+ public void setDescription(String description) {
+ this.description = description;
+ }
- public void setDescription(String description) {
- this.description = description;
- }
+ public String getHobbies() {
+ return hobbies;
+ }
- public String getHobbies() {
- return hobbies;
- }
+ public void setHobbies(String hobbies) {
+ this.hobbies = hobbies;
+ }
- public void setHobbies(String hobbies) {
- this.hobbies = hobbies;
- }
+ public Date getRegistrationTime() {
+ return registrationTime;
+ }
- public Date getRegistrationTime() {
- return registrationTime;
- }
+ public void setRegistrationTime(Date registrationTime) {
+ this.registrationTime = registrationTime;
+ }
- public void setRegistrationTime(Date registrationTime) {
- this.registrationTime = registrationTime;
- }
+ // 新增的lastupdatetime的getter和setter方法
+ public Date getLastupdatetime() {
+ return lastupdatetime;
+ }
- }
+ public void setLastupdatetime(Date lastupdatetime) {
+ this.lastupdatetime = lastupdatetime;
+ }
+}
\ No newline at end of file
diff --git a/src/main/java/com/example/myproject/mapper/AuditRecordMapper.java b/src/main/java/com/example/myproject/mapper/AuditRecordMapper.java
index 5d002b2..092b167 100644
--- a/src/main/java/com/example/myproject/mapper/AuditRecordMapper.java
+++ b/src/main/java/com/example/myproject/mapper/AuditRecordMapper.java
@@ -1,3 +1,18 @@
+//package com.example.myproject.mapper;
+//
+//import com.example.myproject.entity.AuditRecord;
+//import org.apache.ibatis.annotations.Insert;
+//import org.apache.ibatis.annotations.Mapper;
+//
+//@Mapper
+//public interface AuditRecordMapper {
+//
+// @Insert("REPLACE INTO audit_record (torrent_id, upload, download, create_time) " +
+// "VALUES (#{torrentId}, #{upload}, #{download}, #{createTime})")
+// void upsert(AuditRecord record);
+//
+//}
+
package com.example.myproject.mapper;
import com.example.myproject.entity.AuditRecord;
diff --git a/src/main/java/com/example/myproject/mapper/FavoriteMapper.java b/src/main/java/com/example/myproject/mapper/FavoriteMapper.java
index 08b8151..d40ec09 100644
--- a/src/main/java/com/example/myproject/mapper/FavoriteMapper.java
+++ b/src/main/java/com/example/myproject/mapper/FavoriteMapper.java
@@ -4,12 +4,16 @@
import com.example.myproject.entity.FavoriteEntity;
import org.apache.ibatis.annotations.*;
+import java.util.List;
+
@Mapper
public interface FavoriteMapper extends BaseMapper<FavoriteEntity> {
- @Select("SELECT * FROM favorite WHERE user_id = #{userId} AND seed_id = #{seedId} LIMIT 1")
+ @Select("SELECT * FROM favorite WHERE user_id = #{userId} AND seed_id = #{seedId} LIMIT 1")
FavoriteEntity selectByUserIdAndSeedId(@Param("userId") Long userId, @Param("seedId") Long seedId);
@Delete("DELETE FROM favorite WHERE user_id = #{userId} AND seed_id = #{seedId}")
void deleteByUserIdAndSeedId(@Param("userId") Long userId, @Param("seedId") Long seedId);
+
+ List<FavoriteEntity> selectByUserId(Long userId); // 声明方法userId);
}
\ No newline at end of file
diff --git a/src/main/java/com/example/myproject/mapper/SeedRatingMapper.java b/src/main/java/com/example/myproject/mapper/SeedRatingMapper.java
new file mode 100644
index 0000000..3443135
--- /dev/null
+++ b/src/main/java/com/example/myproject/mapper/SeedRatingMapper.java
@@ -0,0 +1,12 @@
+package com.example.myproject.mapper;
+
+import com.baomidou.mybatisplus.core.mapper.BaseMapper;
+import com.example.myproject.entity.SeedRating;
+import org.springframework.data.repository.query.Param;
+
+import java.util.List;
+
+public interface SeedRatingMapper extends BaseMapper<SeedRating> {
+ List<SeedRating> selectHighRatingsByUserId(Long userId);
+ List<SeedRating> selectBySeedIds(@Param("seedIds") List<Long> seedIds);
+}
\ No newline at end of file
diff --git a/src/main/java/com/example/myproject/mapper/TorrentMapper.java b/src/main/java/com/example/myproject/mapper/TorrentMapper.java
index 28e4dfc..c72954a 100644
--- a/src/main/java/com/example/myproject/mapper/TorrentMapper.java
+++ b/src/main/java/com/example/myproject/mapper/TorrentMapper.java
@@ -13,15 +13,12 @@
@Mapper
public interface TorrentMapper extends BaseMapper<TorrentEntity> {
- @Select("SELECT * FROM torrents WHERE info_hash = #{infoHash}")
- TorrentEntity selectByInfoHash(String infoHash);
-
List<TorrentEntity> search(@Param("param") TorrentParam param);
boolean checkFavorite(@Param("seedId") Long seedId, @Param("userId") Long userId);
-
+
void addFavorite(@Param("seedId") Long seedId, @Param("userId") Long userId);
-
+
void removeFavorite(@Param("seedId") Long seedId, @Param("userId") Long userId);
List<TorrentEntity> selectMyFavorite(@Param("userId") Long userId);
@Select("SELECT * FROM torrent")
@@ -39,7 +36,5 @@
void incrementDownloadCount(@Param("torrentId") Long torrentId);
-
-
-
+ TorrentEntity selectByInfoHash(String infoHash);
}
\ No newline at end of file
diff --git a/src/main/java/com/example/myproject/repository/GroupMembersRepository.java b/src/main/java/com/example/myproject/repository/GroupMembersRepository.java
index 2d31462..f0e0a0e 100644
--- a/src/main/java/com/example/myproject/repository/GroupMembersRepository.java
+++ b/src/main/java/com/example/myproject/repository/GroupMembersRepository.java
@@ -1,8 +1,13 @@
package com.example.myproject.repository;
import com.example.myproject.entity.GroupMembers;
+import org.apache.ibatis.annotations.Delete;
import org.springframework.data.jpa.repository.JpaRepository;
+import org.springframework.data.jpa.repository.Modifying;
+import org.springframework.data.jpa.repository.Query;
+import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;
+import org.springframework.transaction.annotation.Transactional;
import java.util.List;
@@ -13,8 +18,17 @@
boolean existsByGroupIdAndUserId(Long groupId, Long userId);
// 删除用户和小组的关联记录
- void deleteByGroupIdAndUserId(Long groupId, Long userId);
+// void deleteByGroupIdAndUserId(Long groupId, Long userId);
// 通过 groupId 获取所有成员的 userId
List<GroupMembers> findByGroupId(Long groupId);
+
+ // 根据类别筛选小组
+ @Transactional
+ @Modifying
+ @Query("DELETE FROM GroupMembers gm WHERE gm.groupId = :groupId AND gm.userId = :userId")
+ int deleteByGroupIdAndUserId(
+ @Param("groupId") Long groupId,
+ @Param("userId") Long userId
+ );
}
diff --git a/src/main/java/com/example/myproject/repository/GroupRepository.java b/src/main/java/com/example/myproject/repository/GroupRepository.java
index 7280e50..33224d5 100644
--- a/src/main/java/com/example/myproject/repository/GroupRepository.java
+++ b/src/main/java/com/example/myproject/repository/GroupRepository.java
@@ -1,6 +1,7 @@
package com.example.myproject.repository;
import com.example.myproject.entity.Group;
+import org.apache.ibatis.annotations.Delete;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
@@ -18,4 +19,6 @@
// 根据类别筛选小组
@Query("SELECT g FROM Group g WHERE (:category IS NULL OR g.category = :category)")
Page<Group> searchWithFilters(@Param("category") String category, Pageable pageable);
+
+
}
diff --git a/src/main/java/com/example/myproject/repository/SeedRatingRepository.java b/src/main/java/com/example/myproject/repository/SeedRatingRepository.java
new file mode 100644
index 0000000..8ac303d
--- /dev/null
+++ b/src/main/java/com/example/myproject/repository/SeedRatingRepository.java
@@ -0,0 +1,10 @@
+package com.example.myproject.repository;
+
+import com.example.myproject.entity.SeedRating;
+import org.springframework.data.jpa.repository.JpaRepository;
+
+import java.util.Optional;
+
+public interface SeedRatingRepository extends JpaRepository<SeedRating, Long> {
+ Optional<SeedRating> findByUserIdAndSeedId(Long userId, Long seedId);
+}
\ No newline at end of file
diff --git a/src/main/java/com/example/myproject/repository/TorrentLikesRepository.java b/src/main/java/com/example/myproject/repository/TorrentLikesRepository.java
deleted file mode 100644
index 2a4e712..0000000
--- a/src/main/java/com/example/myproject/repository/TorrentLikesRepository.java
+++ /dev/null
@@ -1,24 +0,0 @@
-//package com.example.myproject.repository;
-//
-//import com.example.myproject.entity.TorrentLikes;
-//import org.springframework.data.jpa.repository.JpaRepository;
-//import org.springframework.data.jpa.repository.Modifying;
-//import org.springframework.data.jpa.repository.Query;
-//import org.springframework.data.repository.query.Param;
-//import org.springframework.transaction.annotation.Transactional;
-//
-//import java.util.Optional;
-//
-//public interface TorrentLikesRepository extends JpaRepository<TorrentLikes, Long> {
-//
-// // 根据用户ID和种子ID查询记录
-// Optional<TorrentLikes> findByUserIdAndTorrentId(Integer userId, Long torrentId);
-//
-// // 更新点赞状态(基于用户ID和种子ID)
-// @Modifying
-// @Transactional
-// @Query("UPDATE TorrentLikes t SET t.isLiked = :isLiked WHERE t.userId = :userId AND t.torrentId = :torrentId")
-// int updateIsLikedByUserIdAndTorrentId(@Param("userId") Integer userId,
-// @Param("torrentId") Long torrentId,
-// @Param("isLiked") Integer isLiked);
-//}
\ No newline at end of file
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
diff --git a/src/main/resources/mapper/FavoriteMapper.xml b/src/main/resources/mapper/FavoriteMapper.xml
index 1048ec2..2c9abe4 100644
--- a/src/main/resources/mapper/FavoriteMapper.xml
+++ b/src/main/resources/mapper/FavoriteMapper.xml
@@ -2,5 +2,5 @@
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.example.myproject.mapper.FavoriteMapper">
-
+ <select id="selectByUserId" resultType="com.example.myproject.entity.FavoriteEntity"> SELECT * FROM favorite WHERE user_id = #{userId} </select>
</mapper>
diff --git a/src/main/resources/mapper/SeedRatingMapper.xml b/src/main/resources/mapper/SeedRatingMapper.xml
new file mode 100644
index 0000000..34fa7a0
--- /dev/null
+++ b/src/main/resources/mapper/SeedRatingMapper.xml
@@ -0,0 +1,16 @@
+<?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.example.myproject.mapper.SeedRatingMapper">
+ <select id="selectHighRatingsByUserId" resultType="com.example.myproject.entity.SeedRating">
+ SELECT * FROM seed_rating
+ WHERE user_id = #{userId}
+ AND score >= 7
+ </select>
+
+ <select id="selectBySeedIds" resultType="com.example.myproject.entity.SeedRating">
+ SELECT * FROM seed_rating WHERE seed_id IN
+ <foreach collection="seedIds" item="id" open="(" separator="," close=")">
+ #{id}
+ </foreach>
+ </select>
+</mapper>
\ No newline at end of file