金额问题
This commit is contained in:
parent
99f9f8b4e3
commit
5719fdef74
|
|
@ -0,0 +1,22 @@
|
|||
package com.czg.service.order.dto;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
import java.math.BigDecimal;
|
||||
|
||||
/**
|
||||
* @author ww
|
||||
* @description
|
||||
*/
|
||||
@Data
|
||||
public class BigDecimalDTO {
|
||||
private BigDecimal price;
|
||||
|
||||
public BigDecimalDTO(BigDecimal price) {
|
||||
this.price = price;
|
||||
}
|
||||
|
||||
public BigDecimal getPrice() {
|
||||
return price == null ? BigDecimal.ZERO : price;
|
||||
}
|
||||
}
|
||||
|
|
@ -31,6 +31,7 @@ import com.czg.order.vo.OrderDetailSmallVO;
|
|||
import com.czg.order.vo.OrderInfoVo;
|
||||
import com.czg.sa.StpKit;
|
||||
import com.czg.service.RedisService;
|
||||
import com.czg.service.order.dto.BigDecimalDTO;
|
||||
import com.czg.service.order.enums.OrderStatusEnums;
|
||||
import com.czg.service.order.mapper.OrderInfoMapper;
|
||||
import com.czg.utils.AssertUtil;
|
||||
|
|
@ -225,49 +226,47 @@ public class OrderInfoServiceImpl extends ServiceImpl<OrderInfoMapper, OrderInfo
|
|||
if (!shopUser.getIsVip().equals(1) && pointSetting.getDeductionGroup().contains("vip")) {
|
||||
throw new ValidateException("生成支付订单失败,该店铺仅会员可使用积分抵扣");
|
||||
}
|
||||
if (param.getDiscountRatio().compareTo(BigDecimal.ZERO) <= 0 && param.getDiscountRatio().compareTo(BigDecimal.ONE) > 0) {
|
||||
throw new ValidateException("生成支付订单失败,折扣比例不正确");
|
||||
}
|
||||
param.setUserId(userInfo.getId());
|
||||
}
|
||||
if (param.getDiscountRatio().compareTo(BigDecimal.ZERO) <= 0 && param.getDiscountRatio().compareTo(BigDecimal.ONE) > 0) {
|
||||
throw new ValidateException("生成支付订单失败,折扣比例不正确");
|
||||
}
|
||||
//商品券 <商品id,数量>
|
||||
Map<Long, Integer> prodCouponMap = new HashMap<>();
|
||||
//满减券 满fullCouponAmount 减disCouponAmount
|
||||
String fullAmountStr = "0";
|
||||
String discountAmountStr = "0";
|
||||
BigDecimalDTO fullAmount = new BigDecimalDTO(BigDecimal.ZERO);
|
||||
BigDecimalDTO discountAmount = new BigDecimalDTO(BigDecimal.ZERO);
|
||||
//校验优惠券
|
||||
checkCoupon(prodCouponMap, fullAmountStr, discountAmountStr, param);
|
||||
BigDecimal fullAmount = new BigDecimal(fullAmountStr);
|
||||
BigDecimal discountAmount = new BigDecimal(discountAmountStr);
|
||||
checkCoupon(prodCouponMap, fullAmount, discountAmount, param);
|
||||
|
||||
//获取商品信息 计算金额 需要传入优惠券 减去优惠券
|
||||
List<OrderDetail> orderDetails = orderDetailService.queryChain().eq(OrderDetail::getOrderId, param.getOrderId()).select().list();
|
||||
//商品优惠券金额
|
||||
String prodCouponAmountStr = "0";
|
||||
BigDecimalDTO prodCouponAmount = new BigDecimalDTO(BigDecimal.ZERO);
|
||||
//总商品支付金额 不包含打包费 用来计算后续
|
||||
String totalAmountStr = "0";
|
||||
processOrderDetails2(orderDetails, prodCouponMap, prodCouponAmountStr, totalAmountStr);
|
||||
BigDecimal prodCouponAmount = new BigDecimal(prodCouponAmountStr);
|
||||
BigDecimalDTO totalAmount = new BigDecimalDTO(BigDecimal.ZERO);
|
||||
processOrderDetails2(orderDetails, prodCouponMap, prodCouponAmount, totalAmount);
|
||||
//总商品支付金额 不包含打包费 用来计算后续
|
||||
BigDecimal totalAmount = new BigDecimal(totalAmountStr);
|
||||
log.info("支付前置,商品金额{} 商品优惠券金额{} 总金额{}", totalAmount, prodCouponAmount, totalAmount.add(orderInfo.getPackFee()));
|
||||
if (prodCouponAmount.compareTo(param.getProductCouponDiscountAmount()) != 0) {
|
||||
log.info("支付前置,商品金额{} 商品优惠券金额{} 总金额{}", totalAmount, prodCouponAmount, totalAmount.getPrice().add(orderInfo.getPackFee()));
|
||||
if (prodCouponAmount.getPrice().compareTo(param.getProductCouponDiscountAmount()) != 0) {
|
||||
throw new ValidateException("生成支付订单失败,商品优惠券优惠金额不正确");
|
||||
}
|
||||
//折扣金额 如 9折 计算 为 订单金额*0.9 向上取整
|
||||
BigDecimal newTotalAmount = BigDecimal.ZERO;
|
||||
//折扣金额 如 9折 计算 为 订单金额*0.9 向上取整
|
||||
if (param.getDiscountRatio().compareTo(BigDecimal.ONE) != 0) {
|
||||
newTotalAmount = totalAmount.multiply(param.getDiscountRatio()).setScale(2, RoundingMode.UP);
|
||||
if (param.getDiscountAmount().compareTo(totalAmount.subtract(newTotalAmount)) != 0) {
|
||||
newTotalAmount = totalAmount.getPrice().multiply(param.getDiscountRatio()).setScale(2, RoundingMode.UP);
|
||||
if (param.getDiscountAmount().compareTo(totalAmount.getPrice().subtract(newTotalAmount)) != 0) {
|
||||
throw new ValidateException("生成支付订单失败,折扣金额不正确");
|
||||
}
|
||||
} else {
|
||||
newTotalAmount = totalAmount.getPrice();
|
||||
}
|
||||
//满减券 校验
|
||||
if (newTotalAmount.compareTo(fullAmount) < 0) {
|
||||
if (newTotalAmount.compareTo(fullAmount.getPrice()) < 0) {
|
||||
throw new ValidateException("生成支付订单失败,满减券不满足条件");
|
||||
}
|
||||
//减去满减优惠券
|
||||
newTotalAmount = newTotalAmount.subtract(discountAmount);
|
||||
newTotalAmount = newTotalAmount.subtract(discountAmount.getPrice());
|
||||
//积分抵扣 金额范围校验 抵扣金额校验
|
||||
if (param.getPointsNum() > 0) {
|
||||
if (pointSetting.getMinPaymentAmount().compareTo(newTotalAmount) > 0) {
|
||||
|
|
@ -325,15 +324,13 @@ public class OrderInfoServiceImpl extends ServiceImpl<OrderInfoMapper, OrderInfo
|
|||
/**
|
||||
* 填充 优惠券使用数量 以及 付款金额
|
||||
*
|
||||
* @param orderDetails 订单详情 需要回填 优惠券抵扣数量
|
||||
* @param prodCouponMap 使用优惠券<商品id,数量>
|
||||
* @param prodCouponAmountStr 商品券优惠金额 商品单价*优惠数量 的总和
|
||||
* @param totalAmountStr 最终总金额(没加打包费 餐位费) 去除优惠券金额后的
|
||||
* @param orderDetails 订单详情 需要回填 优惠券抵扣数量
|
||||
* @param prodCouponMap 使用优惠券<商品id,数量>
|
||||
* @param prodCouponAmount 商品券优惠金额 商品单价*优惠数量 的总和
|
||||
* @param totalAmount 最终总金额(没加打包费 餐位费) 去除优惠券金额后的
|
||||
*/
|
||||
private void processOrderDetails2(List<OrderDetail> orderDetails, Map<Long, Integer> prodCouponMap,
|
||||
String prodCouponAmountStr, String totalAmountStr) {
|
||||
BigDecimal prodCouponAmount = BigDecimal.ZERO;
|
||||
BigDecimal totalAmount = BigDecimal.ZERO;
|
||||
BigDecimalDTO prodCouponAmount, BigDecimalDTO totalAmount) {
|
||||
Map<Long, List<OrderDetail>> detailMap = new HashMap<>();
|
||||
for (OrderDetail detail : orderDetails) {
|
||||
detailMap.computeIfAbsent(detail.getProductId(), k -> new ArrayList<>()).add(detail);
|
||||
|
|
@ -360,12 +357,12 @@ public class OrderInfoServiceImpl extends ServiceImpl<OrderInfoMapper, OrderInfo
|
|||
orderDetail.setPayAmount((orderDetail.getNum().subtract(couponNum)).multiply(orderDetail.getPrice()));
|
||||
couponNum = BigDecimal.ZERO;
|
||||
}
|
||||
prodCouponAmount = prodCouponAmount.add(orderDetail.getPrice().multiply(orderDetail.getCouponNum()));
|
||||
prodCouponAmount.setPrice(prodCouponAmount.getPrice().add(orderDetail.getPrice().multiply(orderDetail.getCouponNum())));
|
||||
} else {
|
||||
orderDetail.setCouponNum(BigDecimal.ZERO);
|
||||
orderDetail.setPayAmount(orderDetail.getNum().multiply(orderDetail.getPrice()));
|
||||
}
|
||||
totalAmount = totalAmount.add(orderDetail.getPayAmount());
|
||||
totalAmount.setPrice(totalAmount.getPrice().add(orderDetail.getPayAmount()));
|
||||
resultList.add(orderDetail);
|
||||
}
|
||||
if (couponNum.compareTo(BigDecimal.ZERO) != 0) {
|
||||
|
|
@ -373,16 +370,13 @@ public class OrderInfoServiceImpl extends ServiceImpl<OrderInfoMapper, OrderInfo
|
|||
}
|
||||
}
|
||||
orderDetails = resultList;
|
||||
//值传递问题
|
||||
prodCouponAmountStr = prodCouponAmount.toPlainString();
|
||||
totalAmountStr = totalAmount.toPlainString();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 校验优惠券可用性
|
||||
*/
|
||||
private void checkCoupon(Map<Long, Integer> prodCouponMap, String fullAmountStr, String discountAmountStr, CheckOrderPay param) {
|
||||
private void checkCoupon(Map<Long, Integer> prodCouponMap, BigDecimalDTO fullAmount, BigDecimalDTO discountAmount, CheckOrderPay param) {
|
||||
if (CollUtil.isNotEmpty(param.getCouponList())) {
|
||||
//校验优惠券
|
||||
List<ShopActivateCouponRecord> records = couponRecordService.list(QueryWrapper.create()
|
||||
|
|
@ -399,14 +393,14 @@ public class OrderInfoServiceImpl extends ServiceImpl<OrderInfoMapper, OrderInfo
|
|||
if (isFullMinus) {
|
||||
throw new ValidateException("生成支付订单失败,满减券仅可使用一张");
|
||||
}
|
||||
fullAmountStr = record.getFullAmount().toPlainString();
|
||||
discountAmountStr = record.getDiscountAmount().toPlainString();
|
||||
fullAmount.setPrice(record.getFullAmount());
|
||||
discountAmount.setPrice(record.getDiscountAmount());
|
||||
isFullMinus = true;
|
||||
} else if (record.getType().equals(2)) {
|
||||
prodCouponMap.compute(record.getProId(), (key, oldValue) -> oldValue == null ? 1 : oldValue + 1);
|
||||
}
|
||||
}
|
||||
if (new BigDecimal(discountAmountStr).compareTo(param.getFullCouponDiscountAmount()) != 0) {
|
||||
if (discountAmount.getPrice().compareTo(param.getFullCouponDiscountAmount()) != 0) {
|
||||
throw new ValidateException("生成支付订单失败,满减券减免金额不正确");
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue