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> getDashboardStats() { try { Map stats = adminService.getDashboardStats(); Map 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 response = new HashMap<>(); response.put("success", false); response.put("message", "获取统计数据失败"); return ResponseEntity.badRequest().body(response); } } /** * 获取用户统计数据 */ @Operation(summary = "获取用户统计数据") @GetMapping("/users/stats") public ResponseEntity> getUserStats() { try { Map stats = adminService.getUserStats(); Map 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 response = new HashMap<>(); response.put("success", false); response.put("message", "获取用户统计数据失败"); return ResponseEntity.badRequest().body(response); } } /** * 获取订单统计数据 */ @Operation(summary = "获取订单统计数据") @GetMapping("/orders/stats") public ResponseEntity> getOrderStats() { try { Map stats = adminService.getOrderStats(); Map 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 response = new HashMap<>(); response.put("success", false); response.put("message", "获取订单统计数据失败"); return ResponseEntity.badRequest().body(response); } } /** * 获取商品统计数据 */ @Operation(summary = "获取商品统计数据") @GetMapping("/products/stats") public ResponseEntity> getProductStats() { try { Map stats = adminService.getProductStats(); Map 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 response = new HashMap<>(); response.put("success", false); response.put("message", "获取商品统计数据失败"); return ResponseEntity.badRequest().body(response); } } /** * 获取限时活动统计数据 */ @Operation(summary = "获取限时活动统计数据") @GetMapping("/flashsales/stats") public ResponseEntity> getFlashSaleStats() { try { Map stats = adminService.getFlashSaleStats(); Map 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 response = new HashMap<>(); response.put("success", false); response.put("message", "获取限时活动统计数据失败"); return ResponseEntity.badRequest().body(response); } } /** * 获取最近订单列表 */ @Operation(summary = "获取最近订单列表") @GetMapping("/orders/recent") public ResponseEntity> getRecentOrders(@RequestParam(defaultValue = "10") int limit) { try { Object orders = adminService.getRecentOrders(limit); Map 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 response = new HashMap<>(); response.put("success", false); response.put("message", "获取最近订单失败"); return ResponseEntity.badRequest().body(response); } } /** * 获取热门商品列表 */ @Operation(summary = "获取热门商品列表") @GetMapping("/products/hot") public ResponseEntity> getHotProducts(@RequestParam(defaultValue = "5") int limit) { try { Object products = adminService.getHotProducts(limit); Map 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 response = new HashMap<>(); response.put("success", false); response.put("message", "获取热门商品失败"); return ResponseEntity.badRequest().body(response); } } /** * 获取用户列表 */ @Operation(summary = "获取用户列表") @GetMapping("/users") public ResponseEntity> 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 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 response = new HashMap<>(); response.put("success", false); response.put("message", "获取用户列表失败"); return ResponseEntity.badRequest().body(response); } } /** * 删除普通用户 */ @Operation(summary = "删除普通用户") @DeleteMapping("/users/{id}") public ResponseEntity> deleteUser(@PathVariable Long id) { try { adminService.deleteUser(id); Map response = new HashMap<>(); response.put("success", true); response.put("message", "用户删除成功"); return ResponseEntity.ok(response); } catch (Exception e) { log.error("删除用户失败", e); Map response = new HashMap<>(); response.put("success", false); response.put("message", "删除用户失败: " + e.getMessage()); return ResponseEntity.badRequest().body(response); } } /** * 获取订单列表 */ @Operation(summary = "获取订单列表") @GetMapping("/orders") public ResponseEntity> 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 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 response = new HashMap<>(); response.put("success", false); response.put("message", "获取订单列表失败"); return ResponseEntity.badRequest().body(response); } } /** * 获取商品列表 */ @Operation(summary = "获取商品列表") @GetMapping("/products") public ResponseEntity> 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 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 response = new HashMap<>(); response.put("success", false); response.put("message", "获取商品列表失败"); return ResponseEntity.badRequest().body(response); } } /** * 获取系统状态 */ @Operation(summary = "获取系统状态") @GetMapping("/monitor/system") public ResponseEntity> getSystemStatus() { try { Object systemStatus = adminService.getSystemStatus(); Map 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 response = new HashMap<>(); response.put("success", false); response.put("message", "获取系统状态失败"); return ResponseEntity.badRequest().body(response); } } /** * 获取Redis状态 */ @Operation(summary = "获取Redis状态") @GetMapping("/monitor/redis") public ResponseEntity> getRedisStatus() { try { Object redisStatus = adminService.getRedisStatus(); Map 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 response = new HashMap<>(); response.put("success", false); response.put("message", "获取Redis状态失败"); return ResponseEntity.badRequest().body(response); } } /** * 获取单个商品详情 */ @Operation(summary = "获取商品详情") @GetMapping("/products/{id}") public ResponseEntity> getProduct(@PathVariable Long id) { try { Object product = adminService.getProduct(id); Map 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 response = new HashMap<>(); response.put("success", false); response.put("message", "获取商品详情失败"); return ResponseEntity.badRequest().body(response); } } /** * 更新商品 */ @Operation(summary = "更新商品") @PutMapping("/products/{id}") public ResponseEntity> updateProduct(@PathVariable Long id, @RequestBody Map productData) { try { adminService.updateProduct(id, productData); Map response = new HashMap<>(); response.put("success", true); response.put("message", "商品更新成功"); return ResponseEntity.ok(response); } catch (Exception e) { log.error("更新商品失败", e); Map response = new HashMap<>(); response.put("success", false); response.put("message", "更新商品失败: " + e.getMessage()); return ResponseEntity.badRequest().body(response); } } /** * 删除商品 */ @Operation(summary = "删除商品") @DeleteMapping("/products/{id}") public ResponseEntity> deleteProduct(@PathVariable Long id) { try { adminService.deleteProduct(id); Map response = new HashMap<>(); response.put("success", true); response.put("message", "商品删除成功"); return ResponseEntity.ok(response); } catch (Exception e) { log.error("删除商品失败", e); Map response = new HashMap<>(); response.put("success", false); response.put("message", "删除商品失败: " + e.getMessage()); return ResponseEntity.badRequest().body(response); } } /** * 添加商品 */ @Operation(summary = "添加商品") @PostMapping("/products") public ResponseEntity> addProduct(@RequestBody Map productData) { try { Object product = adminService.addProduct(productData); Map 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 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> uploadProductImage(@RequestParam("file") MultipartFile file) { try { String imageUrl = fileUploadService.uploadProductImage(file); Map 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 response = new HashMap<>(); response.put("success", false); response.put("message", e.getMessage()); return ResponseEntity.badRequest().body(response); } catch (Exception e) { log.error("图片上传失败", e); Map response = new HashMap<>(); response.put("success", false); response.put("message", "图片上传失败: " + e.getMessage()); return ResponseEntity.badRequest().body(response); } } @GetMapping("/reviews/stats") public ResponseEntity> getReviewStats() { Map response = new HashMap<>(); response.put("success", true); response.put("message", "获取评价统计成功"); response.put("data", adminService.getReviewStats()); return ResponseEntity.ok(response); } @GetMapping("/favorites/stats") public ResponseEntity> getFavoriteStats() { Map response = new HashMap<>(); response.put("success", true); response.put("message", "获取收藏统计成功"); response.put("data", adminService.getFavoriteStats()); return ResponseEntity.ok(response); } @GetMapping("/reviews") public ResponseEntity> getReviews(@RequestParam(defaultValue = "1") int page, @RequestParam(defaultValue = "10") int size, @RequestParam(required = false) String keyword) { Map 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> updateReview(@PathVariable Long id, @RequestBody com.org.flashsalesystem.dto.ProductReviewDTO.UpdateDTO updateDTO) { Map 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> deleteReview(@PathVariable Long id) { adminService.deleteReview(id); Map response = new HashMap<>(); response.put("success", true); response.put("message", "删除评价成功"); return ResponseEntity.ok(response); } @GetMapping("/favorites") public ResponseEntity> getFavorites(@RequestParam(defaultValue = "1") int page, @RequestParam(defaultValue = "10") int size, @RequestParam(required = false) String keyword) { Map 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> deleteFavorite(@PathVariable Long id) { adminService.deleteFavorite(id); Map response = new HashMap<>(); response.put("success", true); response.put("message", "删除收藏成功"); return ResponseEntity.ok(response); } @PostMapping("/orders/migrate-items") public ResponseEntity> migrateLegacyOrderItems() { Map response = new HashMap<>(); response.put("success", true); response.put("message", "历史订单明细迁移完成"); response.put("data", orderMigrationService.migrateLegacyOrderItems()); return ResponseEntity.ok(response); } }