作弊&促销

Change-Id: Ibdee947370e11c3a98912569e1a290b5e0968fbe
diff --git a/src/main/java/com/example/myproject/controller/PromotionController.java b/src/main/java/com/example/myproject/controller/PromotionController.java
new file mode 100644
index 0000000..ab6efe7
--- /dev/null
+++ b/src/main/java/com/example/myproject/controller/PromotionController.java
@@ -0,0 +1,141 @@
+package com.example.myproject.controller;
+
+import cn.dev33.satoken.annotation.SaCheckLogin;
+import cn.dev33.satoken.stp.StpUtil;
+import com.example.myproject.common.base.Result;
+import com.example.myproject.dto.PromotionCreateDTO;
+import com.example.myproject.dto.TorrentUpdateDTO;
+import com.example.myproject.entity.Promotion;
+import com.example.myproject.service.PromotionService;
+import com.example.myproject.service.TorrentService;
+import com.example.myproject.service.UserService;
+import com.example.myproject.repository.UserRepository;
+import io.swagger.v3.oas.annotations.Operation;
+import lombok.RequiredArgsConstructor;
+import lombok.extern.slf4j.Slf4j;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.validation.annotation.Validated;
+import org.springframework.web.bind.annotation.*;
+
+import java.util.List;
+@RestController
+@RequestMapping("/seeds")
+@Slf4j
+@RequiredArgsConstructor
+public class PromotionController {
+    @Autowired
+    private TorrentService torrentService;
+    @Autowired
+    private UserRepository userRepository;
+
+
+    @Autowired
+    private PromotionService promotionService;
+
+    @Autowired
+    private UserService userService;
+    @SaCheckLogin
+    @Operation(summary = "删除种子")
+    @DeleteMapping("/{torrentId}")
+    public Result deleteTorrent(@PathVariable Long torrentId) {
+        try {
+            //  验证用户权限
+            Long userId = StpUtil.getLoginIdAsLong();
+            if (!torrentService.canUserDeleteTorrent(torrentId, userId)) {
+                return Result.error("没有权限删除此种子");
+            }
+
+            torrentService.deleteTorrent(torrentId);
+            return Result.ok();
+        } catch (Exception e) {
+            return Result.error("删除失败: " + e.getMessage());
+        }
+    }
+
+    @SaCheckLogin
+    @Operation(summary = "修改种子信息")
+    @PutMapping("/{torrentId}")
+    public Result updateTorrent(
+            @PathVariable Long torrentId,
+            @RequestBody @Validated TorrentUpdateDTO updateDTO) {
+        try {
+            // 验证用户权限
+            Long userId = StpUtil.getLoginIdAsLong();
+            if (!torrentService.canUserUpdateTorrent(torrentId, userId)) {
+                return Result.error("没有权限修改此种子");
+            }
+
+            torrentService.updateTorrent(torrentId, updateDTO);
+            return Result.ok();
+        } catch (Exception e) {
+            return Result.error("更新失败: " + e.getMessage());
+        }
+    }
+
+    @SaCheckLogin
+    @Operation(summary = "创建促销活动")
+    @PostMapping("/promotions")
+    public Result createPromotion(@RequestBody @Validated PromotionCreateDTO promotionDTO) {
+        try {
+            // 验证用户权限(只有管理员可以创建促销)
+//            if (!StpUtil.hasRole("admin")) {
+//                return Result.error("没有权限创建促销活动");
+//            }
+//
+            Promotion promotion = promotionService.createPromotion(promotionDTO);
+            return Result.ok(promotion);
+        } catch (Exception e) {
+            return Result.error("创建促销失败: " + e.getMessage());
+        }
+    }
+
+    @SaCheckLogin
+    @Operation(summary = "获取促销活动列表")
+    @GetMapping("/promotions")
+    public Result getPromotions() {
+        try {
+            List<Promotion> promotions = promotionService.getAllActivePromotions();
+            return Result.ok(promotions);
+        } catch (Exception e) {
+            return Result.error("获取促销列表失败: " + e.getMessage());
+        }
+    }
+
+    @SaCheckLogin
+    @Operation(summary = "获取促销详情")
+    @GetMapping("/promotions/{promotionId}")
+    public Result getPromotionDetails(@PathVariable Long promotionId) {
+        try {
+            Promotion promotion = promotionService.getPromotionById(promotionId);
+            if (promotion == null) {
+                return Result.error("促销活动不存在");
+            }
+            return Result.ok(promotion);
+        } catch (Exception e) {
+            return Result.error("获取促销详情失败: " + e.getMessage());
+        }
+    }
+
+    @SaCheckLogin
+    @Operation(summary = "删除促销活动")
+    @DeleteMapping("/promotions/{promotionId}")
+    public Result deletePromotion(@PathVariable Long promotionId) {
+        try {
+            // 验证用户权限(只有管理员可以删除促销)
+//            if (!StpUtil.hasRole("admin")) {
+//                return Result.error("没有权限删除促销活动");
+//            }
+            Long userId = StpUtil.getLoginIdAsLong();
+            // 查询用户
+            var userOpt = userRepository.findById(userId);
+            if (userOpt.isEmpty() || !"admin".equals(userOpt.get().getRole())) {
+                return Result.error("没有权限删除促销活动");
+            }
+
+            promotionService.deletePromotion(promotionId);
+            return Result.ok();
+        } catch (Exception e) {
+            return Result.error("删除促销失败: " + e.getMessage());
+        }
+    }
+}
diff --git a/src/main/java/com/example/myproject/controller/TorrentController.java b/src/main/java/com/example/myproject/controller/TorrentController.java
index aecfc64..846e5da 100644
--- a/src/main/java/com/example/myproject/controller/TorrentController.java
+++ b/src/main/java/com/example/myproject/controller/TorrentController.java
@@ -325,6 +325,7 @@
 import com.example.myproject.dto.TrackerProtocol;
 import com.example.myproject.entity.TorrentEntity;
 import com.example.myproject.entity.TorrentReport;
