续租逻辑修改,增加requestId追踪

This commit is contained in:
2025-08-11 00:18:16 +08:00
parent c6f6a12997
commit 091d7a6d3e
5 changed files with 140 additions and 43 deletions

View File

@ -33,15 +33,11 @@ public class FacadeAspect {
@Before("facadeLog()")
public void doBefore(JoinPoint joinPoint) {
// 打印请求相关参数
log.info("=========================remote Start =========================");
log.info("******** remote Start *********");
// 打印 Http method
//log.info("HTTP Method : {}", request.getMethod());
// 打印调用 controller 的全路径以及执行方法
log.info("Class Method:{}.{}", joinPoint.getSignature().getDeclaringTypeName(), joinPoint.getSignature().getName());
// 打印请求的 IP
//log.info("IP : {}", request.getRemoteAddr());
// 打印请求入参
log.info("facadeAspect_Request:{}", JSON.toJSONString(joinPoint.getArgs()));
log.info("Class Method:{}.{}, facadeAspect_Request:{}", joinPoint.getSignature().getDeclaringTypeName(), joinPoint.getSignature().getName(),JSON.toJSONString(joinPoint.getArgs()));
}
@Around("facadeLog()")
@ -52,7 +48,7 @@ public class FacadeAspect {
log.info("facadeAspect_Response:{}", JSON.toJSONString(result));
// 执行耗时
log.info("Time-Consuming : {} ms", System.currentTimeMillis() - startTime);
log.info("=========================remote End =========================");
log.info("**************remote End ****************");
return result;
}
}

View File

