Authored by chenchao

calculate ship fee

@@ -80,45 +80,62 @@ public class ChargeService { @@ -80,45 +80,62 @@ public class ChargeService {
80 tariffAmount = BigDecimalHelper.halfUp(goodsRealPrice.multiply(tariffRate)).doubleValue(); 80 tariffAmount = BigDecimalHelper.halfUp(goodsRealPrice.multiply(tariffRate)).doubleValue();
81 } 81 }
82 82
83 - AmountCutType amountCutType = null; 83 + SingleFeeDetail tariffDetail = calculateWithCutPolicy(tariffAmount, tariffCutPolicy, "calculateTariffAmount");
  84 + tariffDetail.setRate(tariffRate==null ? 0D : tariffRate.doubleValue());
  85 + return tariffDetail;
  86 + }
84 87
85 - if (tariffCutPolicy !=null){  
86 - int cutTypeCode = tariffCutPolicy.getType();  
87 - amountCutType = AmountCutType.getByCode(cutTypeCode);  
88 - }  
89 - double tariffCutAmout = 0D;  
90 - double finalAmout = tariffAmount;  
91 - if (tariffAmount > 0D && amountCutType != null){  
92 88
93 - switch (amountCutType){  
94 - case NO_PROMOTION:  
95 - break;  
96 - case PERCENT:  
97 - double percentDouble = 0D;  
98 - try {  
99 - percentDouble = YHMath.div(tariffCutPolicy.getPercent(), 100D, 2);  
100 - } catch (IllegalAccessException e) {  
101 - logger.warn("calculateTariffAmount goodsRealPrice {} tariffCutPolicy {}",  
102 - goodsRealPrice, tariffCutPolicy, e);  
103 - }  
104 - tariffCutAmout = YHMath.mul(tariffAmount, percentDouble);  
105 89
106 - break;  
107 - case FIX_AMOUNT:  
108 - tariffCutAmout = tariffCutPolicy.getAmount();  
109 - break;  
110 - default:  
111 - //do nothing for now  
112 - tariffCutAmout = 0D;  
113 - break; 90 + private SingleFeeDetail calculateWithCutPolicy(double initAmount, AmountCutPolicy cutPolicy, String keyCase){
  91 + double cutAmount = 0D, finalAmount = initAmount;
  92 + if (initAmount != 0D){
  93 + AmountCutType amountCutType = null;
  94 +
  95 + if (cutPolicy !=null){
  96 + int cutTypeCode = cutPolicy.getType();
  97 + amountCutType = AmountCutType.getByCode(cutTypeCode);
  98 + }
  99 + if (amountCutType != null){
  100 +
  101 + switch (amountCutType){
  102 + case NO_PROMOTION:
  103 + break;
  104 + case PERCENT:
  105 + double percentDouble = 0D;
  106 + try {
  107 + percentDouble = YHMath.div(cutPolicy.getPercent(), 100D, 2);
  108 + } catch (IllegalAccessException e) {
  109 + logger.warn("{} init-amount {} CutPolicy {}",
  110 + keyCase, initAmount, cutPolicy, e);
  111 + }
  112 + cutAmount = YHMath.mul(initAmount, percentDouble);
  113 +
  114 + break;
  115 + case FIX_AMOUNT:
  116 + cutAmount = cutPolicy.getAmount();
  117 + break;
  118 + default:
  119 + //do nothing for now
  120 + cutAmount = 0D;
  121 + break;
  122 + }
  123 + finalAmount = YHMath.sub(finalAmount, cutAmount);
114 } 124 }
115 - finalAmout = YHMath.sub(finalAmout, tariffCutAmout);  
116 } 125 }
117 - SingleFeeDetail tariffDetail = SingleFeeDetail.builder().amount(tariffAmount)  
118 - .cutAmount(tariffCutAmout)  
119 - .rate(tariffRate==null ? 0D : tariffRate.doubleValue())  
120 - .finalAmount(finalAmout).build(); 126 +
  127 +
  128 + SingleFeeDetail tariffDetail = SingleFeeDetail.builder()
  129 + .amount(initAmount)
  130 + .cutAmount(cutAmount)
  131 + .finalAmount(finalAmount).build();
121 return tariffDetail; 132 return tariffDetail;
  133 +
  134 + }
  135 +
  136 +
  137 + private SingleFeeDetail calculateOverSeasOrderShipAmount(double shipFee, AmountCutPolicy shipCutPolicy){
  138 + return calculateWithCutPolicy(shipFee, shipCutPolicy, "calculateOverSeasOrderShipAmount");
122 } 139 }
123 /** 140 /**
124 * 计算商品金额 141 * 计算商品金额
@@ -203,6 +220,10 @@ public class ChargeService { @@ -203,6 +220,10 @@ public class ChargeService {
203 break; 220 break;
204 case OVERSEAS_HONGHONG: 221 case OVERSEAS_HONGHONG:
205 deliveryWayCost = deliveryWayCostSupport.getCostFromHk(); 222 deliveryWayCost = deliveryWayCostSupport.getCostFromHk();
  223 + AmountCutPolicy shipFeeCutPolicy = chargeContext.getChargeParam().getShipFeeCutPolicy();
  224 + SingleFeeDetail shipFeeDetail = calculateOverSeasOrderShipAmount(deliveryWayCost, shipFeeCutPolicy);
  225 + deliveryWayCost = shipFeeDetail.getFinalAmount();
  226 + chargeContext.getChargeResult().setShipFeeDetail(shipFeeDetail);
206 break; 227 break;
207 case SF: 228 case SF:
208 default: 229 default:
@@ -33,6 +33,8 @@ public class ChargeResult { @@ -33,6 +33,8 @@ public class ChargeResult {
33 */ 33 */
34 private SingleFeeDetail tariffFee; 34 private SingleFeeDetail tariffFee;
35 35
  36 + private SingleFeeDetail shipFeeDetail;
  37 +
