diff --git a/service/src/main/java/com/hfkj/service/order/impl/BsOrderServiceImpl.java b/service/src/main/java/com/hfkj/service/order/impl/BsOrderServiceImpl.java index 3139569..53fdc9f 100644 --- a/service/src/main/java/com/hfkj/service/order/impl/BsOrderServiceImpl.java +++ b/service/src/main/java/com/hfkj/service/order/impl/BsOrderServiceImpl.java @@ -223,49 +223,64 @@ public class BsOrderServiceImpl implements BsOrderService { BigDecimal payRealPrice = totalPrice.subtract(order.getDeduction().getTotalDeductionPrice()); order.setPayRealPrice(payRealPrice.compareTo(new BigDecimal("0")) > 0 ? payRealPrice : new BigDecimal("0")); order.setOrderStatus(order.getPayRealPrice().compareTo(new BigDecimal("0")) == 0?OrderStatusEnum.status2.getCode():OrderStatusEnum.status1.getCode()); + BigDecimal totalChildDiscountPrice = new BigDecimal("0"); + Long totalIntegralDiscountPrice = 0L; // 订单入库前处理 - for (OrderChildModel childOrder : order.getOrderChildList()) { + for (int i = 0; i < order.getOrderChildList().size();i++) { + OrderChildModel childOrder = order.getOrderChildList().get(i); childOrder.setSurplusRefundCount(childOrder.getProductCount()); // 计算 子订单 在交易订单金额中的占比 BigDecimal ratio = childOrder.getProductTotalPrice().divide(order.getTotalPrice(), 2, BigDecimal.ROUND_DOWN); - // 计算子订单退款积分 + // 优惠券优惠 + if (order.getDeduction().getCouponDiscountActualPrice().compareTo(BigDecimal.ZERO) > 0) { + childOrder.setCouponDiscountPrice(order.getDeduction().getCouponDiscountActualPrice().multiply(ratio).setScale(2, BigDecimal.ROUND_DOWN)); + } else { + childOrder.setCouponDiscountPrice(new BigDecimal("0")); + } + totalChildDiscountPrice = totalChildDiscountPrice.add(childOrder.getCouponDiscountPrice()); + + // 积分 if (order.getDeduction().getIntegralDiscountPrice() > 0) { - childOrder.setSurplusRefundIntegral(new BigDecimal(order.getDeduction().getIntegralDiscountPrice().toString()).multiply(ratio).setScale(2).longValue()); + childOrder.setIntegralDiscountPrice(new BigDecimal(order.getDeduction().getIntegralDiscountPrice().toString()).multiply(ratio).setScale(2, BigDecimal.ROUND_DOWN).longValue()); } else { - childOrder.setSurplusRefundIntegral(0L); + childOrder.setIntegralDiscountPrice(0L); } + totalIntegralDiscountPrice = totalIntegralDiscountPrice + childOrder.getIntegralDiscountPrice(); + + // 如果是循环的最后一笔商品订单 + if ((i+1) == order.getOrderChildList().size()) { + // 优惠券差值 + BigDecimal couponDiscountDifference = order.getDeduction().getCouponDiscountActualPrice().subtract(totalChildDiscountPrice); + if (couponDiscountDifference.compareTo(BigDecimal.ZERO) > 0) { + // 大于0 + childOrder.setCouponDiscountPrice(childOrder.getCouponDiscountPrice().add(couponDiscountDifference)); + } else if (couponDiscountDifference.compareTo(BigDecimal.ZERO) < 0) { + // 小于0 + childOrder.setCouponDiscountPrice(childOrder.getCouponDiscountPrice().subtract(couponDiscountDifference)); + } - if (order.getDeduction().getCouponDiscountActualPrice().compareTo(BigDecimal.ZERO) == 1) { - // 计算子订单退款金额。 - if (order.getPayRealPrice().compareTo(BigDecimal.ZERO) == 1) { - // 计算实付金额中的占比 - childOrder.setSurplusRefundPrice(order.getPayRealPrice().multiply(ratio).setScale(2, BigDecimal.ROUND_DOWN)); - } else { - childOrder.setSurplusRefundPrice(new BigDecimal("0")); + // 积分差值 + Long integralDiscountDifference = order.getDeduction().getIntegralDiscountPrice() - totalIntegralDiscountPrice; + if (integralDiscountDifference > 0) { + // 大于0 + childOrder.setIntegralDiscountPrice(childOrder.getIntegralDiscountPrice() + integralDiscountDifference); + } else if (integralDiscountDifference < 0) { + // 小于0 + childOrder.setIntegralDiscountPrice(childOrder.getIntegralDiscountPrice() - integralDiscountDifference); } - } else { - childOrder.setSurplusRefundPrice( - // 商品总金额 - 积分抵扣金额 - childOrder.getProductTotalPrice().subtract( - new BigDecimal(childOrder.getSurplusRefundIntegral().toString()).divide(new BigDecimal("100")) - )); } - childOrder.setIntegralDiscountPrice(childOrder.getSurplusRefundIntegral()); - childOrder.setCouponDiscountPrice( - // 可退款金额大于0 可退款金额+可退款积分=优惠券优惠 - childOrder.getSurplusRefundPrice().compareTo(BigDecimal.ZERO) == 1 - ? childOrder.getProductTotalPrice().subtract( - childOrder.getSurplusRefundPrice().add(new BigDecimal(childOrder.getSurplusRefundIntegral().toString()).divide(new BigDecimal("100")))) - : childOrder.getSurplusRefundPrice()); - - childOrder.setTotalDeductionPrice( - childOrder.getCouponDiscountPrice() - .add(new BigDecimal(childOrder.getIntegralDiscountPrice().toString()).divide(new BigDecimal("100"))) - ); - childOrder.setProductActualPrice(childOrder.getProductTotalPrice().subtract(childOrder.getTotalDeductionPrice())); + // 商品订单总优惠 + childOrder.setTotalDeductionPrice(childOrder.getCouponDiscountPrice().add(new BigDecimal(childOrder.getIntegralDiscountPrice()))); + + // 可退款金额 产品总价 - 优惠总价 + childOrder.setSurplusRefundPrice(childOrder.getProductTotalPrice().subtract(childOrder.getTotalDeductionPrice())); + // 可退款积分 积分优惠 + childOrder.setSurplusRefundIntegral(childOrder.getIntegralDiscountPrice()); + // 商品实际价格 + childOrder.setProductActualPrice(childOrder.getSurplusRefundPrice()); orderChildService.editData(childOrder); }