blob: 6c0a56f6963440f1889615263f7c095860c7cdf3 [file] [log] [blame]
22301102e0c7c6e2025-05-17 11:08:45 +08001package com.pt.service;
2
3import com.pt.entity.User;
4import com.pt.repository.UserRepository;
5import org.springframework.beans.factory.annotation.Autowired;
6import org.springframework.stereotype.Service;
7
22301102aa5adbc2025-05-18 17:51:55 +08008import java.util.List;
9
22301102e0c7c6e2025-05-17 11:08:45 +080010@Service
11public class UserService {
12
13 @Autowired
14 private UserRepository userRepository;
15
16 public UserService(UserRepository userRepository) {
17 this.userRepository = userRepository;
18 }
19
20 public User findByUsername(String username) {
21 return userRepository.findByUsername(username);
22 }
23
24 public User findByEmail(String email) {
25 return userRepository.findByEmail(email);
26 }
27
28 public User findByUsernameAndPassword(String username, String password) {
29 return userRepository.findByUsernameAndPassword(username, password);
30 }
31
32 public User findByEmailAndPassword(String email, String password) {
33 return userRepository.findByEmailAndPassword(email, password);
34 }
35
36 public User save(User user) {
37 return userRepository.save(user);
38 }
39
40 public User findById(String uid) {
41 return userRepository.findById(uid).orElse(null);
42 }
43
44 public void deleteById(String uid) {
45 userRepository.deleteById(uid);
46 }
22301102aa5adbc2025-05-18 17:51:55 +080047
48 public List<User> listAll() {
49 return userRepository.findAll();
50 }
yyyang061761a2025-06-08 14:50:16 +080051
52 /**
53 * 根据用户的统计数据(上传量、下载量、积分)更新其等级。
54 * 该方法包含等级提升的规则,且只升不降。
55 * @param userId 要更新等级的用户的ID
56 */
57 public void updateUserLevel(String userId) {
58 User user = userRepository.findById(userId).orElse(null);
59 if (user == null) {
60 // 在实际应用中,这里可能需要记录日志或抛出异常
61 return;
62 }
63
64 long uploaded = user.getUploaded();
65 long downloaded = user.getDownloaded();
66 int points = user.getPoints();
67
68 // 定义数据大小常量
69 final long GB = 1024L * 1024L * 1024L;
70 final long TB = 1024L * GB;
71
72 // 计算分享率。如果下载量为0,分享率视为无限大(只要有上传)
73 double shareRatio = (downloaded == 0 && uploaded > 0) ? Double.MAX_VALUE :
74 (downloaded == 0 && uploaded == 0) ? 0 : (double) uploaded / downloaded;
75
76 int newLevel = 1; // 默认为1级
77
78 // 等级规则(从高到低判断)
79 // 规则5: 大佬级
80 if (uploaded > 5 * TB && shareRatio > 2.0 && points > 10000) {
81 newLevel = 5;
82 }
83 // 规则4: 专家级
84 else if (uploaded > 1 * TB && shareRatio > 1.5 && points > 2000) {
85 newLevel = 4;
86 }
87 // 规则3: 熟练级
88 else if (uploaded > 200 * GB && shareRatio > 1.0 && points > 500) {
89 newLevel = 3;
90 }
91 // 规则2: 入门级 (获得发布权限)
92 else if (uploaded > 50 * GB && shareRatio > 0.5 && points > 100) {
93 newLevel = 2;
94 }
95
96 // 如果计算出的新等级高于当前等级,则更新用户等级
97 if (newLevel > user.getLevel()) {
98 user.setLevel(newLevel);
99 userRepository.save(user);
100 }
101 }
102
103 /**
104 * 批量更新所有用户的等级。
105 * 适合用于定时任务,定期刷新所有用户的等级。
106 */
107 public void updateAllUsersLevel() {
108 List<User> allUsers = userRepository.findAll();
109 for (User user : allUsers) {
110 updateUserLevel(user.getUid());
111 }
112 }
22301102e0c7c6e2025-05-17 11:08:45 +0800113}