fix(优惠券中心):用户查询优惠券逻辑优化,直接通过二维码id进行查询
This commit is contained in:
@@ -1,6 +1,8 @@
|
|||||||
package com.central.coupon.dto;
|
package com.central.coupon.dto;
|
||||||
|
|
||||||
import lombok.Data;
|
import lombok.Data;
|
||||||
|
import javax.validation.constraints.Min;
|
||||||
|
import javax.validation.constraints.NotNull;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 订单查询DTO
|
* 订单查询DTO
|
||||||
@@ -11,6 +13,7 @@ public class OrderQueryDTO {
|
|||||||
/**
|
/**
|
||||||
* 二维码ID(来自订单的codeId)
|
* 二维码ID(来自订单的codeId)
|
||||||
*/
|
*/
|
||||||
|
@NotNull(message = "二维码ID不能为空")
|
||||||
private String codeId;
|
private String codeId;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -21,10 +24,12 @@ public class OrderQueryDTO {
|
|||||||
/**
|
/**
|
||||||
* 页码
|
* 页码
|
||||||
*/
|
*/
|
||||||
|
@Min(value = 1, message = "页码必须大于等于1")
|
||||||
private Integer page = 1;
|
private Integer page = 1;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 页大小
|
* 页大小
|
||||||
*/
|
*/
|
||||||
|
@Min(value = 1, message = "页大小必须大于等于1")
|
||||||
private Integer size = 10;
|
private Integer size = 10;
|
||||||
}
|
}
|
||||||
+28
-6
@@ -1,5 +1,7 @@
|
|||||||
package com.central.coupon.service.impl;
|
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.OrderQueryDTO;
|
||||||
import com.central.coupon.dto.ConsumeOrderDTO;
|
import com.central.coupon.dto.ConsumeOrderDTO;
|
||||||
import com.central.coupon.vo.OrderDetailVO;
|
import com.central.coupon.vo.OrderDetailVO;
|
||||||
@@ -85,7 +87,10 @@ public class OrderDetailServiceImpl implements IOrderDetailService {
|
|||||||
QrCodeInfo qr = qrResp.getData();
|
QrCodeInfo qr = qrResp.getData();
|
||||||
if (qr.getPrice() != null) {
|
if (qr.getPrice() != null) {
|
||||||
try {
|
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) {
|
} catch (Exception ex) {
|
||||||
log.warn("二维码价格解析失败, price={}, codeId={}", qr.getPrice(), queryDTO.getCodeId());
|
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.warn("订单中心查询二维码失败,codeId={}, resp={}", queryDTO.getCodeId(), qrResp);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
log.info("根据二维码获得价格: {}", couponPrice);
|
log.info("根据二维码获得价格(元): {}", couponPrice);
|
||||||
|
|
||||||
// 1. 根据手机号查询订单
|
// 1. 根据手机号查询订单
|
||||||
List<CtripOrder> orders = findOrdersByPhone(userPhone);
|
List<CtripOrder> orders = findOrdersByPhone(userPhone);
|
||||||
@@ -110,10 +115,27 @@ public class OrderDetailServiceImpl implements IOrderDetailService {
|
|||||||
// 2. 筛选符合条件的订单(使用从二维码获取的价格)
|
// 2. 筛选符合条件的订单(使用从二维码获取的价格)
|
||||||
List<CtripOrder> filteredOrders = filterOrdersByPriceAndConsumable(orders, couponPrice, queryDTO.getOnlyConsumable());
|
List<CtripOrder> filteredOrders = filterOrdersByPriceAndConsumable(orders, couponPrice, queryDTO.getOnlyConsumable());
|
||||||
|
|
||||||
// 3. 分页处理
|
// 3. 分页处理 - 添加参数验证和标准化
|
||||||
int startIndex = (queryDTO.getPage() - 1) * queryDTO.getSize();
|
int page = Math.max(1, queryDTO.getPage() != null ? queryDTO.getPage() : 1); // 确保page >= 1
|
||||||
int endIndex = Math.min(startIndex + queryDTO.getSize(), filteredOrders.size());
|
int size = Math.max(1, Math.min(100, queryDTO.getSize() != null ? queryDTO.getSize() : 10)); // 确保size在1-100之间
|
||||||
List<CtripOrder> pagedOrders = filteredOrders.subList(startIndex, endIndex);
|
|
||||||
|
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
|
// 4. 转换为VO
|
||||||
List<OrderDetailVO> orderDetails = pagedOrders.stream()
|
List<OrderDetailVO> orderDetails = pagedOrders.stream()
|
||||||
|
|||||||
Reference in New Issue
Block a user