xiukira | 687b9cb | 2025-05-29 15:15:02 +0800 | [diff] [blame] | 1 | package com.g9.g9backend.controller; |
| 2 | |
xiukira | dbe9622 | 2025-06-06 21:25:09 +0800 | [diff] [blame] | 3 | import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; |
| 4 | import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper; |
| 5 | import com.g9.g9backend.mapper.UserPurchaseMapper; |
| 6 | import com.g9.g9backend.pojo.*; |
| 7 | import com.g9.g9backend.pojo.DTO.*; |
| 8 | import com.g9.g9backend.service.*; |
xiukira | 687b9cb | 2025-05-29 15:15:02 +0800 | [diff] [blame] | 9 | import org.slf4j.Logger; |
| 10 | import org.slf4j.LoggerFactory; |
xiukira | b949027 | 2025-06-06 21:53:52 +0800 | [diff] [blame] | 11 | import org.springframework.http.HttpStatus; |
xiukira | dbe9622 | 2025-06-06 21:25:09 +0800 | [diff] [blame] | 12 | import org.springframework.http.ResponseEntity; |
xiukira | d0a7a08 | 2025-06-05 16:28:08 +0800 | [diff] [blame] | 13 | import org.springframework.web.bind.annotation.*; |
| 14 | |
xiukira | dbe9622 | 2025-06-06 21:25:09 +0800 | [diff] [blame] | 15 | import java.util.Date; |
| 16 | |
xiukira | 687b9cb | 2025-05-29 15:15:02 +0800 | [diff] [blame] | 17 | |
| 18 | /** |
| 19 | * ResourceController 资源控制器类,处理与资源相关的请求 |
| 20 | * |
| 21 | * @author hcy |
| 22 | */ |
| 23 | @RestController |
| 24 | @RequestMapping("/resource") |
| 25 | public class ResourceController { |
| 26 | |
xiukira | dbe9622 | 2025-06-06 21:25:09 +0800 | [diff] [blame] | 27 | private final ResourceService resourceService; |
| 28 | |
| 29 | private final GameplayService gameplayService; |
| 30 | |
| 31 | private final RewardService rewardService; |
| 32 | |
| 33 | private final UserUploadService userUploadService; |
| 34 | |
| 35 | private final CommunityService communityService; |
| 36 | |
| 37 | private final UserService userService; |
| 38 | |
| 39 | private final UserPurchaseService userPurchaseService; |
| 40 | |
| 41 | private final UserLikeService userLikeService; |
| 42 | |
| 43 | private final UserCollectionService userCollectionService; |
| 44 | |
| 45 | private final NotificationService notificationService; |
| 46 | |
| 47 | public ResourceController(ResourceService resourceService, GameplayService gameplayService, RewardService rewardService, UserUploadService userUploadService, CommunityService communityService, UserService userService, UserPurchaseMapper userPurchaseMapper, UserPurchaseService userPurchaseService, UserLikeService userLikeService, UserCollectionService userCollectionService, NotificationService notificationService) { |
| 48 | this.resourceService = resourceService; |
| 49 | this.gameplayService = gameplayService; |
| 50 | this.rewardService = rewardService; |
| 51 | this.userUploadService = userUploadService; |
| 52 | this.communityService = communityService; |
| 53 | this.userService = userService; |
| 54 | this.userPurchaseService = userPurchaseService; |
| 55 | this.userLikeService = userLikeService; |
| 56 | this.userCollectionService = userCollectionService; |
| 57 | this.notificationService = notificationService; |
| 58 | } |
| 59 | |
xiukira | 687b9cb | 2025-05-29 15:15:02 +0800 | [diff] [blame] | 60 | private final Logger logger = LoggerFactory.getLogger(ResourceController.class); |
xiukira | dbe9622 | 2025-06-06 21:25:09 +0800 | [diff] [blame] | 61 | |
| 62 | /** |
| 63 | * 上传资源 |
| 64 | * |
| 65 | * @param postResourceDTO 上传资源信息 |
| 66 | * @return 上传资源结果 |
| 67 | */ |
| 68 | @PostMapping |
| 69 | public ResponseEntity<String> uploadResource(@RequestBody PostResourceDTO postResourceDTO) { |
| 70 | // 存资源 |
| 71 | Resource resource = postResourceDTO.getResource(); |
| 72 | resourceService.save(resource); |
| 73 | // 存玩法列表 |
| 74 | String[] gameplayList = postResourceDTO.getGameplayList(); |
| 75 | for (String gameplayName : gameplayList) { |
| 76 | Gameplay gameplay = new Gameplay(); |
| 77 | gameplay.setGameplayName(gameplayName); |
| 78 | gameplay.setResourceId(postResourceDTO.getResource().getResourceId()); |
| 79 | gameplayService.save(gameplay); |
| 80 | } |
| 81 | // 完成对应悬赏 |
| 82 | if (postResourceDTO.getCompleteRewardId() != 0) { |
| 83 | UpdateWrapper<Reward> rewardUpdate = new UpdateWrapper<>(); |
| 84 | rewardUpdate.eq("reward_id", postResourceDTO.getCompleteRewardId()).set("completed_by", postResourceDTO.getUserId()).set("completed_at", postResourceDTO.getResource().getUploadTime()).set("resource_id", postResourceDTO.getResource().getResourceId()); |
| 85 | rewardService.update(rewardUpdate); |
| 86 | } |
| 87 | // 存用户上传表 |
| 88 | UserUpload userUpload = new UserUpload(); |
| 89 | userUpload.setUserId(postResourceDTO.getUserId()); |
| 90 | userUpload.setResourceId(postResourceDTO.getResource().getResourceId()); |
| 91 | userUploadService.save(userUpload); |
| 92 | // 创建资源社区 |
| 93 | Community community = new Community(); |
| 94 | community.setCommunityName(postResourceDTO.getResource().getResourceName()); |
| 95 | community.setType(postResourceDTO.getResource().getClassify()); |
| 96 | community.setResourceId(postResourceDTO.getResource().getResourceId()); |
| 97 | communityService.save(community); |
| 98 | return ResponseEntity.ok(""); |
| 99 | } |
| 100 | |
| 101 | /** |
| 102 | * 购买资源 |
| 103 | * |
| 104 | * @param userResourceDTO 购买资源信息 |
| 105 | * @return 购买资源结果 |
| 106 | */ |
| 107 | @PostMapping("purchase") |
| 108 | public ResponseEntity<String> purchaseResource(@RequestBody UserResourceDTO userResourceDTO) { |
| 109 | |
| 110 | QueryWrapper<User> userQuery = new QueryWrapper<>(); |
| 111 | userQuery.eq("user_id", userResourceDTO.getUserId()); |
| 112 | User user = userService.getOne(userQuery); |
| 113 | |
| 114 | QueryWrapper<Resource> ResourceQuery = new QueryWrapper<>(); |
| 115 | ResourceQuery.eq("resource_id", userResourceDTO.getResourceId()); |
| 116 | Resource resource = resourceService.getOne(ResourceQuery); |
| 117 | |
| 118 | if (user.getCredits() < resource.getPrice()) { |
| 119 | // 积分余额不足 |
| 120 | logger.info("The balance of points is insufficient to cover the price of this resource: {}", resource.getPrice()); |
| 121 | return ResponseEntity.status(412).body(""); |
| 122 | } else { |
| 123 | // 扣除用户积分 |
| 124 | UpdateWrapper<User> userUpdate = new UpdateWrapper<>(); |
| 125 | userUpdate.eq("user_id", user.getUserId()).set("credits", user.getCredits() - resource.getPrice()); |
| 126 | userService.update(userUpdate); |
| 127 | // 添加购买资源记录 |
| 128 | UserPurchase userPurchase = new UserPurchase(); |
| 129 | userPurchase.setUserId(user.getUserId()); |
| 130 | userPurchase.setResourceId(resource.getResourceId()); |
| 131 | userPurchaseService.save(userPurchase); |
| 132 | // 给上传该资源的用户发送通知 |
| 133 | Notification notification = new Notification(); |
| 134 | QueryWrapper<UserUpload> userUploadQuery = new QueryWrapper<>(); |
| 135 | userUploadQuery.eq("resource_id", userResourceDTO.getResourceId()); |
| 136 | UserUpload userUpload = userUploadService.getOne(userUploadQuery); |
| 137 | notification.setUserId(userUpload.getUserId()); |
| 138 | notification.setTitle("资源被购买"); |
| 139 | notification.setContent("你的资源:" + resource.getResourceName() + " 被: " + user.getUsername() + " 购买了!"); |
| 140 | notification.setCreateAt(new Date()); |
| 141 | notification.setRead(false); |
| 142 | notification.setTriggeredBy(userResourceDTO.getUserId()); |
| 143 | notification.setRelatedId(userResourceDTO.getResourceId()); |
| 144 | notificationService.save(notification); |
| 145 | return ResponseEntity.ok(""); |
| 146 | } |
| 147 | } |
| 148 | |
| 149 | /** |
| 150 | * 点赞资源 |
| 151 | * |
| 152 | * @param userResourceDTO 点赞资源信息 |
| 153 | * @return 点赞资源结果 |
| 154 | */ |
| 155 | @PostMapping("like") |
| 156 | public ResponseEntity<String> likeResource(@RequestBody UserResourceDTO userResourceDTO) { |
| 157 | |
| 158 | QueryWrapper<User> userQuery = new QueryWrapper<>(); |
| 159 | userQuery.eq("user_id", userResourceDTO.getUserId()); |
| 160 | User user = userService.getOne(userQuery); |
| 161 | |
| 162 | QueryWrapper<Resource> ResourceQuery = new QueryWrapper<>(); |
| 163 | ResourceQuery.eq("resource_id", userResourceDTO.getResourceId()); |
| 164 | Resource resource = resourceService.getOne(ResourceQuery); |
| 165 | |
| 166 | UserLike userLike = new UserLike(); |
| 167 | userLike.setUserId(userResourceDTO.getUserId()); |
| 168 | userLike.setResourceId(userResourceDTO.getResourceId()); |
| 169 | userLikeService.save(userLike); |
| 170 | |
| 171 | // 给上传该资源的用户发送通知 |
| 172 | Notification notification = new Notification(); |
| 173 | QueryWrapper<UserUpload> userUploadQuery = new QueryWrapper<>(); |
| 174 | userUploadQuery.eq("resource_id", userResourceDTO.getResourceId()); |
| 175 | UserUpload userUpload = userUploadService.getOne(userUploadQuery); |
| 176 | notification.setUserId(userUpload.getUserId()); |
| 177 | notification.setTitle("资源被点赞"); |
| 178 | notification.setContent("你的资源:" + resource.getResourceName() + " 被: " + user.getUsername() + " 点赞了!"); |
| 179 | notification.setCreateAt(new Date()); |
| 180 | notification.setRead(false); |
| 181 | notification.setTriggeredBy(userResourceDTO.getUserId()); |
| 182 | notification.setRelatedId(userResourceDTO.getResourceId()); |
| 183 | notificationService.save(notification); |
| 184 | |
| 185 | return ResponseEntity.ok(""); |
| 186 | } |
| 187 | |
| 188 | /** |
| 189 | * 收藏资源 |
| 190 | * |
| 191 | * @param userResourceDTO 收藏资源信息 |
| 192 | * @return 收藏资源结果 |
| 193 | */ |
| 194 | @PostMapping("collection") |
| 195 | public ResponseEntity<String> collectResource(@RequestBody UserResourceDTO userResourceDTO) { |
| 196 | |
| 197 | QueryWrapper<User> userQuery = new QueryWrapper<>(); |
| 198 | userQuery.eq("user_id", userResourceDTO.getUserId()); |
| 199 | User user = userService.getOne(userQuery); |
| 200 | |
| 201 | QueryWrapper<Resource> ResourceQuery = new QueryWrapper<>(); |
| 202 | ResourceQuery.eq("resource_id", userResourceDTO.getResourceId()); |
| 203 | Resource resource = resourceService.getOne(ResourceQuery); |
| 204 | |
| 205 | UserCollection userCollection = new UserCollection(); |
| 206 | userCollection.setUserId(userResourceDTO.getUserId()); |
| 207 | userCollection.setResourceId(userResourceDTO.getResourceId()); |
| 208 | userCollectionService.save(userCollection); |
| 209 | |
| 210 | |
| 211 | // 给上传该资源的用户发送通知 |
| 212 | Notification notification = new Notification(); |
| 213 | QueryWrapper<UserUpload> userUploadQuery = new QueryWrapper<>(); |
| 214 | userUploadQuery.eq("resource_id", userResourceDTO.getResourceId()); |
| 215 | UserUpload userUpload = userUploadService.getOne(userUploadQuery); |
| 216 | notification.setUserId(userUpload.getUserId()); |
| 217 | notification.setTitle("资源被收藏"); |
| 218 | notification.setContent("你的资源:" + resource.getResourceName() + " 被: " + user.getUsername() + " 收藏了!"); |
| 219 | notification.setCreateAt(new Date()); |
| 220 | notification.setRead(false); |
| 221 | notification.setTriggeredBy(userResourceDTO.getUserId()); |
| 222 | notification.setRelatedId(userResourceDTO.getResourceId()); |
| 223 | notificationService.save(notification); |
| 224 | |
| 225 | return ResponseEntity.ok(""); |
| 226 | } |
xiukira | b949027 | 2025-06-06 21:53:52 +0800 | [diff] [blame] | 227 | |
| 228 | /** |
| 229 | * 删除资源 |
| 230 | * |
| 231 | * @param resourceId 资源id |
| 232 | * @param userId 用户id |
| 233 | * @param password 密码 |
| 234 | * @return 删除资源结果 |
| 235 | */ |
| 236 | @DeleteMapping |
| 237 | @ResponseStatus(HttpStatus.NO_CONTENT) |
| 238 | public ResponseEntity<String> userDelete(@RequestParam int resourceId, @RequestParam int userId, @RequestParam String password) { |
| 239 | logger.warn("Delete resource with id: {}", resourceId); |
| 240 | // 根据用户id查询该用户 |
| 241 | QueryWrapper<User> userQuery = new QueryWrapper<>(); |
| 242 | userQuery.eq("user_id", userId); |
| 243 | User userCheck = userService.getOne(userQuery); |
| 244 | |
| 245 | // 只允许在用户上传资源页进行删除操作,所以不需要验证该资源是由该用户上传的 |
| 246 | if (userCheck.getPassword().equals(password)) { |
| 247 | // 删除资源 |
| 248 | resourceService.removeById(resourceId); |
| 249 | return ResponseEntity.noContent().build(); |
| 250 | } else { |
| 251 | logger.warn("Delete failed. Incorrect password for account: {}", password); |
| 252 | return ResponseEntity.status(408).body(""); |
| 253 | } |
| 254 | } |
| 255 | |
| 256 | /** |
| 257 | * 取消点赞 |
| 258 | * |
| 259 | * @param userId 用户id |
| 260 | * @param resourceId 资源id |
| 261 | * @return 取消点赞结果 |
| 262 | */ |
| 263 | @DeleteMapping("like") |
| 264 | @ResponseStatus(HttpStatus.NO_CONTENT) |
| 265 | public ResponseEntity<String> likeDelete(@RequestParam int userId, @RequestParam int resourceId) { |
| 266 | QueryWrapper<UserLike> userLikeQuery = new QueryWrapper<>(); |
| 267 | userLikeQuery.eq("user_id", userId).eq("resource_id", resourceId); |
| 268 | userLikeService.remove(userLikeQuery); |
| 269 | return ResponseEntity.noContent().build(); |
| 270 | } |
| 271 | |
| 272 | /** |
| 273 | * 取消收藏 |
| 274 | * |
| 275 | * @param userId 用户id |
| 276 | * @param resourceId 资源id |
| 277 | * @return 取消收藏结果 |
| 278 | */ |
| 279 | @DeleteMapping("collection") |
| 280 | @ResponseStatus(HttpStatus.NO_CONTENT) |
| 281 | public ResponseEntity<String> collectionDelete(@RequestParam int userId, @RequestParam int resourceId) { |
| 282 | QueryWrapper<UserCollection> userCollectionQuery = new QueryWrapper<>(); |
| 283 | userCollectionQuery.eq("user_id", userId).eq("resource_id", resourceId); |
| 284 | userCollectionService.remove(userCollectionQuery); |
| 285 | return ResponseEntity.noContent().build(); |
| 286 | } |
xiukira | dbe9622 | 2025-06-06 21:25:09 +0800 | [diff] [blame] | 287 | } |