Authored by LUOXC

Merge branch 'dev6.8.6' into test6.8.6

package com.yohoufo.order.service.handler;
import com.yoho.error.ServiceError;
import com.yoho.error.exception.ServiceException;
import com.yohobuy.ufo.model.order.common.OrderAttributes;
import com.yohobuy.ufo.model.order.common.OrderStatus;
import com.yohoufo.common.exception.UfoServiceException;
import com.yohoufo.common.utils.BigDecimalHelper;
import com.yohoufo.common.utils.DateUtil;
import com.yohoufo.dal.order.OrdersPayMapper;
import com.yohoufo.dal.order.model.BuyerOrder;
import com.yohoufo.dal.order.model.OrdersPay;
import com.yohoufo.order.common.ActionStatusHold;
import com.yohoufo.order.model.dto.BuyerPenalty;
import com.yohoufo.order.model.dto.BuyerPenaltyCalResult;
... ... @@ -22,8 +17,6 @@ import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import java.math.BigDecimal;
import java.util.Collection;
import java.util.Map;
import java.util.Objects;
/**
... ... @@ -38,7 +31,7 @@ public class BuyerCancelCompensateComputeHandler {
private MetaConfigService metaConfigService;
@Autowired
private OrdersPayMapper ordersPayMapper;
private BuyerOrderPayDiffTimeHandler buyerOrderPayDiffTimeHandler;
public BuyerPenaltyCalResult calBuyerPenaltyCalResult(BuyerOrder buyerOrder,OrderStatus targetStatus){
Integer buyerUid = buyerOrder.getUid();
... ... @@ -78,7 +71,7 @@ public class BuyerCancelCompensateComputeHandler {
logger.warn("in calBuyerPenalty order buyerPenalty config illegal buyerUid {} orderCode {} orderAttributes {}", buyerUid, orderCode, orderAttributes);
throw new UfoServiceException(501, "buyerPenalty config missed");
}
int diffTime = getDiffTimeBeginWithPayTime(buyerUid, orderCode);
int diffTime = buyerOrderPayDiffTimeHandler.diffTime(buyerUid, orderCode);
PenaltyRule penaltyRule = BuyerPenaltyRuleSelector.of(buyerPenalty)
.orderAttributes(orderAttributes)
.orderStatus(orderStatus)
... ... @@ -92,17 +85,6 @@ public class BuyerCancelCompensateComputeHandler {
return bpcr;
}
private int getDiffTimeBeginWithPayTime(Integer buyerUid, Long orderCode) {
OrdersPay ordersPay = ordersPayMapper.selectOrdersPay(orderCode, buyerUid);
Integer payTime;
if (Objects.isNull(ordersPay) || Objects.isNull(payTime = ordersPay.getCreateTime())) {
logger.warn("in calBuyerPenalty not exist paid record.orderCode {}", orderCode);
throw new ServiceException(ServiceError.ORDER_HAS_NOT_PAID);
}
int currentTime = DateUtil.getCurrentTimeSecond();
return currentTime - payTime;
}
public BuyerPenaltyCalResult calBuyerPenalty(PenaltyRule penaltyRule, Integer buyerUid, Long orderCode,
BigDecimal orderActualAmount, BigDecimal shipFee) {
BigDecimal penaltyRate = penaltyRule.getPenaltyRate();
... ...
package com.yohoufo.order.service.handler;
import com.yoho.error.ServiceError;
import com.yoho.error.exception.ServiceException;
import com.yohoufo.common.utils.DateUtil;
import com.yohoufo.dal.order.OrdersPayMapper;
import com.yohoufo.dal.order.model.OrdersPay;
import com.yohoufo.order.utils.LoggerUtils;
import org.slf4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.Objects;
@Service
public class BuyerOrderPayDiffTimeHandler {
private final Logger logger = LoggerUtils.getBuyerOrderLogger();
@Autowired
private OrdersPayMapper ordersPayMapper;
public int diffTime(Integer buyerUid, Long orderCode) {
OrdersPay ordersPay = ordersPayMapper.selectOrdersPay(orderCode, buyerUid);
Integer payTime;
if (Objects.isNull(ordersPay) || Objects.isNull(payTime = ordersPay.getCreateTime())) {
logger.warn("in calBuyerPenalty not exist paid record.orderCode {}", orderCode);
throw new ServiceException(ServiceError.ORDER_HAS_NOT_PAID);
}
int currentTime = DateUtil.getCurrentTimeSecond();
return currentTime - payTime;
}
}
... ...
... ... @@ -5,11 +5,14 @@ import com.alibaba.fastjson.JSONObject;
import com.yoho.error.exception.ServiceException;
import com.yohoufo.dal.order.model.SellerOrderMeta;
import com.yohoufo.order.utils.LoggerUtils;
import lombok.Setter;
import lombok.experimental.Accessors;
import org.slf4j.Logger;
import java.math.BigDecimal;
import java.util.Objects;
import java.util.Optional;
import java.util.function.Supplier;
import static java.math.BigDecimal.ROUND_HALF_UP;
... ... @@ -20,6 +23,9 @@ public class SellerEarnestMoney2BuyerPenaltyCalculator implements PenaltyCalcula
private final Integer uid;
private final Integer skup;
private final JSONObject fee;
@Setter
@Accessors(fluent = true)
private Supplier<Integer> diffTimeSupplier;
SellerEarnestMoney2BuyerPenaltyCalculator(Integer uid, Integer skup, JSONObject fee) {
this.uid = uid;
... ... @@ -56,6 +62,7 @@ public class SellerEarnestMoney2BuyerPenaltyCalculator implements PenaltyCalcula
return Optional.empty();
}
return new SellerEarnestMoney2BuyerPenaltyRuleSelector(uid, skup, fee)
.diffTimeSupplier(diffTimeSupplier)
.select()
.map(penaltyRule -> PenaltyResult.builder()
.penaltyRule(penaltyRule)
... ...
package com.yohoufo.order.service.handler.penalty;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.yohoufo.order.utils.LoggerUtils;
import lombok.Setter;
import lombok.experimental.Accessors;
import org.slf4j.Logger;
import java.math.BigDecimal;
import java.util.Objects;
import java.util.Optional;
import java.util.function.Supplier;
public class SellerEarnestMoney2BuyerPenaltyRuleSelector implements PenaltyRuleSelector {
... ... @@ -15,6 +19,9 @@ public class SellerEarnestMoney2BuyerPenaltyRuleSelector implements PenaltyRuleS
private final Integer uid;
private final Integer skup;
private final JSONObject fee;
@Setter
@Accessors(fluent = true)
private Supplier<Integer> diffTimeSupplier;
SellerEarnestMoney2BuyerPenaltyRuleSelector(Integer uid, Integer skup, JSONObject fee) {
this.uid = uid;
... ... @@ -30,10 +37,18 @@ public class SellerEarnestMoney2BuyerPenaltyRuleSelector implements PenaltyRuleS
*/
@Override
public Optional<PenaltyRule> select() {
JSONObject penaltyFeeRate = fee.getJSONObject("penaltyFeeRate");
// 保证金抽成
BigDecimal penaltyRate;
JSONObject penaltyFeeRate = getPenaltyFeeRate();
if (Objects.nonNull(penaltyFeeRate)) {
penaltyRate = penaltyFeeRate.getBigDecimal("rate");
} else {
penaltyRate = BigDecimal.ONE;
}
if (Objects.isNull(penaltyRate)) {
logger.warn("feeMeta penaltyRate is null, uid is {}, skup is {}", uid, skup);
return Optional.empty();
}
// 平台服务费
JSONObject serviceFeeRate = fee.getJSONObject("serviceFeeRate");
if (Objects.isNull(serviceFeeRate)) {
logger.warn("feeMeta serviceFeeRate is null, uid is {}, skup is {}", uid, skup);
... ... @@ -44,8 +59,38 @@ public class SellerEarnestMoney2BuyerPenaltyRuleSelector implements PenaltyRuleS
logger.warn("feeMeta serviceFeeRate earnestMoneyRate err, uid is {}, skup is {}, rate is {}", uid, skup, yhRate);
return Optional.empty();
}
return Optional.of(BigDecimal.ONE.subtract(yhRate))
.map(penaltyRate -> PenaltyRule.builder().penaltyRate(penaltyRate).build());
return Optional.of(penaltyRate.subtract(yhRate))
.map(realPenaltyRate -> PenaltyRule.builder().penaltyRate(realPenaltyRate).build());
}
private JSONObject getPenaltyFeeRate() {
JSONArray penaltyFeeRate = fee.getJSONArray("penaltyFeeRate");
if (penaltyFeeRate == null) {
return null;
}
int diffTime = diffTimeSupplier.get();
JSONObject nonTimeRangeFee = null;
for (Object o : penaltyFeeRate) {
if (o instanceof JSONObject) {
JSONObject timeRangeFee = (JSONObject) o;
JSONObject timeRange = timeRangeFee.getJSONObject("timeRange");
if (Objects.isNull(timeRange)) {
if (Objects.isNull(nonTimeRangeFee)) {
nonTimeRangeFee = timeRangeFee;
}
continue;
}
Integer min = timeRange.getInteger("min");
Integer max = timeRange.getInteger("max");
boolean gtMin = Objects.nonNull(min) && diffTime > min.intValue();
//可以不设置最大值,没有就默认为无穷大,所以默认为满足小于最大值
boolean ltMax = Objects.isNull(max) || diffTime <= max.intValue();
if (gtMin && ltMax) {
return timeRangeFee;
}
}
}
return nonTimeRangeFee;
}
}
... ...
... ... @@ -34,6 +34,7 @@ import com.yohoufo.order.service.IPaymentService;
import com.yohoufo.order.service.MerchantOrderPaymentService;
import com.yohoufo.order.service.SellerOrderPaymentService;
import com.yohoufo.order.service.handler.BuyerCancelCompensateComputeHandler;
import com.yohoufo.order.service.handler.BuyerOrderPayDiffTimeHandler;
import com.yohoufo.order.service.handler.penalty.PenaltyResult;
import com.yohoufo.order.service.handler.penalty.SellerEarnestMoney2BuyerPenaltyCalculator;
import com.yohoufo.order.service.pay.AbstractPayService;
... ... @@ -136,6 +137,9 @@ public class PaymentServiceImpl implements IPaymentService {
@Autowired
OrdersPrePayMapper ordersPrePayMapper;
@Autowired
BuyerOrderPayDiffTimeHandler buyerOrderPayDiffTimeHandler;
/**
* 获取主场的订单service
* @param codeMeta
... ... @@ -579,7 +583,7 @@ public class PaymentServiceImpl implements IPaymentService {
logger.info("transferMon参数检查成功!插入初始化转账信息成功,接下来计算费用");
TranseferCellNode transeferCellNode = request.getTranseferCellNode();
// 算费
BigDecimal transferAmount = calcTransferAmount(sellerOrder.getUid(),
BigDecimal transferAmount = calcTransferAmount(buyerOrder,sellerOrder.getUid(),
sellerOrder.getSkup(), transferCase, transeferCellNode);
logger.info("transferMon计算费用结果为 {}", transferAmount);
if (transferAmount == null) {
... ... @@ -1159,7 +1163,7 @@ public class PaymentServiceImpl implements IPaymentService {
private BigDecimal calcTransferAmount(Integer sellerUid, Integer skup,
private BigDecimal calcTransferAmount(BuyerOrder buyerOrder,Integer sellerUid, Integer skup,
TransferCase transferCase,TranseferCellNode transeferCellNode) {
if (transferCase == TransferCase.PART_GOODS_MONEY_TO_SELLER) {
... ... @@ -1172,6 +1176,7 @@ public class PaymentServiceImpl implements IPaymentService {
return getSaleIncome(sellerUid, skup, meta);
}
return SellerEarnestMoney2BuyerPenaltyCalculator.from(meta)
.diffTimeSupplier(() -> buyerOrderPayDiffTimeHandler.diffTime(buyerOrder.getUid(), buyerOrder.getOrderCode()))
.calculate()
.map(PenaltyResult::getPenaltyAmount)
.orElse(null);
... ...
... ... @@ -6,7 +6,6 @@ import org.junit.Test;
import java.math.BigDecimal;
import static java.math.BigDecimal.ROUND_HALF_UP;
import static org.junit.Assert.*;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
... ... @@ -25,7 +24,7 @@ public class SellerEarnestMoney2BuyerPenaltyCalculatorTest {
)
.fluentPut("income", "8.34")
.fluentPut("serviceFeeRate", new JSONObject()
.fluentPut("earnestMoneyRate", 0.00)
.fluentPut("earnestMoneyRate", 0.01)
);
SellerOrderMeta feeMeta = mock(SellerOrderMeta.class);
when(feeMeta.getUid()).thenReturn(600033050);
... ... @@ -33,8 +32,8 @@ public class SellerEarnestMoney2BuyerPenaltyCalculatorTest {
when(feeMeta.getMetaKey()).thenReturn("fee");
when(feeMeta.getMetaValue()).thenReturn(feeMetaValue.toJSONString());
PenaltyResult penaltyResult = SellerEarnestMoney2BuyerPenaltyCalculator.from(feeMeta).calculate().orElse(null);
assertEquals(BigDecimal.ONE, penaltyResult.getPenaltyRule().getPenaltyRate());
assertEquals(BigDecimal.ONE.setScale(2, ROUND_HALF_UP), penaltyResult.getPenaltyAmount());
assertEquals(BigDecimal.valueOf(0.99), penaltyResult.getPenaltyRule().getPenaltyRate());
assertEquals(BigDecimal.valueOf(0.99), penaltyResult.getPenaltyAmount());
}
}
\ No newline at end of file
... ...