| package com.g9.g9backend.controller; |
| |
| import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; |
| import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper; |
| import com.g9.g9backend.mapper.UserPurchaseMapper; |
| import com.g9.g9backend.pojo.*; |
| import com.g9.g9backend.pojo.DTO.*; |
| import com.g9.g9backend.service.*; |
| import org.slf4j.Logger; |
| import org.slf4j.LoggerFactory; |
| import org.springframework.http.ResponseEntity; |
| import org.springframework.web.bind.annotation.*; |
| |
| import java.util.Date; |
| |
| |
| /** |
| * ResourceController 资源控制器类,处理与资源相关的请求 |
| * |
| * @author hcy |
| */ |
| @RestController |
| @RequestMapping("/resource") |
| public class ResourceController { |
| |
| private final ResourceService resourceService; |
| |
| private final GameplayService gameplayService; |
| |
| private final RewardService rewardService; |
| |
| private final UserUploadService userUploadService; |
| |
| private final CommunityService communityService; |
| |
| private final UserService userService; |
| |
| private final UserPurchaseService userPurchaseService; |
| |
| private final UserLikeService userLikeService; |
| |
| private final UserCollectionService userCollectionService; |
| |
| private final NotificationService notificationService; |
| |
| public ResourceController(ResourceService resourceService, GameplayService gameplayService, RewardService rewardService, UserUploadService userUploadService, CommunityService communityService, UserService userService, UserPurchaseMapper userPurchaseMapper, UserPurchaseService userPurchaseService, UserLikeService userLikeService, UserCollectionService userCollectionService, NotificationService notificationService) { |
| this.resourceService = resourceService; |
| this.gameplayService = gameplayService; |
| this.rewardService = rewardService; |
| this.userUploadService = userUploadService; |
| this.communityService = communityService; |
| this.userService = userService; |
| this.userPurchaseService = userPurchaseService; |
| this.userLikeService = userLikeService; |
| this.userCollectionService = userCollectionService; |
| this.notificationService = notificationService; |
| } |
| |
| private final Logger logger = LoggerFactory.getLogger(ResourceController.class); |
| |
| /** |
| * 上传资源 |
| * |
| * @param postResourceDTO 上传资源信息 |
| * @return 上传资源结果 |
| */ |
| @PostMapping |
| public ResponseEntity<String> uploadResource(@RequestBody PostResourceDTO postResourceDTO) { |
| // 存资源 |
| Resource resource = postResourceDTO.getResource(); |
| resourceService.save(resource); |
| // 存玩法列表 |
| String[] gameplayList = postResourceDTO.getGameplayList(); |
| for (String gameplayName : gameplayList) { |
| Gameplay gameplay = new Gameplay(); |
| gameplay.setGameplayName(gameplayName); |
| gameplay.setResourceId(postResourceDTO.getResource().getResourceId()); |
| gameplayService.save(gameplay); |
| } |
| // 完成对应悬赏 |
| if (postResourceDTO.getCompleteRewardId() != 0) { |
| UpdateWrapper<Reward> rewardUpdate = new UpdateWrapper<>(); |
| rewardUpdate.eq("reward_id", postResourceDTO.getCompleteRewardId()).set("completed_by", postResourceDTO.getUserId()).set("completed_at", postResourceDTO.getResource().getUploadTime()).set("resource_id", postResourceDTO.getResource().getResourceId()); |
| rewardService.update(rewardUpdate); |
| } |
| // 存用户上传表 |
| UserUpload userUpload = new UserUpload(); |
| userUpload.setUserId(postResourceDTO.getUserId()); |
| userUpload.setResourceId(postResourceDTO.getResource().getResourceId()); |
| userUploadService.save(userUpload); |
| // 创建资源社区 |
| Community community = new Community(); |
| community.setCommunityName(postResourceDTO.getResource().getResourceName()); |
| community.setType(postResourceDTO.getResource().getClassify()); |
| community.setResourceId(postResourceDTO.getResource().getResourceId()); |
| communityService.save(community); |
| return ResponseEntity.ok(""); |
| } |
| |
| /** |
| * 购买资源 |
| * |
| * @param userResourceDTO 购买资源信息 |
| * @return 购买资源结果 |
| */ |
| @PostMapping("purchase") |
| public ResponseEntity<String> purchaseResource(@RequestBody UserResourceDTO userResourceDTO) { |
| |
| QueryWrapper<User> userQuery = new QueryWrapper<>(); |
| userQuery.eq("user_id", userResourceDTO.getUserId()); |
| User user = userService.getOne(userQuery); |
| |
| QueryWrapper<Resource> ResourceQuery = new QueryWrapper<>(); |
| ResourceQuery.eq("resource_id", userResourceDTO.getResourceId()); |
| Resource resource = resourceService.getOne(ResourceQuery); |
| |
| if (user.getCredits() < resource.getPrice()) { |
| // 积分余额不足 |
| logger.info("The balance of points is insufficient to cover the price of this resource: {}", resource.getPrice()); |
| return ResponseEntity.status(412).body(""); |
| } else { |
| // 扣除用户积分 |
| UpdateWrapper<User> userUpdate = new UpdateWrapper<>(); |
| userUpdate.eq("user_id", user.getUserId()).set("credits", user.getCredits() - resource.getPrice()); |
| userService.update(userUpdate); |
| // 添加购买资源记录 |
| UserPurchase userPurchase = new UserPurchase(); |
| userPurchase.setUserId(user.getUserId()); |
| userPurchase.setResourceId(resource.getResourceId()); |
| userPurchaseService.save(userPurchase); |
| // 给上传该资源的用户发送通知 |
| Notification notification = new Notification(); |
| QueryWrapper<UserUpload> userUploadQuery = new QueryWrapper<>(); |
| userUploadQuery.eq("resource_id", userResourceDTO.getResourceId()); |
| UserUpload userUpload = userUploadService.getOne(userUploadQuery); |
| notification.setUserId(userUpload.getUserId()); |
| notification.setTitle("资源被购买"); |
| notification.setContent("你的资源:" + resource.getResourceName() + " 被: " + user.getUsername() + " 购买了!"); |
| notification.setCreateAt(new Date()); |
| notification.setRead(false); |
| notification.setTriggeredBy(userResourceDTO.getUserId()); |
| notification.setRelatedId(userResourceDTO.getResourceId()); |
| notificationService.save(notification); |
| return ResponseEntity.ok(""); |
| } |
| } |
| |
| /** |
| * 点赞资源 |
| * |
| * @param userResourceDTO 点赞资源信息 |
| * @return 点赞资源结果 |
| */ |
| @PostMapping("like") |
| public ResponseEntity<String> likeResource(@RequestBody UserResourceDTO userResourceDTO) { |
| |
| QueryWrapper<User> userQuery = new QueryWrapper<>(); |
| userQuery.eq("user_id", userResourceDTO.getUserId()); |
| User user = userService.getOne(userQuery); |
| |
| QueryWrapper<Resource> ResourceQuery = new QueryWrapper<>(); |
| ResourceQuery.eq("resource_id", userResourceDTO.getResourceId()); |
| Resource resource = resourceService.getOne(ResourceQuery); |
| |
| UserLike userLike = new UserLike(); |
| userLike.setUserId(userResourceDTO.getUserId()); |
| userLike.setResourceId(userResourceDTO.getResourceId()); |
| userLikeService.save(userLike); |
| |
| // 给上传该资源的用户发送通知 |
| Notification notification = new Notification(); |
| QueryWrapper<UserUpload> userUploadQuery = new QueryWrapper<>(); |
| userUploadQuery.eq("resource_id", userResourceDTO.getResourceId()); |
| UserUpload userUpload = userUploadService.getOne(userUploadQuery); |
| notification.setUserId(userUpload.getUserId()); |
| notification.setTitle("资源被点赞"); |
| notification.setContent("你的资源:" + resource.getResourceName() + " 被: " + user.getUsername() + " 点赞了!"); |
| notification.setCreateAt(new Date()); |
| notification.setRead(false); |
| notification.setTriggeredBy(userResourceDTO.getUserId()); |
| notification.setRelatedId(userResourceDTO.getResourceId()); |
| notificationService.save(notification); |
| |
| return ResponseEntity.ok(""); |
| } |
| |
| /** |
| * 收藏资源 |
| * |
| * @param userResourceDTO 收藏资源信息 |
| * @return 收藏资源结果 |
| */ |
| @PostMapping("collection") |
| public ResponseEntity<String> collectResource(@RequestBody UserResourceDTO userResourceDTO) { |
| |
| QueryWrapper<User> userQuery = new QueryWrapper<>(); |
| userQuery.eq("user_id", userResourceDTO.getUserId()); |
| User user = userService.getOne(userQuery); |
| |
| QueryWrapper<Resource> ResourceQuery = new QueryWrapper<>(); |
| ResourceQuery.eq("resource_id", userResourceDTO.getResourceId()); |
| Resource resource = resourceService.getOne(ResourceQuery); |
| |
| UserCollection userCollection = new UserCollection(); |
| userCollection.setUserId(userResourceDTO.getUserId()); |
| userCollection.setResourceId(userResourceDTO.getResourceId()); |
| userCollectionService.save(userCollection); |
| |
| |
| // 给上传该资源的用户发送通知 |
| Notification notification = new Notification(); |
| QueryWrapper<UserUpload> userUploadQuery = new QueryWrapper<>(); |
| userUploadQuery.eq("resource_id", userResourceDTO.getResourceId()); |
| UserUpload userUpload = userUploadService.getOne(userUploadQuery); |
| notification.setUserId(userUpload.getUserId()); |
| notification.setTitle("资源被收藏"); |
| notification.setContent("你的资源:" + resource.getResourceName() + " 被: " + user.getUsername() + " 收藏了!"); |
| notification.setCreateAt(new Date()); |
| notification.setRead(false); |
| notification.setTriggeredBy(userResourceDTO.getUserId()); |
| notification.setRelatedId(userResourceDTO.getResourceId()); |
| notificationService.save(notification); |
| |
| return ResponseEntity.ok(""); |
| } |
| } |