免押接口实现

This commit is contained in:
2025-09-26 14:26:11 +08:00
parent 879678cb56
commit 1330b8cb68
2 changed files with 135 additions and 2 deletions

View File

@ -239,8 +239,140 @@ public class OrderServiceImpl implements OrderService {
@Override
public RentCarOrderResultDTO depositFreeSubmitRentCarOrder(RentCarOrderReq rentCarOrderReq) {
//TODO - 免押冻结
return null;
SimpleUserInfoDTO userInfoDTO = jwtUtil.getUserInfoFromToken();
if(!(rentCarOrderReq.getIsDepositFree() && StringUtils.equalsIgnoreCase(userInfoDTO.getMiniProgramType(), MiniProgramTypeEnum.ALIPAY.getType()))){
throw new BizException("不满足支付宝免押条件");
}
String redisLockKey = RedisKeyConstants.ORDER_SUB_KEY + userInfoDTO.getUserId();
if(redisUtil.getRedisLock(redisLockKey, "租车下单")) {
try{
//判断是否还有未完成的订单
LambdaQueryWrapper<OrderMainPO> currentOrderWrapper = new LambdaQueryWrapper<>();
currentOrderWrapper.eq(OrderMainPO::getCustomerId, userInfoDTO.getUserId())
.notIn(OrderMainPO::getOrderStatus, Arrays.asList(OrderStatusEnum.AUTO_END.getCode(), OrderStatusEnum.MANUAL_END.getCode()))
.eq(OrderMainPO::getDelFlag, "0");
List<OrderMainPO> currentOrderList = orderMainRepo.list(currentOrderWrapper);
OrderMainPO waitPayOrder = currentOrderList.stream().filter(order -> order.getOrderStatus().equals(OrderStatusEnum.WAIT_PAY.getCode())).findFirst().orElse(null);
if(!currentOrderList.isEmpty() && waitPayOrder==null){
throw new BizException("您有未完成的订单,请先完成订单");
}
if(waitPayOrder!=null){
log.info("存在待支付的订单,取消订单");
PayOrderReq payOrderReq = new PayOrderReq();
payOrderReq.setOrderNo(waitPayOrder.getOrderNo());
cancelOrder(payOrderReq);
}
//判断是否存有空闲车辆可用
LambdaQueryWrapper<CarPO> carWrapper = new LambdaQueryWrapper<>();
carWrapper.eq(CarPO::getModelId, rentCarOrderReq.getCarModelId())
.eq(CarPO::getDelFlag, "0")
.eq(CarPO::getBrsStatus, "0")
.eq(CarPO::getStoreId, rentCarOrderReq.getStoreId());
List<CarPO> carPOList = carRepo.list(carWrapper);
if(CollectionUtils.isEmpty(carPOList)){
throw new BizException("门店没有该车型的车辆可租");
}
//获取门店信息
CompanyStoreDTO companyStoreDTO = storeInteg.getStoreById(Integer.valueOf(rentCarOrderReq.getStoreId().toString()));
CarModelSimpleDTO carModelSimpleDTO = carInteg.getCarModelByModelId(rentCarOrderReq.getCarModelId());
RentCarRuleDTO rentCarRuleDTO = carInteg.getRentCarRuleByCarRuleId(rentCarOrderReq.getRentCarRuleId());
RentBatteyRuleDTO rentBatteyRuleDTO = null;
if(rentCarOrderReq.getRentBatteyRuleId()!=null){
rentBatteyRuleDTO = carInteg.getRentBatteyRuleByBatteyRuleId(rentCarOrderReq.getRentBatteyRuleId());
}
//生成订单主表
OrderMainPO orderMainPO = OrderConvert.INSTANCE.subOrderToPo(rentCarOrderReq, userInfoDTO, rentCarRuleDTO);
orderMainPO.setOperatorId(Long.valueOf(companyStoreDTO.getOperatingCompanyId()));
orderMainPO.setOrderNo(OrderUtil.generateOrderNo());
orderMainPO.setOrderStatus(OrderStatusEnum.WAIT_PAY.getCode());
orderMainPO.setFirstOrderTime(LocalDateTime.now());
//设置预计还车时间
LocalDateTime endRentTime = OrderUtil.getEndRentTime(orderMainPO.getFirstOrderTime(),1,rentCarRuleDTO.getRentalDays(), rentCarRuleDTO.getRentalType());
orderMainPO.setEndRentTime(endRentTime);
//生成子表订单
String paymentType = PaymentTypeEnum.ZFB_PAY.getCode();
List<OrderSubPO> orderSubPOList = new ArrayList<>();
//如果未开通免押则要生成押金订单
OrderSubPO depositOrder = new OrderSubPO();
depositOrder.setSuborderNo(OrderUtil.generateSubOrderNo(OrderUtil.MY_PREFIX));
depositOrder.setSuborderType(SubOrderTypeEnum.NO_DEPOSIT.getCode());
depositOrder.setAmount(orderMainPO.getDepositPrice());
depositOrder.setCreatedAt(LocalDateTime.now());
depositOrder.setPaymentMethod(paymentType);
orderSubPOList.add(depositOrder);
//如果选择了租电套餐,则还需要生成租电子订单
if(rentBatteyRuleDTO!=null){
rentBatteyRuleDTO = carInteg.getRentBatteyRuleByBatteyRuleId(rentCarOrderReq.getRentBatteyRuleId());
OrderSubPO rentBatteyRuleOrder = new OrderSubPO();
rentBatteyRuleOrder.setSuborderNo(OrderUtil.generateSubOrderNo(OrderUtil.ZD_PREFIX));
rentBatteyRuleOrder.setSuborderType(SubOrderTypeEnum.RENTBATTEY.getCode());
rentBatteyRuleOrder.setAmount(rentBatteyRuleDTO.getRentPrice());
rentBatteyRuleOrder.setCreatedAt(LocalDateTime.now());
rentBatteyRuleOrder.setPaymentMethod(paymentType);
orderSubPOList.add(rentBatteyRuleOrder);
}
//生成租车订单
BigDecimal rentCarOrderAmount = OrderUtil.getRentCarAmount(rentCarRuleDTO.getRentalType(), rentCarRuleDTO.getRentalPrice(), rentCarRuleDTO.getRentalDays());
OrderSubPO rentOrder = new OrderSubPO();
rentOrder.setSuborderNo(OrderUtil.generateSubOrderNo(OrderUtil.ZC_PREFIX));
rentOrder.setSuborderType(SubOrderTypeEnum.RENTCAR.getCode());
rentOrder.setAmount(rentCarOrderAmount);
rentOrder.setCreatedAt(LocalDateTime.now());
rentOrder.setPaymentMethod(paymentType);
rentOrder.setReturnTime(endRentTime);
orderSubPOList.add(rentOrder);
BigDecimal orderAmount = orderSubPOList.stream().map(OrderSubPO::getAmount).reduce(BigDecimal.ZERO, BigDecimal::add);
orderMainPO.setOrderAmount(orderAmount);
//发起支付返回预支付信息
String paymentId = OrderUtil.generateSubOrderNo(OrderUtil.ZF_PREFIX);
UnifiedPaymentInfoDTO unifiedPaymentInfoDTO = payService.prepayOrder(paymentType,rentCarRuleDTO.getRuleName(),Long.valueOf(companyStoreDTO.getOperatingCompanyId()),paymentId,userInfoDTO
,orderMainPO.getOrderAmount());
for(OrderSubPO orderSubPO : orderSubPOList){
orderSubPO.setPaymentId(paymentId);
orderSubPO.setPayStatus(PayStatusEnum.USERPAYING.getCode());
}
orderMainRepo.save(orderMainPO);
orderSubPOList.forEach(orderSubPO -> {
orderSubPO.setOrderId(orderMainPO.getOrderId());
});
orderSubRepo.saveBatch(orderSubPOList);
//返回订单信息
OrderDTO orderDTO = OrderConvert.INSTANCE.poToDto(orderMainPO);
orderDTO.setCompanyStoreDTO(companyStoreDTO);
orderDTO.setCarModelSimpleDTO(carModelSimpleDTO);
RentCarOrderResultDTO rentCarOrderResultDTO = new RentCarOrderResultDTO();
rentCarOrderResultDTO.setOrderMainInfo(orderDTO);
rentCarOrderResultDTO.setUnifiedPaymentInfo(unifiedPaymentInfoDTO);
return rentCarOrderResultDTO;
}catch (Exception e){
log.warn("下单失败", e);
throw e;
} finally {
redisUtil.deleteRedisLock(redisLockKey);
}
} else {
log.warn("下单失败,锁已被占用");
throw new InnerException("服务器正在处理,请稍后再试");
}
}

View File

@ -13,6 +13,7 @@ public class OrderUtil {
public static final String ORDER_PREFIX = "OC";
public static final String MY_PREFIX = "MY";
public static final String YJ_PREFIX = "YJ";
public static final String ZD_PREFIX = "ZD";