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