liujie
2026-04-29 2d56c1d1ca0f7cbc05150b8cd48e63372f1a7f32
UserYKDTravel/guns-admin/src/main/java/com/stylefeng/guns/modular/specialTrain/server/impl/OrderPrivateCarServiceImpl.java
@@ -1138,7 +1138,7 @@
      if (!Integer.valueOf(2).equals(orderPrivateCar.getPaymentMode()) && (orderPrivateCar.getCancelType() == 2 || StrUtil.isNotEmpty(orderPrivateCar.getPrepaySecondPayNo()))) {
      if (!Integer.valueOf(2).equals(orderPrivateCar.getPaymentMode()) && StrUtil.isNotEmpty(orderPrivateCar.getPrepaySecondPayNo())) {
         ResultUtil refundResult = handlePrivateCarPrepayCancelRefund(orderPrivateCar, reason, remark, uid);
         if (refundResult != null) {
            if (!ResultUtil.SUCCESS.equals(refundResult.getCode())) {
@@ -1148,6 +1148,11 @@
            map.put("id", null);
//            return ResultUtil.success(map);
         }
      }else if(!Integer.valueOf(2).equals(orderPrivateCar.getPaymentMode()) && StrUtil.isEmpty(orderPrivateCar.getPrepaySecondPayNo())){
         UserInfo userInfo = userInfoService.selectById(orderPrivateCar.getUserId());
         userInfo.setBalance(userInfo.getBalance() + orderPrivateCar.getPayMoney());
         userInfoService.updateById(userInfo);
      }
      Integer number1 = this.selectCount(new EntityWrapper<OrderPrivateCar>().eq("pid", orderPrivateCar.getPid() == null ? orderPrivateCar.getId() : orderPrivateCar.getPid()).lt("state", 6));
      Integer number2 = this.selectCount(new EntityWrapper<OrderPrivateCar>().eq("id", orderPrivateCar.getPid() == null ? orderPrivateCar.getId() : orderPrivateCar.getPid()).lt("state", 6));
@@ -2038,6 +2043,11 @@
         }
      }
      orderMoney = orderMoney < 0 ? 0 : orderMoney;
      ResultUtil prepaySettleResult = settlePrivateCarPrepayIfNoSecondPay(orderPrivateCar, userInfo, orderMoney);
      if (prepaySettleResult != null) {
         return prepaySettleResult;
      }
      orderMoney = getPrivateCarPrepayFinalPayAmount(orderPrivateCar, orderMoney);
      if (payType == 1) {//微信支付
         String outTradeNo = orderId + "_1_" + UUIDUtil.getRandomCode(5);
         JSONObject response = cbyzxOrderPayHelper.createOrder(orderPrivateCar.getUserId(), userInfo.getAppletsOpenId(), outTradeNo, orderMoney);
@@ -2298,7 +2308,14 @@
      orderMoney = new BigDecimal(orderMoney).setScale(2, RoundingMode.HALF_UP).doubleValue();
      System.out.println("!!!" + orderMoney + "!!!!");
      orderMoney = orderMoney < 0 ? 0 : orderMoney;
      orderPrivateCar.setPayMoney(orderMoney);
      ResultUtil prepaySettleResult = settlePrivateCarPrepayIfNoSecondPay(orderPrivateCar, userInfo, orderMoney);
      if (prepaySettleResult != null) {
         return prepaySettleResult;
      }
      orderMoney = getPrivateCarPrepayFinalPayAmount(orderPrivateCar, orderMoney);
      if (!isPrivateCarPrepaySpreadStage(orderPrivateCar)) {
         orderPrivateCar.setPayMoney(orderMoney);
      }
      if (payType == 1) {//微信支付
         if (orderMoney == 0) {
            return ResultUtil.error("请使用余额支付");
@@ -2594,6 +2611,146 @@
            .doubleValue();
   }
   private boolean isPrivateCarPrepayFinalStage(OrderPrivateCar orderPrivateCar) {
      return orderPrivateCar != null
            && Integer.valueOf(1).equals(orderPrivateCar.getPaymentMode())
            && !Integer.valueOf(5).equals(orderPrivateCar.getPrepayStage());
   }
   private BigDecimal toMoney(Double amount) {
      return new BigDecimal(String.valueOf(amount == null ? 0D : amount)).setScale(2, RoundingMode.HALF_UP);
   }
   private PaymentRecord getPrivateCarPrepayPaidRecord(OrderPrivateCar orderPrivateCar) throws Exception {
      PaymentRecord paymentRecord = paymentRecordService.query(1, orderPrivateCar.getUserId(), 1, orderPrivateCar.getId(), 1, orderPrivateCar.getPayType(), 2);
      if (paymentRecord == null) {
         paymentRecord = paymentRecordService.query(1, orderPrivateCar.getUserId(), 1, orderPrivateCar.getId(), 1, null, 2);
      }
      if (paymentRecord != null && orderPrivateCar.getPayType() == null) {
         orderPrivateCar.setPayType(paymentRecord.getPayType());
      }
      return paymentRecord;
   }
   private BigDecimal getPrivateCarPrepayPaidAmount(OrderPrivateCar orderPrivateCar) throws Exception {
      PaymentRecord paymentRecord = getPrivateCarPrepayPaidRecord(orderPrivateCar);
      if (paymentRecord != null && paymentRecord.getAmount() != null && paymentRecord.getAmount() > 0) {
         return toMoney(paymentRecord.getAmount());
      }
      if (orderPrivateCar.getPayMoney() != null && orderPrivateCar.getPayMoney() > 0) {
         return toMoney(orderPrivateCar.getPayMoney());
      }
      return BigDecimal.ZERO.setScale(2, RoundingMode.HALF_UP);
   }
   private Double getPrivateCarPrepayFinalPayAmount(OrderPrivateCar orderPrivateCar, Double finalAmount) throws Exception {
      if (!isPrivateCarPrepayFinalStage(orderPrivateCar)) {
         return toMoney(finalAmount).doubleValue();
      }
      BigDecimal paidAmount = getPrivateCarPrepayPaidAmount(orderPrivateCar);
      if (paidAmount.compareTo(BigDecimal.ZERO) <= 0) {
         return toMoney(finalAmount).doubleValue();
      }
      BigDecimal spreadAmount = toMoney(finalAmount).subtract(paidAmount).setScale(2, RoundingMode.HALF_UP);
      if (spreadAmount.compareTo(BigDecimal.ZERO) > 0) {
         orderPrivateCar.setPrepayStage(3);
         return spreadAmount.doubleValue();
      }
      return 0D;
   }
   private ResultUtil settlePrivateCarPrepayIfNoSecondPay(OrderPrivateCar orderPrivateCar, UserInfo userInfo, Double finalAmount) throws Exception {
      if (!isPrivateCarPrepayFinalStage(orderPrivateCar)) {
         return null;
      }
      BigDecimal paidAmount = getPrivateCarPrepayPaidAmount(orderPrivateCar);
      if (paidAmount.compareTo(BigDecimal.ZERO) <= 0) {
         return null;
      }
      BigDecimal settleAmount = toMoney(finalAmount);
      BigDecimal refundAmount = paidAmount.subtract(settleAmount).setScale(2, RoundingMode.HALF_UP);
      if (refundAmount.compareTo(BigDecimal.ZERO) < 0) {
         return null;
      }
      if (refundAmount.compareTo(BigDecimal.ZERO) > 0) {
         ResultUtil refundResult = refundPrivateCarPrepayDifference(orderPrivateCar, userInfo, refundAmount.doubleValue());
         if (!ResultUtil.SUCCESS.equals(refundResult.getCode())) {
            return refundResult;
         }
      }
      finishPrivateCarPrepayOrder(orderPrivateCar, settleAmount.doubleValue());
      return ResultUtil.success();
   }
   private ResultUtil refundPrivateCarPrepayDifference(OrderPrivateCar orderPrivateCar, UserInfo userInfo, Double refundAmount) throws Exception {
      if (Integer.valueOf(3).equals(orderPrivateCar.getPayType())) {
         userInfo.setBalance(new BigDecimal(null != userInfo.getBalance() ? userInfo.getBalance() : 0D)
               .add(toMoney(refundAmount)).setScale(2, BigDecimal.ROUND_HALF_EVEN).doubleValue());
         userInfoService.updateById(userInfo);
         orderPrivateCar.setPrepayRefundNo("BALANCE_REFUND_1_" + orderPrivateCar.getId() + "_" + ToolUtil.getRandomString(5));
         orderPrivateCar.setPrepayRefundAmount(refundAmount);
         orderPrivateCar.setPrepayRefundTime(new Date());
         transactionDetailsService.saveData(orderPrivateCar.getUserId(), "快车先付订单退款", refundAmount, 1, 1, 1, 1, orderPrivateCar.getId());
         return ResultUtil.success();
      }
      PaymentRecord paymentRecord = getPrivateCarPrepayPaidRecord(orderPrivateCar);
      if (paymentRecord == null) {
         return ResultUtil.error("先付订单缺少已支付流水,无法退款");
      }
      if (Integer.valueOf(1).equals(orderPrivateCar.getPayType())) {
         CbyzxPayUtil.ClosePayOrderRequest request = new CbyzxPayUtil.ClosePayOrderRequest();
         request.setOutRequestNo(ToolUtil.isNotEmpty(orderPrivateCar.getPrepaySecondPayNo()) ? orderPrivateCar.getPrepaySecondPayNo() : paymentRecord.getCode());
         request.setRefundOutRequestNo("PREPAY_DIFF_REFUND_1_" + orderPrivateCar.getId() + ToolUtil.getRandomString(5));
         request.setRefundAmount(toFen(refundAmount));
         request.setRefundDesc("快车先付订单多退少补退款");
         JSONObject response = CbyzxPayUtil.closePayOrder(request);
         if (!isPrivateCarPrepayRefundSuccess(response)) {
            return ResultUtil.error(extractPrivateCarRefundMessage(response), response);
         }
         orderPrivateCar.setPrepayRefundNo(extractPrivateCarRefundNo(response));
         orderPrivateCar.setPrepayRefundAmount(refundAmount);
         orderPrivateCar.setPrepayRefundTime(new Date());
         transactionDetailsService.saveData(orderPrivateCar.getUserId(), "快车先付订单退款", refundAmount, 1, 1, 1, 1, orderPrivateCar.getId());
         return ResultUtil.success(response);
      }
      if (Integer.valueOf(2).equals(orderPrivateCar.getPayType())) {
         Map<String, String> map = payMoneyUtil.aliRefund(paymentRecord.getCode(), refundAmount.toString());
         if (!"10000".equals(map.get("code"))) {
            return ResultUtil.error(map.get("msg"));
         }
         orderPrivateCar.setPrepayRefundNo(map.get("trade_no"));
         orderPrivateCar.setPrepayRefundAmount(refundAmount);
         orderPrivateCar.setPrepayRefundTime(new Date());
         transactionDetailsService.saveData(orderPrivateCar.getUserId(), "快车先付订单退款", refundAmount, 1, 1, 1, 1, orderPrivateCar.getId());
         return ResultUtil.success(map);
      }
      return ResultUtil.error("先付订单支付方式不支持退款");
   }
   private void finishPrivateCarPrepayOrder(OrderPrivateCar orderPrivateCar, Double settleAmount) throws Exception {
      orderPrivateCar.setState(8);
      orderPrivateCar.setPayMoney(settleAmount);
      orderPrivateCar.setPayTime(new Date());
      orderPrivateCar.setPrepayStage(5);
      if (orderPrivateCar.getRecoveryOrder() != null && 0 == orderPrivateCar.getRecoveryOrder()) {
         Double incomeAmount = (2 == orderPrivateCar.getAbnormal() && 2 == orderPrivateCar.getAbnormalStatus()
               ? orderPrivateCar.getResponsibilityMoney().doubleValue() : orderPrivateCar.getOrderMoney());
         Map<String, Object> map = incomeService.saveIncome(orderPrivateCar.getId(), 1, incomeAmount);
         orderPrivateCar.setSplitAllocation(JSON.toJSONString(map));
      }
      this.updateAllColumnById(orderPrivateCar);
      promotion(orderPrivateCar.getId());
      new Thread(new Runnable() {
         @Override
         public void run() {
            pushUtil.pushOrderState(1, orderPrivateCar.getUserId(), orderPrivateCar.getId(), 1, orderPrivateCar.getState(), 0);
            pushUtil.pushOrderState(2, orderPrivateCar.getDriverId(), orderPrivateCar.getId(), 1, orderPrivateCar.getState(), 0);
         }
      }).start();
      systemNoticeService.addSystemNotice(1, "您的快车先付订单已完成结算,请注意查收。", orderPrivateCar.getUserId(), 1);
   }
   private void consumePrivateCarBenefits(OrderPrivateCar orderPrivateCar) throws Exception {
      if (null != orderPrivateCar.getCouponId()) {
         UserCouponRecord userCouponRecord = userCouponRecordService.selectById(orderPrivateCar.getCouponId());