fix: 优惠券折扣修改

This commit is contained in:
SongZhang 2024-11-05 17:07:20 +08:00
parent 648bd55e7a
commit b583d01312
5 changed files with 112 additions and 23 deletions

View File

@ -56,5 +56,12 @@ public interface MpOrderDetailService extends IService<TbOrderDetail> {
* @return 详情信息
*/
List<TbOrderDetail> selectByCartIdOrOrderId(Integer shopId, List<Integer> cartIdList, Integer orderId);
/**
* 根据订单id修改详情状态
* @param status 状态
* @param orderId 订单id
*/
boolean updateStatusByOrderId(TableConstant.OrderInfo.Status status, Integer orderId);
}

View File

@ -61,5 +61,12 @@ public class MpOrderDetailServiceImpl extends ServiceImpl<TbOrderDetailMapper, T
.and(q -> q.in(TbOrderDetail::getCartId, cartIdList).or().eq(TbOrderDetail::getOrderId, orderId))
.eq(TbOrderDetail::getShopId, shopId));
}
@Override
public boolean updateStatusByOrderId(TableConstant.OrderInfo.Status status, Integer orderId) {
return update(new LambdaUpdateWrapper<TbOrderDetail>()
.eq(TbOrderDetail::getOrderId, orderId)
.set(TbOrderDetail::getStatus, status.getValue()));
}
}

View File

