600 lines
21 KiB
Java
600 lines
21 KiB
Java
package com.org.flashsalesystem.controller;
|
|
|
|
import com.org.flashsalesystem.service.AdminService;
|
|
import com.org.flashsalesystem.service.FileUploadService;
|
|
import com.org.flashsalesystem.service.OrderMigrationService;
|
|
import io.swagger.v3.oas.annotations.Operation;
|
|
import io.swagger.v3.oas.annotations.tags.Tag;
|
|
import lombok.extern.slf4j.Slf4j;
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
import org.springframework.http.ResponseEntity;
|
|
import org.springframework.web.bind.annotation.*;
|
|
import org.springframework.web.multipart.MultipartFile;
|
|
|
|
import java.util.HashMap;
|
|
import java.util.Map;
|
|
|
|
/**
|
|
* 管理后台API控制器
|
|
*/
|
|
@Tag(name = "管理后台API", description = "管理后台相关接口")
|
|
@RestController
|
|
@RequestMapping("/api/admin")
|
|
@Slf4j
|
|
public class AdminController {
|
|
|
|
@Autowired
|
|
private AdminService adminService;
|
|
|
|
@Autowired
|
|
private FileUploadService fileUploadService;
|
|
|
|
@Autowired
|
|
private OrderMigrationService orderMigrationService;
|
|
|
|
/**
|
|
* 获取仪表盘统计数据
|
|
*/
|
|
@Operation(summary = "获取仪表盘统计数据")
|
|
@GetMapping("/dashboard/stats")
|
|
public ResponseEntity<Map<String, Object>> getDashboardStats() {
|
|
try {
|
|
Map<String, Object> stats = adminService.getDashboardStats();
|
|
|
|
Map<String, Object> response = new HashMap<>();
|
|
response.put("success", true);
|
|
response.put("message", "获取统计数据成功");
|
|
response.put("data", stats);
|
|
|
|
return ResponseEntity.ok(response);
|
|
} catch (Exception e) {
|
|
log.error("获取仪表盘统计数据失败", e);
|
|
|
|
Map<String, Object> response = new HashMap<>();
|
|
response.put("success", false);
|
|
response.put("message", "获取统计数据失败");
|
|
|
|
return ResponseEntity.badRequest().body(response);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 获取用户统计数据
|
|
*/
|
|
@Operation(summary = "获取用户统计数据")
|
|
@GetMapping("/users/stats")
|
|
public ResponseEntity<Map<String, Object>> getUserStats() {
|
|
try {
|
|
Map<String, Object> stats = adminService.getUserStats();
|
|
|
|
Map<String, Object> response = new HashMap<>();
|
|
response.put("success", true);
|
|
response.put("message", "获取用户统计数据成功");
|
|
response.put("data", stats);
|
|
|
|
return ResponseEntity.ok(response);
|
|
} catch (Exception e) {
|
|
log.error("获取用户统计数据失败", e);
|
|
|
|
Map<String, Object> response = new HashMap<>();
|
|
response.put("success", false);
|
|
response.put("message", "获取用户统计数据失败");
|
|
|
|
return ResponseEntity.badRequest().body(response);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 获取订单统计数据
|
|
*/
|
|
@Operation(summary = "获取订单统计数据")
|
|
@GetMapping("/orders/stats")
|
|
public ResponseEntity<Map<String, Object>> getOrderStats() {
|
|
try {
|
|
Map<String, Object> stats = adminService.getOrderStats();
|
|
|
|
Map<String, Object> response = new HashMap<>();
|
|
response.put("success", true);
|
|
response.put("message", "获取订单统计数据成功");
|
|
response.put("data", stats);
|
|
|
|
return ResponseEntity.ok(response);
|
|
} catch (Exception e) {
|
|
log.error("获取订单统计数据失败", e);
|
|
|
|
Map<String, Object> response = new HashMap<>();
|
|
response.put("success", false);
|
|
response.put("message", "获取订单统计数据失败");
|
|
|
|
return ResponseEntity.badRequest().body(response);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 获取商品统计数据
|
|
*/
|
|
@Operation(summary = "获取商品统计数据")
|
|
@GetMapping("/products/stats")
|
|
public ResponseEntity<Map<String, Object>> getProductStats() {
|
|
try {
|
|
Map<String, Object> stats = adminService.getProductStats();
|
|
|
|
Map<String, Object> response = new HashMap<>();
|
|
response.put("success", true);
|
|
response.put("message", "获取商品统计数据成功");
|
|
response.put("data", stats);
|
|
|
|
return ResponseEntity.ok(response);
|
|
} catch (Exception e) {
|
|
log.error("获取商品统计数据失败", e);
|
|
|
|
Map<String, Object> response = new HashMap<>();
|
|
response.put("success", false);
|
|
response.put("message", "获取商品统计数据失败");
|
|
|
|
return ResponseEntity.badRequest().body(response);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 获取限时活动统计数据
|
|
*/
|
|
@Operation(summary = "获取限时活动统计数据")
|
|
@GetMapping("/flashsales/stats")
|
|
public ResponseEntity<Map<String, Object>> getFlashSaleStats() {
|
|
try {
|
|
Map<String, Object> stats = adminService.getFlashSaleStats();
|
|
|
|
Map<String, Object> response = new HashMap<>();
|
|
response.put("success", true);
|
|
response.put("message", "获取限时活动统计数据成功");
|
|
response.put("data", stats);
|
|
|
|
return ResponseEntity.ok(response);
|
|
} catch (Exception e) {
|
|
log.error("获取限时活动统计数据失败", e);
|
|
|
|
Map<String, Object> response = new HashMap<>();
|
|
response.put("success", false);
|
|
response.put("message", "获取限时活动统计数据失败");
|
|
|
|
return ResponseEntity.badRequest().body(response);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 获取最近订单列表
|
|
*/
|
|
@Operation(summary = "获取最近订单列表")
|
|
@GetMapping("/orders/recent")
|
|
public ResponseEntity<Map<String, Object>> getRecentOrders(@RequestParam(defaultValue = "10") int limit) {
|
|
try {
|
|
Object orders = adminService.getRecentOrders(limit);
|
|
|
|
Map<String, Object> response = new HashMap<>();
|
|
response.put("success", true);
|
|
response.put("message", "获取最近订单成功");
|
|
response.put("data", orders);
|
|
|
|
return ResponseEntity.ok(response);
|
|
} catch (Exception e) {
|
|
log.error("获取最近订单失败", e);
|
|
|
|
Map<String, Object> response = new HashMap<>();
|
|
response.put("success", false);
|
|
response.put("message", "获取最近订单失败");
|
|
|
|
return ResponseEntity.badRequest().body(response);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 获取热门商品列表
|
|
*/
|
|
@Operation(summary = "获取热门商品列表")
|
|
@GetMapping("/products/hot")
|
|
public ResponseEntity<Map<String, Object>> getHotProducts(@RequestParam(defaultValue = "5") int limit) {
|
|
try {
|
|
Object products = adminService.getHotProducts(limit);
|
|
|
|
Map<String, Object> response = new HashMap<>();
|
|
response.put("success", true);
|
|
response.put("message", "获取热门商品成功");
|
|
response.put("data", products);
|
|
|
|
return ResponseEntity.ok(response);
|
|
} catch (Exception e) {
|
|
log.error("获取热门商品失败", e);
|
|
|
|
Map<String, Object> response = new HashMap<>();
|
|
response.put("success", false);
|
|
response.put("message", "获取热门商品失败");
|
|
|
|
return ResponseEntity.badRequest().body(response);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 获取用户列表
|
|
*/
|
|
@Operation(summary = "获取用户列表")
|
|
@GetMapping("/users")
|
|
public ResponseEntity<Map<String, Object>> getUsers(
|
|
@RequestParam(defaultValue = "1") int page,
|
|
@RequestParam(defaultValue = "10") int size,
|
|
@RequestParam(required = false) String keyword,
|
|
@RequestParam(required = false) Integer status) {
|
|
try {
|
|
Object users = adminService.getUsers(page, size, keyword, status);
|
|
|
|
Map<String, Object> response = new HashMap<>();
|
|
response.put("success", true);
|
|
response.put("message", "获取用户列表成功");
|
|
response.put("data", users);
|
|
|
|
return ResponseEntity.ok(response);
|
|
} catch (Exception e) {
|
|
log.error("获取用户列表失败", e);
|
|
|
|
Map<String, Object> response = new HashMap<>();
|
|
response.put("success", false);
|
|
response.put("message", "获取用户列表失败");
|
|
|
|
return ResponseEntity.badRequest().body(response);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 删除普通用户
|
|
*/
|
|
@Operation(summary = "删除普通用户")
|
|
@DeleteMapping("/users/{id}")
|
|
public ResponseEntity<Map<String, Object>> deleteUser(@PathVariable Long id) {
|
|
try {
|
|
adminService.deleteUser(id);
|
|
|
|
Map<String, Object> response = new HashMap<>();
|
|
response.put("success", true);
|
|
response.put("message", "用户删除成功");
|
|
|
|
return ResponseEntity.ok(response);
|
|
} catch (Exception e) {
|
|
log.error("删除用户失败", e);
|
|
|
|
Map<String, Object> response = new HashMap<>();
|
|
response.put("success", false);
|
|
response.put("message", "删除用户失败: " + e.getMessage());
|
|
|
|
return ResponseEntity.badRequest().body(response);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 获取订单列表
|
|
*/
|
|
@Operation(summary = "获取订单列表")
|
|
@GetMapping("/orders")
|
|
public ResponseEntity<Map<String, Object>> getOrders(
|
|
@RequestParam(defaultValue = "1") int page,
|
|
@RequestParam(defaultValue = "10") int size,
|
|
@RequestParam(required = false) String keyword,
|
|
@RequestParam(required = false) String status) {
|
|
try {
|
|
Object orders = adminService.getOrders(page, size, keyword, status);
|
|
|
|
Map<String, Object> response = new HashMap<>();
|
|
response.put("success", true);
|
|
response.put("message", "获取订单列表成功");
|
|
response.put("data", orders);
|
|
|
|
return ResponseEntity.ok(response);
|
|
} catch (Exception e) {
|
|
log.error("获取订单列表失败", e);
|
|
|
|
Map<String, Object> response = new HashMap<>();
|
|
response.put("success", false);
|
|
response.put("message", "获取订单列表失败");
|
|
|
|
return ResponseEntity.badRequest().body(response);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 获取商品列表
|
|
*/
|
|
@Operation(summary = "获取商品列表")
|
|
@GetMapping("/products")
|
|
public ResponseEntity<Map<String, Object>> getProducts(
|
|
@RequestParam(defaultValue = "1") int page,
|
|
@RequestParam(defaultValue = "10") int size,
|
|
@RequestParam(required = false) String keyword,
|
|
@RequestParam(required = false) String category,
|
|
@RequestParam(required = false) Integer status) {
|
|
try {
|
|
Object products = adminService.getProducts(page, size, keyword, category, status);
|
|
|
|
Map<String, Object> response = new HashMap<>();
|
|
response.put("success", true);
|
|
response.put("message", "获取商品列表成功");
|
|
response.put("data", products);
|
|
|
|
return ResponseEntity.ok(response);
|
|
} catch (Exception e) {
|
|
log.error("获取商品列表失败", e);
|
|
|
|
Map<String, Object> response = new HashMap<>();
|
|
response.put("success", false);
|
|
response.put("message", "获取商品列表失败");
|
|
|
|
return ResponseEntity.badRequest().body(response);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 获取系统状态
|
|
*/
|
|
@Operation(summary = "获取系统状态")
|
|
@GetMapping("/monitor/system")
|
|
public ResponseEntity<Map<String, Object>> getSystemStatus() {
|
|
try {
|
|
Object systemStatus = adminService.getSystemStatus();
|
|
|
|
Map<String, Object> response = new HashMap<>();
|
|
response.put("success", true);
|
|
response.put("message", "获取系统状态成功");
|
|
response.put("data", systemStatus);
|
|
|
|
return ResponseEntity.ok(response);
|
|
} catch (Exception e) {
|
|
log.error("获取系统状态失败", e);
|
|
|
|
Map<String, Object> response = new HashMap<>();
|
|
response.put("success", false);
|
|
response.put("message", "获取系统状态失败");
|
|
|
|
return ResponseEntity.badRequest().body(response);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 获取Redis状态
|
|
*/
|
|
@Operation(summary = "获取Redis状态")
|
|
@GetMapping("/monitor/redis")
|
|
public ResponseEntity<Map<String, Object>> getRedisStatus() {
|
|
try {
|
|
Object redisStatus = adminService.getRedisStatus();
|
|
|
|
Map<String, Object> response = new HashMap<>();
|
|
response.put("success", true);
|
|
response.put("message", "获取Redis状态成功");
|
|
response.put("data", redisStatus);
|
|
|
|
return ResponseEntity.ok(response);
|
|
} catch (Exception e) {
|
|
log.error("获取Redis状态失败", e);
|
|
|
|
Map<String, Object> response = new HashMap<>();
|
|
response.put("success", false);
|
|
response.put("message", "获取Redis状态失败");
|
|
|
|
return ResponseEntity.badRequest().body(response);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 获取单个商品详情
|
|
*/
|
|
@Operation(summary = "获取商品详情")
|
|
@GetMapping("/products/{id}")
|
|
public ResponseEntity<Map<String, Object>> getProduct(@PathVariable Long id) {
|
|
try {
|
|
Object product = adminService.getProduct(id);
|
|
|
|
Map<String, Object> response = new HashMap<>();
|
|
response.put("success", true);
|
|
response.put("message", "获取商品详情成功");
|
|
response.put("data", product);
|
|
|
|
return ResponseEntity.ok(response);
|
|
} catch (Exception e) {
|
|
log.error("获取商品详情失败", e);
|
|
|
|
Map<String, Object> response = new HashMap<>();
|
|
response.put("success", false);
|
|
response.put("message", "获取商品详情失败");
|
|
|
|
return ResponseEntity.badRequest().body(response);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 更新商品
|
|
*/
|
|
@Operation(summary = "更新商品")
|
|
@PutMapping("/products/{id}")
|
|
public ResponseEntity<Map<String, Object>> updateProduct(@PathVariable Long id,
|
|
@RequestBody Map<String, Object> productData) {
|
|
try {
|
|
adminService.updateProduct(id, productData);
|
|
|
|
Map<String, Object> response = new HashMap<>();
|
|
response.put("success", true);
|
|
response.put("message", "商品更新成功");
|
|
|
|
return ResponseEntity.ok(response);
|
|
} catch (Exception e) {
|
|
log.error("更新商品失败", e);
|
|
|
|
Map<String, Object> response = new HashMap<>();
|
|
response.put("success", false);
|
|
response.put("message", "更新商品失败: " + e.getMessage());
|
|
|
|
return ResponseEntity.badRequest().body(response);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 删除商品
|
|
*/
|
|
@Operation(summary = "删除商品")
|
|
@DeleteMapping("/products/{id}")
|
|
public ResponseEntity<Map<String, Object>> deleteProduct(@PathVariable Long id) {
|
|
try {
|
|
adminService.deleteProduct(id);
|
|
|
|
Map<String, Object> response = new HashMap<>();
|
|
response.put("success", true);
|
|
response.put("message", "商品删除成功");
|
|
|
|
return ResponseEntity.ok(response);
|
|
} catch (Exception e) {
|
|
log.error("删除商品失败", e);
|
|
|
|
Map<String, Object> response = new HashMap<>();
|
|
response.put("success", false);
|
|
response.put("message", "删除商品失败: " + e.getMessage());
|
|
|
|
return ResponseEntity.badRequest().body(response);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 添加商品
|
|
*/
|
|
@Operation(summary = "添加商品")
|
|
@PostMapping("/products")
|
|
public ResponseEntity<Map<String, Object>> addProduct(@RequestBody Map<String, Object> productData) {
|
|
try {
|
|
Object product = adminService.addProduct(productData);
|
|
|
|
Map<String, Object> response = new HashMap<>();
|
|
response.put("success", true);
|
|
response.put("message", "商品添加成功");
|
|
response.put("data", product);
|
|
|
|
return ResponseEntity.ok(response);
|
|
} catch (Exception e) {
|
|
log.error("添加商品失败", e);
|
|
|
|
Map<String, Object> response = new HashMap<>();
|
|
response.put("success", false);
|
|
response.put("message", "添加商品失败: " + e.getMessage());
|
|
|
|
return ResponseEntity.badRequest().body(response);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 上传商品图片
|
|
*/
|
|
@Operation(summary = "上传商品图片")
|
|
@PostMapping("/products/upload-image")
|
|
public ResponseEntity<Map<String, Object>> uploadProductImage(@RequestParam("file") MultipartFile file) {
|
|
try {
|
|
String imageUrl = fileUploadService.uploadProductImage(file);
|
|
|
|
Map<String, Object> response = new HashMap<>();
|
|
response.put("success", true);
|
|
response.put("message", "图片上传成功");
|
|
response.put("imageUrl", imageUrl);
|
|
|
|
return ResponseEntity.ok(response);
|
|
} catch (IllegalArgumentException e) {
|
|
log.error("图片上传失败 - 参数错误", e);
|
|
|
|
Map<String, Object> response = new HashMap<>();
|
|
response.put("success", false);
|
|
response.put("message", e.getMessage());
|
|
|
|
return ResponseEntity.badRequest().body(response);
|
|
} catch (Exception e) {
|
|
log.error("图片上传失败", e);
|
|
|
|
Map<String, Object> response = new HashMap<>();
|
|
response.put("success", false);
|
|
response.put("message", "图片上传失败: " + e.getMessage());
|
|
|
|
return ResponseEntity.badRequest().body(response);
|
|
}
|
|
}
|
|
|
|
@GetMapping("/reviews/stats")
|
|
public ResponseEntity<Map<String, Object>> getReviewStats() {
|
|
Map<String, Object> response = new HashMap<>();
|
|
response.put("success", true);
|
|
response.put("message", "获取评价统计成功");
|
|
response.put("data", adminService.getReviewStats());
|
|
return ResponseEntity.ok(response);
|
|
}
|
|
|
|
@GetMapping("/favorites/stats")
|
|
public ResponseEntity<Map<String, Object>> getFavoriteStats() {
|
|
Map<String, Object> response = new HashMap<>();
|
|
response.put("success", true);
|
|
response.put("message", "获取收藏统计成功");
|
|
response.put("data", adminService.getFavoriteStats());
|
|
return ResponseEntity.ok(response);
|
|
}
|
|
|
|
@GetMapping("/reviews")
|
|
public ResponseEntity<Map<String, Object>> getReviews(@RequestParam(defaultValue = "1") int page,
|
|
@RequestParam(defaultValue = "10") int size,
|
|
@RequestParam(required = false) String keyword) {
|
|
Map<String, Object> response = new HashMap<>();
|
|
response.put("success", true);
|
|
response.put("message", "获取评价列表成功");
|
|
response.put("data", adminService.getReviews(page, size, keyword));
|
|
return ResponseEntity.ok(response);
|
|
}
|
|
|
|
@PutMapping("/reviews/{id}")
|
|
public ResponseEntity<Map<String, Object>> updateReview(@PathVariable Long id,
|
|
@RequestBody com.org.flashsalesystem.dto.ProductReviewDTO.UpdateDTO updateDTO) {
|
|
Map<String, Object> response = new HashMap<>();
|
|
response.put("success", true);
|
|
response.put("message", "更新评价成功");
|
|
response.put("data", adminService.updateReview(id, updateDTO));
|
|
return ResponseEntity.ok(response);
|
|
}
|
|
|
|
@DeleteMapping("/reviews/{id}")
|
|
public ResponseEntity<Map<String, Object>> deleteReview(@PathVariable Long id) {
|
|
adminService.deleteReview(id);
|
|
Map<String, Object> response = new HashMap<>();
|
|
response.put("success", true);
|
|
response.put("message", "删除评价成功");
|
|
return ResponseEntity.ok(response);
|
|
}
|
|
|
|
@GetMapping("/favorites")
|
|
public ResponseEntity<Map<String, Object>> getFavorites(@RequestParam(defaultValue = "1") int page,
|
|
@RequestParam(defaultValue = "10") int size,
|
|
@RequestParam(required = false) String keyword) {
|
|
Map<String, Object> response = new HashMap<>();
|
|
response.put("success", true);
|
|
response.put("message", "获取收藏列表成功");
|
|
response.put("data", adminService.getFavorites(page, size, keyword));
|
|
return ResponseEntity.ok(response);
|
|
}
|
|
|
|
@DeleteMapping("/favorites/{id}")
|
|
public ResponseEntity<Map<String, Object>> deleteFavorite(@PathVariable Long id) {
|
|
adminService.deleteFavorite(id);
|
|
Map<String, Object> response = new HashMap<>();
|
|
response.put("success", true);
|
|
response.put("message", "删除收藏成功");
|
|
return ResponseEntity.ok(response);
|
|
}
|
|
|
|
@PostMapping("/orders/migrate-items")
|
|
public ResponseEntity<Map<String, Object>> migrateLegacyOrderItems() {
|
|
Map<String, Object> response = new HashMap<>();
|
|
response.put("success", true);
|
|
response.put("message", "历史订单明细迁移完成");
|
|
response.put("data", orderMigrationService.migrateLegacyOrderItems());
|
|
return ResponseEntity.ok(response);
|
|
}
|
|
|
|
}
|