blob: a102cddd0bfe6677733ee24ec24c1fdb0b1f1857 [file] [log] [blame]
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.dto.vo.TorrentVO;
import com.example.myproject.entity.Promotion;
import com.example.myproject.entity.TorrentEntity;
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;
@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 = "根据分类创建促销活动")
@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 = "获取促销活动列表")
@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());
}
}
@SaCheckLogin
@Operation(summary = "查看冷门资源")
@GetMapping("/cold")
public Result getColdResources() {
try {
List<TorrentEntity> coldList = torrentService.getColdTorrents(10); // 例如 views < 10
return Result.ok(coldList);
} catch (Exception e) {
return Result.error("获取冷门资源失败: " + e.getMessage());
}
}
}