@ -95,7 +95,7 @@ public class TestTask {
log.info("购物车清楚记录开始结束");
}
@Transactional(rollbackFor = Exception.class)
// @Scheduled(fixedRate = 30000)
public void cancelOrder(){
log.info("订单取消定时任务执行");
orderInfoService.cancelOrder();

View File

@ -6,6 +6,7 @@ import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import cn.ysk.cashier.cons.TableConstant;
import cn.ysk.cashier.cons.rabbit.RabbitConstants;
import cn.ysk.cashier.dto.order.OrderCouponInfoDTO;
import cn.ysk.cashier.dto.order.TbOrderInfoDto;
import cn.ysk.cashier.dto.order.TbOrderInfoQueryCriteria;
import cn.ysk.cashier.dto.order.TbPayCountQueryCriteria;
@ -85,9 +86,6 @@ public class TbOrderInfoServiceImpl implements TbOrderInfoService {
private final TbOrderDetailRepository tbOrderDetailRepository;
private final TbShopPayTypeRepository payTypeRepository;
private final TbProductSkuRepository skuRepository;
private final TbProductMapper productMapper;
private final TbProductSkuMapper productSkuMapper;
private final TbProductService productService;
private final TbOrderPaymentService paymentService;
private final TbMerchantThirdApplyRepository thirdApplyRepository;
@ -100,13 +98,14 @@ public class TbOrderInfoServiceImpl implements TbOrderInfoService {
private final TbMShopUserMapper tbMShopUserMapper;
private final TbShopUserFlowMapper tbShopUserFlowMapper;
private final RabbitMsgUtils rabbitMsgUtils;
private final TbCashierCartMapper tbCashierCartMapper;
private final PayService payService;
private final MpOrderDetailService mpOrderDetailService;
private final TbOrderDetailMapper tbOrderDetailMapper;
private final MpCashierCartService mpCashierCartService;
private final MpShopInfoMapper mpShopInfoMapper;
private final TbMemberPointsService memberPointsService;
private final TbShopCouponService shopCouponService;
private final MpOrderDetailService mpOrderDetailService;
@Value("${thirdPay.url}")
private String url;
@ -205,7 +204,7 @@ public class TbOrderInfoServiceImpl implements TbOrderInfoService {
for (TbFullOrderDetail detail : details) {
if (TableConstant.CART_SEAT_ID.equals(detail.getProductId().toString())) {
seatInfo = detail;
}else {
} else {
detailList.add(detail);
}
}
@ -661,12 +660,48 @@ public class TbOrderInfoServiceImpl implements TbOrderInfoService {
public void cancelOrder() {
List<TbOrderInfo> tbOrderInfos = orderInfoMapper.selectList(new LambdaQueryWrapper<TbOrderInfo>()
.eq(TbOrderInfo::getStatus, "unpaid")
.lt(TbOrderInfo::getCreatedAt, cn.hutool.core.date.DateUtil.current() - 15 * 60 * 1000));
.and(q -> q.and(qu -> qu.eq(TbOrderInfo::getUseType, TableConstant.OrderInfo.UseType.DINE_IN_AFTER.getValue()).lt(TbOrderInfo::getCreatedAt, cn.hutool.core.date.DateUtil.current() - 12 * 60 * 60 * 1000))
.or(qu -> qu.ne(TbOrderInfo::getUseType, TableConstant.OrderInfo.UseType.DINE_IN_AFTER.getValue())
.lt(TbOrderInfo::getCreatedAt, cn.hutool.core.date.DateUtil.current() - 15 * 60 * 1000))));
log.info("超时订单: {}", tbOrderInfos);
List<Integer> ids = tbOrderInfos.stream().map(TbOrderInfo::getId).collect(Collectors.toList());
orderInfoMapper.update(null, new LambdaUpdateWrapper<TbOrderInfo>()
.in(TbOrderInfo::getId, ids)
.eq(TbOrderInfo::getStatus, "unpaid")
.set(TbOrderInfo::getStatus, "cancel"));
tbOrderInfos.parallelStream().forEach(orderInfo -> {
try {
// 查询是否消耗了积分优惠券返还对应的卷或商品
if (orderInfo.getCouponInfoList() != null) {
OrderCouponInfoDTO couponInfoDTO = JSONObject.parseObject(orderInfo.getCouponInfoList(), OrderCouponInfoDTO.class);
// 券返还
if (!couponInfoDTO.getOutRecordList().isEmpty()) {
couponInfoDTO.getOutRecordList().forEach(item -> {
item.setRefNum(item.getUseNum());
});
shopCouponService.refund(couponInfoDTO.getOutRecordList());
couponInfoDTO.setOutRecordList(new ArrayList<>());
orderInfo.setCouponInfoList(null);
}
}
// 返还积分
if (orderInfo.getPointsNum() != null && orderInfo.getPointsNum() != 0 && orderInfo.getMemberId() != null) {
memberPointsService.addPoints(Long.valueOf(orderInfo.getMemberId()), orderInfo.getPointsNum(), "订单取消返还: " + orderInfo.getPointsNum() + "积分", Long.valueOf(orderInfo.getId()));
}
mpCashierCartService.updateStateByOrderId(TableConstant.OrderInfo.Status.CLOSED, orderInfo.getId());
mpOrderDetailService.updateStatusByOrderId(TableConstant.OrderInfo.Status.CLOSED, orderInfo.getId());
// 修改订单信息
orderInfoMapper.update(null, new LambdaUpdateWrapper<TbOrderInfo>()
.eq(TbOrderInfo::getId, orderInfo.getId())
.eq(TbOrderInfo::getStatus, "unpaid")
.set(TbOrderInfo::getCouponInfoList, null)
.set(TbOrderInfo::getPointsNum, 0)
.set(TbOrderInfo::getUpdatedAt, cn.hutool.core.date.DateUtil.current())
.set(TbOrderInfo::getStatus, "cancel"));
}catch (Exception e) {
log.error("取消订单失败: 订单信息: {}", orderInfo);
log.error("异常信息: ", e);
}
});
// List<Integer> ids = tbOrderInfos.stream().map(TbOrderInfo::getId).collect(Collectors.toList());
}
}

View File

@ -1405,6 +1405,9 @@ public class TbShopTableServiceImpl implements TbShopTableService {
if (!"return".equals(cashierCart.getStatus()) && cashierCart.getUserCouponId() == null) {
priceDTO.setTotalAmount(priceDTO.getTotalAmount().add(cashierCart.getTotalAmount()));
priceDTO.setPackAmount(priceDTO.getPackAmount().add(cashierCart.getPackFee()));
}
if(!"return".equals(cashierCart.getStatus())) {
priceDTO.setOriginAmount(priceDTO.getOriginAmount().add(cashierCart.getTotalAmount()));
}
@ -1891,7 +1894,30 @@ public class TbShopTableServiceImpl implements TbShopTableService {
return discountAmount;
}
private void calcDiscountAndUpdateInfo(PayDTO payDTO, TbOrderInfo orderInfo) {
private BigDecimal getCartCouponDiscount(TbOrderInfo orderInfo, PayDTO payDTO) {
Set<String> productIdSet = new HashSet<>();
List<TbCashierCart> cashierCarts = mpCashierCartService.selectByOrderIdAndState(orderInfo.getId(), null);
ArrayList<TbCashierCart> activateCartInfo = new ArrayList<>();
for (TbCashierCart cashierCart : cashierCarts) {
productIdSet.add(cashierCart.getProductId());
if (TableConstant.OrderInfo.Status.CREATE.equalsVals(cashierCart.getStatus()) || TableConstant.OrderInfo.Status.FINAL.equalsVals(cashierCart.getStatus())) {
activateCartInfo.add(cashierCart);
}
}
// 获取优惠券信息
OrderCouponInfoDTO couponInfo = new OrderCouponInfoDTO();
if (!payDTO.getUserCouponInfos().isEmpty()) {
couponInfo = getCouponInfo(payDTO.getVipUserId(), payDTO.getShopId(), payDTO.getUserCouponInfos(),
orderInfo.getOrderAmount(), productIdSet);
}
BigDecimal productDiscount = calcCartPriceWithCoupon(activateCartInfo, couponInfo, payDTO.getVipUserId(), orderInfo);
orderInfo.setProductCouponDiscountAmount(productDiscount);
return productDiscount;
}
private BigDecimal calcDiscountAndUpdateInfo(PayDTO payDTO, TbOrderInfo orderInfo) {
// 返还上次使用的券
returnCoupon(orderInfo);
@ -1915,11 +1941,15 @@ public class TbShopTableServiceImpl implements TbShopTableService {
BigDecimal productDiscount = calcCartPriceWithCoupon(activateCartInfo, couponInfo, payDTO.getVipUserId(), orderInfo);
orderInfo.setProductCouponDiscountAmount(productDiscount);
// 更新订单信息
OrderPriceDTO priceDTO = createOrderDetailWithCoupon(activateCartInfo, orderInfo.getId(), payDTO.getShopId(), false);
BigDecimal finalAmount = priceDTO.getTotalAmount().multiply(BigDecimal.valueOf(payDTO.getDiscount())).setScale(2, RoundingMode.HALF_UP);
orderInfo.setUpdatedAt(System.currentTimeMillis());
orderInfo.setSettlementAmount(priceDTO.getTotalAmount());
orderInfo.setAmount(priceDTO.getTotalAmount());
orderInfo.setOrderAmount(priceDTO.getTotalAmount());
orderInfo.setSettlementAmount(finalAmount);
orderInfo.setAmount(finalAmount);
orderInfo.setOrderAmount(finalAmount);
// 计算订单优惠
TbActivateOutRecord outRecord = calcOrderInfoDiscount(payDTO, orderInfo, couponInfo);
@ -1931,8 +1961,7 @@ public class TbShopTableServiceImpl implements TbShopTableService {
consumeCoupon(couponInfo.getOutRecordList(), payDTO.getVipUserId(), orderInfo);
orderInfo.setCouponInfoList(JSONObject.toJSONString(couponInfo));
// 更新订单信息
return orderInfo.getOrderAmount();
}
@Override
@ -1950,10 +1979,13 @@ public class TbShopTableServiceImpl implements TbShopTableService {
TbOrderInfo orderInfo = orderInfoMapper.selectById(payDTO.getOrderId());
// 计算优惠券价格信息
if (payDTO.getVipUserId() != null) {
calcDiscountAndUpdateInfo(payDTO, orderInfo);
if (payDTO.getVipUserId() == null) {
payDTO.setVipUserId(Integer.valueOf(orderInfo.getMemberId()));
}
if (ObjectUtil.isEmpty(orderInfo)) {
throw new BadRequestException("订单不存在");
}
@ -1967,7 +1999,16 @@ public class TbShopTableServiceImpl implements TbShopTableService {
payDTO.setDiscount((double) 1);
}
BigDecimal finalAmount = orderInfo.getOrderAmount().multiply(BigDecimal.valueOf(payDTO.getDiscount())).setScale(2, RoundingMode.HALF_UP);
// 计算优惠券积分折扣信息
BigDecimal finalAmount = null;
if (payDTO.getVipUserId() != null) {
// 计算商品券折扣
finalAmount = calcDiscountAndUpdateInfo(payDTO, orderInfo);
}
if (finalAmount == null) {
finalAmount = orderInfo.getOrderAmount().multiply(BigDecimal.valueOf(payDTO.getDiscount())).setScale(2, RoundingMode.HALF_UP);
}
boolean isOnline = false;
@ -2085,7 +2126,6 @@ public class TbShopTableServiceImpl implements TbShopTableService {
return "success";
}, stringRedisTemplate, RedisConstant.getLockKey("", payDTO.getShopId(), payDTO.getOrderId()));
}
@Override