fix GET /user/purchase

Change-Id: I09ae5c51e446a6cab135ab4552e5a0726bc0f629
diff --git a/src/main/java/com/g9/g9backend/controller/UserController.java b/src/main/java/com/g9/g9backend/controller/UserController.java
index 49dbd21..aa05ff7 100644
--- a/src/main/java/com/g9/g9backend/controller/UserController.java
+++ b/src/main/java/com/g9/g9backend/controller/UserController.java
@@ -46,7 +46,15 @@
 
     private final ThreadService threadService;
 
-    public UserController(UserService userService, InvitationService invitationService, SubscriptionService subscriptionService, SearchHistoryService searchHistoryService, RewardService rewardService, UserCollectionService userCollectionService, UserUploadService userUploadService, UserPurchaseService userPurchaseService, ResourceService resourceService, ThreadService threadService) {
+    private final GameplayService gameplayService;
+
+    private final ResourceVersionService resourceVersionService;
+
+    private final GameVersionService gameVersionService;
+
+    private final TorrentRecordService torrentRecordService;
+
+    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) {
         this.userService = userService;
         this.invitationService = invitationService;
         this.subscriptionService = subscriptionService;
@@ -57,6 +65,10 @@
         this.userPurchaseService = userPurchaseService;
         this.resourceService = resourceService;
         this.threadService = threadService;
+        this.gameplayService = gameplayService;
+        this.resourceVersionService = resourceVersionService;
+        this.gameVersionService = gameVersionService;
+        this.torrentRecordService = torrentRecordService;
     }
 
 
@@ -364,7 +376,7 @@
      * @return 获取用户购买资源结果
      */
     @GetMapping("/purchase")
-    public ResponseEntity<GetUserResourceListDTO> getUserPurchase(@RequestParam int userId, @RequestParam int pageNumber, @RequestParam int rows) {
+    public ResponseEntity<GetUserPurchaseDTO> 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);
@@ -375,13 +387,46 @@
             Resource resource = resourceService.getById(userPurchase.getResourceId());
             purchaseList.add(resource);
         }
+        List<Resource> resources = purchaseList;
+        List<List<Gameplay>> gameplays = new ArrayList<>();
+        List<List<ResourceVersion>> resourceVersions = new ArrayList<>();
+        List<List<List<GameVersion>>> gameVersion = new ArrayList<>();
+        List<List<List<TorrentRecord>>> torrentRecord = new ArrayList<>();
+
+        for (Resource resource : purchaseList) {
+            int resourceId = resource.getResourceId();
+            // 获取Gameplay列表
+            List<Gameplay> gameplayList = gameplayService.list(new QueryWrapper<Gameplay>().eq("resource_id", resourceId));
+
+            // 获取ResourceVersion列表
+            List<ResourceVersion> resourceVersionList = resourceVersionService.list(new QueryWrapper<ResourceVersion>().eq("resource_id", resourceId));
+
+            // 获取GameVersion二维列表
+            List<List<GameVersion>> gameVersionLists = new ArrayList<>();
+            for (ResourceVersion resourceVersion : resourceVersionList) {
+                List<GameVersion> gameVersionList = gameVersionService.list(new QueryWrapper<GameVersion>().eq("resource_version_id", resourceVersion.getResourceVersionId()));
+                gameVersionLists.add(gameVersionList);
+            }
+
+            // 获取TorrentRecord二维列表
+            List<List<TorrentRecord>> torrentRecordLists = new ArrayList<>();
+            for (ResourceVersion resourceVersion : resourceVersionList) {
+                List<TorrentRecord> torrentRecordList = torrentRecordService.list(new QueryWrapper<TorrentRecord>().eq("resource_version_id", resourceVersion.getResourceVersionId()));
+                torrentRecordLists.add(torrentRecordList);
+            }
+
+            gameplays.add(gameplayList);
+            resourceVersions.add(resourceVersionList);
+            gameVersion.add(gameVersionLists);
+            torrentRecord.add(torrentRecordLists);
+        }
         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);
