添加按类别创建促销

Change-Id: If9a0d78bfd14030782c24a0fa07b66cddea69d1e
diff --git a/.idea/workspace.xml b/.idea/workspace.xml
index a8762d7..0d4874b 100644
--- a/.idea/workspace.xml
+++ b/.idea/workspace.xml
@@ -8,17 +8,18 @@
       <change beforePath="$PROJECT_DIR$/.idea/workspace.xml" beforeDir="false" afterPath="$PROJECT_DIR$/.idea/workspace.xml" afterDir="false" />
       <change beforePath="$PROJECT_DIR$/src/main/java/com/example/myproject/controller/PromotionController.java" beforeDir="false" afterPath="$PROJECT_DIR$/src/main/java/com/example/myproject/controller/PromotionController.java" afterDir="false" />
       <change beforePath="$PROJECT_DIR$/src/main/java/com/example/myproject/controller/TorrentController.java" beforeDir="false" afterPath="$PROJECT_DIR$/src/main/java/com/example/myproject/controller/TorrentController.java" afterDir="false" />
+      <change beforePath="$PROJECT_DIR$/src/main/java/com/example/myproject/dto/PromotionCreateDTO.java" beforeDir="false" afterPath="$PROJECT_DIR$/src/main/java/com/example/myproject/dto/PromotionCreateDTO.java" afterDir="false" />
       <change beforePath="$PROJECT_DIR$/src/main/java/com/example/myproject/mapper/TorrentMapper.java" beforeDir="false" afterPath="$PROJECT_DIR$/src/main/java/com/example/myproject/mapper/TorrentMapper.java" afterDir="false" />
+      <change beforePath="$PROJECT_DIR$/src/main/java/com/example/myproject/service/PromotionService.java" beforeDir="false" afterPath="$PROJECT_DIR$/src/main/java/com/example/myproject/service/PromotionService.java" afterDir="false" />
       <change beforePath="$PROJECT_DIR$/src/main/java/com/example/myproject/service/TorrentService.java" beforeDir="false" afterPath="$PROJECT_DIR$/src/main/java/com/example/myproject/service/TorrentService.java" afterDir="false" />
       <change beforePath="$PROJECT_DIR$/src/main/java/com/example/myproject/service/serviceImpl/PromotionServiceImpl.java" beforeDir="false" afterPath="$PROJECT_DIR$/src/main/java/com/example/myproject/service/serviceImpl/PromotionServiceImpl.java" afterDir="false" />
       <change beforePath="$PROJECT_DIR$/src/main/java/com/example/myproject/service/serviceImpl/TorrentServiceImpl.java" beforeDir="false" afterPath="$PROJECT_DIR$/src/main/java/com/example/myproject/service/serviceImpl/TorrentServiceImpl.java" afterDir="false" />
-      <change beforePath="$PROJECT_DIR$/src/main/resources/mapper/TorrentMapper.xml" beforeDir="false" afterPath="$PROJECT_DIR$/src/main/resources/mapper/TorrentMapper.xml" afterDir="false" />
       <change beforePath="$PROJECT_DIR$/target/classes/com/example/myproject/controller/PromotionController.class" beforeDir="false" afterPath="$PROJECT_DIR$/target/classes/com/example/myproject/controller/PromotionController.class" afterDir="false" />
-      <change beforePath="$PROJECT_DIR$/target/classes/com/example/myproject/controller/TorrentController.class" beforeDir="false" afterPath="$PROJECT_DIR$/target/classes/com/example/myproject/controller/TorrentController.class" afterDir="false" />
+      <change beforePath="$PROJECT_DIR$/target/classes/com/example/myproject/dto/PromotionCreateDTO.class" beforeDir="false" afterPath="$PROJECT_DIR$/target/classes/com/example/myproject/dto/PromotionCreateDTO.class" afterDir="false" />
       <change beforePath="$PROJECT_DIR$/target/classes/com/example/myproject/mapper/TorrentMapper.class" beforeDir="false" afterPath="$PROJECT_DIR$/target/classes/com/example/myproject/mapper/TorrentMapper.class" afterDir="false" />
       <change beforePath="$PROJECT_DIR$/target/classes/com/example/myproject/service/TorrentService.class" beforeDir="false" afterPath="$PROJECT_DIR$/target/classes/com/example/myproject/service/TorrentService.class" afterDir="false" />
