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 | dbe9622 | 2025-06-06 21:25:09 +0800 | [diff] [blame^] | 11 | import org.springframework.http.ResponseEntity; |
xiukira | d0a7a08 | 2025-06-05 16:28:08 +0800 | [diff] [blame] | 12 | import org.springframework.web.bind.annotation.*; |
| 13 | |
xiukira | dbe9622 | 2025-06-06 21:25:09 +0800 | [diff] [blame^] | 14 | import java.util.Date; |
| 15 | |
xiukira | 687b9cb | 2025-05-29 15:15:02 +0800 | [diff] [blame] | 16 | |
| 17 | /** |
| 18 | * ResourceController 资源控制器类,处理与资源相关的请求 |
| 19 | * |
| 20 | * @author hcy |
| 21 | */ |
| 22 | @RestController |
| 23 | @RequestMapping("/resource") |
| 24 | public class ResourceController { |
| 25 | |
xiukira | dbe9622 | 2025-06-06 21:25:09 +0800 | [diff] [blame^] | 26 | private final ResourceService resourceService; |
| 27 | |
| 28 | private final GameplayService gameplayService; |
| 29 | |
| 30 | private final RewardService rewardService; |
| 31 | |
| 32 | private final UserUploadService userUploadService; |
| 33 | |
| 34 | private final CommunityService communityService; |
| 35 | |
| 36 | private final UserService userService; |
| 37 | |
| 38 | private final UserPurchaseService userPurchaseService; |
| 39 | |
| 40 | private final UserLikeService userLikeService; |
| 41 | |
| 42 | private final UserCollectionService userCollectionService; |
| 43 | |
| 44 | private final NotificationService notificationService; |
| 45 | |
| 46 | public ResourceController(ResourceService resourceService, GameplayService gameplayService, RewardService rewardService, UserUploadService userUploadService, CommunityService communityService, UserService userService, UserPurchaseMapper userPurchaseMapper, UserPurchaseService userPurchaseService, UserLikeService userLikeService, UserCollectionService userCollectionService, NotificationService notificationService) { |
| 47 | this.resourceService = resourceService; |
| 48 | this.gameplayService = gameplayService; |
| 49 | this.rewardService = rewardService; |
| 50 | this.userUploadService = userUploadService; |
| 51 | this.communityService = communityService; |
| 52 | this.userService = userService; |
| 53 | this.userPurchaseService = userPurchaseService; |
| 54 | this.userLikeService = userLikeService; |
| 55 | this.userCollectionService = userCollectionService; |
| 56 | this.notificationService = notificationService; |
| 57 | } |
| 58 | |
xiukira | 687b9cb | 2025-05-29 15:15:02 +0800 | [diff] [blame] | 59 | private final Logger logger = LoggerFactory.getLogger(ResourceController.class); |
xiukira | dbe9622 | 2025-06-06 21:25:09 +0800 | [diff] [blame^] | 60 | |
| 61 | /** |
| 62 | * 上传资源 |
| 63 | * |
| 64 | * @param postResourceDTO 上传资源信息 |
| 65 | * @return 上传资源结果 |
| 66 | */ |
| 67 | @PostMapping |
| 68 | public ResponseEntity<String> uploadResource(@RequestBody PostResourceDTO postResourceDTO) { |
| 69 | // 存资源 |
| 70 | Resource resource = postResourceDTO.getResource(); |
| 71 | resourceService.save(resource); |
| 72 | // 存玩法列表 |
| 73 | String[] gameplayList = postResourceDTO.getGameplayList(); |
| 74 | for (String gameplayName : gameplayList) { |
| 75 | Gameplay gameplay = new Gameplay(); |
| 76 | gameplay.setGameplayName(gameplayName); |
| 77 | gameplay.setResourceId(postResourceDTO.getResource().getResourceId()); |
| 78 | gameplayService.save(gameplay); |
| 79 | } |
| 80 | // 完成对应悬赏 |
| 81 | if (postResourceDTO.getCompleteRewardId() != 0) { |
| 82 | UpdateWrapper<Reward> rewardUpdate = new UpdateWrapper<>(); |
| 83 | rewardUpdate.eq("reward_id", postResourceDTO.getCompleteRewardId()).set("completed_by", postResourceDTO.getUserId()).set("completed_at", postResourceDTO.getResource().getUploadTime()).set("resource_id", postResourceDTO.getResource().getResourceId()); |
| 84 | rewardService.update(rewardUpdate); |
| 85 | } |
| 86 | // 存用户上传表 |
| 87 | UserUpload userUpload = new UserUpload(); |
| 88 | userUpload.setUserId(postResourceDTO.getUserId()); |
| 89 | userUpload.setResourceId(postResourceDTO.getResource().getResourceId()); |
| 90 | userUploadService.save(userUpload); |
| 91 | // 创建资源社区 |
| 92 | Community community = new Community(); |
| 93 | community.setCommunityName(postResourceDTO.getResource().getResourceName()); |
| 94 | community.setType(postResourceDTO.getResource().getClassify()); |
| 95 | community.setResourceId(postResourceDTO.getResource().getResourceId()); |
| 96 | communityService.save(community); |
| 97 | return ResponseEntity.ok(""); |
| 98 | } |
| 99 | |
| 100 | /** |
| 101 | * 购买资源 |
| 102 | * |
| 103 | * @param userResourceDTO 购买资源信息 |
| 104 | * @return 购买资源结果 |
| 105 | */ |
| 106 | @PostMapping("purchase") |
| 107 | public ResponseEntity<String> purchaseResource(@RequestBody UserResourceDTO userResourceDTO) { |
| 108 | |
| 109 | QueryWrapper<User> userQuery = new QueryWrapper<>(); |
| 110 | userQuery.eq("user_id", userResourceDTO.getUserId()); |
| 111 | User user = userService.getOne(userQuery); |
| 112 | |
| 113 | QueryWrapper<Resource> ResourceQuery = new QueryWrapper<>(); |
| 114 | ResourceQuery.eq("resource_id", userResourceDTO.getResourceId()); |
| 115 | Resource resource = resourceService.getOne(ResourceQuery); |
| 116 | |
| 117 | if (user.getCredits() < resource.getPrice()) { |
| 118 | // 积分余额不足 |
| 119 | logger.info("The balance of points is insufficient to cover the price of this resource: {}", resource.getPrice()); |
| 120 | return ResponseEntity.status(412).body(""); |
| 121 | } else { |
| 122 | // 扣除用户积分 |
| 123 | UpdateWrapper<User> userUpdate = new UpdateWrapper<>(); |
| 124 | userUpdate.eq("user_id", user.getUserId()).set("credits", user.getCredits() - resource.getPrice()); |
| 125 | userService.update(userUpdate); |
| 126 | // 添加购买资源记录 |
| 127 | UserPurchase userPurchase = new UserPurchase(); |
| 128 | userPurchase.setUserId(user.getUserId()); |
| 129 | userPurchase.setResourceId(resource.getResourceId()); |
| 130 | userPurchaseService.save(userPurchase); |
| 131 | // 给上传该资源的用户发送通知 |
| 132 | Notification notification = new Notification(); |
| 133 | QueryWrapper<UserUpload> userUploadQuery = new QueryWrapper<>(); |
| 134 | userUploadQuery.eq("resource_id", userResourceDTO.getResourceId()); |
| 135 | UserUpload userUpload = userUploadService.getOne(userUploadQuery); |
| 136 | notification.setUserId(userUpload.getUserId()); |
| 137 | notification.setTitle("资源被购买"); |
| 138 | notification.setContent("你的资源:" + resource.getResourceName() + " 被: " + user.getUsername() + " 购买了!"); |
| 139 | notification.setCreateAt(new Date()); |
| 140 | notification.setRead(false); |
| 141 | notification.setTriggeredBy(userResourceDTO.getUserId()); |
| 142 | notification.setRelatedId(userResourceDTO.getResourceId()); |
| 143 | notificationService.save(notification); |
| 144 | return ResponseEntity.ok(""); |
| 145 | } |
| 146 | } |
| 147 | |
| 148 | /** |
| 149 | * 点赞资源 |
| 150 | * |
| 151 | * @param userResourceDTO 点赞资源信息 |
| 152 | * @return 点赞资源结果 |
| 153 | */ |
| 154 | @PostMapping("like") |
| 155 | public ResponseEntity<String> likeResource(@RequestBody UserResourceDTO userResourceDTO) { |
| 156 | |
| 157 | QueryWrapper<User> userQuery = new QueryWrapper<>(); |
| 158 | userQuery.eq("user_id", userResourceDTO.getUserId()); |
| 159 | User user = userService.getOne(userQuery); |
| 160 | |
| 161 | QueryWrapper<Resource> ResourceQuery = new QueryWrapper<>(); |
| 162 | ResourceQuery.eq("resource_id", userResourceDTO.getResourceId()); |
| 163 | Resource resource = resourceService.getOne(ResourceQuery); |
| 164 | |
| 165 | UserLike userLike = new UserLike(); |
| 166 | userLike.setUserId(userResourceDTO.getUserId()); |
| 167 | userLike.setResourceId(userResourceDTO.getResourceId()); |
| 168 | userLikeService.save(userLike); |
| 169 | |
| 170 | // 给上传该资源的用户发送通知 |
| 171 | Notification notification = new Notification(); |
| 172 | QueryWrapper<UserUpload> userUploadQuery = new QueryWrapper<>(); |
| 173 | userUploadQuery.eq("resource_id", userResourceDTO.getResourceId()); |
| 174 | UserUpload userUpload = userUploadService.getOne(userUploadQuery); |
| 175 | notification.setUserId(userUpload.getUserId()); |
| 176 | notification.setTitle("资源被点赞"); |
| 177 | notification.setContent("你的资源:" + resource.getResourceName() + " 被: " + user.getUsername() + " 点赞了!"); |
| 178 | notification.setCreateAt(new Date()); |
| 179 | notification.setRead(false); |
| 180 | notification.setTriggeredBy(userResourceDTO.getUserId()); |
| 181 | notification.setRelatedId(userResourceDTO.getResourceId()); |
| 182 | notificationService.save(notification); |
| 183 | |
| 184 | return ResponseEntity.ok(""); |
| 185 | } |
| 186 | |
| 187 | /** |
| 188 | * 收藏资源 |
| 189 | * |
| 190 | * @param userResourceDTO 收藏资源信息 |
| 191 | * @return 收藏资源结果 |
| 192 | */ |
| 193 | @PostMapping("collection") |
| 194 | public ResponseEntity<String> collectResource(@RequestBody UserResourceDTO userResourceDTO) { |
| 195 | |
| 196 | QueryWrapper<User> userQuery = new QueryWrapper<>(); |
| 197 | userQuery.eq("user_id", userResourceDTO.getUserId()); |
| 198 | User user = userService.getOne(userQuery); |
| 199 | |
| 200 | QueryWrapper<Resource> ResourceQuery = new QueryWrapper<>(); |
| 201 | ResourceQuery.eq("resource_id", userResourceDTO.getResourceId()); |
| 202 | Resource resource = resourceService.getOne(ResourceQuery); |
| 203 | |
| 204 | UserCollection userCollection = new UserCollection(); |
| 205 | userCollection.setUserId(userResourceDTO.getUserId()); |
| 206 | userCollection.setResourceId(userResourceDTO.getResourceId()); |
| 207 | userCollectionService.save(userCollection); |
| 208 | |
| 209 | |
| 210 | // 给上传该资源的用户发送通知 |
| 211 | Notification notification = new Notification(); |
| 212 | QueryWrapper<UserUpload> userUploadQuery = new QueryWrapper<>(); |
| 213 | userUploadQuery.eq("resource_id", userResourceDTO.getResourceId()); |
| 214 | UserUpload userUpload = userUploadService.getOne(userUploadQuery); |
| 215 | notification.setUserId(userUpload.getUserId()); |
| 216 | notification.setTitle("资源被收藏"); |
| 217 | notification.setContent("你的资源:" + resource.getResourceName() + " 被: " + user.getUsername() + " 收藏了!"); |
| 218 | notification.setCreateAt(new Date()); |
| 219 | notification.setRead(false); |
| 220 | notification.setTriggeredBy(userResourceDTO.getUserId()); |
| 221 | notification.setRelatedId(userResourceDTO.getResourceId()); |
| 222 | notificationService.save(notification); |
| 223 | |
| 224 | return ResponseEntity.ok(""); |
| 225 | } |
| 226 | } |