+        GetUserPurchaseDTO getUserPurchaseDTO = new GetUserPurchaseDTO(resources, gameplays, resourceVersions, gameVersion, torrentRecord, total, pages, current, size);
+        return ResponseEntity.ok(getUserPurchaseDTO);
     }
 
     /**
diff --git a/src/main/java/com/g9/g9backend/pojo/DTO/GetUserPurchaseDTO.java b/src/main/java/com/g9/g9backend/pojo/DTO/GetUserPurchaseDTO.java
new file mode 100644
index 0000000..a51a5b3
--- /dev/null
+++ b/src/main/java/com/g9/g9backend/pojo/DTO/GetUserPurchaseDTO.java
@@ -0,0 +1,138 @@
+package com.g9.g9backend.pojo.DTO;
+
+import com.g9.g9backend.pojo.*;
+import lombok.AllArgsConstructor;
+import lombok.Data;
+import lombok.NoArgsConstructor;
+
+import java.util.ArrayList;
+import java.util.Date;
+import java.util.List;
+
+@Data
+public class GetUserPurchaseDTO {
+
+    private List<RecordInfo> records;
+
+    private long total;
+
+    private long pages;
+
+    private long current;
+
+    private long size;
+
+    public GetUserPurchaseDTO(List<Resource> resource, List<List<Gameplay>> gameplay, List<List<ResourceVersion>> resourceVersionList, List<List<List<GameVersion>>> gameVersionLists, List<List<List<TorrentRecord>>> torrentRecordLists, long total, long pages, long current, long size){
+        List<RecordInfo> records = new ArrayList<>();
+        for( int i = 0; i < resource.size(); i++ ){
+            RecordInfo resourceInfo = new RecordInfo(resource.get(i), gameplay.get(i), resourceVersionList.get(i), gameVersionLists.get(i), torrentRecordLists.get(i));
+            records.add(resourceInfo);
+        }
+        this.records = records;
+        this.total = total;
+        this.pages = pages;
+        this.current = current;
+        this.size = size;
+    }
+
+    @Data
+    @AllArgsConstructor
+    @NoArgsConstructor
+    public static class RecordInfo {
+        private int resourceId;
+
+        private String resourceName;
+
+        private String resourcePicture;
+
+        private Date lastUpdateTime;
+
+        private String classify;
+
+        private String[] gameplayList;
+
+        private List<ResourceVersionInfo> resourceVersionList;
+
+        public RecordInfo(Resource resource, List<Gameplay> gameplayList, List<ResourceVersion> resourceVersionList, List<List<GameVersion>> gameVersionLists, List<List<TorrentRecord>> torrentRecordLists) {
+            // 创建资源基本信息
+            this.resourceId = resource.getResourceId();
+            this.resourceName = resource.getResourceName();
+            this.resourcePicture = resource.getResourcePicture();
+            this.lastUpdateTime = resource.getLastUpdateTime();
+            this.classify = resource.getClassify();
+
+            // 创建玩法列表
+            String[] gameplays = new String[gameplayList.size()];
+            // 处理对应的 gameplay 列表
+            for (int i = 0; i < gameplayList.size(); i++) {
+                Gameplay gameplay = gameplayList.get(i);
+                gameplays[i] = gameplay.getGameplayName();
+            }
+            this.gameplayList = gameplays;
+
+            // 创建资源版本列表
+            List<ResourceVersionInfo> resourceVersions = new ArrayList<>();
+            for (int i = 0; i < resourceVersionList.size(); i++) {
+                ResourceVersionInfo resourceVersionInfo = new ResourceVersionInfo();
+                ResourceVersion resourceVersion = resourceVersionList.get(i);
+                List<GameVersion> gameVersions = gameVersionLists.get(i);
+                List<TorrentRecord> torrentRecords = torrentRecordLists.get(i);
+
+                resourceVersionInfo.setResourceVersionId(resourceVersion.getResourceVersionId());
+                resourceVersionInfo.setResourceVersionName(resourceVersion.getResourceVersionName());
+                String[] compatibleVersions = new String[gameVersions.size()];
+                for (int j = 0; j < gameVersions.size(); j++) {
+                    GameVersion gameVersion = gameVersions.get(j);
+                    compatibleVersions[j] = gameVersion.getGameVersionName();
+                }
+                resourceVersionInfo.setCompatibleVersions(compatibleVersions);
+                List<TorrentRecordInfo> torrentRecordInfos = new ArrayList<>();
+                for (TorrentRecord torrentRecord : torrentRecords) {
+                    TorrentRecordInfo torrentRecordInfo = new TorrentRecordInfo();
+                    torrentRecordInfo.setTorrentRecordId(torrentRecord.getTorrentRecordId());
+                    torrentRecordInfo.setTorrentUrl(torrentRecord.getTorrentUrl());
+                    torrentRecordInfo.setInfoHash(torrentRecord.getInfoHash());
+                    torrentRecordInfo.setUploadTime(torrentRecord.getUploadTime());
+                    torrentRecordInfo.setUploaderUserId(torrentRecord.getUploaderUserId());
+
+                    torrentRecordInfos.add(torrentRecordInfo);
+                }
+                resourceVersionInfo.setTorrentList(torrentRecordInfos);
+                resourceVersionInfo.setSeeds(resourceVersion.getSeeds());
+
+                resourceVersions.add(resourceVersionInfo);
+            }
+            this.resourceVersionList = resourceVersions;
+        }
+    }
+
+    @Data
+    @AllArgsConstructor
+    @NoArgsConstructor
+    public static class ResourceVersionInfo {
+        private int resourceVersionId;
+
+        private String resourceVersionName;
+
+        private String[] compatibleVersions;
+
+        private List<TorrentRecordInfo> torrentList;
+
+        private int seeds;
+    }
+
+    @Data
+    @AllArgsConstructor
+    @NoArgsConstructor
+    public static class TorrentRecordInfo {
+        private int torrentRecordId;
+
+        private String torrentUrl;
+
+        private String infoHash;
+
+        private Date uploadTime;
+
+        private int uploaderUserId;
+    }
+}
diff --git a/src/test/java/com/g9/g9backend/controller/UserControllerTest.java b/src/test/java/com/g9/g9backend/controller/UserControllerTest.java
index bb768a0..216cdf2 100644
--- a/src/test/java/com/g9/g9backend/controller/UserControllerTest.java
+++ b/src/test/java/com/g9/g9backend/controller/UserControllerTest.java
@@ -61,6 +61,18 @@
     @Mock
     private ThreadService threadService;
 
+    @Mock
+    private GameplayService gameplayService;
+
+    @Mock
+    private ResourceVersionService resourceVersionService;
+
+    @Mock
+    private GameVersionService gameVersionService;
+
+    @Mock
+    private TorrentRecordService torrentRecordService;
+
     private final ObjectMapper objectMapper = new ObjectMapper();
 
     @BeforeEach
@@ -335,6 +347,14 @@
 
         when(userPurchaseService.page(any(), any())).thenReturn(new Page<>(1, 2));
         when(resourceService.getById(anyInt())).thenReturn(new Resource(1, "a", null, null, null, new Date(), new Date(), 100, 0, 0, 0, 0,  "mod"));
+        List<Gameplay> gameplayList = new ArrayList<>();
+        when(gameplayService.list(new QueryWrapper<Gameplay>().eq("resource_id", 1))).thenReturn(gameplayList);
+        List<ResourceVersion> resourceVersionList = new ArrayList<>();
+        when(resourceVersionService.list(new QueryWrapper<ResourceVersion>().eq("resource_id", 1))).thenReturn(resourceVersionList);
+        List<GameVersion> gameVersionList = new ArrayList<>();
+        when( gameVersionService.list(new QueryWrapper<GameVersion>().eq("resource_version_id", 1))).thenReturn(gameVersionList);
+        List<TorrentRecord> torrentRecordList = new ArrayList<>();
+        when(torrentRecordService.list(new QueryWrapper<TorrentRecord>().eq("resource_version_id", 1))).thenReturn(torrentRecordList);
 
         mockMvc.perform(get("/user/purchase")
                         .param("userId", "1")