@ -37,14 +37,23 @@ public class WebLogAspect {
*/
@Before("webLog()")
public void doBefore(JoinPoint joinPoint) {
String uid = UUID.randomUUID().toString();
MDC.put("requestId",uid.substring(24,36));
// 开始打印请求日志
ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
HttpServletRequest request = null;
String requestId = null;
if (attributes != null) {
request = attributes.getRequest();
// 从请求头中获取 requestId
requestId = request.getHeader("requestId");
}
// 如果请求头中没有 requestId则生成新的
if (StringUtils.isBlank(requestId)) {
requestId = UUID.randomUUID().toString();
}
// 使用 requestId 的一部分作为日志标识
MDC.put("requestId", requestId.substring(Math.max(0, requestId.length() - 12)));
// 打印请求相关参数
log.info("========================= Start =========================");
// 打印请求入参

View File

@ -0,0 +1,19 @@
package com.sczx.order.config;
import feign.RequestInterceptor;
import feign.RequestTemplate;
import org.slf4j.MDC;
import org.springframework.context.annotation.Configuration;
@Configuration
public class FeignRequestInterceptor implements RequestInterceptor {
@Override
public void apply(RequestTemplate template) {
// 从MDC中获取requestId并添加到请求头
String requestId = MDC.get("requestId");
if (requestId != null && !requestId.isEmpty()) {
template.header("requestId", requestId);
}
}
}

View File

@ -104,11 +104,15 @@ public class OrderServiceImpl implements OrderService {
orderMainPO.setOrderStatus(OrderStatusEnum.WAIT_PICK.getCode());
orderMainPO.setFirstOrderTime(LocalDateTime.now());
//设置预计还车时间
if(StringUtils.equalsIgnoreCase(RentCarTypeEnum.DAILY_RENTAL.getCode(), rentCarRuleDTO.getRentalType())
|| StringUtils.equalsIgnoreCase(RentCarTypeEnum.HOUR_RENTAL.getCode(), orderMainPO.getRentalType())){
if(StringUtils.equalsIgnoreCase(RentCarTypeEnum.HOUR_RENTAL.getCode(), orderMainPO.getRentalType())){
orderMainPO.setEndRentTime(LocalDateTime.now().plusHours(1));
}else if(StringUtils.equalsIgnoreCase(RentCarTypeEnum.DAILY_RENTAL.getCode(), rentCarRuleDTO.getRentalType())){
orderMainPO.setEndRentTime(LocalDateTime.now().plusDays(1));
} else if(StringUtils.equalsIgnoreCase(RentCarTypeEnum.DAYS_RENTAL.getCode(), rentCarRuleDTO.getRentalType())){
orderMainPO.setEndRentTime(LocalDateTime.now().plusDays(rentCarRuleDTO.getRentalDays()));
} else if(StringUtils.equalsIgnoreCase(RentCarTypeEnum.RENT_INSTEAD_SELL.getCode(), rentCarRuleDTO.getRentalType())){
//以租代售默认期限为30天
orderMainPO.setEndRentTime(LocalDateTime.now().plusDays(30));
}
//生成子表订单
@ -150,12 +154,7 @@ public class OrderServiceImpl implements OrderService {
orderSubPOList.add(depositOrder);
}
//生成租车订单
BigDecimal rentCarOrderAmount = new BigDecimal(0);
if(RentCarTypeEnum.DAYS_RENTAL.getCode().equalsIgnoreCase(rentCarRuleDTO.getRentalType())){
rentCarOrderAmount = rentCarRuleDTO.getRentalPrice().multiply(new BigDecimal(rentCarRuleDTO.getRentalDays()));
}else {
rentCarOrderAmount = rentCarOrderAmount.add(rentCarRuleDTO.getRentalPrice());
}
BigDecimal rentCarOrderAmount = getRentCarAmount(rentCarRuleDTO.getRentalType(), rentCarRuleDTO.getRentalPrice(), rentCarRuleDTO.getRentalDays());
OrderSubPO rentOrder = new OrderSubPO();
rentOrder.setSuborderNo(OrderUtil.generateSubOrderNo(OrderUtil.ZC_PREFIX));
rentOrder.setSuborderType(SubOrderTypeEnum.RENTCAR.getCode());
@ -198,6 +197,8 @@ public class OrderServiceImpl implements OrderService {
}
}
@Transactional(rollbackFor = Exception.class)
@Override
public RentCarOrderResultDTO reRentalCarOrder(ReRentCarReq rentCarOrderReq) {
@ -228,12 +229,24 @@ public class OrderServiceImpl implements OrderService {
}
}
//生成租车订单
// BigDecimal rentCarOrderAmount = getRentCarAmount(orderMainPO.getRentalType(), orderMainPO.getRentalPrice(), orderMainPO.getRentalDays());
//计算续租金额
BigDecimal rentCarOrderAmount = new BigDecimal(0);
if(RentCarTypeEnum.DAYS_RENTAL.getCode().equalsIgnoreCase(orderMainPO.getRentalType())){
rentCarOrderAmount = orderMainPO.getRentalPrice().multiply(new BigDecimal(orderMainPO.getRentalDays()));
}else {
rentCarOrderAmount = rentCarOrderAmount.add(orderMainPO.getRentalPrice());
if(RentCarTypeEnum.HOUR_RENTAL.getCode().equalsIgnoreCase(orderMainPO.getRentalType())){
Integer overdueHours = OrderUtil.getOrderOverdueHours(orderMainPO.getEndRentTime());
rentCarOrderAmount = rentCarOrderAmount.add(orderMainPO.getRentalPrice().multiply(new BigDecimal(overdueHours+1)));
} else if(RentCarTypeEnum.DAILY_RENTAL.getCode().equalsIgnoreCase(orderMainPO.getRentalType())){
Integer rentalDays = OrderUtil.getOrderExpectedDays(orderMainPO.getEndRentTime());
rentCarOrderAmount = rentCarOrderAmount.add(orderMainPO.getRentalPrice().multiply(new BigDecimal(rentalDays+1)));
} else if(RentCarTypeEnum.DAYS_RENTAL.getCode().equalsIgnoreCase(orderMainPO.getRentalType())){
Integer rentalDays = OrderUtil.getOrderExpectedDays(orderMainPO.getEndRentTime());
rentCarOrderAmount = rentCarOrderAmount.add(orderMainPO.getRentalPrice().multiply(new BigDecimal(rentalDays+orderMainPO.getRentalDays())));
} else {
Integer rentalDays = OrderUtil.getOrderExpectedDays(orderMainPO.getEndRentTime());
rentCarOrderAmount = rentCarOrderAmount.add(orderMainPO.getRentalPrice().multiply(new BigDecimal(rentalDays+30)));
}
//生成租车子订单
OrderSubPO rentOrder = new OrderSubPO();
rentOrder.setOrderId(orderMainPO.getOrderId());
@ -244,15 +257,18 @@ public class OrderServiceImpl implements OrderService {
rentOrder.setPaymentMethod(paymentType);
orderSubRepo.save(rentOrder);
LocalDateTime oldEndRentTime = orderMainPO.getEndRentTime();
LocalDateTime nowTime = LocalDateTime.now();
LocalDateTime oldEndRentTime = orderMainPO.getEndRentTime().isAfter(nowTime) ? orderMainPO.getEndRentTime() : nowTime;
LocalDateTime newEndRentTime = oldEndRentTime;
//刷新预计还车时间
if(StringUtils.equalsIgnoreCase(RentCarTypeEnum.DAILY_RENTAL.getCode(), orderMainPO.getRentalType())
|| StringUtils.equalsIgnoreCase(RentCarTypeEnum.HOUR_RENTAL.getCode(), orderMainPO.getRentalType())){
if(StringUtils.equalsIgnoreCase(RentCarTypeEnum.HOUR_RENTAL.getCode(), orderMainPO.getRentalType())){
newEndRentTime = oldEndRentTime.plusHours(1);
} else if(StringUtils.equalsIgnoreCase(RentCarTypeEnum.DAILY_RENTAL.getCode(), orderMainPO.getRentalType())){
newEndRentTime = oldEndRentTime.plusDays(1);
} else if(StringUtils.equalsIgnoreCase(RentCarTypeEnum.DAYS_RENTAL.getCode(), orderMainPO.getRentalType())){
orderMainPO.setEndRentTime(LocalDateTime.now().plusDays(orderMainPO.getRentalDays()));
newEndRentTime = oldEndRentTime.plusDays(orderMainPO.getRentalDays());
} else if(StringUtils.equalsIgnoreCase(RentCarTypeEnum.RENT_INSTEAD_SELL.getCode(), orderMainPO.getRentalType())){
newEndRentTime = oldEndRentTime.plusDays(30);
}
//更新订单状态及信息
LambdaUpdateWrapper<OrderMainPO> updateWrapper = new LambdaUpdateWrapper<>();
@ -314,8 +330,12 @@ public class OrderServiceImpl implements OrderService {
paymentType = PaymentTypeEnum.ZFB_PAY.getCode();
}
}
Integer overDueDaysOrHours = OrderUtil.getOrderOverdueDays(orderMainPO.getEndRentTime());
if(StringUtils.equalsIgnoreCase(RentCarTypeEnum.HOUR_RENTAL.getCode(), orderMainPO.getRentalType())){
overDueDaysOrHours = OrderUtil.getOrderOverdueHours(orderMainPO.getEndRentTime());
}
//生成租车订单
BigDecimal overDueAmount = OrderUtil.getOrderOverdueAmount(orderMainPO.getOverdueDays(), orderMainPO.getOverdueFee());
BigDecimal overDueAmount = OrderUtil.getOrderOverdueAmount(overDueDaysOrHours, orderMainPO.getOverdueFee());
//生成租车子订单
OrderSubPO rentOrder = new OrderSubPO();
rentOrder.setOrderId(orderMainPO.getOrderId());
@ -436,6 +456,18 @@ public class OrderServiceImpl implements OrderService {
//如果是租车中,需要判断是否逾期了
log.info("判断订单是否逾期");
if(orderMainPO.getEndRentTime()!=null){
if(StringUtils.equalsIgnoreCase(RentCarTypeEnum.HOUR_RENTAL.getCode(), orderMainPO.getRentalType())){
Integer overdueHours = OrderUtil.getOrderOverdueHours(orderMainPO.getEndRentTime());
log.info("预计还车时间:{},订单逾期小时数:{}",orderMainPO.getEndRentTime(),overdueHours);
if(overdueHours>0){
orderDetailDTO.setOverdueAmount(OrderUtil.getOrderOverdueAmount(overdueHours, orderMainPO.getOverdueFee()));
orderDetailDTO.setOrderStatus(OrderStatusEnum.RENT_OVERDUE.getCode());
LambdaUpdateWrapper<OrderMainPO> updateWrapper = new LambdaUpdateWrapper<>();
updateWrapper.set(OrderMainPO::getOrderStatus, OrderStatusEnum.RENT_OVERDUE.getCode());
updateWrapper.eq(OrderMainPO::getOrderId, orderMainPO.getOrderId());
orderMainRepo.update(updateWrapper);
}
} else {
Integer overdueDays = OrderUtil.getOrderOverdueDays(orderMainPO.getEndRentTime());
log.info("预计还车时间:{},订单逾期天数:{}",orderMainPO.getEndRentTime(),overdueDays);
//逾期天数>0则改为逾期并且计算逾期天数以及逾期金额
@ -453,11 +485,17 @@ public class OrderServiceImpl implements OrderService {
orderDetailDTO.setExpectedDays(OrderUtil.getOrderExpectedDays(orderMainPO.getEndRentTime()));
}
}
}
} else if(OrderStatusEnum.RENT_OVERDUE.getCode().equalsIgnoreCase(orderMainPO.getOrderStatus())){
log.info("订单已逾期的,计算逾期金额");
if(orderMainPO.getEndRentTime()!=null){
orderDetailDTO.setOverdueAmount(OrderUtil.getOrderOverdueAmount(orderMainPO.getOverdueDays(), orderMainPO.getOverdueFee()));
Integer overdueDaysOrHours = OrderUtil.getOrderOverdueDays(orderMainPO.getEndRentTime());
if(StringUtils.equalsIgnoreCase(RentCarTypeEnum.HOUR_RENTAL.getCode(), orderMainPO.getRentalType())){
overdueDaysOrHours = OrderUtil.getOrderOverdueHours(orderMainPO.getEndRentTime());
}
orderDetailDTO.setOverdueAmount(OrderUtil.getOrderOverdueAmount(overdueDaysOrHours, orderMainPO.getOverdueFee()));
}
} else if(OrderStatusEnum.WAIT_PAY.getCode().equalsIgnoreCase(orderMainPO.getOrderStatus())||OrderStatusEnum.RERENT_WAIT_PAY.getCode().equalsIgnoreCase(orderMainPO.getOrderStatus())){
//TODO 待支付状态要拉起支付
@ -608,4 +646,24 @@ public class OrderServiceImpl implements OrderService {
List<String> orderStatusList = orderQueryReq.getOrderStatusList().stream().map(OrderStatusEnum::getCode).collect(Collectors.toList());
return orderMainRepo.pageQueryOrder(pageNo, pageSize, orderQueryReq.getCustomerId(), orderQueryReq.getStoreId(), orderStatusList, orderQueryReq.getQueryBrandName());
}
/**
* 获取租车订单金额
* @param rentalType
* @param rentalPrice
* @param rentalDays
* @return
*/
private static BigDecimal getRentCarAmount(String rentalType, BigDecimal rentalPrice, Integer rentalDays) {
BigDecimal rentCarOrderAmount = new BigDecimal(0);
if(RentCarTypeEnum.HOUR_RENTAL.getCode().equalsIgnoreCase(rentalType)||RentCarTypeEnum.DAILY_RENTAL.getCode().equalsIgnoreCase(rentalType)){
rentCarOrderAmount = rentCarOrderAmount.add(rentalPrice);
} else if(RentCarTypeEnum.DAYS_RENTAL.getCode().equalsIgnoreCase(rentalType)){
rentCarOrderAmount = rentalPrice.multiply(new BigDecimal(rentalDays));
} else {
rentCarOrderAmount = rentalPrice.multiply(new BigDecimal(30));
}
return rentCarOrderAmount;
}
}

View File

@ -40,6 +40,21 @@ public class OrderUtil {
return prefix + timestamp + uuidSuffix; // sub代表子订单号
}
/**
* 计算订单逾期小时
* @param endRentTime 订单结束时间
* @return 逾期小时数
*/
public static Integer getOrderOverdueHours(LocalDateTime endRentTime) {
if(endRentTime!=null){
LocalDateTime now = LocalDateTime.now();
if(now.isAfter(endRentTime)){
return (int) ChronoUnit.HOURS.between(endRentTime, now);
}
}
return 0;
}
/**
* 计算订单逾期天数
* @param endRentTime 订单结束时间