...
|
...
|
@@ -3,11 +3,11 @@ package com.yohoufo.order.charge; |
|
|
import com.yoho.core.common.utils.YHMath;
|
|
|
import com.yoho.error.ServiceError;
|
|
|
import com.yoho.error.exception.ServiceException;
|
|
|
import com.yohobuy.ufo.model.order.constants.OrderConstant;
|
|
|
import com.yohobuy.ufo.model.promotion.UserCouponsBo;
|
|
|
import com.yohoufo.order.charge.coupon.CouponService;
|
|
|
import com.yohoufo.order.charge.model.*;
|
|
|
import com.yohoufo.order.constants.CouponConstants;
|
|
|
import com.yohoufo.order.service.support.DeliveryWayCostSupport;
|
|
|
import com.yohoufo.order.service.proxy.CouponProxyService;
|
|
|
import com.yohoufo.order.service.support.ShoppingSupport;
|
|
|
import com.yohoufo.order.utils.LoggerUtils;
|
|
|
import org.apache.commons.collections.CollectionUtils;
|
...
|
...
|
@@ -16,6 +16,7 @@ import org.springframework.beans.factory.annotation.Autowired; |
|
|
import org.springframework.stereotype.Component;
|
|
|
|
|
|
import java.util.List;
|
|
|
import java.util.Objects;
|
|
|
import java.util.stream.Collectors;
|
|
|
|
|
|
/**
|
...
|
...
|
@@ -27,17 +28,14 @@ public class ChargeService { |
|
|
private final Logger logger = LoggerUtils.getBuyerOrderLogger();
|
|
|
|
|
|
@Autowired
|
|
|
private CouponProxyService couponProxyService;
|
|
|
|
|
|
@Autowired
|
|
|
private CouponRuleMatcher couponRuleMatcher;
|
|
|
|
|
|
@Autowired
|
|
|
private DeliveryWayCostSupport deliveryWayCostSupport;
|
|
|
|
|
|
@Autowired
|
|
|
private ShoppingSupport shoppingSupport;
|
|
|
|
|
|
@Autowired
|
|
|
private CouponService couponService;
|
|
|
|
|
|
|
|
|
/**
|
|
|
* 算费
|
...
|
...
|
@@ -120,11 +118,49 @@ public class ChargeService { |
|
|
* @param chargeContext
|
|
|
*/
|
|
|
private void calculateCouponIfNeed(ChargeContext chargeContext) {
|
|
|
if (CollectionUtils.isEmpty(chargeContext.getChargeParam().getCouponCodes())) {
|
|
|
List<String> couponCodes = chargeContext.getChargeParam().getCouponCodes();
|
|
|
if (CollectionUtils.isEmpty(couponCodes)) {
|
|
|
return;
|
|
|
}
|
|
|
|
|
|
if (couponCodes.contains(CouponConstants.AUTO_RECOMMENDED_COUPON_CODE)) {
|
|
|
//自动匹配
|
|
|
calculateRecommendedCoupons(chargeContext);
|
|
|
} else {
|
|
|
calculateSpecifiedCoupons(chargeContext);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 推荐券
|
|
|
*
|
|
|
* @param chargeContext
|
|
|
*/
|
|
|
private void calculateRecommendedCoupons(ChargeContext chargeContext) {
|
|
|
List<CouponMatchResult> usableCouponMatchResults = couponService.getRecommendedCoupons(chargeContext);
|
|
|
if (Objects.isNull(usableCouponMatchResults)) {
|
|
|
//获取券列表发生异常了
|
|
|
logger.warn("[{}] step recommended coupon charge,usable coupon count is null", chargeContext.getChargeParam().getUid());
|
|
|
return;
|
|
|
}
|
|
|
|
|
|
ChargeResult chargeResult = chargeContext.getChargeResult();
|
|
|
|
|
|
//只统计可用券的数量,不自动使用
|
|
|
RecommendedCouponPayResult recommendedCouponPayResult = new RecommendedCouponPayResult(null,
|
|
|
null,0,0,usableCouponMatchResults.size());
|
|
|
|
|
|
chargeResult.setCouponPayResult(recommendedCouponPayResult);
|
|
|
|
|
|
logger.info("[{}] step recommended coupon charge,newFinalAmount:{},couponPayResult:{}",
|
|
|
chargeContext.getChargeParam().getUid(), chargeResult.getFinalAmount(), chargeResult.getCouponPayResult());
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 使用指定券
|
|
|
* @param chargeContext
|
|
|
*/
|
|
|
private void calculateSpecifiedCoupons(ChargeContext chargeContext) {
|
|
|
/**
|
|
|
* 目前只能使用一个券
|
|
|
*/
|
...
|
...
|
@@ -132,6 +168,8 @@ public class ChargeService { |
|
|
//券金额 取最小
|
|
|
double couponPayAmount = Math.min(chargeContext.getChargeGoods().getGoodsAmount().doubleValue(), userCouponsBo.getCouponAmount().doubleValue());
|
|
|
|
|
|
ChargeResult chargeResult = chargeContext.getChargeResult();
|
|
|
|
|
|
double oldFinalAmount = chargeResult.getFinalAmount();
|
|
|
double newFinalAmount = YHMath.sub(oldFinalAmount, couponPayAmount);
|
|
|
chargeResult.setFinalAmount(newFinalAmount);
|
...
|
...
|
@@ -142,27 +180,19 @@ public class ChargeService { |
|
|
chargeResult.setCouponPayResult(couponPayResult);
|
|
|
|
|
|
logger.info("[{}] step coupon charge,oldFinalAmount:{},newFinalAmount:{},couponPayResult:{}",
|
|
|
chargeContext.getChargeParam().getUid(),
|
|
|
couponPayAmount, oldFinalAmount, chargeResult.getFinalAmount(), chargeResult.getCouponPayResult());
|
|
|
|
|
|
chargeContext.getChargeParam().getUid(), oldFinalAmount, chargeResult.getFinalAmount(), chargeResult.getCouponPayResult());
|
|
|
}
|
|
|
|
|
|
private UserCouponsBo getOneUsableUserCouponBo(ChargeContext chargeContext) {
|
|
|
ChargeParam chargeParam = chargeContext.getChargeParam();
|
|
|
List<UserCouponsBo> couponsBos = couponProxyService.checkAndGetCoupons(chargeParam.getUid(), chargeParam.getCouponCodes());
|
|
|
if (CollectionUtils.isEmpty(couponsBos)) {
|
|
|
logger.info("[{}] not find any coupons by couponCodes:{}", chargeParam.getUid(), chargeParam.getCouponCodes());
|
|
|
throw new ServiceException(ServiceError.PROMOTION_COUPON_IS_NOT_VAILD);
|
|
|
}
|
|
|
//对券规则进行匹配
|
|
|
List<CouponMatchResult> couponMatchResults = couponRuleMatcher.match(couponsBos, chargeContext);
|
|
|
|
|
|
//找到可用的券
|
|
|
List<UserCouponsBo> usableCoupons = couponMatchResults.stream().filter(result -> result.isUsable()).map(result -> result.getUserCouponsBo()).collect(Collectors.toList());
|
|
|
List<CouponMatchResult> usableCouponMatchResults = couponService.getSpecifiedCoupons(chargeContext);
|
|
|
|
|
|
|
|
|
List<UserCouponsBo> usableCoupons = usableCouponMatchResults.stream().map(result -> result.getUserCouponsBo()).collect(Collectors.toList());
|
|
|
|
|
|
//当前只有使用一张券
|
|
|
if (CollectionUtils.isEmpty(usableCoupons) || usableCoupons.size() != 1) {
|
|
|
logger.info("[{}] get coupons by couponCodes:{} is invalid", chargeParam.getUid(), chargeParam.getCouponCodes());
|
|
|
logger.info("[{}] get coupons by couponCodes:{} is invalid", chargeContext.getChargeParam().getUid(), chargeContext.getChargeParam().getCouponCodes());
|
|
|
throw new ServiceException(ServiceError.PROMOTION_COUPON_IS_NOT_VAILD);
|
|
|
}
|
|
|
|
...
|
...
|
|