fix(优惠券中心):新增订单支付类型后,优惠券查询逻辑优化
This commit is contained in:
+3
-3
@@ -57,14 +57,14 @@ public class OrderDetailController {
|
||||
private CtripOrderItemMapper ctripOrderItemMapper;
|
||||
|
||||
/**
|
||||
* 根据手机号和优惠券价格查询可核销的订单明细
|
||||
* 根据二维码ID查询可核销的订单明细
|
||||
*
|
||||
* @param queryDTO 查询条件
|
||||
* @param queryDTO 查询条件(codeId、page、size)
|
||||
* @return 订单明细分页结果
|
||||
*/
|
||||
@PostMapping("/query")
|
||||
public Result<PageResult<OrderDetailVO>> queryOrderDetails(@Valid @RequestBody OrderQueryDTO queryDTO) {
|
||||
log.info("查询可核销订单明细,手机号: {}, 优惠券价格: {}", queryDTO.getUserPhone(), queryDTO.getCouponPrice());
|
||||
log.info("查询可核销订单明细,codeId: {},page: {},size: {}", queryDTO.getCodeId(), queryDTO.getPage(), queryDTO.getSize());
|
||||
|
||||
try {
|
||||
PageResult<OrderDetailVO> result = orderDetailService.queryOrderDetails(queryDTO);
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
package com.central.coupon.dto;
|
||||
|
||||
import lombok.Data;
|
||||
import java.math.BigDecimal;
|
||||
|
||||
/**
|
||||
* 订单查询DTO
|
||||
@@ -10,17 +9,12 @@ import java.math.BigDecimal;
|
||||
public class OrderQueryDTO {
|
||||
|
||||
/**
|
||||
* 用户手机号
|
||||
* 二维码ID(来自订单的codeId)
|
||||
*/
|
||||
private String userPhone;
|
||||
|
||||
private String codeId;
|
||||
|
||||
/**
|
||||
* 优惠券价格(用于筛选匹配的订单)
|
||||
*/
|
||||
private BigDecimal couponPrice;
|
||||
|
||||
/**
|
||||
* 是否只查询可核销的订单
|
||||
* 是否只查询可核销的订单(内部控制,前端不必传,默认true)
|
||||
*/
|
||||
private Boolean onlyConsumable = true;
|
||||
|
||||
|
||||
+14
@@ -0,0 +1,14 @@
|
||||
package com.central.coupon.feign;
|
||||
|
||||
import com.central.common.constant.ServiceNameConstants;
|
||||
import com.central.common.model.Result;
|
||||
import com.central.coupon.feign.dto.QrCodeInfo;
|
||||
import org.springframework.cloud.openfeign.FeignClient;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.PathVariable;
|
||||
|
||||
@FeignClient(name = ServiceNameConstants.ORDER_SERVICE, dismiss404 = true)
|
||||
public interface OrderCenterService {
|
||||
@GetMapping("/qrcode/{codeId}")
|
||||
Result<QrCodeInfo> getQrCode(@PathVariable("codeId") String codeId);
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
package com.central.coupon.feign.dto;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
@Data
|
||||
public class QrCodeInfo {
|
||||
private String codeId;
|
||||
private String price;
|
||||
}
|
||||
+2
-2
@@ -11,9 +11,9 @@ import com.central.common.model.PageResult;
|
||||
public interface IOrderDetailService {
|
||||
|
||||
/**
|
||||
* 根据手机号和价格查询订单明细
|
||||
* 根据二维码ID及分页参数查询订单明细
|
||||
*
|
||||
* @param queryDTO 查询条件
|
||||
* @param queryDTO 查询条件(codeId、page、size)
|
||||
* @return 订单明细分页结果
|
||||
*/
|
||||
PageResult<OrderDetailVO> queryOrderDetails(OrderQueryDTO queryDTO);
|
||||
|
||||
+44
-22
@@ -32,6 +32,12 @@ import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.stream.Collectors;
|
||||
import com.central.coupon.dto.ConsumeItemDTO;
|
||||
import com.central.coupon.feign.OrderCenterService;
|
||||
import com.central.coupon.feign.dto.QrCodeInfo;
|
||||
import com.central.common.model.Result;
|
||||
import com.central.common.context.LoginUserContextHolder;
|
||||
import com.central.common.model.LoginAppUser;
|
||||
import java.math.BigDecimal;
|
||||
|
||||
/**
|
||||
* 订单明细服务实现类
|
||||
@@ -58,15 +64,42 @@ public class OrderDetailServiceImpl implements IOrderDetailService {
|
||||
@Autowired
|
||||
private CtripConsumedService ctripConsumedService;
|
||||
|
||||
@Autowired
|
||||
private OrderCenterService orderCenterService;
|
||||
|
||||
@Override
|
||||
public PageResult<OrderDetailVO> queryOrderDetails(OrderQueryDTO queryDTO) {
|
||||
log.info("查询可核销订单明细,手机号: {}, 优惠券价格: {}", queryDTO.getUserPhone(), queryDTO.getCouponPrice());
|
||||
log.info("查询可核销订单明细,codeId: {},page: {},size: {}", queryDTO.getCodeId(), queryDTO.getPage(), queryDTO.getSize());
|
||||
|
||||
try {
|
||||
// 0. 从登录上下文获取用户手机号
|
||||
LoginAppUser user = LoginUserContextHolder.getUser();
|
||||
String userPhone = user != null ? user.getMobile() : null;
|
||||
log.info("当前登录用户手机号: {}", userPhone);
|
||||
|
||||
// 0.1 通过codeId调用订单中心获取二维码信息,得到价格
|
||||
BigDecimal couponPrice = null;
|
||||
if (!StringUtils.isEmpty(queryDTO.getCodeId())) {
|
||||
Result<QrCodeInfo> qrResp = orderCenterService.getQrCode(queryDTO.getCodeId());
|
||||
if (qrResp != null && qrResp.getResp_code() != null && qrResp.getResp_code() == 0 && qrResp.getData() != null) {
|
||||
QrCodeInfo qr = qrResp.getData();
|
||||
if (qr.getPrice() != null) {
|
||||
try {
|
||||
couponPrice = new BigDecimal(qr.getPrice());
|
||||
} catch (Exception ex) {
|
||||
log.warn("二维码价格解析失败, price={}, codeId={}", qr.getPrice(), queryDTO.getCodeId());
|
||||
}
|
||||
}
|
||||
} else {
|
||||
log.warn("订单中心查询二维码失败,codeId={}, resp={}", queryDTO.getCodeId(), qrResp);
|
||||
}
|
||||
}
|
||||
log.info("根据二维码获得价格: {}", couponPrice);
|
||||
|
||||
// 1. 根据手机号查询订单
|
||||
List<CtripOrder> orders = findOrdersByPhone(queryDTO.getUserPhone());
|
||||
List<CtripOrder> orders = findOrdersByPhone(userPhone);
|
||||
if (orders == null || orders.isEmpty()) {
|
||||
log.info("未找到手机号 {} 的订单", queryDTO.getUserPhone());
|
||||
log.info("未找到手机号 {} 的订单", userPhone);
|
||||
return PageResult.<OrderDetailVO>builder()
|
||||
.data(new ArrayList<>())
|
||||
.count(0L)
|
||||
@@ -74,8 +107,8 @@ public class OrderDetailServiceImpl implements IOrderDetailService {
|
||||
.build();
|
||||
}
|
||||
|
||||
// 2. 筛选符合条件的订单
|
||||
List<CtripOrder> filteredOrders = filterOrders(orders, queryDTO);
|
||||
// 2. 筛选符合条件的订单(使用从二维码获取的价格)
|
||||
List<CtripOrder> filteredOrders = filterOrdersByPriceAndConsumable(orders, couponPrice, queryDTO.getOnlyConsumable());
|
||||
|
||||
// 3. 分页处理
|
||||
int startIndex = (queryDTO.getPage() - 1) * queryDTO.getSize();
|
||||
@@ -263,40 +296,29 @@ public class OrderDetailServiceImpl implements IOrderDetailService {
|
||||
return orders;
|
||||
}
|
||||
|
||||
/**
|
||||
* 筛选订单
|
||||
*/
|
||||
private List<CtripOrder> filterOrders(List<CtripOrder> orders, OrderQueryDTO queryDTO) {
|
||||
private List<CtripOrder> filterOrdersByPriceAndConsumable(List<CtripOrder> orders, BigDecimal couponPrice, Boolean onlyConsumable) {
|
||||
log.info("开始筛选订单,原始订单数量: {}", orders.size());
|
||||
|
||||
List<CtripOrder> filteredOrders = orders.stream()
|
||||
.filter(order -> {
|
||||
log.debug("筛选订单: otaOrderId={}, totalAmount={}", order.getOtaOrderId(), order.getTotalAmount());
|
||||
|
||||
// 按优惠券价格筛选
|
||||
if (queryDTO.getCouponPrice() != null) {
|
||||
if (order.getTotalAmount() == null ||
|
||||
!order.getTotalAmount().equals(queryDTO.getCouponPrice())) {
|
||||
log.debug("订单 {} 价格不匹配,期望: {}, 实际: {}",
|
||||
order.getOtaOrderId(), queryDTO.getCouponPrice(), order.getTotalAmount());
|
||||
if (couponPrice != null) {
|
||||
if (order.getTotalAmount() == null || !order.getTotalAmount().equals(couponPrice)) {
|
||||
log.debug("订单 {} 价格不匹配,期望: {}, 实际: {}",
|
||||
order.getOtaOrderId(), couponPrice, order.getTotalAmount());
|
||||
return false;
|
||||
}
|
||||
log.debug("订单 {} 价格匹配", order.getOtaOrderId());
|
||||
}
|
||||
|
||||
// 如果只查询可核销订单,则过滤掉不可核销的订单
|
||||
if (queryDTO.getOnlyConsumable() != null && queryDTO.getOnlyConsumable()) {
|
||||
if (onlyConsumable != null && onlyConsumable) {
|
||||
if (!validateOrderForConsume(order.getOtaOrderId())) {
|
||||
log.debug("订单 {} 不可核销", order.getOtaOrderId());
|
||||
return false;
|
||||
}
|
||||
log.debug("订单 {} 可核销", order.getOtaOrderId());
|
||||
}
|
||||
|
||||
return true;
|
||||
})
|
||||
.collect(Collectors.toList());
|
||||
|
||||
log.info("筛选后订单数量: {}", filteredOrders.size());
|
||||
return filteredOrders;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user