运费计算模式调整 同一运费模版的商品,采用累加计算,起送费/虚件(重)费用。

This commit is contained in:
Chopper 2022-06-07 18:30:21 +08:00
parent ca352ca341
commit f32c7a7253

View File

@ -11,11 +11,15 @@ import cn.lili.modules.store.entity.dto.FreightTemplateChildDTO;
import cn.lili.modules.store.entity.enums.FreightTemplateEnum;
import cn.lili.modules.store.entity.vos.FreightTemplateVO;
import cn.lili.modules.store.service.FreightTemplateService;
import org.apache.xmlbeans.impl.store.Cur;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
/**
* sku 运费计算
@ -43,31 +47,35 @@ public class SkuFreightRender implements CartRenderStep {
if (memberAddress == null) {
return;
}
//循环渲染购物车商品运费价格
forSku:
for (CartSkuVO cartSkuVO : cartSkuVOS) {
//获取sku运费模版
String freightTemplateId = cartSkuVO.getGoodsSku().getFreightTemplateId();
//免运费则跳出运费计算
if (Boolean.TRUE.equals(cartSkuVO.getIsFreeFreight()) || freightTemplateId == null) {
continue;
}
//运费分组信息
Map<String, List<String>> freightGroups = freightTemplateGrouping(cartSkuVOS);
//循环运费模版
for (String freightTemplateId : freightGroups.keySet()) {
//商品id列表
List<String> skuIds = freightGroups.get(freightTemplateId);
//当前购物车商品列表
List<CartSkuVO> currentCartSkus = cartSkuVOS.stream().filter(item -> skuIds.contains(item.getGoodsSku().getId())).collect(Collectors.toList());
//寻找对应对商品运费计算模版
FreightTemplateVO freightTemplate = freightTemplateService.getFreightTemplate(freightTemplateId);
if (freightTemplate != null
&& freightTemplate.getFreightTemplateChildList() != null
&& !freightTemplate.getFreightTemplateChildList().isEmpty()) {
//店铺支付运费则跳过
//店铺模版免运费则跳过
if (freightTemplate.getPricingMethod().equals(FreightTemplateEnum.FREE.name())) {
break;
}
//运费模版
FreightTemplateChild freightTemplateChild = null;
//获取市级别id
//获取市级别id匹配运费模版
String addressId = memberAddress.getConsigneeAddressIdPath().split(",")[1];
//获取匹配的收货地址
for (FreightTemplateChild templateChild : freightTemplate.getFreightTemplateChildList()) {
//如果当前模版包含则返回
//模版匹配判定
if (templateChild.getAreaId().contains(addressId)) {
freightTemplateChild = templateChild;
break;
@ -78,28 +86,97 @@ public class SkuFreightRender implements CartRenderStep {
if (tradeDTO.getNotSupportFreight() == null) {
tradeDTO.setNotSupportFreight(new ArrayList<>());
}
tradeDTO.getNotSupportFreight().add(cartSkuVO);
continue forSku;
tradeDTO.getNotSupportFreight().addAll(currentCartSkus);
continue;
}
//物流规则模型创立
FreightTemplateChildDTO freightTemplateChildDTO = new FreightTemplateChildDTO(freightTemplateChild);
//模型写入运费模版设置的计费方式
freightTemplateChildDTO.setPricingMethod(freightTemplate.getPricingMethod());
//要计算的基数 数量/重量
Double count = (freightTemplateChildDTO.getPricingMethod().equals(FreightTemplateEnum.NUM.name())) ?
cartSkuVO.getNum() :
cartSkuVO.getGoodsSku().getWeight() * cartSkuVO.getNum();
//计算运费总数
Double count = currentCartSkus.stream().mapToDouble(item ->
// 根据计费规则 累加计费基数
freightTemplateChildDTO.getPricingMethod().equals(FreightTemplateEnum.NUM.name()) ?
item.getNum() :
CurrencyUtil.mul(item.getNum(), item.getGoodsSku().getWeight())
).sum();
//计算运费
Double countFreight = countFreight(count, freightTemplateChildDTO);
//写入SKU运费
cartSkuVO.getPriceDetailDTO().setFreightPrice(countFreight);
resetFreightPrice(FreightTemplateEnum.valueOf(freightTemplateChildDTO.getPricingMethod()), count, countFreight, currentCartSkus);
}
}
}
/**
* sku运费写入
*
* @param freightTemplateEnum 运费计算模式
* @param count 计费基数总数
* @param countFreight 总运费
* @param cartSkuVOS 与运费相关的购物车商品
*/
private void resetFreightPrice(FreightTemplateEnum freightTemplateEnum, Double count, Double countFreight, List<CartSkuVO> cartSkuVOS) {
//剩余运费 默认等于总运费
Double surplusFreightPrice = countFreight;
//当前下标
int index = 1;
for (CartSkuVO cartSkuVO : cartSkuVOS) {
//如果是最后一个 则将剩余运费直接赋值
//PS: 循环中避免百分比累加不等于100%所以最后一个运费不以比例计算直接将剩余运费赋值
if (index == cartSkuVOS.size()) {
cartSkuVO.getPriceDetailDTO().setFreightPrice(surplusFreightPrice);
break;
}
Double freightPrice = freightTemplateEnum == FreightTemplateEnum.NUM ?
CurrencyUtil.mul(countFreight, CurrencyUtil.div(cartSkuVO.getNum(), count)) :
CurrencyUtil.mul(countFreight,
CurrencyUtil.div(CurrencyUtil.mul(cartSkuVO.getNum(), cartSkuVO.getGoodsSku().getWeight()), count));
//剩余运费=总运费-当前循环的商品运费
surplusFreightPrice = CurrencyUtil.sub(surplusFreightPrice, freightPrice);
cartSkuVO.getPriceDetailDTO().setFreightPrice(freightPrice);
index++;
}
}
/**
* 运费模版分组
*
* @param cartSkuVOS 购物车商品
* @return map<运费模版id List < skuid>>
*/
private Map<String, List<String>> freightTemplateGrouping(List<CartSkuVO> cartSkuVOS) {
Map<String, List<String>> map = new HashMap<>();
//循环渲染购物车商品运费价格
for (CartSkuVO cartSkuVO : cartSkuVOS) {
////免运费判定
String freightTemplateId = cartSkuVO.getGoodsSku().getFreightTemplateId();
if (Boolean.TRUE.equals(cartSkuVO.getIsFreeFreight()) || freightTemplateId == null) {
continue;
}
//包含 则value值中写入sku标识否则直接写入新的对象key为模版idvalue为new arraylist
if (map.containsKey(freightTemplateId)) {
map.get(freightTemplateId).add(cartSkuVO.getGoodsSku().getId());
} else {
List<String> skuIdsList = new ArrayList<>();
skuIdsList.add(cartSkuVO.getGoodsSku().getId());
map.put(freightTemplateId, skuIdsList);
}
}
return map;
}
/**
* 计算运费
*