36 /** 38 /**
37 * 发货方式 39 * 发货方式
38 */ 40 */
@@ -2,4 +2,5 @@ package com.yohoufo.order.constants; @@ -2,4 +2,5 @@ package com.yohoufo.order.constants;
2 2
3 public interface ClientSpecialSemanticCode { 3 public interface ClientSpecialSemanticCode {
4 int needRealName = 440; 4 int needRealName = 440;
  5 + int WaitingPayLimit = 512;
5 } 6 }
@@ -40,7 +40,7 @@ public class ShoppingRiskWatchDog { @@ -40,7 +40,7 @@ public class ShoppingRiskWatchDog {
40 if (cnt >= waitingPayCnt){ 40 if (cnt >= waitingPayCnt){
41 logger.warn("in ShoppingRiskWatchDog.checkWaitingPayCnt try monopolizing skup another time,uid {} waiting pay cnt {}", 41 logger.warn("in ShoppingRiskWatchDog.checkWaitingPayCnt try monopolizing skup another time,uid {} waiting pay cnt {}",
42 uid, cnt); 42 uid, cnt);
43 - throw new UfoServiceException(512, "您有未支付的订单,支付或取消后可提交新的订单"); 43 + throw new UfoServiceException(ClientSpecialSemanticCode.WaitingPayLimit, "您有未支付的订单,支付或取消后可提交新的订单");
44 } 44 }
45 } 45 }
46 46
@@ -21,6 +21,7 @@ import org.springframework.stereotype.Service; @@ -21,6 +21,7 @@ import org.springframework.stereotype.Service;
21 import java.math.BigDecimal; 21 import java.math.BigDecimal;
22 import java.text.MessageFormat; 22 import java.text.MessageFormat;
23 import java.util.List; 23 import java.util.List;
  24 +import java.util.Objects;
24 import java.util.stream.Collectors; 25 import java.util.stream.Collectors;
25 26
26 @Service 27 @Service
@@ -104,7 +105,19 @@ public class ShoppingSupport { @@ -104,7 +105,19 @@ public class ShoppingSupport {
104 //运费 105 //运费
105 PromotionFormula expressFormula = new PromotionFormula(); 106 PromotionFormula expressFormula = new PromotionFormula();
106 expressFormula.setPromotion(OrderConstant.DELIVERY_DESC); 107 expressFormula.setPromotion(OrderConstant.DELIVERY_DESC);
107 - String shipFee = OrderConstant.PLUS_SIGN + MathUtils.formatCurrencyStr(chargeResult.getShippingAmount()); 108 + String shipFee;
  109 + SingleFeeDetail shipFeeDetail;
  110 + if (Objects.nonNull(shipFeeDetail = chargeResult.getShipFeeDetail())){
  111 + if (shipFeeDetail.getCutAmount()>0D){
  112 + expressFormula.setPromotionAside(OrderConstant.DELIVERY_DESC_ASIDE);
  113 + }
  114 + String normalShipFee = OrderConstant.PLUS_SIGN + MathUtils.formatCurrencyStr(shipFeeDetail.getAmount());
  115 + expressFormula.setNormalAmount(normalShipFee);
  116 + //
  117 + shipFee = OrderConstant.PLUS_SIGN + MathUtils.formatCurrencyStr(shipFeeDetail.getFinalAmount());
  118 + }else{
  119 + shipFee = OrderConstant.PLUS_SIGN + MathUtils.formatCurrencyStr(chargeResult.getShippingAmount());
  120 + }
108 expressFormula.setPromotionAmount(shipFee); 121 expressFormula.setPromotionAmount(shipFee);
109 formulas.add(expressFormula); 122 formulas.add(expressFormula);
110 //税费 123 //税费