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; |
22301138 | 5e9c35a | 2025-06-04 15:52:45 +0800 | [diff] [blame] | 337 | import com.example.myproject.repository.UserRepository; |
| 338 | import com.example.myproject.repository.UserInviteCodeRepository; |
Krishya | b0cc188 | 2025-06-09 10:54:09 +0800 | [diff] [blame] | 339 | import jakarta.transaction.Transactional; |
22301138 | 5e9c35a | 2025-06-04 15:52:45 +0800 | [diff] [blame] | 340 | import org.springframework.beans.factory.annotation.Autowired; |
| 341 | import org.springframework.stereotype.Service; |
22301138 | b253341 | 2025-06-05 00:18:58 +0800 | [diff] [blame] | 342 | import org.springframework.web.multipart.MultipartFile; |
22301138 | 5e9c35a | 2025-06-04 15:52:45 +0800 | [diff] [blame] | 343 | |
22301138 | b253341 | 2025-06-05 00:18:58 +0800 | [diff] [blame] | 344 | import javax.servlet.http.HttpServletRequest; |
| 345 | import java.io.File; |
| 346 | import java.io.IOException; |
| 347 | import java.nio.file.Files; |
| 348 | import java.nio.file.Path; |
| 349 | import java.nio.file.Paths; |
Krishya | b0cc188 | 2025-06-09 10:54:09 +0800 | [diff] [blame] | 350 | import java.time.LocalDate; |
22301138 | b253341 | 2025-06-05 00:18:58 +0800 | [diff] [blame] | 351 | import java.time.LocalDateTime; |
Krishya | b0cc188 | 2025-06-09 10:54:09 +0800 | [diff] [blame] | 352 | import java.time.ZoneId; |
22301138 | 5e9c35a | 2025-06-04 15:52:45 +0800 | [diff] [blame] | 353 | import java.util.*; |
| 354 | |
| 355 | @Service |
| 356 | public class UserService { |
| 357 | |
| 358 | @Autowired |
| 359 | private UserRepository userRepository; |
| 360 | |
| 361 | @Autowired |
| 362 | private UserInviteCodeRepository userInviteCodeRepository; |
| 363 | |
JinGe | fe5140c | 2025-06-06 20:07:42 +0800 | [diff] [blame] | 364 | @Autowired |
| 365 | private FriendRelationRepository friendRelationRepository; |
| 366 | |
22301138 | 5e9c35a | 2025-06-04 15:52:45 +0800 | [diff] [blame] | 367 | // 生成邀请码 |
| 368 | public Map<String, Object> generateInviteCode(Long userId) { |
| 369 | // 获取用户信息 |
| 370 | Users user = userRepository.findById(userId).orElse(null); |
| 371 | |
| 372 | // 如果用户不存在,返回错误 |
| 373 | if (user == null) { |
| 374 | Map<String, Object> errorResponse = new HashMap<>(); |
| 375 | errorResponse.put("status", "failure"); |
| 376 | errorResponse.put("message", "用户不存在"); |
| 377 | return errorResponse; |
| 378 | } |
| 379 | |
| 380 | // 检查用户的邀请数量 |
| 381 | if (user.getInviteCount() <= 0) { |
| 382 | Map<String, Object> errorResponse = new HashMap<>(); |
| 383 | errorResponse.put("status", "failure"); |
| 384 | errorResponse.put("message", "没有剩余的邀请码"); |
| 385 | return errorResponse; |
| 386 | } |
| 387 | |
| 388 | // 生成唯一的邀请码 |
| 389 | String inviteCode = generateUniqueInviteCode(); |
| 390 | |
| 391 | // 将邀请码保存到 `user_invite_code` 表 |
| 392 | UserInviteCode userInviteCode = new UserInviteCode(); |
| 393 | userInviteCode.setUserId(userId); |
| 394 | userInviteCode.setInviteCode(inviteCode); |
| 395 | userInviteCode.setCreatedAt(java.time.LocalDateTime.now()); |
| 396 | |
| 397 | userInviteCodeRepository.save(userInviteCode); |
| 398 | |
| 399 | // 更新用户的 `invite_count`,减少1 |
| 400 | user.setInviteCount(user.getInviteCount() - 1); |
| 401 | userRepository.save(user); |
| 402 | |
| 403 | // 返回成功信息 |
| 404 | Map<String, Object> response = new HashMap<>(); |
| 405 | response.put("status", "success"); |
| 406 | response.put("message", "邀请码生成成功"); |
| 407 | response.put("invite_code", inviteCode); |
| 408 | |
| 409 | return response; |
| 410 | } |
| 411 | |
| 412 | // 生成唯一的邀请码,使用26个字母(大小写) |
| 413 | private String generateUniqueInviteCode() { |
| 414 | String characters = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"; |
| 415 | StringBuilder inviteCode = new StringBuilder(); |
| 416 | |
| 417 | Random random = new Random(); |
| 418 | for (int i = 0; i < 10; i++) { |
| 419 | inviteCode.append(characters.charAt(random.nextInt(characters.length()))); |
| 420 | } |
| 421 | |
| 422 | return inviteCode.toString(); |
| 423 | } |
| 424 | |
| 425 | public String registerUser(String username, String email, String password, String role, String inviteCode) { |
| 426 | // 检查邮箱是否已经注册 |
| 427 | Optional<Users> existingEmailUser = userRepository.findByEmail(email); |
| 428 | if (existingEmailUser.isPresent()) { |
| 429 | return "该邮箱已被注册"; |
| 430 | } |
| 431 | |
| 432 | // 检查用户名是否已经存在 |
| 433 | Optional<Users> existingUsernameUser = userRepository.findByUsername(username); // 需要根据用户名查询 |
| 434 | if (existingUsernameUser.isPresent()) { |
| 435 | return "该用户名已被注册"; |
| 436 | } |
| 437 | |
| 438 | // 检查邀请码是否有效 |
| 439 | if (inviteCode == null || inviteCode.isEmpty()) { |
| 440 | return "邀请码不能为空"; |
| 441 | } |
| 442 | |
| 443 | Optional<UserInviteCode> invite = userInviteCodeRepository.findByInviteCode(inviteCode); |
| 444 | if (invite.isEmpty() || invite.get().getIsUsed()) { |
| 445 | return "邀请码无效或已被使用"; |
| 446 | } |
| 447 | |
| 448 | // 设置默认等级为2(由于邀请码有效) |
| 449 | Long level = 2L; |
| 450 | |
| 451 | // 设置默认头像 URL |
| 452 | String avatarUrl = "https://example.com/default-avatar.jpg"; // 默认头像 |
| 453 | |
| 454 | // 获取邀请码对应的用户ID |
| 455 | UserInviteCode inviteEntity = invite.get(); |
| 456 | Long inviteUserId = inviteEntity.getUserId(); |
| 457 | |
| 458 | // 创建新用户 |
| 459 | Users newUser = new Users(); |
| 460 | newUser.setUsername(username); |
| 461 | newUser.setEmail(email); |
| 462 | newUser.setPassword(password); |
| 463 | newUser.setRole(role); |
| 464 | newUser.setInviteCount(0); // 初始邀请码数量为 0 |
| 465 | newUser.setLevel(level); |
| 466 | newUser.setAvatarUrl(avatarUrl); // 设置默认头像 |
| 467 | newUser.setRegistrationDate(new java.util.Date()); // 设置注册日期 |
| 468 | newUser.setCurrentExperience(0); // 默认经验为 0 |
| 469 | newUser.setCurrentSeedingHours(0f); // 默认做种时长为 0 |
| 470 | newUser.setRegistrationTime(new java.util.Date()); // 设置注册时间为当前时间 |
| 471 | |
| 472 | // 保存用户信息 |
| 473 | userRepository.save(newUser); |
| 474 | |
JinGe | fe5140c | 2025-06-06 20:07:42 +0800 | [diff] [blame] | 475 | FriendRelation newFriendRelation = new FriendRelation(); |
| 476 | newFriendRelation.setUserId(newUser.getUserId()); |
| 477 | newFriendRelation.setCreateTime(new Date()); |
| 478 | newFriendRelation.setFriendId(inviteUserId); |
| 479 | |
| 480 | FriendRelation newFriendRelations = new FriendRelation(); |
| 481 | newFriendRelations.setUserId(inviteUserId); |
| 482 | newFriendRelations.setCreateTime(new Date()); |
| 483 | newFriendRelations.setFriendId(newUser.getUserId()); |
| 484 | friendRelationRepository.save(newFriendRelation); |
| 485 | friendRelationRepository.save(newFriendRelations); |
22301138 | 5e9c35a | 2025-06-04 15:52:45 +0800 | [diff] [blame] | 486 | // 更新邀请码的使用状态 |
| 487 | inviteEntity.setIsUsed(true); |
| 488 | userInviteCodeRepository.save(inviteEntity); |
| 489 | |
| 490 | return "用户注册成功"; |
| 491 | } |
22301115 | cf6dba2 | 2025-03-25 19:06:21 +0800 | [diff] [blame] | 492 | |
| 493 | |
22301138 | 5e9c35a | 2025-06-04 15:52:45 +0800 | [diff] [blame] | 494 | public String loginUser(String username, String password) { |
| 495 | // 检查用户是否存在 |
| 496 | Optional<Users> userOptional = userRepository.findByUsername(username); |
| 497 | if (userOptional.isEmpty()) { |
| 498 | return "用户名不存在"; |
| 499 | } |
22301115 | cf6dba2 | 2025-03-25 19:06:21 +0800 | [diff] [blame] | 500 | |
22301138 | 5e9c35a | 2025-06-04 15:52:45 +0800 | [diff] [blame] | 501 | Users user = userOptional.get(); |
22301115 | cf6dba2 | 2025-03-25 19:06:21 +0800 | [diff] [blame] | 502 | |
22301138 | 5e9c35a | 2025-06-04 15:52:45 +0800 | [diff] [blame] | 503 | // 检查密码是否正确 |
| 504 | if (!user.getPassword().equals(password)) { |
| 505 | return "密码错误"; |
| 506 | } |
Krishya | b0cc188 | 2025-06-09 10:54:09 +0800 | [diff] [blame] | 507 | |
22301138 | 5e9c35a | 2025-06-04 15:52:45 +0800 | [diff] [blame] | 508 | // 登录成功 |
| 509 | return "登录成功"; |
| 510 | } |
| 511 | |
| 512 | public String changePassword(Long userId, String oldPassword, String newPassword, String confirmPassword) { |
| 513 | // 查找用户 |
| 514 | Users user = userRepository.findById(userId).orElse(null); |
| 515 | |
| 516 | if (user == null) { |
| 517 | return "用户不存在"; |
| 518 | } |
| 519 | |
| 520 | // 检查旧密码是否正确 |
| 521 | if (!user.getPassword().equals(oldPassword)) { |
| 522 | return "旧密码错误"; |
| 523 | } |
| 524 | |
| 525 | // 检查新密码和确认密码是否一致 |
| 526 | if (!newPassword.equals(confirmPassword)) { |
| 527 | return "新密码与确认密码不一致"; |
| 528 | } |
| 529 | |
| 530 | // 更新密码 |
| 531 | user.setPassword(newPassword); |
| 532 | userRepository.save(user); |
| 533 | |
| 534 | return "密码修改成功"; |
| 535 | } |
| 536 | |
| 537 | // 获取用户个人资料 |
| 538 | public Map<String, Object> getProfile(Long userId) { |
| 539 | Optional<Users> userOptional = userRepository.findById(userId); |
| 540 | |
| 541 | // 如果用户不存在,返回null或者可以抛出异常 |
| 542 | if (userOptional.isEmpty()) { |
| 543 | return null; // 可以返回 null 或者根据需要返回错误信息 |
| 544 | } |
| 545 | |
| 546 | Users user = userOptional.get(); |
| 547 | |
| 548 | // 将需要的字段放入 Map 中 |
| 549 | Map<String, Object> profile = new LinkedHashMap<>(); |
| 550 | profile.put("avatarUrl", user.getAvatarUrl()); |
| 551 | profile.put("username", user.getUsername()); |
| 552 | profile.put("email", user.getEmail()); |
| 553 | profile.put("gender", user.getGender()); |
| 554 | profile.put("description", user.getDescription()); |
| 555 | profile.put("hobbies", user.getHobbies()); |
| 556 | profile.put("level", user.getLevel()); |
| 557 | profile.put("Experience", user.getCurrentExperience()); |
| 558 | profile.put("uploadCount", user.getUploadCount()); |
| 559 | profile.put("downloadCount", user.getDownloadCount()); |
| 560 | profile.put("shareRate", user.getShareRate()); |
| 561 | profile.put("registrationTime", user.getRegistrationTime()); |
| 562 | |
| 563 | return profile; |
| 564 | } |
| 565 | |
| 566 | // 修改用户个人资料 |
22301138 | b253341 | 2025-06-05 00:18:58 +0800 | [diff] [blame] | 567 | public boolean editProfile(Long userId, String nickname, String gender, String description, String hobbies) { |
22301138 | 5e9c35a | 2025-06-04 15:52:45 +0800 | [diff] [blame] | 568 | Optional<Users> userOptional = userRepository.findById(userId); |
| 569 | |
| 570 | // 如果用户不存在,返回false |
| 571 | if (userOptional.isEmpty()) { |
| 572 | return false; // 用户不存在 |
| 573 | } |
| 574 | |
| 575 | Users user = userOptional.get(); |
| 576 | |
| 577 | // 更新用户资料,只有传入值才会更新对应字段 |
22301138 | 5e9c35a | 2025-06-04 15:52:45 +0800 | [diff] [blame] | 578 | if (nickname != null) { |
| 579 | user.setUsername(nickname); |
| 580 | } |
| 581 | if (gender != null) { |
| 582 | user.setGender(gender); |
| 583 | } |
| 584 | if (description != null) { |
| 585 | user.setDescription(description); |
| 586 | } |
| 587 | if (hobbies != null) { |
| 588 | user.setHobbies(hobbies); |
| 589 | } |
| 590 | |
| 591 | // 保存更新后的用户信息 |
| 592 | userRepository.save(user); |
| 593 | |
| 594 | return true; // 更新成功 |
| 595 | } |
| 596 | |
| 597 | public Map<String, Object> calculateShareRate(Long userId) { |
| 598 | // 查找用户 |
| 599 | Users user = userRepository.findById(userId).orElse(null); |
| 600 | if (user == null) { |
| 601 | return Map.of("status", "error", "message", "用户不存在"); |
| 602 | } |
| 603 | |
| 604 | // 获取上传量和下载量 |
| 605 | Float uploadCount = user.getUploadCount(); |
| 606 | Float downloadCount = user.getDownloadCount(); |
| 607 | |
| 608 | // 计算分享率 |
| 609 | Float shareRate = 0f; // 默认分享率为0 |
| 610 | if (downloadCount != 0) { |
| 611 | shareRate = uploadCount / downloadCount; // 分享率 = 上传量 / 下载量 |
| 612 | } |
| 613 | |
| 614 | // 更新用户的分享率 |
| 615 | user.setShareRate(shareRate); |
Krishya | b0cc188 | 2025-06-09 10:54:09 +0800 | [diff] [blame] | 616 | user.setLastupdatetime(new Date()); |
| 617 | |
22301138 | 5e9c35a | 2025-06-04 15:52:45 +0800 | [diff] [blame] | 618 | userRepository.save(user); |
| 619 | |
| 620 | // 返回结果 |
| 621 | return Map.of("status", "success", "message", "分享率计算成功", "shareRate", shareRate); |
| 622 | } |
22301115 | cf6dba2 | 2025-03-25 19:06:21 +0800 | [diff] [blame] | 623 | |
| 624 | |
22301138 | b253341 | 2025-06-05 00:18:58 +0800 | [diff] [blame] | 625 | private static final String AVATAR_DIR = "uploads/avatarUrl/"; |
| 626 | |
| 627 | public Map<String, Object> uploadUserAvatar(Long userId, MultipartFile file) { |
| 628 | Users user = userRepository.findById(userId) |
| 629 | .orElseThrow(() -> new RuntimeException("用户不存在")); |
| 630 | |
| 631 | try { |
| 632 | String avatarUrl = saveAvatar(file, userId); |
| 633 | user.setAvatarUrl(avatarUrl); |
| 634 | userRepository.save(user); |
| 635 | |
| 636 | Map<String, Object> response = new HashMap<>(); |
| 637 | response.put("status", "success"); |
| 638 | response.put("message", "头像上传成功"); |
| 639 | response.put("userId", user.getUserId()); |
| 640 | response.put("avatarUrl", avatarUrl); |
| 641 | return response; |
| 642 | |
| 643 | } catch (IOException e) { |
| 644 | throw new RuntimeException("头像上传失败: " + e.getMessage()); |
| 645 | } |
| 646 | } |
| 647 | |
| 648 | // 保存头像文件并返回可访问 URL |
| 649 | public String saveAvatar(MultipartFile file, Long userId) throws IOException { |
| 650 | String originalFilename = file.getOriginalFilename(); |
| 651 | String extension = originalFilename.substring(originalFilename.lastIndexOf('.')); |
| 652 | String fileName = userId + extension; // 以用户ID作为文件名 |
| 653 | Path path = Paths.get(AVATAR_DIR + fileName); |
| 654 | Files.createDirectories(path.getParent()); |
| 655 | Files.write(path, file.getBytes()); |
| 656 | return "/" + AVATAR_DIR + fileName; // 返回相对URL路径 |
| 657 | } |
| 658 | |
| 659 | |
Krishya | b0cc188 | 2025-06-09 10:54:09 +0800 | [diff] [blame] | 660 | @Transactional |
| 661 | public String checkUserShareRate(Long userId) { |
| 662 | Optional<Users> optionalUser = userRepository.findById(userId); |
| 663 | |
| 664 | if (optionalUser.isEmpty()) { |
| 665 | return "用户不存在"; |
| 666 | } |
| 667 | |
| 668 | Users user = optionalUser.get(); |
| 669 | |
| 670 | // 检查用户是否注册超过3天 |
| 671 | LocalDate registrationDate = user.getRegistrationTime().toInstant() |
| 672 | .atZone(ZoneId.systemDefault()) |
| 673 | .toLocalDate(); |
| 674 | LocalDate today = LocalDate.now(); |
| 675 | |
| 676 | if (today.minusDays(3).isAfter(registrationDate)) { |
| 677 | // 用户注册超过3天,检查分享率 |
| 678 | |
| 679 | // 假设我们有一个方法可以获取用户最近3天的分享率历史 |
| 680 | boolean hasLowShareRateForThreeDays = checkShareRateHistory(userId); |
| 681 | |
| 682 | if (hasLowShareRateForThreeDays) { |
| 683 | // 分享率连续3天低于0.5 |
| 684 | |
| 685 | // 检查是否已发送过警告且超过24小时 |
| 686 | Date lastUpdateTime = user.getLastupdatetime(); |
| 687 | if (lastUpdateTime != null) { |
| 688 | LocalDate warningDate = lastUpdateTime.toInstant() |
| 689 | .atZone(ZoneId.systemDefault()) |
| 690 | .toLocalDate(); |
| 691 | |
| 692 | if (today.minusDays(1).isAfter(warningDate)) { |
| 693 | // 已警告超过24小时,执行账号注销 |
| 694 | userRepository.delete(user); |
| 695 | return "用户因分享率长期低于标准,账号已被注销"; |
| 696 | } |
| 697 | } |
| 698 | |
| 699 | // 首次发现或未超过24小时,发送警告 |
| 700 | user.setLastupdatetime(new Date()); |
| 701 | userRepository.save(user); |
| 702 | return "警告:您的分享率已连续3天低于0.5,请尽快提升,否则账号将被注销"; |
| 703 | } |
| 704 | } |
| 705 | |
| 706 | return "用户状态正常"; |
| 707 | } |
| 708 | |
| 709 | private boolean checkShareRateHistory(Long userId) { |
| 710 | Users user = userRepository.findById(userId).orElse(null); |
| 711 | if (user != null && user.getShareRate() < 0.5) { |
| 712 | // 实际应用中应查询历史记录,这里简化处理 |
| 713 | return true; |
| 714 | } |
| 715 | return false; |
| 716 | } |
| 717 | |
22301138 | b253341 | 2025-06-05 00:18:58 +0800 | [diff] [blame] | 718 | |
22301138 | 5e9c35a | 2025-06-04 15:52:45 +0800 | [diff] [blame] | 719 | } |