Merge "添加更新用户流量的方法,并定时跟新到所有用户"

Change-Id: I558ee7de6767ed1b78685883310a268ea51b198a
diff --git a/src/main/java/com/pt/service/TorrentStatsService.java b/src/main/java/com/pt/service/TorrentStatsService.java
index 9596784..baa63e3 100644
--- a/src/main/java/com/pt/service/TorrentStatsService.java
+++ b/src/main/java/com/pt/service/TorrentStatsService.java
@@ -1,6 +1,7 @@
 package com.pt.service;
 
 import com.pt.entity.PeerInfoEntity;
+import com.pt.entity.TorrentMeta;
 import com.pt.entity.TorrentStats;
 import com.pt.exception.ResourceNotFoundException;
 import com.pt.repository.PeerInfoRepository;
@@ -13,6 +14,7 @@
 
 import java.time.LocalDateTime;
 import java.util.List;
+import java.util.Optional;
 
 @Service
 public class TorrentStatsService {
@@ -91,11 +93,18 @@
         }
 
         // 3. 更新统计记录
-        TorrentStats stats = statsRepository.findByTorrentId(
-                torrentMetaRepository.findByInfoHash(infoHash).getId()
-        ).orElse(new TorrentStats());
+        Optional<TorrentMeta> optionalMeta = torrentMetaRepository.findByInfoHash(infoHash);
+        if (optionalMeta.isEmpty()) {
+            // 处理找不到 info_hash 的情况
+            throw new IllegalArgumentException("Invalid info_hash: not found in database");
+        }
 
-        stats.setTorrentId(torrentMetaRepository.findByInfoHash(infoHash).getId());
+        TorrentMeta meta = optionalMeta.get();
+
+        TorrentStats stats = statsRepository.findByTorrentId(meta.getId())
+                .orElse(new TorrentStats());
+
+        stats.setTorrentId(meta.getId());
         stats.setSeederCount(seeders);
         stats.setLeecherCount(leechers);
         stats.setCompletedCount(completed);