推荐系统完成

Change-Id: I244590be01b1b4f37664a0e7f3103827e607ffbe
diff --git a/ruoyi-admin/src/main/java/com/ruoyi/torrent/controller/BtTorrentController.java b/ruoyi-admin/src/main/java/com/ruoyi/torrent/controller/BtTorrentController.java
index 15dc9f3..559547b 100644
--- a/ruoyi-admin/src/main/java/com/ruoyi/torrent/controller/BtTorrentController.java
+++ b/ruoyi-admin/src/main/java/com/ruoyi/torrent/controller/BtTorrentController.java
@@ -6,6 +6,7 @@
 import java.nio.charset.StandardCharsets;
 import java.util.*;
 
+import com.fasterxml.jackson.annotation.JsonProperty;
 import com.ruoyi.common.utils.http.HttpUtils;
 import com.fasterxml.jackson.core.type.TypeReference;
 import com.fasterxml.jackson.databind.JsonNode;
@@ -61,6 +62,33 @@
     @Autowired
     private IBtTorrentAnnounceService btTorrentAnnounceService;
     private static final String RECOMMEND_API = "http://127.0.0.1:5000/recommend_torrents";
+    private String sendJsonPost(String url, String jsonBody) throws IOException {
+        HttpURLConnection connection = (HttpURLConnection) new URL(url).openConnection();
+        connection.setRequestMethod("POST");
+        connection.setDoOutput(true);
+        connection.setRequestProperty("Content-Type", "application/json");
+        connection.setRequestProperty("Accept", "application/json");
+
+        try (OutputStream os = connection.getOutputStream()) {
+            os.write(jsonBody.getBytes(StandardCharsets.UTF_8));
+        }
+
+        try (InputStream is = connection.getInputStream()) {
+            return new String(is.readAllBytes(), StandardCharsets.UTF_8);
+        }
+    }
+    public static class TorrentRecommendationRequest {
+        @JsonProperty("torrent_ids")
+        private List<Long> ids;
+
+        public List<Long> getIds() {
+            return ids;
+        }
+
+        public void setIds(List<Long> ids) {
+            this.ids = ids;
+        }
+    }
 
     private String torrentPath= "torrents";
 
@@ -92,9 +120,6 @@
                 }
                 TorrentFileUtil.uploadFile(file,torrentPath+"/"+SecurityUtils.getUserId());
 
-
-
-
                 // Assuming the Flask server responds with JSON, parse the response
                 String responseBody = new String(connection.getInputStream().readAllBytes(), StandardCharsets.UTF_8);
                 parseTorrentData(responseBody);
@@ -155,7 +180,6 @@
                 btTorrentFileService.insertBtTorrentFile(btFile);
             }
             );
-
             // Convert btTorrentAnnounceNode to List<BtTorrentAnnounce>
             List<BtTorrentAnnounce> btTorrentAnnounceList = new ArrayList<>();
             if (btTorrentAnnounceNode.isArray()) {
@@ -170,18 +194,9 @@
                         btTorrentAnnounceService.insertBtTorrentAnnounce(btTorrentAnnounce);
                     }
             );
-
-
-
-
-
-
-
-
         } catch (Exception e) {
             e.printStackTrace();
             // Handle the error (e.g., return null or an error message)
-
         }
     }
 
@@ -227,22 +242,41 @@
      */
     @PreAuthorize("@ss.hasPermi('system:torrent:list')")
     @PostMapping("/recommend")
