package com.ycwl.basic.pricing.controller; import com.baomidou.mybatisplus.extension.plugins.pagination.Page; import com.ycwl.basic.common.ApiResponse; import com.ycwl.basic.pricing.dto.req.VoucherUsageHistoryReq; import com.ycwl.basic.pricing.dto.resp.VoucherUsageRecordResp; import com.ycwl.basic.pricing.dto.resp.VoucherUsageStatsResp; import com.ycwl.basic.pricing.service.IVoucherUsageService; import io.swagger.v3.oas.annotations.Operation; import io.swagger.v3.oas.annotations.Parameter; import io.swagger.v3.oas.annotations.tags.Tag; import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; import org.springframework.web.bind.annotation.*; import java.util.List; /** * 券码使用记录管理控制器 */ @Slf4j @RestController @RequestMapping("/api/pricing/voucher/usage") @RequiredArgsConstructor @Tag(name = "券码使用记录管理", description = "券码使用历史和统计API") public class VoucherUsageController { private final IVoucherUsageService voucherUsageService; @PostMapping("/history") @Operation(summary = "分页查询券码使用记录") public ApiResponse> getUsageHistory(@RequestBody VoucherUsageHistoryReq req) { try { Page result = voucherUsageService.getUsageHistory(req); return ApiResponse.success(result); } catch (Exception e) { log.error("查询券码使用记录失败", e); return ApiResponse.fail("查询失败: " + e.getMessage()); } } @GetMapping("/{voucherCode}/records") @Operation(summary = "获取指定券码的使用记录") public ApiResponse> getRecordsByCode( @Parameter(description = "券码") @PathVariable String voucherCode) { try { List records = voucherUsageService.getUsageRecordsByCode(voucherCode); return ApiResponse.success(records); } catch (Exception e) { log.error("查询券码使用记录失败: {}", voucherCode, e); return ApiResponse.fail("查询失败: " + e.getMessage()); } } @GetMapping("/{voucherCode}/stats") @Operation(summary = "获取券码使用统计信息") public ApiResponse getUsageStats( @Parameter(description = "券码") @PathVariable String voucherCode) { try { VoucherUsageStatsResp stats = voucherUsageService.getUsageStats(voucherCode); if (stats == null) { return ApiResponse.fail("券码不存在"); } return ApiResponse.success(stats); } catch (Exception e) { log.error("查询券码统计信息失败: {}", voucherCode, e); return ApiResponse.fail("查询失败: " + e.getMessage()); } } @GetMapping("/user/{faceId}/scenic/{scenicId}") @Operation(summary = "获取用户在指定景区的券码使用记录") public ApiResponse> getUserUsageRecords( @Parameter(description = "用户faceId") @PathVariable Long faceId, @Parameter(description = "景区ID") @PathVariable Long scenicId) { try { List records = voucherUsageService.getUserUsageRecords(faceId, scenicId); return ApiResponse.success(records); } catch (Exception e) { log.error("查询用户券码使用记录失败: faceId={}, scenicId={}", faceId, scenicId, e); return ApiResponse.fail("查询失败: " + e.getMessage()); } } @GetMapping("/batch/{batchId}/stats") @Operation(summary = "获取批次券码使用统计信息") public ApiResponse> getBatchUsageStats( @Parameter(description = "批次ID") @PathVariable Long batchId) { try { List statsList = voucherUsageService.getBatchUsageStats(batchId); return ApiResponse.success(statsList); } catch (Exception e) { log.error("查询批次券码统计信息失败: batchId={}", batchId, e); return ApiResponse.fail("查询失败: " + e.getMessage()); } } }