+import com.example.myproject.mapper.UserMapper;
 import com.example.myproject.repository.TorrentReportRepository;
 import com.example.myproject.service.TorrentService;
 import com.example.myproject.service.PromotionService;
@@ -335,6 +336,7 @@
 import com.example.myproject.dto.TorrentUpdateDTO;
 import com.example.myproject.dto.PromotionCreateDTO;
 import com.example.myproject.entity.Promotion;
+
 import com.example.myproject.service.UserService;
 
 import com.turn.ttorrent.bcodec.BEValue;
@@ -378,6 +380,9 @@
 
     @Autowired
     private PromotionService promotionService;
+    @Autowired
+    private UserMapper userMapper;
+
 
     @Autowired
     private UserService userService;
@@ -529,107 +534,23 @@
             return Result.error("失败: ");
         }
     }
-
-    @SaCheckLogin
-    @Operation(summary = "删除种子")
-    @DeleteMapping("/{torrentId}")
-    public Result deleteTorrent(@PathVariable Long torrentId) {
+    /**
+     * 查看个人收藏
+     */
+    @GetMapping("/my-favorite")
+    public Result getMyFavorite(
+            @RequestParam("user_id") Long  userId) {
         try {
-           //  验证用户权限
-            Long userId = StpUtil.getLoginIdAsLong();
-            if (!torrentService.canUserDeleteTorrent(torrentId, userId)) {
-                return Result.error("没有权限删除此种子");
-            }
-
-            torrentService.deleteTorrent(torrentId);
-            return Result.ok();
+            return  torrentService.getMyfavorite(userId);
         } catch (Exception e) {
-            return Result.error("删除失败: " + e.getMessage());
+            return Result.error("失败: ");
         }
     }
 
-    @SaCheckLogin
-    @Operation(summary = "修改种子信息")
-    @PutMapping("/{torrentId}")
-    public Result updateTorrent(
-            @PathVariable Long torrentId,
-            @RequestBody @Validated TorrentUpdateDTO updateDTO) {
-        try {
-            // 验证用户权限
-            Long userId = StpUtil.getLoginIdAsLong();
-            if (!torrentService.canUserUpdateTorrent(torrentId, userId)) {
-                return Result.error("没有权限修改此种子");
-            }
 
-            torrentService.updateTorrent(torrentId, updateDTO);
-            return Result.ok();
-        } catch (Exception e) {
-            return Result.error("更新失败: " + e.getMessage());
-        }
-    }
-
-    @SaCheckLogin
-    @Operation(summary = "创建促销活动")
-    @PostMapping("/promotions")
-    public Result createPromotion(@RequestBody @Validated PromotionCreateDTO promotionDTO) {
-        try {
-            // 验证用户权限(只有管理员可以创建促销)
-//            if (!StpUtil.hasRole("admin")) {
-//                return Result.error("没有权限创建促销活动");
-//            }
-//
-            Promotion promotion = promotionService.createPromotion(promotionDTO);
-            return Result.ok(promotion);
-        } catch (Exception e) {
-            return Result.error("创建促销失败: " + e.getMessage());
-        }
-    }
-
-    @SaCheckLogin
-    @Operation(summary = "获取促销活动列表")
-    @GetMapping("/promotions")
-    public Result getPromotions() {
-        try {
-            List<Promotion> promotions = promotionService.getAllActivePromotions();
-            return Result.ok(promotions);
-        } catch (Exception e) {
-            return Result.error("获取促销列表失败: " + e.getMessage());
-        }
-    }
-
-    @SaCheckLogin
-    @Operation(summary = "获取促销详情")
-    @GetMapping("/promotions/{promotionId}")
-    public Result getPromotionDetails(@PathVariable Long promotionId) {
-        try {
-            Promotion promotion = promotionService.getPromotionById(promotionId);
-            if (promotion == null) {
-                return Result.error("促销活动不存在");
-            }
-            return Result.ok(promotion);
-        } catch (Exception e) {
-            return Result.error("获取促销详情失败: " + e.getMessage());
-        }
-    }
-
-    @SaCheckLogin
-    @Operation(summary = "删除促销活动")
-    @DeleteMapping("/promotions/{promotionId}")
-    public Result deletePromotion(@PathVariable Long promotionId) {
-        try {
-            // 验证用户权限(只有管理员可以删除促销)
-            if (!StpUtil.hasRole("admin")) {
-                return Result.error("没有权限删除促销活动");
-            }
-
-            promotionService.deletePromotion(promotionId);
-            return Result.ok();
-        } catch (Exception e) {
-            return Result.error("删除促销失败: " + e.getMessage());
-        }
-    }
     @GetMapping("/announce")
     public ResponseEntity<byte[]> announce(TrackerProtocol trackerProtocol, HttpServletRequest request, @RequestParam(value = "info_hash") String encodedInfoHash){
+
         HashMap<String, BEValue> map = new HashMap<>();
         if (StrUtil.isBlank(trackerProtocol.getIp())) {
             trackerProtocol.setIp(request.getRemoteAddr());
@@ -641,13 +562,25 @@
 
             trackerProtocol.setInfo_hash(infoHash);
             TorrentEntity torrent = torrentService.selectByInfoHash(infoHash);
+            Long torrentId = torrent.getId();
+            String peerId = trackerProtocol.getPeer_id();
+
             if (torrent == null) {
                 throw new RuntimeException("种子不存在");
             }
             Integer userId = trackerProtocol.getUserId();
+            double uploaded = trackerProtocol.getUploaded();
+            double downloaded = trackerProtocol.getDownloaded();
+            /**
+             *更新种子上传量,下载量, 更新用户上传量下载量
+             * 为了总量检测作弊,种子的上传量和下载量是真实的
+             * 用户的上传下载量是“促销”后的
+             * 作弊检测——下载速度检测
+             */
+            torrentService.processUploadDownload(Long.valueOf(userId),peerId, infoHash,torrentId,uploaded,downloaded);
 
             TorrentReport report = TorrentReport.builder()
-                    .userId(userId )
+                    .userId(userId)
                     .torrentId(torrent.getId())
                     .peerId(trackerProtocol.getPeer_id())
                     .infoHash(infoHash)