fix(优惠券中心):用户查询优惠券逻辑优化,直接通过二维码id进行查询

This commit is contained in:
2025-09-01 16:13:56 +08:00
parent 717858fad1
commit d262269862
2 changed files with 33 additions and 6 deletions
@@ -1,6 +1,8 @@
package com.central.coupon.dto;
import lombok.Data;
import javax.validation.constraints.Min;
import javax.validation.constraints.NotNull;
/**
* 订单查询DTO
@@ -11,6 +13,7 @@ public class OrderQueryDTO {
/**
* 二维码ID(来自订单的codeId)
*/
@NotNull(message = "二维码ID不能为空")
private String codeId;
/**
@@ -21,10 +24,12 @@ public class OrderQueryDTO {
/**
* 页码
*/
@Min(value = 1, message = "页码必须大于等于1")
private Integer page = 1;
/**
* 页大小
*/
@Min(value = 1, message = "页大小必须大于等于1")
private Integer size = 10;
}
@@ -1,5 +1,7 @@
package com.central.coupon.service.impl;
import com.central.common.model.SysUser;
import com.central.common.utils.LoginUserUtils;
import com.central.coupon.dto.OrderQueryDTO;
import com.central.coupon.dto.ConsumeOrderDTO;
import com.central.coupon.vo.OrderDetailVO;
@@ -85,7 +87,10 @@ public class OrderDetailServiceImpl implements IOrderDetailService {
QrCodeInfo qr = qrResp.getData();
if (qr.getPrice() != null) {
try {
couponPrice = new BigDecimal(qr.getPrice());
// 二维码价格是分,需要转换为元
BigDecimal priceInCents = new BigDecimal(qr.getPrice());
couponPrice = priceInCents.divide(new BigDecimal("100"), 2, BigDecimal.ROUND_HALF_UP);
log.info("二维码价格转换:{}分 = {}元", priceInCents, couponPrice);
} catch (Exception ex) {
log.warn("二维码价格解析失败, price={}, codeId={}", qr.getPrice(), queryDTO.getCodeId());
}
@@ -94,7 +99,7 @@ public class OrderDetailServiceImpl implements IOrderDetailService {
log.warn("订单中心查询二维码失败,codeId={}, resp={}", queryDTO.getCodeId(), qrResp);
}
}
log.info("根据二维码获得价格: {}", couponPrice);
log.info("根据二维码获得价格(元): {}", couponPrice);
// 1. 根据手机号查询订单
List<CtripOrder> orders = findOrdersByPhone(userPhone);
@@ -110,10 +115,27 @@ public class OrderDetailServiceImpl implements IOrderDetailService {
// 2. 筛选符合条件的订单(使用从二维码获取的价格)
List<CtripOrder> filteredOrders = filterOrdersByPriceAndConsumable(orders, couponPrice, queryDTO.getOnlyConsumable());
// 3. 分页处理
int startIndex = (queryDTO.getPage() - 1) * queryDTO.getSize();
int endIndex = Math.min(startIndex + queryDTO.getSize(), filteredOrders.size());
List<CtripOrder> pagedOrders = filteredOrders.subList(startIndex, endIndex);
// 3. 分页处理 - 添加参数验证和标准化
int page = Math.max(1, queryDTO.getPage() != null ? queryDTO.getPage() : 1); // 确保page >= 1
int size = Math.max(1, Math.min(100, queryDTO.getSize() != null ? queryDTO.getSize() : 10)); // 确保size在1-100之间
log.info("分页参数标准化:page={}, size={}", page, size);
int startIndex = (page - 1) * size;
int endIndex = Math.min(startIndex + size, filteredOrders.size());
// 安全检查:确保startIndex不会为负数
if (startIndex < 0) {
startIndex = 0;
}
List<CtripOrder> pagedOrders;
if (startIndex >= filteredOrders.size()) {
// 如果起始索引超出范围,返回空列表
pagedOrders = new ArrayList<>();
} else {
pagedOrders = filteredOrders.subList(startIndex, endIndex);
}
// 4. 转换为VO
List<OrderDetailVO> orderDetails = pagedOrders.stream()