xiukira | 687b9cb | 2025-05-29 15:15:02 +0800 | [diff] [blame] | 1 | package com.g9.g9backend.controller; |
| 2 | |
xiukira | d0a7a08 | 2025-06-05 16:28:08 +0800 | [diff] [blame] | 3 | import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; |
| 4 | import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper; |
xiukira | 07ea6ee | 2025-06-06 19:28:24 +0800 | [diff] [blame] | 5 | import com.baomidou.mybatisplus.core.metadata.IPage; |
| 6 | import com.baomidou.mybatisplus.extension.plugins.pagination.Page; |
xiukira | d0a7a08 | 2025-06-05 16:28:08 +0800 | [diff] [blame] | 7 | import com.g9.g9backend.pojo.*; |
| 8 | import com.g9.g9backend.pojo.DTO.*; |
| 9 | import com.g9.g9backend.service.*; |
xiukira | 687b9cb | 2025-05-29 15:15:02 +0800 | [diff] [blame] | 10 | import org.slf4j.Logger; |
| 11 | import org.slf4j.LoggerFactory; |
xiukira | 29f8d37 | 2025-06-06 18:07:13 +0800 | [diff] [blame] | 12 | import org.springframework.http.HttpStatus; |
xiukira | d0a7a08 | 2025-06-05 16:28:08 +0800 | [diff] [blame] | 13 | import org.springframework.http.ResponseEntity; |
| 14 | import org.springframework.web.bind.annotation.*; |
xiukira | 687b9cb | 2025-05-29 15:15:02 +0800 | [diff] [blame] | 15 | |
xiukira | 07ea6ee | 2025-06-06 19:28:24 +0800 | [diff] [blame] | 16 | import java.util.ArrayList; |
| 17 | import java.util.List; |
| 18 | |
xiukira | 687b9cb | 2025-05-29 15:15:02 +0800 | [diff] [blame] | 19 | /** |
| 20 | * UserController 用户控制器类,处理与用户相关的请求 |
| 21 | * |
| 22 | * @author hcy |
| 23 | */ |
| 24 | @RestController |
| 25 | @RequestMapping("/user") |
| 26 | public class UserController { |
| 27 | |
xiukira | d0a7a08 | 2025-06-05 16:28:08 +0800 | [diff] [blame] | 28 | private final UserService userService; |
| 29 | |
| 30 | private final InvitationService invitationService; |
| 31 | |
| 32 | private final SubscriptionService subscriptionService; |
| 33 | |
xiukira | 29f8d37 | 2025-06-06 18:07:13 +0800 | [diff] [blame] | 34 | private final SearchHistoryService searchHistoryService; |
| 35 | |
xiukira | 07ea6ee | 2025-06-06 19:28:24 +0800 | [diff] [blame] | 36 | private final RewardService rewardService; |
| 37 | |
| 38 | private final UserCollectionService userCollectionService; |
| 39 | |
| 40 | private final UserUploadService userUploadService; |
| 41 | |
| 42 | private final UserPurchaseService userPurchaseService; |
| 43 | |
| 44 | private final ResourceService resourceService; |
| 45 | |
| 46 | public UserController(UserService userService, InvitationService invitationService, SubscriptionService subscriptionService, SearchHistoryService searchHistoryService, RewardService rewardService, UserCollectionService userCollectionService, UserUploadService userUploadService, UserPurchaseService userPurchaseService, ResourceService resourceService) { |
xiukira | d0a7a08 | 2025-06-05 16:28:08 +0800 | [diff] [blame] | 47 | this.userService = userService; |
| 48 | this.invitationService = invitationService; |
| 49 | this.subscriptionService = subscriptionService; |
xiukira | 29f8d37 | 2025-06-06 18:07:13 +0800 | [diff] [blame] | 50 | this.searchHistoryService = searchHistoryService; |
xiukira | 07ea6ee | 2025-06-06 19:28:24 +0800 | [diff] [blame] | 51 | this.rewardService = rewardService; |
| 52 | this.userCollectionService = userCollectionService; |
| 53 | this.userUploadService = userUploadService; |
| 54 | this.userPurchaseService = userPurchaseService; |
| 55 | this.resourceService = resourceService; |
xiukira | d0a7a08 | 2025-06-05 16:28:08 +0800 | [diff] [blame] | 56 | } |
| 57 | |
xiukira | 07ea6ee | 2025-06-06 19:28:24 +0800 | [diff] [blame] | 58 | |
xiukira | 687b9cb | 2025-05-29 15:15:02 +0800 | [diff] [blame] | 59 | private final Logger logger = LoggerFactory.getLogger(UserController.class); |
xiukira | 687b9cb | 2025-05-29 15:15:02 +0800 | [diff] [blame] | 60 | |
xiukira | d0a7a08 | 2025-06-05 16:28:08 +0800 | [diff] [blame] | 61 | /** |
| 62 | * 用户注册 |
| 63 | * |
| 64 | * @param registerDTO 用户注册 |
| 65 | * @return 注册结果 |
| 66 | */ |
| 67 | @PostMapping("/register") |
| 68 | public ResponseEntity<String> register(@RequestBody RegisterDTO registerDTO) { |
| 69 | String username = registerDTO.getUsername(); |
| 70 | String password = registerDTO.getPassword(); |
| 71 | String invitationCode = registerDTO.getInvitationCode(); |
| 72 | logger.info("Register request received for account: {}", username); |
xiukira | 687b9cb | 2025-05-29 15:15:02 +0800 | [diff] [blame] | 73 | |
xiukira | d0a7a08 | 2025-06-05 16:28:08 +0800 | [diff] [blame] | 74 | // 根据用户名查询该用户名是否已存在 |
| 75 | QueryWrapper<User> userQuery = new QueryWrapper<>(); |
| 76 | userQuery.eq("username", username); |
| 77 | User userCheck = userService.getOne(userQuery); |
| 78 | |
| 79 | if (userCheck != null) { |
| 80 | // 用户名重复 |
| 81 | logger.warn("Registration attempt failed. Account already exists: {}", username); |
| 82 | return ResponseEntity.status(407).body(""); |
| 83 | } |
| 84 | |
| 85 | // 查询邀请码是否存在 |
| 86 | QueryWrapper<Invitation> invitationQuery = new QueryWrapper<>(); |
| 87 | invitationQuery.eq("invitation_code", invitationCode); |
| 88 | Invitation invitation = invitationService.getOne(invitationQuery); |
| 89 | |
| 90 | if (invitation == null) { |
| 91 | // 邀请码不存在 |
| 92 | logger.info("The invitation code does not exist: {}", invitationCode); |
| 93 | return ResponseEntity.status(409).body(""); |
| 94 | } else if (invitation.getInviteeId() != 0) { |
| 95 | // 邀请码已被使用 |
| 96 | logger.info("The invitation code has been used: {}", invitationCode); |
| 97 | return ResponseEntity.status(410).body(""); |
| 98 | } |
| 99 | // 注册 |
| 100 | // 添加新用户 |
| 101 | User user = new User(); |
| 102 | user.setUsername(username); |
| 103 | user.setPassword(password); |
| 104 | userService.save(user); |
| 105 | |
| 106 | // 设置该邀请码已被使用 |
| 107 | User userGetId = userService.getOne(userQuery); |
| 108 | int newUserId = userGetId.getUserId(); |
| 109 | |
xiukira | 29f8d37 | 2025-06-06 18:07:13 +0800 | [diff] [blame] | 110 | UpdateWrapper<Invitation> invitationUpdate = new UpdateWrapper<>(); |
| 111 | invitationUpdate.eq("invitation_code", invitationCode).set("invitee_id", newUserId); |
| 112 | invitationService.update(invitationUpdate); |
xiukira | d0a7a08 | 2025-06-05 16:28:08 +0800 | [diff] [blame] | 113 | |
| 114 | // 生成五个邀请码并分配给新用户 |
| 115 | String[] invitationCodes = invitationService.generateInvitationCode(); |
| 116 | |
| 117 | for (String code : invitationCodes) { |
| 118 | Invitation newInvitation = new Invitation(); |
| 119 | newInvitation.setInvitationCode(code); |
| 120 | newInvitation.setUserId(newUserId); |
| 121 | invitationService.save(newInvitation); |
| 122 | } |
| 123 | |
| 124 | logger.info("User registered successfully: {}", username); |
| 125 | return ResponseEntity.ok(""); |
| 126 | } |
| 127 | |
| 128 | /** |
| 129 | * 用户登录 |
| 130 | * |
| 131 | * @param user 登录信息 |
| 132 | * @return 登录结果 |
| 133 | */ |
| 134 | @PostMapping("/login") |
| 135 | public ResponseEntity<String> login(@RequestBody User user) { |
| 136 | String username = user.getUsername(); |
| 137 | String password = user.getPassword(); |
| 138 | logger.info("Login attempt for account: {}", username); |
| 139 | |
| 140 | // 根据用户名查询该用户名是否已存在 |
| 141 | QueryWrapper<User> userQuery = new QueryWrapper<>(); |
| 142 | userQuery.eq("username", username); |
| 143 | User userCheck = userService.getOne(userQuery); |
| 144 | |
| 145 | if (userCheck == null) { |
| 146 | // 用户名不存在 |
| 147 | logger.warn("Login failed. User not found: {}", username); |
| 148 | return ResponseEntity.status(406).body(""); |
| 149 | } else { |
| 150 | if (userCheck.getPassword().equals(password)) { |
| 151 | return ResponseEntity.ok(""); |
| 152 | } else { |
| 153 | // 密码错误 |
| 154 | logger.warn("Login failed. Incorrect password for account: {}", username); |
| 155 | return ResponseEntity.status(408).body(""); |
| 156 | } |
| 157 | } |
| 158 | } |
| 159 | |
| 160 | /** |
| 161 | * 关注 |
| 162 | * |
| 163 | * @param subscription 关注信息 |
| 164 | * @return 关注结果 |
| 165 | */ |
| 166 | @PostMapping("/subscription") |
| 167 | public ResponseEntity<String> subscription(@RequestBody Subscription subscription) { |
| 168 | subscriptionService.save(subscription); |
| 169 | return ResponseEntity.ok(""); |
| 170 | } |
xiukira | 29f8d37 | 2025-06-06 18:07:13 +0800 | [diff] [blame] | 171 | |
| 172 | /** |
| 173 | * 注销账户 |
| 174 | * |
| 175 | * @param userId 用户id |
| 176 | * @param password 密码 |
| 177 | * @return 注销结果 |
| 178 | */ |
| 179 | @DeleteMapping |
| 180 | @ResponseStatus(HttpStatus.NO_CONTENT) |
| 181 | public ResponseEntity<String> userDelete(@RequestParam int userId, @RequestParam String password) { |
| 182 | logger.warn("Delete user with id: {}", userId); |
| 183 | // 根据用户id查询该用户 |
| 184 | QueryWrapper<User> userQuery = new QueryWrapper<>(); |
| 185 | userQuery.eq("user_id", userId); |
| 186 | User userCheck = userService.getOne(userQuery); |
| 187 | |
| 188 | if (userCheck.getPassword().equals(password)) { |
| 189 | // 注销账户(自动清除与该用户相关的记录,注意会回收分配给该用户的邀请码) |
| 190 | userService.remove(userQuery); |
| 191 | |
| 192 | return ResponseEntity.noContent().build(); |
| 193 | } else { |
| 194 | logger.warn("Delete failed. Incorrect password for account: {}", password); |
| 195 | return ResponseEntity.status(408).body(""); |
| 196 | } |
| 197 | } |
| 198 | |
| 199 | /** |
| 200 | * 删除用户搜索历史 |
| 201 | * |
| 202 | * @param searchHistoryId 搜索历史id |
| 203 | * @return 删除用户搜索历史结果 |
| 204 | */ |
| 205 | @DeleteMapping("/search") |
| 206 | @ResponseStatus(HttpStatus.NO_CONTENT) |
| 207 | public ResponseEntity<String> searchDelete(@RequestParam int searchHistoryId) { |
| 208 | searchHistoryService.removeById(searchHistoryId); |
| 209 | return ResponseEntity.noContent().build(); |
| 210 | } |
| 211 | |
| 212 | /** |
| 213 | * 取消关注 |
| 214 | * |
| 215 | * @param userId 被关注者id |
| 216 | * @param followerId 关注被人者id |
| 217 | * @return 取消关注结果 |
| 218 | */ |
| 219 | @DeleteMapping("/subscription") |
| 220 | @ResponseStatus(HttpStatus.NO_CONTENT) |
| 221 | public ResponseEntity<String> subscriptionDelete(@RequestParam int userId, @RequestParam int followerId) { |
| 222 | QueryWrapper<Subscription> subscriptionQuery = new QueryWrapper<>(); |
| 223 | subscriptionQuery.eq("user_id", userId).eq("follower_id", followerId); |
| 224 | subscriptionService.remove(subscriptionQuery); |
| 225 | return ResponseEntity.noContent().build(); |
| 226 | } |
xiukira | 07ea6ee | 2025-06-06 19:28:24 +0800 | [diff] [blame] | 227 | |
| 228 | |
| 229 | /** |
| 230 | * 获取用户ID |
| 231 | * |
| 232 | * @param username 用户名 |
| 233 | * @param password 密码 |
| 234 | * @return 获取邀请码结果 |
| 235 | */ |
| 236 | @GetMapping("/getId") |
| 237 | public ResponseEntity<Integer> getUserId(@RequestParam String username, @RequestParam String password) { |
| 238 | QueryWrapper<User> userQuery = new QueryWrapper<>(); |
| 239 | userQuery.eq("username", username).eq("password", password); |
| 240 | User user = userService.getOne(userQuery); |
| 241 | return ResponseEntity.ok(user.getUserId()); |
| 242 | } |
| 243 | |
| 244 | /** |
| 245 | * 获取邀请码 |
| 246 | * |
| 247 | * @param userId 用户id |
| 248 | * @return 获取邀请码结果 |
| 249 | */ |
| 250 | @GetMapping("/invitation-code") |
| 251 | public ResponseEntity<GetInvitationCodeDTO> getInvitationCode(@RequestParam int userId) { |
| 252 | QueryWrapper<Invitation> invitationQuery = new QueryWrapper<>(); |
| 253 | invitationQuery.eq("user_id", userId).eq("invitee_id", 0); // 获取未被使用过的分配给该用户的邀请码 |
| 254 | invitationQuery.select("invitation_code"); |
| 255 | List<Invitation> invitationList = invitationService.list(invitationQuery); |
| 256 | GetInvitationCodeDTO getInvitationCodeDTO = new GetInvitationCodeDTO(invitationList); |
| 257 | return ResponseEntity.ok(getInvitationCodeDTO); |
| 258 | } |
| 259 | |
| 260 | /** |
| 261 | * 获取用户信息 |
| 262 | * |
| 263 | * @param userId 用户id |
| 264 | * @return 获取用户信息结果 |
| 265 | */ |
| 266 | @GetMapping("/info") |
| 267 | public ResponseEntity<User> getUserInfo(@RequestParam int userId) { |
| 268 | User user = userService.getById(userId); |
| 269 | return ResponseEntity.ok(user); |
| 270 | } |
| 271 | |
| 272 | /** |
| 273 | * 获取用户悬赏 |
| 274 | * |
| 275 | * @param userId 用户id |
| 276 | * @param pageNumber 页数 |
| 277 | * @param rows 行数 |
| 278 | * @return 获取用户悬赏结果 |
| 279 | */ |
| 280 | @GetMapping("/reward") |
| 281 | public ResponseEntity<GetUserRewardDTO> getUserReward(@RequestParam int userId, @RequestParam int pageNumber, @RequestParam int rows) { |
| 282 | IPage<Reward> page = new Page<>(pageNumber, rows); |
| 283 | QueryWrapper<Reward> rewardQuery = new QueryWrapper<>(); |
| 284 | rewardQuery.eq("user_id", userId); |
| 285 | IPage<Reward> rewardPage = rewardService.page(page, rewardQuery); |
| 286 | List<Reward> rewardList = rewardPage.getRecords(); |
| 287 | long total = rewardPage.getTotal(); |
| 288 | long pages = rewardPage.getPages(); |
| 289 | long current = rewardPage.getCurrent(); |
| 290 | long size = rewardPage.getSize(); |
| 291 | |
| 292 | GetUserRewardDTO getUserRewardDTO = new GetUserRewardDTO(rewardList, total, pages, current, size); |
| 293 | return ResponseEntity.ok(getUserRewardDTO); |
| 294 | } |
| 295 | |
| 296 | /** |
| 297 | * 获取用户收藏资源 |
| 298 | * |
| 299 | * @param userId 用户id |
| 300 | * @param pageNumber 页数 |
| 301 | * @param rows 行数 |
| 302 | * @return 获取用户收藏结果 |
| 303 | */ |
| 304 | @GetMapping("/collection") |
| 305 | public ResponseEntity<GetUserResourceListDTO> getUserCollection(@RequestParam int userId, @RequestParam int pageNumber, @RequestParam int rows) { |
| 306 | IPage<UserCollection> page = new Page<>(pageNumber, rows); |
| 307 | QueryWrapper<UserCollection> userCollectionQuery = new QueryWrapper<>(); |
| 308 | userCollectionQuery.eq("user_id", userId); |
| 309 | IPage<UserCollection> userCollectionPage = userCollectionService.page(page, userCollectionQuery); |
| 310 | List<UserCollection> resourceList = userCollectionPage.getRecords(); |
| 311 | List<Resource> collectionList = new ArrayList<>(); |
| 312 | for (UserCollection userCollection : resourceList) { |
| 313 | Resource resource = resourceService.getById(userCollection.getResourceId()); |
| 314 | collectionList.add(resource); |
| 315 | } |
| 316 | long total = userCollectionPage.getTotal(); |
| 317 | long pages = userCollectionPage.getPages(); |
| 318 | long current = userCollectionPage.getCurrent(); |
| 319 | long size = userCollectionPage.getSize(); |
| 320 | |
| 321 | GetUserResourceListDTO getUserResourceListDTO = new GetUserResourceListDTO(collectionList, total, pages, current, size); |
| 322 | return ResponseEntity.ok(getUserResourceListDTO); |
| 323 | } |
| 324 | |
| 325 | /** |
| 326 | * 获取用户上传资源 |
| 327 | * |
| 328 | * @param userId 用户id |
| 329 | * @param pageNumber 页数 |
| 330 | * @param rows 行数 |
| 331 | * @return 获取用户上传资源结果 |
| 332 | */ |
| 333 | @GetMapping("/upload") |
| 334 | public ResponseEntity<GetUserResourceListDTO> getUserUpload(@RequestParam int userId, @RequestParam int pageNumber, @RequestParam int rows) { |
| 335 | IPage<UserUpload> page = new Page<>(pageNumber, rows); |
| 336 | QueryWrapper<UserUpload> userUploadQuery = new QueryWrapper<>(); |
| 337 | userUploadQuery.eq("user_id", userId); |
| 338 | IPage<UserUpload> userUploadPage = userUploadService.page(page, userUploadQuery); |
| 339 | List<UserUpload> resourceList = userUploadPage.getRecords(); |
| 340 | List<Resource> uploadList = new ArrayList<>(); |
| 341 | for (UserUpload userUpload : resourceList) { |
| 342 | Resource resource = resourceService.getById(userUpload.getResourceId()); |
| 343 | uploadList.add(resource); |
| 344 | } |
| 345 | long total = userUploadPage.getTotal(); |
| 346 | long pages = userUploadPage.getPages(); |
| 347 | long current = userUploadPage.getCurrent(); |
| 348 | long size = userUploadPage.getSize(); |
| 349 | |
| 350 | GetUserResourceListDTO getUserResourceListDTO = new GetUserResourceListDTO(uploadList, total, pages, current, size); |
| 351 | return ResponseEntity.ok(getUserResourceListDTO); |
| 352 | } |
| 353 | |
| 354 | /** |
| 355 | * 获取用户购买资源 |
| 356 | * |
| 357 | * @param userId 用户id |
| 358 | * @param pageNumber 页数 |
| 359 | * @param rows 行数 |
| 360 | * @return 获取用户购买资源结果 |
| 361 | */ |
| 362 | @GetMapping("/purchase") |
| 363 | public ResponseEntity<GetUserResourceListDTO> getUserPurchase(@RequestParam int userId, @RequestParam int pageNumber, @RequestParam int rows) { |
| 364 | IPage<UserPurchase> page = new Page<>(pageNumber, rows); |
| 365 | QueryWrapper<UserPurchase> userPurchaseQuery = new QueryWrapper<>(); |
| 366 | userPurchaseQuery.eq("user_id", userId); |
| 367 | IPage<UserPurchase> userPurchasePage = userPurchaseService.page(page, userPurchaseQuery); |
| 368 | List<UserPurchase> resourceList = userPurchasePage.getRecords(); |
| 369 | List<Resource> purchaseList = new ArrayList<>(); |
| 370 | for (UserPurchase userPurchase : resourceList) { |
| 371 | Resource resource = resourceService.getById(userPurchase.getResourceId()); |
| 372 | purchaseList.add(resource); |
| 373 | } |
| 374 | long total = userPurchasePage.getTotal(); |
| 375 | long pages = userPurchasePage.getPages(); |
| 376 | long current = userPurchasePage.getCurrent(); |
| 377 | long size = userPurchasePage.getSize(); |
| 378 | |
| 379 | GetUserResourceListDTO getUserResourceListDTO = new GetUserResourceListDTO(purchaseList, total, pages, current, size); |
| 380 | return ResponseEntity.ok(getUserResourceListDTO); |
| 381 | } |
| 382 | |
| 383 | /** |
| 384 | * 获取用户搜索历史 |
| 385 | * |
| 386 | * @param userId 用户id |
| 387 | * @return 获取用户搜索历史结果 |
| 388 | */ |
| 389 | @GetMapping("/search") |
| 390 | public ResponseEntity<GetUserSearchHistoryDTO> getUserSearchHistory(@RequestParam int userId) { |
| 391 | QueryWrapper<SearchHistory> SearchHistoryQuery = new QueryWrapper<>(); |
| 392 | SearchHistoryQuery.eq("user_id", userId).orderByDesc("search_history_id").last("LIMIT 10"); |
| 393 | List<SearchHistory> searchHistoryList = searchHistoryService.list(SearchHistoryQuery); |
| 394 | |
| 395 | GetUserSearchHistoryDTO getUserSearchHistoryDTO = new GetUserSearchHistoryDTO(searchHistoryList); |
| 396 | return ResponseEntity.ok(getUserSearchHistoryDTO); |
| 397 | } |
| 398 | |
| 399 | /** |
| 400 | * 获取用户数据 |
| 401 | * |
| 402 | * @param userId 用户id |
| 403 | * @return 获取用户数据结果 |
| 404 | */ |
| 405 | @GetMapping("/data") |
| 406 | public ResponseEntity<GetUserDataDTO> getUserData(@RequestParam int userId) { |
| 407 | User user = userService.getById(userId); |
| 408 | int subscriberCount = user.getSubscriberCount(); |
| 409 | int uploadAmount = user.getUploadAmount(); |
| 410 | |
| 411 | QueryWrapper<UserUpload> UserUploadQuery = new QueryWrapper<>(); |
| 412 | UserUploadQuery.eq("user_id", userId); |
| 413 | List<UserUpload> UserUpload = userUploadService.list(UserUploadQuery); |
| 414 | int beDownloadedAmount = 0; // 上传资源被下载量 |
| 415 | float[] seedPercentageList = new float[4]; // 上传资源类型百分比,0-1区间的小数 |
| 416 | int resourcePackAmount = 0; |
| 417 | int modAmount = 0; |
| 418 | int modPackAmount = 0; |
| 419 | int mapAmount = 0; |
| 420 | for (UserUpload userUpload : UserUpload) { |
| 421 | Resource resource = resourceService.getById(userUpload.getResourceId()); |
| 422 | beDownloadedAmount += resource.getDownloads(); |
| 423 | switch (resource.getClassify()) { |
| 424 | case "resourcePack": |
| 425 | resourcePackAmount++; |
| 426 | break; |
| 427 | case "mod": |
| 428 | modAmount++; |
| 429 | break; |
| 430 | case "modPack": |
| 431 | modPackAmount++; |
| 432 | break; |
| 433 | case "map": |
| 434 | mapAmount++; |
| 435 | break; |
| 436 | } |
| 437 | } |
| 438 | int totalAmount = resourcePackAmount + modAmount + modPackAmount + mapAmount; |
| 439 | seedPercentageList[0] = (float) resourcePackAmount / totalAmount; |
| 440 | seedPercentageList[1] = (float) modAmount / totalAmount; |
| 441 | seedPercentageList[2] = (float) modPackAmount / totalAmount; |
| 442 | seedPercentageList[3] = (float) mapAmount / totalAmount; |
| 443 | |
| 444 | GetUserDataDTO getUserDataDTO = new GetUserDataDTO(subscriberCount, uploadAmount, beDownloadedAmount, seedPercentageList); |
| 445 | return ResponseEntity.ok(getUserDataDTO); |
| 446 | } |
| 447 | |
| 448 | /** |
| 449 | * 获取关注列表 |
| 450 | * |
| 451 | * @param userId 用户id |
| 452 | * @return 获取关注列表结果 |
| 453 | */ |
| 454 | @GetMapping("/subscriber") |
| 455 | public ResponseEntity<GetUserDTO> getUserSubscriber(@RequestParam int userId) { |
| 456 | QueryWrapper<Subscription> subscriptionQuery = new QueryWrapper<>(); |
| 457 | subscriptionQuery.eq("follower_id", userId); |
| 458 | List<Subscription> subscriptionList = subscriptionService.list(subscriptionQuery); |
| 459 | |
| 460 | List<User> subscriberList = new ArrayList<>(); |
| 461 | for (Subscription subscription : subscriptionList) { |
| 462 | User user = userService.getById(subscription.getUserId()); |
| 463 | subscriberList.add(user); |
| 464 | } |
| 465 | |
| 466 | GetUserDTO getUserDTO = new GetUserDTO(subscriberList); |
| 467 | return ResponseEntity.ok(getUserDTO); |
| 468 | } |
| 469 | |
| 470 | /** |
| 471 | * 获取粉丝列表 |
| 472 | * |
| 473 | * @param userId 用户id |
| 474 | * @return 获取粉丝列表结果 |
| 475 | */ |
| 476 | @GetMapping("/follower") |
| 477 | public ResponseEntity<GetUserDTO> getUserFollower(@RequestParam int userId) { |
| 478 | QueryWrapper<Subscription> subscriptionQuery = new QueryWrapper<>(); |
| 479 | subscriptionQuery.eq("user_id", userId); |
| 480 | List<Subscription> subscriptionList = subscriptionService.list(subscriptionQuery); |
| 481 | |
| 482 | List<User> subscriberList = new ArrayList<>(); |
| 483 | for (Subscription subscription : subscriptionList) { |
| 484 | User user = userService.getById(subscription.getUserId()); |
| 485 | subscriberList.add(user); |
| 486 | } |
| 487 | |
| 488 | GetUserDTO getUserDTO = new GetUserDTO(subscriberList); |
| 489 | return ResponseEntity.ok(getUserDTO); |
| 490 | } |
xiukira | a3c8418 | 2025-06-06 20:54:38 +0800 | [diff] [blame] | 491 | |
| 492 | /** |
| 493 | * 修改用户签名 |
| 494 | * |
| 495 | * @param user 用户信息 |
| 496 | * @return 用户签名修改结果 |
| 497 | */ |
| 498 | @PutMapping("/signature") |
| 499 | public ResponseEntity<String> modifySignature(@RequestBody User user) { |
| 500 | UpdateWrapper<User> userUpdate = new UpdateWrapper<>(); |
| 501 | userUpdate.eq("user_id", user.getUserId()).set("signature", user.getSignature()); |
| 502 | userService.update(userUpdate); |
| 503 | |
| 504 | return ResponseEntity.ok(""); |
| 505 | } |
| 506 | |
| 507 | /** |
| 508 | * 修改用户密码 |
| 509 | * |
| 510 | * @param modifyPasswordDTO 用户信息 |
| 511 | * @return 用户密码修改结果 |
| 512 | */ |
| 513 | @PutMapping("/password") |
| 514 | public ResponseEntity<String> modifyPassword(@RequestBody ModifyPasswordDTO modifyPasswordDTO) { |
| 515 | // 检查密码是否正确 |
| 516 | QueryWrapper<User> userQuery = new QueryWrapper<>(); |
| 517 | userQuery.eq("user_id", modifyPasswordDTO.getUserId()); |
| 518 | User user = userService.getOne(userQuery); |
| 519 | if (user.getPassword().equals(modifyPasswordDTO.getPassword())) { |
| 520 | UpdateWrapper<User> userUpdate = new UpdateWrapper<>(); |
| 521 | userUpdate.eq("user_id", modifyPasswordDTO.getUserId()).set("password", modifyPasswordDTO.getNewPassword()); |
| 522 | userService.update(userUpdate); |
| 523 | return ResponseEntity.ok(""); |
| 524 | } else { |
| 525 | // 密码错误 |
| 526 | logger.warn("Modify failed. Incorrect password for userID: {}", modifyPasswordDTO.getUserId()); |
| 527 | return ResponseEntity.status(408).body(""); |
| 528 | } |
| 529 | } |
| 530 | |
| 531 | /** |
| 532 | * 修改用户头像 |
| 533 | * |
| 534 | * @param user 用户信息 |
| 535 | * @return 用户头像修改结果 |
| 536 | */ |
| 537 | @PutMapping("/avatar") |
| 538 | public ResponseEntity<String> modifyAvatar(@RequestBody User user) { |
| 539 | UpdateWrapper<User> userUpdate = new UpdateWrapper<>(); |
| 540 | userUpdate.eq("user_id", user.getUserId()).set("avatar", user.getAvatar()); |
| 541 | userService.update(userUpdate); |
| 542 | |
| 543 | return ResponseEntity.ok(""); |
| 544 | } |
xiukira | d0a7a08 | 2025-06-05 16:28:08 +0800 | [diff] [blame] | 545 | } |