feat: 新增支付积分优惠券支持
This commit is contained in:
parent
7c572d7384
commit
b401f23ec4
|
|
@ -1186,14 +1186,24 @@ public class TbShopTableServiceImpl implements TbShopTableService {
|
|||
for (TbCashierCart cashierCart : newCashierCarts) {
|
||||
TbUserCouponVo couponVo = couponMap.get(cashierCart.getProductId());
|
||||
boolean useCoupon;
|
||||
if (couponVo != null) {
|
||||
if (couponVo != null && couponVo.getCurrentUseNum() > 0) {
|
||||
BigDecimal currentUseNum = BigDecimal.ZERO;
|
||||
if (cashierCart.getNumber() < couponVo.getCurrentUseNum()) {
|
||||
throw new BadRequestException("商品数量: {},小于优惠券使用数量: {}", cashierCart.getNumber(), couponVo.getCurrentUseNum());
|
||||
currentUseNum = BigDecimal.valueOf(couponVo.getCurrentUseNum());
|
||||
BigDecimal cartNum = BigDecimal.valueOf(cashierCart.getNumber());
|
||||
BigDecimal singlePackFee = cashierCart.getPackFee().divide(cartNum, RoundingMode.HALF_UP);
|
||||
cashierCart.setPackFee(singlePackFee.multiply(currentUseNum));
|
||||
cashierCart.setTotalAmount(cashierCart.getSalePrice().multiply(currentUseNum).add(singlePackFee.multiply(currentUseNum)));
|
||||
cashierCart.setNumber(couponVo.getCurrentUseNum());
|
||||
cashierCart.setTotalNumber(couponVo.getCurrentUseNum());
|
||||
cashierCart.setUserCouponId(couponVo.getId());
|
||||
discountAmount = discountAmount.add(cashierCart.getTotalAmount());
|
||||
couponVo.setCurrentUseNum(couponVo.getCurrentUseNum() - cashierCart.getNumber());
|
||||
}
|
||||
usedCouponMap.put(Integer.valueOf(cashierCart.getProductId()), couponVo);
|
||||
// 优惠券数量小于购物车数量,分割购物车数据
|
||||
if (cashierCart.getNumber() > couponVo.getCurrentUseNum()) {
|
||||
BigDecimal currentUseNum = BigDecimal.valueOf(couponVo.getCurrentUseNum());
|
||||
currentUseNum = BigDecimal.valueOf(couponVo.getCurrentUseNum());
|
||||
BigDecimal cartNum = BigDecimal.valueOf(cashierCart.getNumber());
|
||||
int balanceNum = cashierCart.getTotalNumber() - couponVo.getCurrentUseNum();
|
||||
BigDecimal singlePackFee = cashierCart.getPackFee().divide(cartNum, RoundingMode.HALF_UP);
|
||||
|
|
@ -1203,6 +1213,8 @@ public class TbShopTableServiceImpl implements TbShopTableService {
|
|||
cashierCart.setTotalNumber(couponVo.getCurrentUseNum());
|
||||
cashierCart.setUserCouponId(couponVo.getId());
|
||||
discountAmount = discountAmount.add(cashierCart.getTotalAmount());
|
||||
couponVo.setCurrentUseNum(couponVo.getCurrentUseNum() - cashierCart.getNumber());
|
||||
|
||||
|
||||
// 创建结余购物车
|
||||
TbCashierCart balanceCart = BeanUtil.copyProperties(cashierCart, TbCashierCart.class);
|
||||
|
|
@ -1215,8 +1227,10 @@ public class TbShopTableServiceImpl implements TbShopTableService {
|
|||
balanceCart.setTotalAmount(cashierCart.getSalePrice().multiply(num).add(balanceCart.getPackFee()));
|
||||
balanceCartList.add(balanceCart);
|
||||
} else {
|
||||
currentUseNum = BigDecimal.valueOf(cashierCart.getNumber());
|
||||
discountAmount = discountAmount.add(cashierCart.getTotalAmount());
|
||||
cashierCart.setUserCouponId(couponVo.getId());
|
||||
couponVo.setCurrentUseNum(couponVo.getCurrentUseNum() - cashierCart.getNumber());
|
||||
}
|
||||
// 消耗并返还商品优惠券
|
||||
Integer shopId = Integer.valueOf(cashierCart.getShopId());
|
||||
|
|
@ -1225,7 +1239,7 @@ public class TbShopTableServiceImpl implements TbShopTableService {
|
|||
tbActivateOutRecord.setGiveId(couponVo.getId());
|
||||
tbActivateOutRecord.setVipUserId(memberId);
|
||||
tbActivateOutRecord.setType(TableConstant.ActivateOutRecord.Type.FULL_REDUCTION.getValue());
|
||||
tbActivateOutRecord.setUseNum(couponVo.getCurrentUseNum());
|
||||
tbActivateOutRecord.setUseNum(currentUseNum.toBigInteger().intValue());
|
||||
tbActivateOutRecord.setStatus(TableConstant.ActivateOutRecord.Status.CLOSED.getValue());
|
||||
tbActivateOutRecord.setCreateTime(DateUtil.date());
|
||||
tbActivateOutRecord.setRefNum(0);
|
||||
|
|
@ -1339,22 +1353,28 @@ public class TbShopTableServiceImpl implements TbShopTableService {
|
|||
return cartInfoDTO;
|
||||
}
|
||||
|
||||
private boolean consumeAndReturnProductCoupon(List<TbActivateOutRecord> outRecordList, Integer memberId, TbOrderInfo orderInfo) {
|
||||
private void consumeCoupon(List<TbActivateOutRecord> outRecordList, Integer memberId, TbOrderInfo orderInfo) {
|
||||
boolean use = shopCouponService.use(Integer.valueOf(orderInfo.getShopId()), orderInfo.getId(), memberId, outRecordList);
|
||||
if (!use) {
|
||||
throw new BadRequestException("消耗券失败");
|
||||
}
|
||||
}
|
||||
|
||||
private void returnCoupon(TbOrderInfo orderInfo) {
|
||||
// 返还优惠券
|
||||
if (StrUtil.isNotBlank(orderInfo.getCouponInfoList())) {
|
||||
OrderCouponInfoDTO couponInfoDTO = JSONObject.parseObject(orderInfo.getCouponInfoList(), OrderCouponInfoDTO.class);
|
||||
// 券返还
|
||||
if (!couponInfoDTO.getOutRecordList().isEmpty()) {
|
||||
return shopCouponService.refund(couponInfoDTO.getOutRecordList());
|
||||
couponInfoDTO.getOutRecordList().forEach(item -> {
|
||||
item.setRefNum(item.getUseNum());
|
||||
});
|
||||
shopCouponService.refund(couponInfoDTO.getOutRecordList());
|
||||
couponInfoDTO.setOutRecordList(new ArrayList<>());
|
||||
orderInfo.setCouponInfoList(JSONObject.toJSONString(couponInfoDTO));
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
@ -1875,6 +1895,9 @@ public class TbShopTableServiceImpl implements TbShopTableService {
|
|||
}
|
||||
|
||||
private void calcDiscountAndUpdateInfo(PayDTO payDTO, TbOrderInfo orderInfo) {
|
||||
// 返还上次使用的券
|
||||
returnCoupon(orderInfo);
|
||||
|
||||
Set<String> productIdSet = new HashSet<>();
|
||||
List<TbCashierCart> cashierCarts = mpCashierCartService.selectByOrderIdAndState(orderInfo.getId(), null);
|
||||
ArrayList<TbCashierCart> activateCartInfo = new ArrayList<>();
|
||||
|
|
@ -1908,7 +1931,7 @@ public class TbShopTableServiceImpl implements TbShopTableService {
|
|||
}
|
||||
|
||||
// 消耗优惠券并返回上次未使用的券
|
||||
consumeAndReturnProductCoupon(couponInfo.getOutRecordList(), payDTO.getVipUserId(), orderInfo);
|
||||
consumeCoupon(couponInfo.getOutRecordList(), payDTO.getVipUserId(), orderInfo);
|
||||
|
||||
orderInfo.setCouponInfoList(JSONObject.toJSONString(couponInfo));
|
||||
// 更新订单信息
|
||||
|
|
@ -1916,7 +1939,7 @@ public class TbShopTableServiceImpl implements TbShopTableService {
|
|||
}
|
||||
|
||||
@Override
|
||||
@Transactional
|
||||
// @Transactional
|
||||
public Object pay(PayDTO payDTO) {
|
||||
return Utils.runFunAndCheckKey(() -> {
|
||||
long count = tbShopPayTypeMapper.selectCount(new LambdaQueryWrapper<TbShopPayType>()
|
||||
|
|
@ -1983,7 +2006,7 @@ public class TbShopTableServiceImpl implements TbShopTableService {
|
|||
if (!isOnline) {
|
||||
orderInfo.setPaidTime(DateUtil.current());
|
||||
orderInfo.setPayAmount(finalAmount);
|
||||
orderInfo.setStatus("closed");
|
||||
// orderInfo.setStatus("closed");
|
||||
if (payDTO.getDiscount() != null) {
|
||||
orderInfo.setDiscountRatio(BigDecimal.valueOf(payDTO.getDiscount()));
|
||||
orderInfo.setDiscountAmount(orderInfo.getAmount().subtract(finalAmount));
|
||||
|
|
|
|||
Loading…
Reference in New Issue