Files
FlashSaleSystem/src/main/java/com/org/flashsalesystem/controller/ApiController.java
2026-05-22 21:37:51 +08:00

233 lines
8.7 KiB
Java

package com.org.flashsalesystem.controller;
import com.org.flashsalesystem.dto.FlashSaleDTO;
import com.org.flashsalesystem.entity.Product;
import com.org.flashsalesystem.repository.ProductRepository;
import com.org.flashsalesystem.service.FlashSaleService;
import com.org.flashsalesystem.service.ProductService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* API控制器 - 为Vue前端提供REST接口
*/
@RestController
@RequestMapping("/api")
@RequiredArgsConstructor
@Slf4j
@Tag(name = "API接口", description = "前端API接口")
public class ApiController {
private final ProductRepository productRepository;
private final ProductService productService;
private final FlashSaleService flashSaleService;
/**
* 获取热门商品
*/
@GetMapping("/products/hot")
@Operation(summary = "获取热门商品")
public ResponseEntity<Map<String, Object>> getHotProducts(
@RequestParam(defaultValue = "8") int limit) {
Map<String, Object> response = new HashMap<>();
try {
// 获取前N个商品作为热门商品
Pageable pageable = PageRequest.of(0, limit, Sort.by(Sort.Direction.DESC, "id"));
Page<Product> productPage = productRepository.findByStatus(1, pageable);
List<Map<String, Object>> products = new ArrayList<>();
for (Product product : productPage.getContent()) {
Map<String, Object> item = new HashMap<>();
item.put("id", product.getId());
item.put("name", product.getName());
item.put("description", product.getDescription());
item.put("price", product.getPrice());
item.put("stock", product.getStock());
item.put("image", product.getImageUrl());
item.put("category", "");
products.add(item);
}
response.put("success", true);
response.put("data", products);
} catch (Exception e) {
log.error("获取热门商品失败", e);
response.put("success", false);
response.put("message", "获取热门商品失败");
}
return ResponseEntity.ok(response);
}
/**
* 获取活跃的限时活动
*/
@GetMapping("/flashsales/active")
@Operation(summary = "获取活跃的限时活动")
public ResponseEntity<Map<String, Object>> getActiveFlashSales() {
Map<String, Object> response = new HashMap<>();
try {
List<FlashSaleDTO> flashSales = flashSaleService.getActiveFlashSales();
// 转换数据格式
List<Map<String, Object>> result = new ArrayList<>();
for (FlashSaleDTO flashSale : flashSales) {
Map<String, Object> item = new HashMap<>();
item.put("id", flashSale.getId());
item.put("productId", flashSale.getProductId());
item.put("productName", flashSale.getProductName());
item.put("productImage", flashSale.getProductImageUrl());
item.put("originalPrice", flashSale.getOriginalPrice());
item.put("flashPrice", flashSale.getFlashPrice());
item.put("flashStock", flashSale.getFlashStock());
item.put("startTime", flashSale.getStartTime());
item.put("endTime", flashSale.getEndTime());
item.put("status", flashSale.getStatus());
result.add(item);
}
response.put("success", true);
response.put("data", result);
} catch (Exception e) {
log.error("获取活跃限时活动失败", e);
response.put("success", false);
response.put("message", "获取活跃限时活动失败");
}
return ResponseEntity.ok(response);
}
/**
* 参与限时活动
*/
@PostMapping("/flashsales/participate")
@Operation(summary = "参与限时活动")
public ResponseEntity<Map<String, Object>> participate(
@RequestBody Map<String, Object> request,
HttpServletRequest httpRequest) {
Map<String, Object> response = new HashMap<>();
try {
// 从session获取用户ID
Long userId = (Long) httpRequest.getSession().getAttribute("userId");
if (userId == null) {
response.put("success", false);
response.put("message", "请先登录");
return ResponseEntity.ok(response);
}
Long flashSaleId = Long.valueOf(request.get("flashSaleId").toString());
Integer quantity = request.containsKey("quantity") ?
Integer.valueOf(request.get("quantity").toString()) : 1;
// 创建参与DTO
FlashSaleDTO.ParticipateDTO participateDTO = new FlashSaleDTO.ParticipateDTO();
participateDTO.setFlashSaleId(flashSaleId);
participateDTO.setQuantity(quantity);
// 调用限时活动服务
FlashSaleDTO.ResultDTO result = flashSaleService.participateFlashSale(userId, participateDTO);
response.put("success", result.getSuccess());
response.put("message", result.getMessage());
if (result.getOrderId() != null) {
response.put("orderId", result.getOrderId());
}
} catch (Exception e) {
log.error("参与限时活动失败", e);
response.put("success", false);
response.put("message", e.getMessage());
}
return ResponseEntity.ok(response);
}
/**
* 获取商品列表
*/
@GetMapping("/products")
@Operation(summary = "获取商品列表")
public ResponseEntity<Map<String, Object>> getProducts(
@RequestParam(defaultValue = "0") int page,
@RequestParam(defaultValue = "12") int size) {
Map<String, Object> response = new HashMap<>();
try {
Pageable pageable = PageRequest.of(page, size, Sort.by(Sort.Direction.DESC, "id"));
Page<Product> productPage = productRepository.findByStatus(1, pageable);
List<Map<String, Object>> products = new ArrayList<>();
for (Product product : productPage.getContent()) {
Map<String, Object> item = new HashMap<>();
item.put("id", product.getId());
item.put("name", product.getName());
item.put("description", product.getDescription());
item.put("price", product.getPrice());
item.put("stock", product.getStock());
item.put("image", product.getImageUrl());
item.put("category", "");
products.add(item);
}
response.put("success", true);
response.put("list", products);
response.put("total", productPage.getTotalElements());
} catch (Exception e) {
log.error("获取商品列表失败", e);
response.put("success", false);
response.put("message", "获取商品列表失败");
}
return ResponseEntity.ok(response);
}
/**
* 获取限时活动列表
*/
@GetMapping("/flashsales")
@Operation(summary = "获取限时活动列表")
public ResponseEntity<Map<String, Object>> getFlashSales() {
Map<String, Object> response = new HashMap<>();
try {
List<FlashSaleDTO> flashSales = flashSaleService.getActiveFlashSales();
response.put("success", true);
response.put("list", flashSales);
response.put("total", flashSales.size());
} catch (Exception e) {
log.error("获取限时活动列表失败", e);
response.put("success", false);
response.put("message", "获取限时活动列表失败");
}
return ResponseEntity.ok(response);
}
}