|
|
|
@@ -2,10 +2,13 @@ package com.iformall.service.helper; |
|
|
|
|
|
|
|
import com.iformall.common.ErrorCode; |
|
|
|
import com.iformall.domain.po.WxCoupon; |
|
|
|
import com.iformall.domain.po.base.TenantEntity; |
|
|
|
import com.iformall.enums.EnumCacheKey; |
|
|
|
import com.iformall.enums.EnumOrderStatus; |
|
|
|
import com.iformall.exception.MallinkException; |
|
|
|
import com.iformall.service.WxCouponChannelService; |
|
|
|
import com.iformall.service.WxCouponService; |
|
|
|
import com.iformall.service.WxOrderService; |
|
|
|
import com.iformall.utils.RedisLock; |
|
|
|
import org.slf4j.Logger; |
|
|
|
import org.slf4j.LoggerFactory; |
|
|
|
@@ -13,48 +16,63 @@ import org.springframework.beans.factory.annotation.Autowired; |
|
|
|
import org.springframework.context.annotation.Lazy; |
|
|
|
import org.springframework.stereotype.Service; |
|
|
|
|
|
|
|
import java.util.concurrent.CompletableFuture; |
|
|
|
|
|
|
|
@Service |
|
|
|
public class WxOrderServiceHelper { |
|
|
|
|
|
|
|
private static final Logger logger = LoggerFactory.getLogger(WxOrderServiceHelper.class); |
|
|
|
|
|
|
|
private static final Logger logger = LoggerFactory.getLogger(WxOrderServiceHelper.class); |
|
|
|
|
|
|
|
@Lazy |
|
|
|
@Autowired |
|
|
|
WxCouponChannelService wxCouponChannelService; |
|
|
|
|
|
|
|
|
|
|
|
@Lazy |
|
|
|
@Autowired |
|
|
|
WxCouponService wxCouponService; |
|
|
|
|
|
|
|
|
|
|
|
@Lazy |
|
|
|
@Autowired |
|
|
|
private WxOrderService wxOrderService; |
|
|
|
|
|
|
|
@Autowired |
|
|
|
RedisLock redisLock; |
|
|
|
|
|
|
|
/** |
|
|
|
* 微信订单是否可以支付 |
|
|
|
*/ |
|
|
|
public void reduceStock(boolean isChannelStockReduce, WxCoupon coupon,Long couponChannelId,int number) throws Exception{ |
|
|
|
if (isChannelStockReduce) { |
|
|
|
try { |
|
|
|
wxCouponChannelService.reduceChannelStock(coupon,couponChannelId,number); |
|
|
|
long stock = redisLock.decrease(EnumCacheKey.COUPON_CHANNEL_STOCK.getMessage()+couponChannelId, number); |
|
|
|
if (stock < 0 ) { |
|
|
|
throw new MallinkException(ErrorCode.ORDER_IS_FAIL.getCode(),"券["+coupon.getTitle()+"]渠道["+couponChannelId+"]库存已为零!"); |
|
|
|
} |
|
|
|
} catch (RuntimeException e) { |
|
|
|
throw new MallinkException(ErrorCode.ORDER_IS_FAIL.getCode(),"券["+coupon.getTitle()+"]渠道["+couponChannelId+"]库存更新失败!"+e.getMessage()); |
|
|
|
} |
|
|
|
|
|
|
|
}else { |
|
|
|
try { |
|
|
|
wxCouponService.reduceRemainInventory(coupon,coupon.getId(),number); |
|
|
|
long stock = redisLock.decrease(EnumCacheKey.COUPON_STOCK.getMessage()+coupon.getId(), number); |
|
|
|
if (stock < 0 ) { |
|
|
|
throw new MallinkException(ErrorCode.ORDER_IS_FAIL.getCode(),"券["+coupon.getTitle()+"]库存已为零!"); |
|
|
|
} |
|
|
|
} catch (RuntimeException e) { |
|
|
|
logger.error("此券减库存失败, couponId: " + coupon.getId(),e); |
|
|
|
throw new MallinkException(ErrorCode.ORDER_IS_FAIL.getCode(),"券["+coupon.getTitle()+"]库存更新失败!"+e.getMessage()); |
|
|
|
} |
|
|
|
} |
|
|
|
public void reduceStock(boolean isChannelStockReduce, WxCoupon coupon, Long couponChannelId, int number, Boolean isPressOrder) throws Exception { |
|
|
|
long stock; |
|
|
|
if (isChannelStockReduce) { |
|
|
|
try { |
|
|
|
wxCouponChannelService.reduceChannelStock(coupon, couponChannelId, number); |
|
|
|
stock = redisLock.decrease(EnumCacheKey.COUPON_CHANNEL_STOCK.getMessage() + couponChannelId, number); |
|
|
|
if (stock < 0) { |
|
|
|
throw new MallinkException(ErrorCode.ORDER_IS_FAIL.getCode(), "券[" + coupon.getTitle() + "]渠道[" + couponChannelId + "]库存已为零!"); |
|
|
|
} |
|
|
|
} catch (RuntimeException e) { |
|
|
|
throw new MallinkException(ErrorCode.ORDER_IS_FAIL.getCode(), "券[" + coupon.getTitle() + "]渠道[" + couponChannelId + "]库存更新失败!" + e.getMessage()); |
|
|
|
} |
|
|
|
|
|
|
|
} else { |
|
|
|
try { |
|
|
|
wxCouponService.reduceRemainInventory(coupon, coupon.getId(), number); |
|
|
|
stock = redisLock.decrease(EnumCacheKey.COUPON_STOCK.getMessage() + coupon.getId(), number); |
|
|
|
if (stock < 0) { |
|
|
|
throw new MallinkException(ErrorCode.ORDER_IS_FAIL.getCode(), "券[" + coupon.getTitle() + "]库存已为零!"); |
|
|
|
} |
|
|
|
} catch (RuntimeException e) { |
|
|
|
logger.error("此券减库存失败, couponId: " + coupon.getId(), e); |
|
|
|
throw new MallinkException(ErrorCode.ORDER_IS_FAIL.getCode(), "券[" + coupon.getTitle() + "]库存更新失败!" + e.getMessage()); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
// 如果是砍价订单,当库存不足时自动取消进行中的订单 |
|
|
|
CompletableFuture.runAsync(() -> { |
|
|
|
if (isPressOrder && stock <= 0) { |
|
|
|
wxOrderService.updateStatusByPressCouponId(coupon.getTenantId(), coupon.getCouponId(), EnumOrderStatus.ORDER_STATUS_PRESS_CANCEL.getCode()); |
|
|
|
} |
|
|
|
}); |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
} |