+      <change beforePath="$PROJECT_DIR$/target/classes/com/example/myproject/service/serviceImpl/PromotionServiceImpl.class" beforeDir="false" afterPath="$PROJECT_DIR$/target/classes/com/example/myproject/service/serviceImpl/PromotionServiceImpl.class" afterDir="false" />
       <change beforePath="$PROJECT_DIR$/target/classes/com/example/myproject/service/serviceImpl/TorrentServiceImpl.class" beforeDir="false" afterPath="$PROJECT_DIR$/target/classes/com/example/myproject/service/serviceImpl/TorrentServiceImpl.class" afterDir="false" />
-      <change beforePath="$PROJECT_DIR$/target/classes/mapper/TorrentMapper.xml" beforeDir="false" afterPath="$PROJECT_DIR$/target/classes/mapper/TorrentMapper.xml" afterDir="false" />
     </list>
     <option name="SHOW_DIALOG" value="false" />
     <option name="HIGHLIGHT_CONFLICTS" value="true" />
diff --git a/src/main/java/com/example/myproject/controller/PromotionController.java b/src/main/java/com/example/myproject/controller/PromotionController.java
index ed90bf7..145692c 100644
--- a/src/main/java/com/example/myproject/controller/PromotionController.java
+++ b/src/main/java/com/example/myproject/controller/PromotionController.java
@@ -34,45 +34,7 @@
     @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 = "创建促销活动")
@@ -90,6 +52,39 @@
             return Result.error("创建促销失败: " + e.getMessage());
         }
     }
+    @SaCheckLogin
+    @Operation(summary = "根据分类创建促销活动")
+    @PostMapping("/promotions/category")
+    public Result createPromotionByCategory(@RequestParam String category, @RequestBody @Validated PromotionCreateDTO promotionDTO) {
+        try {
+            // 校验权限
+//            Long userId = StpUtil.getLoginIdAsLong();
+//            var userOpt = userRepository.findById(userId);
+//            if (userOpt.isEmpty() || !"admin".equals(userOpt.get().getRole())) {
+//                return Result.error("没有权限创建促销活动");
+//            }
+
+            // 查询该分类下的所有种子
+            List<TorrentEntity> torrents = torrentService.getTorrentsByCategory(category);
+            if (torrents.isEmpty()) {
+                return Result.error("分类【" + category + "】下没有可用的资源");
+            }
+
+            // 提取所有种子的 ID
+            List<Long> ids = torrents.stream().map(TorrentEntity::getId).toList();
+
+            // 设置到 DTO 中
+            promotionDTO.setApplicableTorrentIds(ids);
+
+            // 创建促销
+            Promotion promotion = promotionService.createPromotion(promotionDTO);
+
+            return Result.ok("成功创建促销,适用于分类【" + category + "】的 " + ids.size() + " 个资源");
+        } catch (Exception e) {
+            return Result.error("分类创建促销失败: " + e.getMessage());
+        }
+    }
+
 
     @SaCheckLogin
     @Operation(summary = "获取促销活动列表")
diff --git a/src/main/java/com/example/myproject/controller/TorrentController.java b/src/main/java/com/example/myproject/controller/TorrentController.java
index fd79757..4229fc4 100644
--- a/src/main/java/com/example/myproject/controller/TorrentController.java
+++ b/src/main/java/com/example/myproject/controller/TorrentController.java
@@ -173,6 +173,7 @@
             @PathVariable("seed_id") Long seedId,
             @RequestParam("passkey") String passkey,
             HttpServletResponse response) throws IOException {
+        torrentService.incrementDownloadCount(seedId);
 
         // 获取种子实体
         TorrentEntity entity = torrentService.selectBySeedId(seedId);
@@ -205,6 +206,43 @@
         response.getOutputStream().write(torrentBytes);
         response.getOutputStream().flush();
     }
