user module

GET /user/getId
GET /user/invitation-code
GET /user/info
GET /user/reward
GET /user/collection
GET /user/upload
GET /user/purchase
GET /user/search
GET /user/data
GET /user/subscriber
GET /user/follower

Change-Id: I096fb86446831ebdb50ec609d3cde77d5fe77ef5
diff --git a/src/main/java/com/g9/g9backend/controller/UserController.java b/src/main/java/com/g9/g9backend/controller/UserController.java
index 48ec0cd..38b4586 100644
--- a/src/main/java/com/g9/g9backend/controller/UserController.java
+++ b/src/main/java/com/g9/g9backend/controller/UserController.java
@@ -2,6 +2,8 @@
 
 import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
 import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
+import com.baomidou.mybatisplus.core.metadata.IPage;
+import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
 import com.g9.g9backend.pojo.*;
 import com.g9.g9backend.pojo.DTO.*;
 import com.g9.g9backend.service.*;
@@ -11,6 +13,9 @@
 import org.springframework.http.ResponseEntity;
 import org.springframework.web.bind.annotation.*;
 
+import java.util.ArrayList;
+import java.util.List;
+
 /**
  * UserController 用户控制器类,处理与用户相关的请求
  *
@@ -28,13 +33,29 @@
 
     private final SearchHistoryService searchHistoryService;
 
-    public UserController(UserService userService, InvitationService invitationService, SubscriptionService subscriptionService, SearchHistoryService searchHistoryService) {
+    private final RewardService rewardService;
+
+    private final UserCollectionService userCollectionService;
+
+    private final UserUploadService userUploadService;
+
+    private final UserPurchaseService userPurchaseService;
+
+    private final ResourceService resourceService;
+
+    public UserController(UserService userService, InvitationService invitationService, SubscriptionService subscriptionService, SearchHistoryService searchHistoryService, RewardService rewardService, UserCollectionService userCollectionService, UserUploadService userUploadService, UserPurchaseService userPurchaseService, ResourceService resourceService) {
         this.userService = userService;
         this.invitationService = invitationService;
         this.subscriptionService = subscriptionService;
         this.searchHistoryService = searchHistoryService;
+        this.rewardService = rewardService;
+        this.userCollectionService = userCollectionService;
+        this.userUploadService = userUploadService;
+        this.userPurchaseService = userPurchaseService;
+        this.resourceService = resourceService;
     }
 
+
     private final Logger logger = LoggerFactory.getLogger(UserController.class);
 
     /**
@@ -203,4 +224,268 @@
         subscriptionService.remove(subscriptionQuery);
         return ResponseEntity.noContent().build();
     }
+
+
+    /**
+     * 获取用户ID
+     *
+     * @param username 用户名
+     * @param password 密码
+     * @return 获取邀请码结果
+     */
+    @GetMapping("/getId")
+    public ResponseEntity<Integer> getUserId(@RequestParam String username, @RequestParam String password) {
+        QueryWrapper<User> userQuery = new QueryWrapper<>();
+        userQuery.eq("username", username).eq("password", password);
+        User user = userService.getOne(userQuery);
+        return ResponseEntity.ok(user.getUserId());
+    }
+
+    /**
+     * 获取邀请码
+     *
+     * @param userId 用户id
+     * @return 获取邀请码结果
+     */
+    @GetMapping("/invitation-code")
+    public ResponseEntity<GetInvitationCodeDTO> getInvitationCode(@RequestParam int userId) {
+        QueryWrapper<Invitation> invitationQuery = new QueryWrapper<>();
+        invitationQuery.eq("user_id", userId).eq("invitee_id", 0); // 获取未被使用过的分配给该用户的邀请码
+        invitationQuery.select("invitation_code");
+        List<Invitation> invitationList = invitationService.list(invitationQuery);
+        GetInvitationCodeDTO getInvitationCodeDTO = new GetInvitationCodeDTO(invitationList);
+        return ResponseEntity.ok(getInvitationCodeDTO);
+    }
+
+    /**
+     * 获取用户信息
+     *
+     * @param userId 用户id
+     * @return 获取用户信息结果
+     */
+    @GetMapping("/info")
+    public ResponseEntity<User> getUserInfo(@RequestParam int userId) {
+        User user = userService.getById(userId);
+        return ResponseEntity.ok(user);
+    }
+
+    /**
+     * 获取用户悬赏
+     *
+     * @param userId     用户id
+     * @param pageNumber 页数
+     * @param rows       行数
+     * @return 获取用户悬赏结果
+     */
+    @GetMapping("/reward")
+    public ResponseEntity<GetUserRewardDTO> getUserReward(@RequestParam int userId, @RequestParam int pageNumber, @RequestParam int rows) {
+        IPage<Reward> page = new Page<>(pageNumber, rows);
+        QueryWrapper<Reward> rewardQuery = new QueryWrapper<>();
+        rewardQuery.eq("user_id", userId);
+        IPage<Reward> rewardPage = rewardService.page(page, rewardQuery);
+        List<Reward> rewardList = rewardPage.getRecords();
+        long total = rewardPage.getTotal();
+        long pages = rewardPage.getPages();
+        long current = rewardPage.getCurrent();
+        long size = rewardPage.getSize();
+
+        GetUserRewardDTO getUserRewardDTO = new GetUserRewardDTO(rewardList, total, pages, current, size);
+        return ResponseEntity.ok(getUserRewardDTO);
+    }
+
+    /**
+     * 获取用户收藏资源
+     *
+     * @param userId     用户id
+     * @param pageNumber 页数
+     * @param rows       行数
+     * @return 获取用户收藏结果
+     */
+    @GetMapping("/collection")
+    public ResponseEntity<GetUserResourceListDTO> getUserCollection(@RequestParam int userId, @RequestParam int pageNumber, @RequestParam int rows) {
+        IPage<UserCollection> page = new Page<>(pageNumber, rows);
+        QueryWrapper<UserCollection> userCollectionQuery = new QueryWrapper<>();
+        userCollectionQuery.eq("user_id", userId);
+        IPage<UserCollection> userCollectionPage = userCollectionService.page(page, userCollectionQuery);
+        List<UserCollection> resourceList = userCollectionPage.getRecords();
+        List<Resource> collectionList = new ArrayList<>();
+        for (UserCollection userCollection : resourceList) {
+            Resource resource = resourceService.getById(userCollection.getResourceId());
+            collectionList.add(resource);
+        }
+        long total = userCollectionPage.getTotal();
+        long pages = userCollectionPage.getPages();
+        long current = userCollectionPage.getCurrent();
+        long size = userCollectionPage.getSize();
+
+        GetUserResourceListDTO getUserResourceListDTO = new GetUserResourceListDTO(collectionList, total, pages, current, size);
+        return ResponseEntity.ok(getUserResourceListDTO);
+    }
+
+    /**
+     * 获取用户上传资源
+     *
+     * @param userId     用户id
+     * @param pageNumber 页数
+     * @param rows       行数
+     * @return 获取用户上传资源结果
+     */
+    @GetMapping("/upload")
+    public ResponseEntity<GetUserResourceListDTO> getUserUpload(@RequestParam int userId, @RequestParam int pageNumber, @RequestParam int rows) {
+        IPage<UserUpload> page = new Page<>(pageNumber, rows);
+        QueryWrapper<UserUpload> userUploadQuery = new QueryWrapper<>();
+        userUploadQuery.eq("user_id", userId);
+        IPage<UserUpload> userUploadPage = userUploadService.page(page, userUploadQuery);
+        List<UserUpload> resourceList = userUploadPage.getRecords();
+        List<Resource> uploadList = new ArrayList<>();
+        for (UserUpload userUpload : resourceList) {
+            Resource resource = resourceService.getById(userUpload.getResourceId());
+            uploadList.add(resource);
+        }
+        long total = userUploadPage.getTotal();
+        long pages = userUploadPage.getPages();
+        long current = userUploadPage.getCurrent();
+        long size = userUploadPage.getSize();
+
+        GetUserResourceListDTO getUserResourceListDTO = new GetUserResourceListDTO(uploadList, total, pages, current, size);
+        return ResponseEntity.ok(getUserResourceListDTO);
+    }
+
+    /**
+     * 获取用户购买资源
+     *
+     * @param userId     用户id
+     * @param pageNumber 页数
+     * @param rows       行数
+     * @return 获取用户购买资源结果
+     */
+    @GetMapping("/purchase")
+    public ResponseEntity<GetUserResourceListDTO> getUserPurchase(@RequestParam int userId, @RequestParam int pageNumber, @RequestParam int rows) {
+        IPage<UserPurchase> page = new Page<>(pageNumber, rows);
+        QueryWrapper<UserPurchase> userPurchaseQuery = new QueryWrapper<>();
+        userPurchaseQuery.eq("user_id", userId);
+        IPage<UserPurchase> userPurchasePage = userPurchaseService.page(page, userPurchaseQuery);
+        List<UserPurchase> resourceList = userPurchasePage.getRecords();
+        List<Resource> purchaseList = new ArrayList<>();
+        for (UserPurchase userPurchase : resourceList) {
+            Resource resource = resourceService.getById(userPurchase.getResourceId());
+            purchaseList.add(resource);
+        }
+        long total = userPurchasePage.getTotal();
+        long pages = userPurchasePage.getPages();
+        long current = userPurchasePage.getCurrent();
+        long size = userPurchasePage.getSize();
+
+        GetUserResourceListDTO getUserResourceListDTO = new GetUserResourceListDTO(purchaseList, total, pages, current, size);
+        return ResponseEntity.ok(getUserResourceListDTO);
+    }
+
+    /**
+     * 获取用户搜索历史
+     *
+     * @param userId 用户id
+     * @return 获取用户搜索历史结果
+     */
+    @GetMapping("/search")
+    public ResponseEntity<GetUserSearchHistoryDTO> getUserSearchHistory(@RequestParam int userId) {
+        QueryWrapper<SearchHistory> SearchHistoryQuery = new QueryWrapper<>();
+        SearchHistoryQuery.eq("user_id", userId).orderByDesc("search_history_id").last("LIMIT 10");
+        List<SearchHistory> searchHistoryList = searchHistoryService.list(SearchHistoryQuery);
+
+        GetUserSearchHistoryDTO getUserSearchHistoryDTO = new GetUserSearchHistoryDTO(searchHistoryList);
+        return ResponseEntity.ok(getUserSearchHistoryDTO);
+    }
+
+    /**
+     * 获取用户数据
+     *
+     * @param userId 用户id
+     * @return 获取用户数据结果
+     */
+    @GetMapping("/data")
+    public ResponseEntity<GetUserDataDTO> getUserData(@RequestParam int userId) {
+        User user = userService.getById(userId);
+        int subscriberCount = user.getSubscriberCount();
+        int uploadAmount = user.getUploadAmount();
+
+        QueryWrapper<UserUpload> UserUploadQuery = new QueryWrapper<>();
+        UserUploadQuery.eq("user_id", userId);
+        List<UserUpload> UserUpload = userUploadService.list(UserUploadQuery);
+        int beDownloadedAmount = 0; // 上传资源被下载量
+        float[] seedPercentageList = new float[4]; // 上传资源类型百分比,0-1区间的小数
+        int resourcePackAmount = 0;
+        int modAmount = 0;
+        int modPackAmount = 0;
+        int mapAmount = 0;
+        for (UserUpload userUpload : UserUpload) {
+            Resource resource = resourceService.getById(userUpload.getResourceId());
+            beDownloadedAmount += resource.getDownloads();
+            switch (resource.getClassify()) {
+                case "resourcePack":
+                    resourcePackAmount++;
+                    break;
+                case "mod":
+                    modAmount++;
+                    break;
+                case "modPack":
+                    modPackAmount++;
+                    break;
+                case "map":
+                    mapAmount++;
+                    break;
+            }
+        }
+        int totalAmount = resourcePackAmount + modAmount + modPackAmount + mapAmount;
+        seedPercentageList[0] = (float) resourcePackAmount / totalAmount;
+        seedPercentageList[1] = (float) modAmount / totalAmount;
+        seedPercentageList[2] = (float) modPackAmount / totalAmount;
+        seedPercentageList[3] = (float) mapAmount / totalAmount;
+
+        GetUserDataDTO getUserDataDTO = new GetUserDataDTO(subscriberCount, uploadAmount, beDownloadedAmount, seedPercentageList);
+        return ResponseEntity.ok(getUserDataDTO);
+    }
+
+    /**
+     * 获取关注列表
+     *
+     * @param userId 用户id
+     * @return 获取关注列表结果
+     */
+    @GetMapping("/subscriber")
+    public ResponseEntity<GetUserDTO> getUserSubscriber(@RequestParam int userId) {
+        QueryWrapper<Subscription> subscriptionQuery = new QueryWrapper<>();
+        subscriptionQuery.eq("follower_id", userId);
+        List<Subscription> subscriptionList = subscriptionService.list(subscriptionQuery);
+
+        List<User> subscriberList = new ArrayList<>();
+        for (Subscription subscription : subscriptionList) {
+            User user = userService.getById(subscription.getUserId());
+            subscriberList.add(user);
+        }
+
+        GetUserDTO getUserDTO = new GetUserDTO(subscriberList);
+        return ResponseEntity.ok(getUserDTO);
+    }
+
+    /**
+     * 获取粉丝列表
+     *
+     * @param userId 用户id
+     * @return 获取粉丝列表结果
+     */
+    @GetMapping("/follower")
+    public ResponseEntity<GetUserDTO> getUserFollower(@RequestParam int userId) {
+        QueryWrapper<Subscription> subscriptionQuery = new QueryWrapper<>();
+        subscriptionQuery.eq("user_id", userId);
+        List<Subscription> subscriptionList = subscriptionService.list(subscriptionQuery);
+
+        List<User> subscriberList = new ArrayList<>();
+        for (Subscription subscription : subscriptionList) {
+            User user = userService.getById(subscription.getUserId());
+            subscriberList.add(user);
+        }
+
+        GetUserDTO getUserDTO = new GetUserDTO(subscriberList);
+        return ResponseEntity.ok(getUserDTO);
+    }
 }
\ No newline at end of file