-    public TableDataInfo recommendTorrents(@RequestParam("user_id") String userId) {
+    public TableDataInfo recommendTorrents() {
+        // 假设当前用户的 user_id 是通过 SecurityContext 或 session 中获取的
+        String userId = String.valueOf(getUserId());  // 获取当前用户的 user_id
+
+        if (userId == null || userId.isEmpty()) {
+            TableDataInfo error = new TableDataInfo();
+            error.setCode(400);
+            error.setMsg("用户ID无效或未登录");
+            return error;
+        }
+
+        System.out.println("当前用户ID: " + userId);  // 打印调试信息
+
         try {
-            // 1. 构造 JSON 请求体
+            // 1. 构造 JSON 请求体,包含用户ID
             String jsonRequest = "{\"user_id\": \"" + userId + "\"}";
 
-            // 2. 向 Flask 服务发 POST 请求,返回 JSON 数组
-            String jsonResponse = HttpUtils.sendPost(RECOMMEND_API, jsonRequest);
+            // 2. 向 Flask 服务发 POST 请求,返回 JSON 响应
+            String jsonResponse = sendJsonPost(RECOMMEND_API, jsonRequest);
 
-            // 3. 使用 Jackson 解析 JSON 数组为 List<Long>
+            // 3. 使用 Jackson 解析 JSON 响应为 TorrentRecommendationRequest 对象
             ObjectMapper mapper = new ObjectMapper();
-            List<Long> idList = mapper.readValue(jsonResponse, new TypeReference<List<Long>>() {});
+            TorrentRecommendationRequest recommendationRequest = mapper.readValue(jsonResponse, TorrentRecommendationRequest.class);
 
-            // 4. 根据 ID 查询完整种子信息,并保持推荐顺序
+            // 4. 提取 ids 字段
+            List<Long> idList = recommendationRequest.getIds();
+
+            if (idList == null || idList.isEmpty()) {
+                throw new RuntimeException("推荐的种子ID列表为空");
+            }
+
+            // 5. 根据种子ID查询完整的种子信息,保持推荐顺序
             List<BtTorrent> resultList = btTorrentService.selectBtTorrentsByIdsOrdered(idList);
 
-            // 5. 封装成 TableDataInfo 返回
+            // 6. 封装查询结果并返回
             TableDataInfo rsp = new TableDataInfo();
             rsp.setCode(200);
             rsp.setMsg("推荐成功");
@@ -251,7 +285,9 @@
             return rsp;
 
         } catch (Exception e) {
-            e.printStackTrace();
+            e.printStackTrace();  // 打印异常信息
+
+            // 如果出错,返回错误信息
             TableDataInfo error = new TableDataInfo();
             error.setCode(500);
             error.setMsg("推荐失败:" + e.getMessage());
@@ -259,6 +295,7 @@
         }
     }
 
+
     /**
      * 导出种子主列表
      */
diff --git a/ruoyi-admin/src/main/java/com/ruoyi/torrent/mapper/BtTorrentMapper.java b/ruoyi-admin/src/main/java/com/ruoyi/torrent/mapper/BtTorrentMapper.java
index 529043c..05fc282 100644
--- a/ruoyi-admin/src/main/java/com/ruoyi/torrent/mapper/BtTorrentMapper.java
+++ b/ruoyi-admin/src/main/java/com/ruoyi/torrent/mapper/BtTorrentMapper.java
@@ -68,5 +68,5 @@
      * @param ids 种子ID列表
      * @return 种子列表
      */
-    List<BtTorrent> selectBtTorrentsByIdsOrdered(@Param("list") List<Long> ids);
+    public List<BtTorrent> selectBtTorrentsByIdsOrdered(@Param("list") List<Long> ids);
 }
diff --git a/ruoyi-admin/src/main/resources/mapper/system/BtTorrentMapper.xml b/ruoyi-admin/src/main/resources/mapper/system/BtTorrentMapper.xml
index 4f5cf3f..097d7b8 100644
--- a/ruoyi-admin/src/main/resources/mapper/system/BtTorrentMapper.xml
+++ b/ruoyi-admin/src/main/resources/mapper/system/BtTorrentMapper.xml
@@ -45,14 +45,14 @@
         where torrent_id = #{torrentId}
     </select>
 
-    <select id="selectBtTorrentsByIdsOrdered" resultType="BtTorrent">
+    <select id="selectBtTorrentsByIdsOrdered" resultMap="BtTorrentResult">
         SELECT * FROM bt_torrent
-        WHERE id IN
-        <foreach collection="list" item="id" open="(" separator="," close=")">
+        WHERE torrent_id IN  <!-- 修改这里 -->
+        <foreach item="id" collection="list" open="(" separator="," close=")">
             #{id}
         </foreach>
-        ORDER BY FIELD(id
-        <foreach collection="list" item="id" separator=",">
+        ORDER BY FIELD(torrent_id,  <!-- 修改这里 -->
+        <foreach item="id" collection="list" separator=",">
             #{id}
         </foreach>
         )