直接上业务代码
/**
* 套组获取物流费用
*
* @param pricedSkuList
* @return
*/
//异步获取物流费,汇总返回
public BigDecimal getLogisticsCostsByList2(List pricedSkuList) {
Result fedexToken = biApi.getFedexToken();
BigDecimal zero = new BigDecimal(0);
for (PricedSku pricedSku : pricedSkuList) {
//为核心代码
CompletableFuture countCheckState = CompletableFuture.supplyAsync(() -> getGroupExpressFee(pricedSku, fedexToken));
try {
zero = zero.add(countCheckState.get());
} catch (InterruptedException e) {
e.printStackTrace();
} catch (ExecutionException e) {
e.printStackTrace();
}
}
return zero;
}
private BigDecimal getGroupExpressFee(PricedSku pricedSku, Result<String> fedexToken) {
BigDecimal zero = new BigDecimal(0);
if (MapperUtils.isFail(fedexToken)) {
return zero;
}
String token = fedexToken.getData();
if (ObjectUtil.isNotNull(pricedSku.getPackWeightPound()) && ObjectUtil.isNotNull(pricedSku.getPackHeight())
&& ObjectUtil.isNotNull(pricedSku.getPackWidth()) && ObjectUtil.isNotNull(pricedSku.getPackLength())) {
// 组装请求参数 获取预估快递费用
FedexQuotesRequestDto requestDto = new FedexQuotesRequestDto()
.setToken(token)
.setShipperCity("")
.setShipperPostalCode("76104")
.setShipperStateOrProvinceCode("")
.setRecipientCity("")
.setRecipientPostalCode("37027")
.setRecipientStateOrProvinceCode("")
.setPackingWeight(pricedSku.getPackWeightPound().setScale(0, BigDecimal.ROUND_DOWN))
.setHeight(pricedSku.getPackHeight().setScale(0, BigDecimal.ROUND_DOWN).intValue())
.setLength(pricedSku.getPackLength().setScale(0, BigDecimal.ROUND_DOWN).intValue())
.setWidth(pricedSku.getPackWidth().setScale(0, BigDecimal.ROUND_DOWN).intValue());
Result<BigDecimal> fedexQuotes = biApi.getFedexQuotes(requestDto);
if (MapperUtils.isFail(fedexQuotes)) {
return zero;
} else {
zero = fedexQuotes.getData();
}
}
return zero;
}