Krishya | b0cc188 | 2025-06-09 10:54:09 +0800 | [diff] [blame] | 1 | //package com.example.myproject.service; |
| 2 | //import cn.dev33.satoken.stp.StpUtil; |
| 3 | //import com.example.myproject.entity.FriendRelation; |
| 4 | //import com.example.myproject.entity.User; |
| 5 | //import com.example.myproject.entity.Users; |
| 6 | //import com.example.myproject.entity.UserInviteCode; |
| 7 | //import com.example.myproject.repository.FriendRelationRepository; |
| 8 | //import com.example.myproject.repository.UserRepository; |
| 9 | //import com.example.myproject.repository.UserInviteCodeRepository; |
| 10 | //import org.springframework.beans.factory.annotation.Autowired; |
| 11 | //import org.springframework.stereotype.Service; |
| 12 | //import org.springframework.web.multipart.MultipartFile; |
| 13 | // |
| 14 | //import javax.servlet.http.HttpServletRequest; |
| 15 | //import java.io.File; |
| 16 | //import java.io.IOException; |
| 17 | //import java.nio.file.Files; |
| 18 | //import java.nio.file.Path; |
| 19 | //import java.nio.file.Paths; |
| 20 | //import java.time.LocalDateTime; |
| 21 | //import java.util.*; |
| 22 | // |
| 23 | //@Service |
| 24 | //public class UserService { |
| 25 | // |
| 26 | // @Autowired |
| 27 | // private UserRepository userRepository; |
| 28 | // |
| 29 | // @Autowired |
| 30 | // private UserInviteCodeRepository userInviteCodeRepository; |
| 31 | // |
| 32 | // @Autowired |
| 33 | // private FriendRelationRepository friendRelationRepository; |
| 34 | // |
| 35 | // // 生成邀请码 |
| 36 | // public Map<String, Object> generateInviteCode(Long userId) { |
| 37 | // // 获取用户信息 |
| 38 | // Users user = userRepository.findById(userId).orElse(null); |
| 39 | // |
| 40 | // // 如果用户不存在,返回错误 |
| 41 | // if (user == null) { |
| 42 | // Map<String, Object> errorResponse = new HashMap<>(); |
| 43 | // errorResponse.put("status", "failure"); |
| 44 | // errorResponse.put("message", "用户不存在"); |
| 45 | // return errorResponse; |
| 46 | // } |
| 47 | // |
| 48 | // // 检查用户的邀请数量 |
| 49 | // if (user.getInviteCount() <= 0) { |
| 50 | // Map<String, Object> errorResponse = new HashMap<>(); |
| 51 | // errorResponse.put("status", "failure"); |
| 52 | // errorResponse.put("message", "没有剩余的邀请码"); |
| 53 | // return errorResponse; |
| 54 | // } |
| 55 | // |
| 56 | // // 生成唯一的邀请码 |
| 57 | // String inviteCode = generateUniqueInviteCode(); |
| 58 | // |
| 59 | // // 将邀请码保存到 `user_invite_code` 表 |
| 60 | // UserInviteCode userInviteCode = new UserInviteCode(); |
| 61 | // userInviteCode.setUserId(userId); |
| 62 | // userInviteCode.setInviteCode(inviteCode); |
| 63 | // userInviteCode.setCreatedAt(java.time.LocalDateTime.now()); |
| 64 | // |
| 65 | // userInviteCodeRepository.save(userInviteCode); |
| 66 | // |
| 67 | // // 更新用户的 `invite_count`,减少1 |
| 68 | // user.setInviteCount(user.getInviteCount() - 1); |
| 69 | // userRepository.save(user); |
| 70 | // |
| 71 | // // 返回成功信息 |
| 72 | // Map<String, Object> response = new HashMap<>(); |
| 73 | // response.put("status", "success"); |
| 74 | // response.put("message", "邀请码生成成功"); |
| 75 | // response.put("invite_code", inviteCode); |
| 76 | // |
| 77 | // return response; |
| 78 | // } |
| 79 | // |
| 80 | // // 生成唯一的邀请码,使用26个字母(大小写) |
| 81 | // private String generateUniqueInviteCode() { |
| 82 | // String characters = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"; |
| 83 | // StringBuilder inviteCode = new StringBuilder(); |
| 84 | // |
| 85 | // Random random = new Random(); |
| 86 | // for (int i = 0; i < 10; i++) { |
| 87 | // inviteCode.append(characters.charAt(random.nextInt(characters.length()))); |
| 88 | // } |
| 89 | // |
| 90 | // return inviteCode.toString(); |
| 91 | // } |
| 92 | // |
| 93 | // public String registerUser(String username, String email, String password, String role, String inviteCode) { |
| 94 | // // 检查邮箱是否已经注册 |
| 95 | // Optional<Users> existingEmailUser = userRepository.findByEmail(email); |
| 96 | // if (existingEmailUser.isPresent()) { |
| 97 | // return "该邮箱已被注册"; |
| 98 | // } |
| 99 | // |
| 100 | // // 检查用户名是否已经存在 |
| 101 | // Optional<Users> existingUsernameUser = userRepository.findByUsername(username); // 需要根据用户名查询 |
| 102 | // if (existingUsernameUser.isPresent()) { |
| 103 | // return "该用户名已被注册"; |
| 104 | // } |
| 105 | // |
| 106 | // // 检查邀请码是否有效 |
| 107 | // if (inviteCode == null || inviteCode.isEmpty()) { |
| 108 | // return "邀请码不能为空"; |
| 109 | // } |
| 110 | // |
| 111 | // Optional<UserInviteCode> invite = userInviteCodeRepository.findByInviteCode(inviteCode); |
| 112 | // if (invite.isEmpty() || invite.get().getIsUsed()) { |
| 113 | // return "邀请码无效或已被使用"; |
| 114 | // } |
| 115 | // |
| 116 | // // 设置默认等级为2(由于邀请码有效) |
| 117 | // Long level = 2L; |
| 118 | // |
| 119 | // // 设置默认头像 URL |
| 120 | // String avatarUrl = "https://example.com/default-avatar.jpg"; // 默认头像 |
| 121 | // |
| 122 | // // 获取邀请码对应的用户ID |
| 123 | // UserInviteCode inviteEntity = invite.get(); |
| 124 | // Long inviteUserId = inviteEntity.getUserId(); |
| 125 | // |
| 126 | // // 创建新用户 |
| 127 | // Users newUser = new Users(); |
| 128 | // newUser.setUsername(username); |
| 129 | // newUser.setEmail(email); |
| 130 | // newUser.setPassword(password); |
| 131 | // newUser.setRole(role); |
| 132 | // newUser.setInviteCount(0); // 初始邀请码数量为 0 |
| 133 | // newUser.setLevel(level); |
| 134 | // newUser.setAvatarUrl(avatarUrl); // 设置默认头像 |
| 135 | // newUser.setRegistrationDate(new java.util.Date()); // 设置注册日期 |
| 136 | // newUser.setCurrentExperience(0); // 默认经验为 0 |
| 137 | // newUser.setCurrentSeedingHours(0f); // 默认做种时长为 0 |
| 138 | // newUser.setRegistrationTime(new java.util.Date()); // 设置注册时间为当前时间 |
| 139 | // |
| 140 | // // 保存用户信息 |
| 141 | // userRepository.save(newUser); |
| 142 | // |
| 143 | // FriendRelation newFriendRelation = new FriendRelation(); |
| 144 | // newFriendRelation.setUserId(newUser.getUserId()); |
| 145 | // newFriendRelation.setCreateTime(new Date()); |
| 146 | // newFriendRelation.setFriendId(inviteUserId); |
| 147 | // |
| 148 | // FriendRelation newFriendRelations = new FriendRelation(); |
| 149 | // newFriendRelations.setUserId(inviteUserId); |
| 150 | // newFriendRelations.setCreateTime(new Date()); |
| 151 | // newFriendRelations.setFriendId(newUser.getUserId()); |
| 152 | // friendRelationRepository.save(newFriendRelation); |
| 153 | // friendRelationRepository.save(newFriendRelations); |
| 154 | // // 更新邀请码的使用状态 |
| 155 | // inviteEntity.setIsUsed(true); |
| 156 | // userInviteCodeRepository.save(inviteEntity); |
| 157 | // |
| 158 | // return "用户注册成功"; |
| 159 | // } |
| 160 | // |
| 161 | // |
| 162 | // public String loginUser(String username, String password) { |
| 163 | // // 检查用户是否存在 |
| 164 | // Optional<Users> userOptional = userRepository.findByUsername(username); |
| 165 | // if (userOptional.isEmpty()) { |
| 166 | // return "用户名不存在"; |
| 167 | // } |
| 168 | // |
| 169 | // Users user = userOptional.get(); |
| 170 | // |
| 171 | // // 检查密码是否正确 |
| 172 | // if (!user.getPassword().equals(password)) { |
| 173 | // return "密码错误"; |
| 174 | // } |
| 175 | // StpUtil.login(user.getUserId()); |
| 176 | // |
| 177 | // // 登录成功 |
| 178 | // return "登录成功"; |
| 179 | // } |
| 180 | // |
| 181 | // public String changePassword(Long userId, String oldPassword, String newPassword, String confirmPassword) { |
| 182 | // // 查找用户 |
| 183 | // Users user = userRepository.findById(userId).orElse(null); |
| 184 | // |
| 185 | // if (user == null) { |
| 186 | // return "用户不存在"; |
| 187 | // } |
| 188 | // |
| 189 | // // 检查旧密码是否正确 |
| 190 | // if (!user.getPassword().equals(oldPassword)) { |
| 191 | // return "旧密码错误"; |
| 192 | // } |
| 193 | // |
| 194 | // // 检查新密码和确认密码是否一致 |
| 195 | // if (!newPassword.equals(confirmPassword)) { |
| 196 | // return "新密码与确认密码不一致"; |
| 197 | // } |
| 198 | // |
| 199 | // // 更新密码 |
| 200 | // user.setPassword(newPassword); |
| 201 | // userRepository.save(user); |
| 202 | // |
| 203 | // return "密码修改成功"; |
| 204 | // } |
| 205 | // |
| 206 | // // 获取用户个人资料 |
| 207 | // public Map<String, Object> getProfile(Long userId) { |
| 208 | // Optional<Users> userOptional = userRepository.findById(userId); |
| 209 | // |
| 210 | // // 如果用户不存在,返回null或者可以抛出异常 |
| 211 | // if (userOptional.isEmpty()) { |
| 212 | // return null; // 可以返回 null 或者根据需要返回错误信息 |
| 213 | // } |
| 214 | // |
| 215 | // Users user = userOptional.get(); |
| 216 | // |
| 217 | // // 将需要的字段放入 Map 中 |
| 218 | // Map<String, Object> profile = new LinkedHashMap<>(); |
| 219 | // profile.put("avatarUrl", user.getAvatarUrl()); |
| 220 | // profile.put("username", user.getUsername()); |
| 221 | // profile.put("email", user.getEmail()); |
| 222 | // profile.put("gender", user.getGender()); |
| 223 | // profile.put("description", user.getDescription()); |
| 224 | // profile.put("hobbies", user.getHobbies()); |
| 225 | // profile.put("level", user.getLevel()); |
| 226 | // profile.put("Experience", user.getCurrentExperience()); |
| 227 | // profile.put("uploadCount", user.getUploadCount()); |
| 228 | // profile.put("downloadCount", user.getDownloadCount()); |
| 229 | // profile.put("shareRate", user.getShareRate()); |
| 230 | // profile.put("registrationTime", user.getRegistrationTime()); |
| 231 | // |
| 232 | // return profile; |
| 233 | // } |
| 234 | // |
| 235 | // // 修改用户个人资料 |
| 236 | // public boolean editProfile(Long userId, String nickname, String gender, String description, String hobbies) { |
| 237 | // Optional<Users> userOptional = userRepository.findById(userId); |
| 238 | // |
| 239 | // // 如果用户不存在,返回false |
| 240 | // if (userOptional.isEmpty()) { |
| 241 | // return false; // 用户不存在 |
| 242 | // } |
| 243 | // |
| 244 | // Users user = userOptional.get(); |
| 245 | // |
| 246 | // // 更新用户资料,只有传入值才会更新对应字段 |
| 247 | // if (nickname != null) { |
| 248 | // user.setUsername(nickname); |
| 249 | // } |
| 250 | // if (gender != null) { |
| 251 | // user.setGender(gender); |
| 252 | // } |
| 253 | // if (description != null) { |
| 254 | // user.setDescription(description); |
| 255 | // } |
| 256 | // if (hobbies != null) { |
| 257 | // user.setHobbies(hobbies); |
| 258 | // } |
| 259 | // |
| 260 | // // 保存更新后的用户信息 |
| 261 | // userRepository.save(user); |
| 262 | // |
| 263 | // return true; // 更新成功 |
| 264 | // } |
| 265 | // |
| 266 | // public Map<String, Object> calculateShareRate(Long userId) { |
| 267 | // // 查找用户 |
| 268 | // Users user = userRepository.findById(userId).orElse(null); |
| 269 | // if (user == null) { |
| 270 | // return Map.of("status", "error", "message", "用户不存在"); |
| 271 | // } |
| 272 | // |
| 273 | // // 获取上传量和下载量 |
| 274 | // Float uploadCount = user.getUploadCount(); |
| 275 | // Float downloadCount = user.getDownloadCount(); |
| 276 | // |
| 277 | // // 计算分享率 |
| 278 | // Float shareRate = 0f; // 默认分享率为0 |
| 279 | // if (downloadCount != 0) { |
| 280 | // shareRate = uploadCount / downloadCount; // 分享率 = 上传量 / 下载量 |
| 281 | // } |
| 282 | // |
| 283 | // // 更新用户的分享率 |
| 284 | // user.setShareRate(shareRate); |
| 285 | // userRepository.save(user); |
| 286 | // |
| 287 | // // 返回结果 |
| 288 | // return Map.of("status", "success", "message", "分享率计算成功", "shareRate", shareRate); |
| 289 | // } |
| 290 | // |
| 291 | // |
| 292 | // private static final String AVATAR_DIR = "uploads/avatarUrl/"; |
| 293 | // |
| 294 | // public Map<String, Object> uploadUserAvatar(Long userId, MultipartFile file) { |
| 295 | // Users user = userRepository.findById(userId) |
| 296 | // .orElseThrow(() -> new RuntimeException("用户不存在")); |
| 297 | // |
| 298 | // try { |
| 299 | // String avatarUrl = saveAvatar(file, userId); |
| 300 | // user.setAvatarUrl(avatarUrl); |
| 301 | // userRepository.save(user); |
| 302 | // |
| 303 | // Map<String, Object> response = new HashMap<>(); |
| 304 | // response.put("status", "success"); |
| 305 | // response.put("message", "头像上传成功"); |
| 306 | // response.put("userId", user.getUserId()); |
| 307 | // response.put("avatarUrl", avatarUrl); |
| 308 | // return response; |
| 309 | // |
| 310 | // } catch (IOException e) { |
| 311 | // throw new RuntimeException("头像上传失败: " + e.getMessage()); |
| 312 | // } |
| 313 | // } |
| 314 | // |
| 315 | // // 保存头像文件并返回可访问 URL |
| 316 | // public String saveAvatar(MultipartFile file, Long userId) throws IOException { |
| 317 | // String originalFilename = file.getOriginalFilename(); |
| 318 | // String extension = originalFilename.substring(originalFilename.lastIndexOf('.')); |
| 319 | // String fileName = userId + extension; // 以用户ID作为文件名 |
| 320 | // Path path = Paths.get(AVATAR_DIR + fileName); |
| 321 | // Files.createDirectories(path.getParent()); |
| 322 | // Files.write(path, file.getBytes()); |
| 323 | // return "/" + AVATAR_DIR + fileName; // 返回相对URL路径 |
| 324 | // } |
| 325 | // |
| 326 | // |
| 327 | // |
| 328 | //} |
| 329 | |
22301115 | cf6dba2 | 2025-03-25 19:06:21 +0800 | [diff] [blame] | 330 | package com.example.myproject.service; |
Krishya | b0cc188 | 2025-06-09 10:54:09 +0800 | [diff] [blame] | 331 | |
JinGe | fe5140c | 2025-06-06 20:07:42 +0800 | [diff] [blame] | 332 | import com.example.myproject.entity.FriendRelation; |
22301138 | b253341 | 2025-06-05 00:18:58 +0800 | [diff] [blame] | 333 | import com.example.myproject.entity.User; |
22301138 | 5e9c35a | 2025-06-04 15:52:45 +0800 | [diff] [blame] | 334 | import com.example.myproject.entity.Users; |
| 335 | import com.example.myproject.entity.UserInviteCode; |
JinGe | fe5140c | 2025-06-06 20:07:42 +0800 | [diff] [blame] | 336 | import com.example.myproject.repository.FriendRelationRepository; |
Jin | f50fba6 | 2025-06-09 22:47:24 +0800 | [diff] [blame^] | 337 | import com.example.myproject.repository.LoginLogRepository; |
22301138 | 5e9c35a | 2025-06-04 15:52:45 +0800 | [diff] [blame] | 338 | import com.example.myproject.repository.UserRepository; |
| 339 | import com.example.myproject.repository.UserInviteCodeRepository; |
Krishya | b0cc188 | 2025-06-09 10:54:09 +0800 | [diff] [blame] | 340 | import jakarta.transaction.Transactional; |
22301138 | 5e9c35a | 2025-06-04 15:52:45 +0800 | [diff] [blame] | 341 | import org.springframework.beans.factory.annotation.Autowired; |
| 342 | import org.springframework.stereotype.Service; |
22301138 | b253341 | 2025-06-05 00:18:58 +0800 | [diff] [blame] | 343 | import org.springframework.web.multipart.MultipartFile; |
22301138 | 5e9c35a | 2025-06-04 15:52:45 +0800 | [diff] [blame] | 344 | |
22301138 | b253341 | 2025-06-05 00:18:58 +0800 | [diff] [blame] | 345 | import javax.servlet.http.HttpServletRequest; |
| 346 | import java.io.File; |
| 347 | import java.io.IOException; |
Jin | f50fba6 | 2025-06-09 22:47:24 +0800 | [diff] [blame^] | 348 | import java.math.BigDecimal; |
22301138 | b253341 | 2025-06-05 00:18:58 +0800 | [diff] [blame] | 349 | import java.nio.file.Files; |
| 350 | import java.nio.file.Path; |
| 351 | import java.nio.file.Paths; |
Krishya | b0cc188 | 2025-06-09 10:54:09 +0800 | [diff] [blame] | 352 | import java.time.LocalDate; |
22301138 | b253341 | 2025-06-05 00:18:58 +0800 | [diff] [blame] | 353 | import java.time.LocalDateTime; |
Krishya | b0cc188 | 2025-06-09 10:54:09 +0800 | [diff] [blame] | 354 | import java.time.ZoneId; |
22301138 | 5e9c35a | 2025-06-04 15:52:45 +0800 | [diff] [blame] | 355 | import java.util.*; |
| 356 | |
| 357 | @Service |
| 358 | public class UserService { |
| 359 | |
| 360 | @Autowired |
| 361 | private UserRepository userRepository; |
| 362 | |
| 363 | @Autowired |
| 364 | private UserInviteCodeRepository userInviteCodeRepository; |
| 365 | |
JinGe | fe5140c | 2025-06-06 20:07:42 +0800 | [diff] [blame] | 366 | @Autowired |
| 367 | private FriendRelationRepository friendRelationRepository; |
Jin | f50fba6 | 2025-06-09 22:47:24 +0800 | [diff] [blame^] | 368 | private final LoginLogRepository loginLogRepository; |
| 369 | |
| 370 | public UserService(LoginLogRepository loginLogRepository) { |
| 371 | this.loginLogRepository = loginLogRepository; |
| 372 | } |
JinGe | fe5140c | 2025-06-06 20:07:42 +0800 | [diff] [blame] | 373 | |
22301138 | 5e9c35a | 2025-06-04 15:52:45 +0800 | [diff] [blame] | 374 | // 生成邀请码 |
| 375 | public Map<String, Object> generateInviteCode(Long userId) { |
| 376 | // 获取用户信息 |
| 377 | Users user = userRepository.findById(userId).orElse(null); |
| 378 | |
| 379 | // 如果用户不存在,返回错误 |
| 380 | if (user == null) { |
| 381 | Map<String, Object> errorResponse = new HashMap<>(); |
| 382 | errorResponse.put("status", "failure"); |
| 383 | errorResponse.put("message", "用户不存在"); |
| 384 | return errorResponse; |
| 385 | } |
| 386 | |
| 387 | // 检查用户的邀请数量 |
| 388 | if (user.getInviteCount() <= 0) { |
| 389 | Map<String, Object> errorResponse = new HashMap<>(); |
| 390 | errorResponse.put("status", "failure"); |
| 391 | errorResponse.put("message", "没有剩余的邀请码"); |
| 392 | return errorResponse; |
| 393 | } |
| 394 | |
| 395 | // 生成唯一的邀请码 |
| 396 | String inviteCode = generateUniqueInviteCode(); |
| 397 | |
| 398 | // 将邀请码保存到 `user_invite_code` 表 |
| 399 | UserInviteCode userInviteCode = new UserInviteCode(); |
| 400 | userInviteCode.setUserId(userId); |
| 401 | userInviteCode.setInviteCode(inviteCode); |
| 402 | userInviteCode.setCreatedAt(java.time.LocalDateTime.now()); |
| 403 | |
| 404 | userInviteCodeRepository.save(userInviteCode); |
| 405 | |
| 406 | // 更新用户的 `invite_count`,减少1 |
| 407 | user.setInviteCount(user.getInviteCount() - 1); |
| 408 | userRepository.save(user); |
| 409 | |
| 410 | // 返回成功信息 |
| 411 | Map<String, Object> response = new HashMap<>(); |
| 412 | response.put("status", "success"); |
| 413 | response.put("message", "邀请码生成成功"); |
| 414 | response.put("invite_code", inviteCode); |
| 415 | |
| 416 | return response; |
| 417 | } |
| 418 | |
| 419 | // 生成唯一的邀请码,使用26个字母(大小写) |
| 420 | private String generateUniqueInviteCode() { |
| 421 | String characters = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"; |
| 422 | StringBuilder inviteCode = new StringBuilder(); |
| 423 | |
| 424 | Random random = new Random(); |
| 425 | for (int i = 0; i < 10; i++) { |
| 426 | inviteCode.append(characters.charAt(random.nextInt(characters.length()))); |
| 427 | } |
| 428 | |
| 429 | return inviteCode.toString(); |
| 430 | } |
| 431 | |
| 432 | public String registerUser(String username, String email, String password, String role, String inviteCode) { |
| 433 | // 检查邮箱是否已经注册 |
| 434 | Optional<Users> existingEmailUser = userRepository.findByEmail(email); |
| 435 | if (existingEmailUser.isPresent()) { |
| 436 | return "该邮箱已被注册"; |
| 437 | } |
| 438 | |
| 439 | // 检查用户名是否已经存在 |
| 440 | Optional<Users> existingUsernameUser = userRepository.findByUsername(username); // 需要根据用户名查询 |
| 441 | if (existingUsernameUser.isPresent()) { |
| 442 | return "该用户名已被注册"; |
| 443 | } |
| 444 | |
| 445 | // 检查邀请码是否有效 |
| 446 | if (inviteCode == null || inviteCode.isEmpty()) { |
| 447 | return "邀请码不能为空"; |
| 448 | } |
| 449 | |
| 450 | Optional<UserInviteCode> invite = userInviteCodeRepository.findByInviteCode(inviteCode); |
| 451 | if (invite.isEmpty() || invite.get().getIsUsed()) { |
| 452 | return "邀请码无效或已被使用"; |
| 453 | } |
| 454 | |
Jin | f50fba6 | 2025-06-09 22:47:24 +0800 | [diff] [blame^] | 455 | // 设置默认等级为1(由于邀请码有效) |
| 456 | Long level = 1L; |
22301138 | 5e9c35a | 2025-06-04 15:52:45 +0800 | [diff] [blame] | 457 | |
| 458 | // 设置默认头像 URL |
| 459 | String avatarUrl = "https://example.com/default-avatar.jpg"; // 默认头像 |
| 460 | |
| 461 | // 获取邀请码对应的用户ID |
| 462 | UserInviteCode inviteEntity = invite.get(); |
| 463 | Long inviteUserId = inviteEntity.getUserId(); |
| 464 | |
| 465 | // 创建新用户 |
| 466 | Users newUser = new Users(); |
| 467 | newUser.setUsername(username); |
| 468 | newUser.setEmail(email); |
| 469 | newUser.setPassword(password); |
| 470 | newUser.setRole(role); |
| 471 | newUser.setInviteCount(0); // 初始邀请码数量为 0 |
| 472 | newUser.setLevel(level); |
| 473 | newUser.setAvatarUrl(avatarUrl); // 设置默认头像 |
| 474 | newUser.setRegistrationDate(new java.util.Date()); // 设置注册日期 |
| 475 | newUser.setCurrentExperience(0); // 默认经验为 0 |
| 476 | newUser.setCurrentSeedingHours(0f); // 默认做种时长为 0 |
| 477 | newUser.setRegistrationTime(new java.util.Date()); // 设置注册时间为当前时间 |
| 478 | |
| 479 | // 保存用户信息 |
| 480 | userRepository.save(newUser); |
| 481 | |
JinGe | fe5140c | 2025-06-06 20:07:42 +0800 | [diff] [blame] | 482 | FriendRelation newFriendRelation = new FriendRelation(); |
| 483 | newFriendRelation.setUserId(newUser.getUserId()); |
| 484 | newFriendRelation.setCreateTime(new Date()); |
| 485 | newFriendRelation.setFriendId(inviteUserId); |
| 486 | |
| 487 | FriendRelation newFriendRelations = new FriendRelation(); |
| 488 | newFriendRelations.setUserId(inviteUserId); |
| 489 | newFriendRelations.setCreateTime(new Date()); |
| 490 | newFriendRelations.setFriendId(newUser.getUserId()); |
| 491 | friendRelationRepository.save(newFriendRelation); |
| 492 | friendRelationRepository.save(newFriendRelations); |
22301138 | 5e9c35a | 2025-06-04 15:52:45 +0800 | [diff] [blame] | 493 | // 更新邀请码的使用状态 |
| 494 | inviteEntity.setIsUsed(true); |
| 495 | userInviteCodeRepository.save(inviteEntity); |
| 496 | |
| 497 | return "用户注册成功"; |
| 498 | } |
22301115 | cf6dba2 | 2025-03-25 19:06:21 +0800 | [diff] [blame] | 499 | |
| 500 | |
22301138 | 5e9c35a | 2025-06-04 15:52:45 +0800 | [diff] [blame] | 501 | public String loginUser(String username, String password) { |
| 502 | // 检查用户是否存在 |
| 503 | Optional<Users> userOptional = userRepository.findByUsername(username); |
| 504 | if (userOptional.isEmpty()) { |
| 505 | return "用户名不存在"; |
| 506 | } |
22301115 | cf6dba2 | 2025-03-25 19:06:21 +0800 | [diff] [blame] | 507 | |
22301138 | 5e9c35a | 2025-06-04 15:52:45 +0800 | [diff] [blame] | 508 | Users user = userOptional.get(); |
22301115 | cf6dba2 | 2025-03-25 19:06:21 +0800 | [diff] [blame] | 509 | |
22301138 | 5e9c35a | 2025-06-04 15:52:45 +0800 | [diff] [blame] | 510 | // 检查密码是否正确 |
| 511 | if (!user.getPassword().equals(password)) { |
| 512 | return "密码错误"; |
| 513 | } |
Krishya | b0cc188 | 2025-06-09 10:54:09 +0800 | [diff] [blame] | 514 | |
22301138 | 5e9c35a | 2025-06-04 15:52:45 +0800 | [diff] [blame] | 515 | // 登录成功 |
| 516 | return "登录成功"; |
| 517 | } |
| 518 | |
| 519 | public String changePassword(Long userId, String oldPassword, String newPassword, String confirmPassword) { |
| 520 | // 查找用户 |
| 521 | Users user = userRepository.findById(userId).orElse(null); |
| 522 | |
| 523 | if (user == null) { |
| 524 | return "用户不存在"; |
| 525 | } |
| 526 | |
| 527 | // 检查旧密码是否正确 |
| 528 | if (!user.getPassword().equals(oldPassword)) { |
| 529 | return "旧密码错误"; |
| 530 | } |
| 531 | |
| 532 | // 检查新密码和确认密码是否一致 |
| 533 | if (!newPassword.equals(confirmPassword)) { |
| 534 | return "新密码与确认密码不一致"; |
| 535 | } |
| 536 | |
| 537 | // 更新密码 |
| 538 | user.setPassword(newPassword); |
| 539 | userRepository.save(user); |
| 540 | |
| 541 | return "密码修改成功"; |
| 542 | } |
| 543 | |
| 544 | // 获取用户个人资料 |
| 545 | public Map<String, Object> getProfile(Long userId) { |
| 546 | Optional<Users> userOptional = userRepository.findById(userId); |
| 547 | |
| 548 | // 如果用户不存在,返回null或者可以抛出异常 |
| 549 | if (userOptional.isEmpty()) { |
| 550 | return null; // 可以返回 null 或者根据需要返回错误信息 |
| 551 | } |
| 552 | |
| 553 | Users user = userOptional.get(); |
| 554 | |
| 555 | // 将需要的字段放入 Map 中 |
| 556 | Map<String, Object> profile = new LinkedHashMap<>(); |
| 557 | profile.put("avatarUrl", user.getAvatarUrl()); |
| 558 | profile.put("username", user.getUsername()); |
| 559 | profile.put("email", user.getEmail()); |
| 560 | profile.put("gender", user.getGender()); |
| 561 | profile.put("description", user.getDescription()); |
| 562 | profile.put("hobbies", user.getHobbies()); |
| 563 | profile.put("level", user.getLevel()); |
| 564 | profile.put("Experience", user.getCurrentExperience()); |
| 565 | profile.put("uploadCount", user.getUploadCount()); |
| 566 | profile.put("downloadCount", user.getDownloadCount()); |
| 567 | profile.put("shareRate", user.getShareRate()); |
| 568 | profile.put("registrationTime", user.getRegistrationTime()); |
| 569 | |
| 570 | return profile; |
| 571 | } |
| 572 | |
| 573 | // 修改用户个人资料 |
22301138 | b253341 | 2025-06-05 00:18:58 +0800 | [diff] [blame] | 574 | public boolean editProfile(Long userId, String nickname, String gender, String description, String hobbies) { |
22301138 | 5e9c35a | 2025-06-04 15:52:45 +0800 | [diff] [blame] | 575 | Optional<Users> userOptional = userRepository.findById(userId); |
| 576 | |
| 577 | // 如果用户不存在,返回false |
| 578 | if (userOptional.isEmpty()) { |
| 579 | return false; // 用户不存在 |
| 580 | } |
| 581 | |
| 582 | Users user = userOptional.get(); |
| 583 | |
| 584 | // 更新用户资料,只有传入值才会更新对应字段 |
22301138 | 5e9c35a | 2025-06-04 15:52:45 +0800 | [diff] [blame] | 585 | if (nickname != null) { |
| 586 | user.setUsername(nickname); |
| 587 | } |
| 588 | if (gender != null) { |
| 589 | user.setGender(gender); |
| 590 | } |
| 591 | if (description != null) { |
| 592 | user.setDescription(description); |
| 593 | } |
| 594 | if (hobbies != null) { |
| 595 | user.setHobbies(hobbies); |
| 596 | } |
| 597 | |
| 598 | // 保存更新后的用户信息 |
| 599 | userRepository.save(user); |
| 600 | |
| 601 | return true; // 更新成功 |
| 602 | } |
| 603 | |
| 604 | public Map<String, Object> calculateShareRate(Long userId) { |
| 605 | // 查找用户 |
| 606 | Users user = userRepository.findById(userId).orElse(null); |
| 607 | if (user == null) { |
| 608 | return Map.of("status", "error", "message", "用户不存在"); |
| 609 | } |
| 610 | |
| 611 | // 获取上传量和下载量 |
| 612 | Float uploadCount = user.getUploadCount(); |
| 613 | Float downloadCount = user.getDownloadCount(); |
| 614 | |
| 615 | // 计算分享率 |
| 616 | Float shareRate = 0f; // 默认分享率为0 |
| 617 | if (downloadCount != 0) { |
| 618 | shareRate = uploadCount / downloadCount; // 分享率 = 上传量 / 下载量 |
| 619 | } |
| 620 | |
| 621 | // 更新用户的分享率 |
| 622 | user.setShareRate(shareRate); |
Krishya | b0cc188 | 2025-06-09 10:54:09 +0800 | [diff] [blame] | 623 | user.setLastupdatetime(new Date()); |
| 624 | |
22301138 | 5e9c35a | 2025-06-04 15:52:45 +0800 | [diff] [blame] | 625 | userRepository.save(user); |
| 626 | |
| 627 | // 返回结果 |
| 628 | return Map.of("status", "success", "message", "分享率计算成功", "shareRate", shareRate); |
| 629 | } |
22301115 | cf6dba2 | 2025-03-25 19:06:21 +0800 | [diff] [blame] | 630 | |
| 631 | |
22301138 | b253341 | 2025-06-05 00:18:58 +0800 | [diff] [blame] | 632 | private static final String AVATAR_DIR = "uploads/avatarUrl/"; |
| 633 | |
| 634 | public Map<String, Object> uploadUserAvatar(Long userId, MultipartFile file) { |
| 635 | Users user = userRepository.findById(userId) |
| 636 | .orElseThrow(() -> new RuntimeException("用户不存在")); |
| 637 | |
| 638 | try { |
| 639 | String avatarUrl = saveAvatar(file, userId); |
| 640 | user.setAvatarUrl(avatarUrl); |
| 641 | userRepository.save(user); |
| 642 | |
| 643 | Map<String, Object> response = new HashMap<>(); |
| 644 | response.put("status", "success"); |
| 645 | response.put("message", "头像上传成功"); |
| 646 | response.put("userId", user.getUserId()); |
| 647 | response.put("avatarUrl", avatarUrl); |
| 648 | return response; |
| 649 | |
| 650 | } catch (IOException e) { |
| 651 | throw new RuntimeException("头像上传失败: " + e.getMessage()); |
| 652 | } |
| 653 | } |
| 654 | |
| 655 | // 保存头像文件并返回可访问 URL |
| 656 | public String saveAvatar(MultipartFile file, Long userId) throws IOException { |
| 657 | String originalFilename = file.getOriginalFilename(); |
| 658 | String extension = originalFilename.substring(originalFilename.lastIndexOf('.')); |
| 659 | String fileName = userId + extension; // 以用户ID作为文件名 |
| 660 | Path path = Paths.get(AVATAR_DIR + fileName); |
| 661 | Files.createDirectories(path.getParent()); |
| 662 | Files.write(path, file.getBytes()); |
| 663 | return "/" + AVATAR_DIR + fileName; // 返回相对URL路径 |
| 664 | } |
| 665 | |
| 666 | |
Krishya | b0cc188 | 2025-06-09 10:54:09 +0800 | [diff] [blame] | 667 | @Transactional |
| 668 | public String checkUserShareRate(Long userId) { |
| 669 | Optional<Users> optionalUser = userRepository.findById(userId); |
| 670 | |
| 671 | if (optionalUser.isEmpty()) { |
| 672 | return "用户不存在"; |
| 673 | } |
| 674 | |
| 675 | Users user = optionalUser.get(); |
| 676 | |
| 677 | // 检查用户是否注册超过3天 |
| 678 | LocalDate registrationDate = user.getRegistrationTime().toInstant() |
| 679 | .atZone(ZoneId.systemDefault()) |
| 680 | .toLocalDate(); |
| 681 | LocalDate today = LocalDate.now(); |
| 682 | |
| 683 | if (today.minusDays(3).isAfter(registrationDate)) { |
| 684 | // 用户注册超过3天,检查分享率 |
| 685 | |
| 686 | // 假设我们有一个方法可以获取用户最近3天的分享率历史 |
| 687 | boolean hasLowShareRateForThreeDays = checkShareRateHistory(userId); |
| 688 | |
| 689 | if (hasLowShareRateForThreeDays) { |
| 690 | // 分享率连续3天低于0.5 |
| 691 | |
| 692 | // 检查是否已发送过警告且超过24小时 |
| 693 | Date lastUpdateTime = user.getLastupdatetime(); |
| 694 | if (lastUpdateTime != null) { |
| 695 | LocalDate warningDate = lastUpdateTime.toInstant() |
| 696 | .atZone(ZoneId.systemDefault()) |
| 697 | .toLocalDate(); |
| 698 | |
| 699 | if (today.minusDays(1).isAfter(warningDate)) { |
| 700 | // 已警告超过24小时,执行账号注销 |
| 701 | userRepository.delete(user); |
| 702 | return "用户因分享率长期低于标准,账号已被注销"; |
| 703 | } |
| 704 | } |
| 705 | |
| 706 | // 首次发现或未超过24小时,发送警告 |
| 707 | user.setLastupdatetime(new Date()); |
| 708 | userRepository.save(user); |
| 709 | return "警告:您的分享率已连续3天低于0.5,请尽快提升,否则账号将被注销"; |
| 710 | } |
| 711 | } |
| 712 | |
| 713 | return "用户状态正常"; |
| 714 | } |
| 715 | |
| 716 | private boolean checkShareRateHistory(Long userId) { |
| 717 | Users user = userRepository.findById(userId).orElse(null); |
| 718 | if (user != null && user.getShareRate() < 0.5) { |
| 719 | // 实际应用中应查询历史记录,这里简化处理 |
| 720 | return true; |
| 721 | } |
| 722 | return false; |
| 723 | } |
Jin | 80d879a | 2025-06-09 20:53:18 +0800 | [diff] [blame] | 724 | public String recharge(Long userId, Integer amount) { |
| 725 | if (amount == null || amount <= 0) { |
| 726 | return "充值金额必须大于0"; |
| 727 | } |
| 728 | Users user = userRepository.findById(userId).orElse(null); |
| 729 | if (user == null) { |
| 730 | return "用户不存在"; |
| 731 | } |
Jin | f50fba6 | 2025-06-09 22:47:24 +0800 | [diff] [blame^] | 732 | // 处理money为BigDecimal |
| 733 | BigDecimal currentMoney = user.getMoney() == null ? BigDecimal.ZERO : user.getMoney(); |
| 734 | BigDecimal rechargeAmount = new BigDecimal(amount); |
| 735 | BigDecimal newMoney = currentMoney.add(rechargeAmount); |
| 736 | |
| 737 | user.setMoney(newMoney); |
Jin | 80d879a | 2025-06-09 20:53:18 +0800 | [diff] [blame] | 738 | userRepository.save(user); |
| 739 | return "充值成功,当前余额:" + user.getMoney(); |
| 740 | } |
| 741 | |
Krishya | b0cc188 | 2025-06-09 10:54:09 +0800 | [diff] [blame] | 742 | |
Jin | f50fba6 | 2025-06-09 22:47:24 +0800 | [diff] [blame^] | 743 | public Users getById(Long userId) { |
| 744 | return userRepository.findById(userId).orElse(null); |
| 745 | } |
| 746 | |
| 747 | |
| 748 | /** |
| 749 | * 检查用户是否有连续三天的登录记录 |
| 750 | * @param userId 用户ID |
| 751 | * @return 是否有连续三天的登录记录 |
| 752 | */ |
| 753 | public boolean hasConsecutiveLoginDays(Long userId) { |
| 754 | // 获取用户的所有登录日期 |
| 755 | List<LocalDateTime> loginDates = loginLogRepository.findDistinctLoginDates(userId); |
| 756 | |
| 757 | if (loginDates.size() < 3) { |
| 758 | return false; |
| 759 | } |
| 760 | |
| 761 | // 将日期转换为LocalDate并排序 |
| 762 | List<LocalDate> dates = loginDates.stream() |
| 763 | .map(LocalDateTime::toLocalDate) |
| 764 | .distinct() |
| 765 | .sorted() |
| 766 | .toList(); |
| 767 | |
| 768 | // 检查是否有连续的三天 |
| 769 | for (int i = 0; i < dates.size() - 2; i++) { |
| 770 | LocalDate date1 = dates.get(i); |
| 771 | LocalDate date2 = dates.get(i + 1); |
| 772 | LocalDate date3 = dates.get(i + 2); |
| 773 | |
| 774 | if (date2.equals(date1.plusDays(1)) && date3.equals(date2.plusDays(1))) { |
| 775 | return true; |
| 776 | } |
| 777 | } |
| 778 | |
| 779 | return false; |
| 780 | } |
| 781 | |
| 782 | |
22301138 | b253341 | 2025-06-05 00:18:58 +0800 | [diff] [blame] | 783 | |
22301138 | 5e9c35a | 2025-06-04 15:52:45 +0800 | [diff] [blame] | 784 | } |