blob: a102cddd0bfe6677733ee24ec24c1fdb0b1f1857 [file] [log] [blame]
YelinCui09ee07c2025-06-07 05:09:55 +08001package com.example.myproject.controller;
2
3import cn.dev33.satoken.annotation.SaCheckLogin;
4import cn.dev33.satoken.stp.StpUtil;
5import com.example.myproject.common.base.Result;
6import com.example.myproject.dto.PromotionCreateDTO;
7import com.example.myproject.dto.TorrentUpdateDTO;
YelinCuid6e8d432025-06-08 22:10:16 +08008import com.example.myproject.dto.vo.TorrentVO;
YelinCui09ee07c2025-06-07 05:09:55 +08009import com.example.myproject.entity.Promotion;
YelinCuid6e8d432025-06-08 22:10:16 +080010import com.example.myproject.entity.TorrentEntity;
YelinCui09ee07c2025-06-07 05:09:55 +080011import com.example.myproject.service.PromotionService;
12import com.example.myproject.service.TorrentService;
13import com.example.myproject.service.UserService;
14import com.example.myproject.repository.UserRepository;
15import io.swagger.v3.oas.annotations.Operation;
16import lombok.RequiredArgsConstructor;
17import lombok.extern.slf4j.Slf4j;
18import org.springframework.beans.factory.annotation.Autowired;
19import org.springframework.validation.annotation.Validated;
20import org.springframework.web.bind.annotation.*;
21
22import java.util.List;
23@RestController
24@RequestMapping("/seeds")
25@Slf4j
26@RequiredArgsConstructor
27public class PromotionController {
28 @Autowired
29 private TorrentService torrentService;
30 @Autowired
31 private UserRepository userRepository;
32
33
34 @Autowired
35 private PromotionService promotionService;
36
YelinCui09ee07c2025-06-07 05:09:55 +080037
YelinCui09ee07c2025-06-07 05:09:55 +080038
39 @SaCheckLogin
40 @Operation(summary = "创建促销活动")
41 @PostMapping("/promotions")
42 public Result createPromotion(@RequestBody @Validated PromotionCreateDTO promotionDTO) {
43 try {
44 // 验证用户权限(只有管理员可以创建促销)
45// if (!StpUtil.hasRole("admin")) {
46// return Result.error("没有权限创建促销活动");
47// }
48//
49 Promotion promotion = promotionService.createPromotion(promotionDTO);
50 return Result.ok(promotion);
51 } catch (Exception e) {
52 return Result.error("创建促销失败: " + e.getMessage());
53 }
54 }
YelinCuia08f5f02025-06-08 23:03:34 +080055 @SaCheckLogin
56 @Operation(summary = "根据分类创建促销活动")
57 @PostMapping("/promotions/category")
58 public Result createPromotionByCategory(@RequestParam String category, @RequestBody @Validated PromotionCreateDTO promotionDTO) {
59 try {
60 // 校验权限
61// Long userId = StpUtil.getLoginIdAsLong();
62// var userOpt = userRepository.findById(userId);
63// if (userOpt.isEmpty() || !"admin".equals(userOpt.get().getRole())) {
64// return Result.error("没有权限创建促销活动");
65// }
66
67 // 查询该分类下的所有种子
68 List<TorrentEntity> torrents = torrentService.getTorrentsByCategory(category);
69 if (torrents.isEmpty()) {
70 return Result.error("分类【" + category + "】下没有可用的资源");
71 }
72
73 // 提取所有种子的 ID
74 List<Long> ids = torrents.stream().map(TorrentEntity::getId).toList();
75
76 // 设置到 DTO 中
77 promotionDTO.setApplicableTorrentIds(ids);
78
79 // 创建促销
80 Promotion promotion = promotionService.createPromotion(promotionDTO);
81
82 return Result.ok("成功创建促销,适用于分类【" + category + "】的 " + ids.size() + " 个资源");
83 } catch (Exception e) {
84 return Result.error("分类创建促销失败: " + e.getMessage());
85 }
86 }
87
YelinCui09ee07c2025-06-07 05:09:55 +080088
89 @SaCheckLogin
90 @Operation(summary = "获取促销活动列表")
91 @GetMapping("/promotions")
92 public Result getPromotions() {
93 try {
94 List<Promotion> promotions = promotionService.getAllActivePromotions();
95 return Result.ok(promotions);
96 } catch (Exception e) {
97 return Result.error("获取促销列表失败: " + e.getMessage());
98 }
99 }
100
101 @SaCheckLogin
102 @Operation(summary = "获取促销详情")
103 @GetMapping("/promotions/{promotionId}")
104 public Result getPromotionDetails(@PathVariable Long promotionId) {
105 try {
106 Promotion promotion = promotionService.getPromotionById(promotionId);
107 if (promotion == null) {
108 return Result.error("促销活动不存在");
109 }
110 return Result.ok(promotion);
111 } catch (Exception e) {
112 return Result.error("获取促销详情失败: " + e.getMessage());
113 }
114 }
115
116 @SaCheckLogin
117 @Operation(summary = "删除促销活动")
118 @DeleteMapping("/promotions/{promotionId}")
119 public Result deletePromotion(@PathVariable Long promotionId) {
120 try {
121 // 验证用户权限(只有管理员可以删除促销)
122// if (!StpUtil.hasRole("admin")) {
123// return Result.error("没有权限删除促销活动");
124// }
YelinCuiedbf91e2025-06-08 23:56:51 +0800125// Long userId = StpUtil.getLoginIdAsLong();
126// // 查询用户
127// var userOpt = userRepository.findById(userId);
128// if (userOpt.isEmpty() || !"admin".equals(userOpt.get().getRole())) {
129// return Result.error("没有权限删除促销活动");
130// }
YelinCui09ee07c2025-06-07 05:09:55 +0800131
132 promotionService.deletePromotion(promotionId);
133 return Result.ok();
134 } catch (Exception e) {
135 return Result.error("删除促销失败: " + e.getMessage());
136 }
137 }
YelinCuid6e8d432025-06-08 22:10:16 +0800138 @SaCheckLogin
139 @Operation(summary = "查看冷门资源")
140 @GetMapping("/cold")
141 public Result getColdResources() {
142 try {
143 List<TorrentEntity> coldList = torrentService.getColdTorrents(10); // 例如 views < 10
144 return Result.ok(coldList);
145 } catch (Exception e) {
146 return Result.error("获取冷门资源失败: " + e.getMessage());
147 }
148 }
149
150
YelinCui09ee07c2025-06-07 05:09:55 +0800151}