+    @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 = "删除种子")
+    @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());
+        }
+    }
+
 
     /**
      * 收藏或者取消收藏
diff --git a/src/main/java/com/example/myproject/dto/PromotionCreateDTO.java b/src/main/java/com/example/myproject/dto/PromotionCreateDTO.java
index 211979b..ca710d0 100644
--- a/src/main/java/com/example/myproject/dto/PromotionCreateDTO.java
+++ b/src/main/java/com/example/myproject/dto/PromotionCreateDTO.java
@@ -25,7 +25,7 @@
     @Max(value = 100, message = "折扣比例不能大于100")
     private double discountPercentage;
     
-    @NotEmpty(message = "适用种子列表不能为空")
+
     private List<Long> applicableTorrentIds;
     
     @AssertTrue(message = "结束时间必须晚于开始时间")
diff --git a/src/main/java/com/example/myproject/mapper/TorrentMapper.java b/src/main/java/com/example/myproject/mapper/TorrentMapper.java
index 3419938..28e4dfc 100644
--- a/src/main/java/com/example/myproject/mapper/TorrentMapper.java
+++ b/src/main/java/com/example/myproject/mapper/TorrentMapper.java
@@ -33,6 +33,12 @@
 
     void incrementViews(@Param("id") Long id);
     List<TorrentEntity> selectColdTorrents(@Param("threshold") int threshold);
+    @Select("SELECT * FROM torrent WHERE category = #{category}")
+    List<TorrentEntity> selectByCategory(@Param("category") String category);
+    @Update("UPDATE torrent SET leechers = leechers + 1 WHERE id = #{torrentId}")
+    void incrementDownloadCount(@Param("torrentId") Long torrentId);
+
+
 
 
 
diff --git a/src/main/java/com/example/myproject/service/PromotionService.java b/src/main/java/com/example/myproject/service/PromotionService.java
index b83bce5..2122411 100644
--- a/src/main/java/com/example/myproject/service/PromotionService.java
+++ b/src/main/java/com/example/myproject/service/PromotionService.java
@@ -18,4 +18,5 @@
     double getDownloadDiscount(Long torrentId);
 
 
-} 
\ No newline at end of file
+
+}
\ No newline at end of file
diff --git a/src/main/java/com/example/myproject/service/TorrentService.java b/src/main/java/com/example/myproject/service/TorrentService.java
index 0baee2d..700b8ea 100644
--- a/src/main/java/com/example/myproject/service/TorrentService.java
+++ b/src/main/java/com/example/myproject/service/TorrentService.java
@@ -89,4 +89,8 @@
     //        return torrentMapper.selectColdTorrents(threshold);
     //    }
     List<TorrentEntity> getColdTorrents(int threshold);
+
+    List<TorrentEntity> getTorrentsByCategory(String category);
+
+    void incrementDownloadCount(Long torrentId);
 }
diff --git a/src/main/java/com/example/myproject/service/serviceImpl/PromotionServiceImpl.java b/src/main/java/com/example/myproject/service/serviceImpl/PromotionServiceImpl.java
index caec8e1..dccba96 100644
--- a/src/main/java/com/example/myproject/service/serviceImpl/PromotionServiceImpl.java
+++ b/src/main/java/com/example/myproject/service/serviceImpl/PromotionServiceImpl.java
@@ -3,12 +3,12 @@
 import com.example.myproject.entity.Promotion;
 import com.example.myproject.entity.TorrentEntity;
 import com.example.myproject.mapper.PromotionMapper;
+import com.example.myproject.mapper.TorrentMapper;
 import com.example.myproject.service.PromotionService;
 import com.example.myproject.dto.PromotionCreateDTO;
 
 import com.fasterxml.jackson.core.JsonProcessingException;
 import com.fasterxml.jackson.databind.ObjectMapper;
-import io.swagger.models.auth.In;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Service;
 import org.springframework.transaction.annotation.Transactional;
@@ -23,6 +23,9 @@
     @Autowired
     private PromotionMapper promotionMapper;
 
+    @Autowired
+    private TorrentMapper torrentMapper;
+
     @Override
     @Transactional
     public Promotion createPromotion(PromotionCreateDTO promotionDTO) {
@@ -165,4 +168,5 @@
                 .min()
                 .orElse(0.0);
     }
+
 }
diff --git a/src/main/java/com/example/myproject/service/serviceImpl/TorrentServiceImpl.java b/src/main/java/com/example/myproject/service/serviceImpl/TorrentServiceImpl.java
index 2fb4b4d..cf97746 100644
--- a/src/main/java/com/example/myproject/service/serviceImpl/TorrentServiceImpl.java
+++ b/src/main/java/com/example/myproject/service/serviceImpl/TorrentServiceImpl.java
@@ -606,5 +606,15 @@
     public List<TorrentEntity> getColdTorrents(int threshold) {
         return torrentMapper.selectColdTorrents(threshold);
     }
+    @Override
+    public List<TorrentEntity> getTorrentsByCategory(String category) {
+        return torrentMapper.selectByCategory(category);
+    }
+    @Override
+    public void incrementDownloadCount(Long torrentId) {
+        torrentMapper.incrementDownloadCount(torrentId);
+    }
+
+
 
 }
\ No newline at end of file
diff --git a/target/classes/com/example/myproject/controller/PromotionController.class b/target/classes/com/example/myproject/controller/PromotionController.class
index 2c5370a..2b81885 100644
--- a/target/classes/com/example/myproject/controller/PromotionController.class
+++ b/target/classes/com/example/myproject/controller/PromotionController.class
Binary files differ
diff --git a/target/classes/com/example/myproject/controller/TorrentController.class b/target/classes/com/example/myproject/controller/TorrentController.class
index 4713572..49dbd70 100644
--- a/target/classes/com/example/myproject/controller/TorrentController.class
+++ b/target/classes/com/example/myproject/controller/TorrentController.class
Binary files differ
diff --git a/target/classes/com/example/myproject/dto/PromotionCreateDTO.class b/target/classes/com/example/myproject/dto/PromotionCreateDTO.class
index 3087d39..8dd281b 100644
--- a/target/classes/com/example/myproject/dto/PromotionCreateDTO.class
+++ b/target/classes/com/example/myproject/dto/PromotionCreateDTO.class
Binary files differ
diff --git a/target/classes/com/example/myproject/mapper/TorrentMapper.class b/target/classes/com/example/myproject/mapper/TorrentMapper.class
index 1a9a7c9..b530a48 100644
--- a/target/classes/com/example/myproject/mapper/TorrentMapper.class
+++ b/target/classes/com/example/myproject/mapper/TorrentMapper.class
Binary files differ
diff --git a/target/classes/com/example/myproject/service/TorrentService.class b/target/classes/com/example/myproject/service/TorrentService.class
index 579be31..b4d3e30 100644
--- a/target/classes/com/example/myproject/service/TorrentService.class
+++ b/target/classes/com/example/myproject/service/TorrentService.class
Binary files differ
diff --git a/target/classes/com/example/myproject/service/serviceImpl/PromotionServiceImpl.class b/target/classes/com/example/myproject/service/serviceImpl/PromotionServiceImpl.class
index 83cf11d..353c7df 100644
--- a/target/classes/com/example/myproject/service/serviceImpl/PromotionServiceImpl.class
+++ b/target/classes/com/example/myproject/service/serviceImpl/PromotionServiceImpl.class
Binary files differ
diff --git a/target/classes/com/example/myproject/service/serviceImpl/TorrentServiceImpl.class b/target/classes/com/example/myproject/service/serviceImpl/TorrentServiceImpl.class
index ac29866..4ee1413 100644
--- a/target/classes/com/example/myproject/service/serviceImpl/TorrentServiceImpl.class
+++ b/target/classes/com/example/myproject/service/serviceImpl/TorrentServiceImpl.class
Binary files differ