运费计算模式调整 同一运费模版的商品,采用累加计算,起送费/虚件(重)费用。
This commit is contained in:
parent
ca352ca341
commit
f32c7a7253
@ -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.enums.FreightTemplateEnum;
|
||||||
import cn.lili.modules.store.entity.vos.FreightTemplateVO;
|
import cn.lili.modules.store.entity.vos.FreightTemplateVO;
|
||||||
import cn.lili.modules.store.service.FreightTemplateService;
|
import cn.lili.modules.store.service.FreightTemplateService;
|
||||||
|
import org.apache.xmlbeans.impl.store.Cur;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
|
import java.util.HashMap;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
import java.util.Map;
|
||||||
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* sku 运费计算
|
* sku 运费计算
|
||||||
@ -43,31 +47,35 @@ public class SkuFreightRender implements CartRenderStep {
|
|||||||
if (memberAddress == null) {
|
if (memberAddress == null) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
//循环渲染购物车商品运费价格
|
//运费分组信息
|
||||||
forSku:
|
Map<String, List<String>> freightGroups = freightTemplateGrouping(cartSkuVOS);
|
||||||
for (CartSkuVO cartSkuVO : cartSkuVOS) {
|
|
||||||
//获取sku运费模版
|
//循环运费模版
|
||||||
String freightTemplateId = cartSkuVO.getGoodsSku().getFreightTemplateId();
|
for (String freightTemplateId : freightGroups.keySet()) {
|
||||||
//免运费则跳出运费计算
|
|
||||||
if (Boolean.TRUE.equals(cartSkuVO.getIsFreeFreight()) || freightTemplateId == null) {
|
//商品id列表
|
||||||
continue;
|
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);
|
FreightTemplateVO freightTemplate = freightTemplateService.getFreightTemplate(freightTemplateId);
|
||||||
if (freightTemplate != null
|
if (freightTemplate != null
|
||||||
&& freightTemplate.getFreightTemplateChildList() != null
|
&& freightTemplate.getFreightTemplateChildList() != null
|
||||||
&& !freightTemplate.getFreightTemplateChildList().isEmpty()) {
|
&& !freightTemplate.getFreightTemplateChildList().isEmpty()) {
|
||||||
//店铺支付运费则跳过
|
//店铺模版免运费则跳过
|
||||||
if (freightTemplate.getPricingMethod().equals(FreightTemplateEnum.FREE.name())) {
|
if (freightTemplate.getPricingMethod().equals(FreightTemplateEnum.FREE.name())) {
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//运费模版
|
||||||
FreightTemplateChild freightTemplateChild = null;
|
FreightTemplateChild freightTemplateChild = null;
|
||||||
|
|
||||||
//获取市级别id
|
//获取市级别id匹配运费模版
|
||||||
String addressId = memberAddress.getConsigneeAddressIdPath().split(",")[1];
|
String addressId = memberAddress.getConsigneeAddressIdPath().split(",")[1];
|
||||||
//获取匹配的收货地址
|
|
||||||
for (FreightTemplateChild templateChild : freightTemplate.getFreightTemplateChildList()) {
|
for (FreightTemplateChild templateChild : freightTemplate.getFreightTemplateChildList()) {
|
||||||
//如果当前模版包含,则返回
|
//模版匹配判定
|
||||||
if (templateChild.getAreaId().contains(addressId)) {
|
if (templateChild.getAreaId().contains(addressId)) {
|
||||||
freightTemplateChild = templateChild;
|
freightTemplateChild = templateChild;
|
||||||
break;
|
break;
|
||||||
@ -78,28 +86,97 @@ public class SkuFreightRender implements CartRenderStep {
|
|||||||
if (tradeDTO.getNotSupportFreight() == null) {
|
if (tradeDTO.getNotSupportFreight() == null) {
|
||||||
tradeDTO.setNotSupportFreight(new ArrayList<>());
|
tradeDTO.setNotSupportFreight(new ArrayList<>());
|
||||||
}
|
}
|
||||||
tradeDTO.getNotSupportFreight().add(cartSkuVO);
|
tradeDTO.getNotSupportFreight().addAll(currentCartSkus);
|
||||||
continue forSku;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
//物流规则模型创立
|
//物流规则模型创立
|
||||||
FreightTemplateChildDTO freightTemplateChildDTO = new FreightTemplateChildDTO(freightTemplateChild);
|
FreightTemplateChildDTO freightTemplateChildDTO = new FreightTemplateChildDTO(freightTemplateChild);
|
||||||
|
//模型写入运费模版设置的计费方式
|
||||||
freightTemplateChildDTO.setPricingMethod(freightTemplate.getPricingMethod());
|
freightTemplateChildDTO.setPricingMethod(freightTemplate.getPricingMethod());
|
||||||
|
|
||||||
//要计算的基数 数量/重量
|
//计算运费总数
|
||||||
Double count = (freightTemplateChildDTO.getPricingMethod().equals(FreightTemplateEnum.NUM.name())) ?
|
Double count = currentCartSkus.stream().mapToDouble(item ->
|
||||||
cartSkuVO.getNum() :
|
// 根据计费规则 累加计费基数
|
||||||
cartSkuVO.getGoodsSku().getWeight() * cartSkuVO.getNum();
|
freightTemplateChildDTO.getPricingMethod().equals(FreightTemplateEnum.NUM.name()) ?
|
||||||
|
item.getNum() :
|
||||||
|
CurrencyUtil.mul(item.getNum(), item.getGoodsSku().getWeight())
|
||||||
|
).sum();
|
||||||
|
|
||||||
//计算运费
|
//计算运费
|
||||||
Double countFreight = countFreight(count, freightTemplateChildDTO);
|
Double countFreight = countFreight(count, freightTemplateChildDTO);
|
||||||
|
|
||||||
//写入SKU运费
|
//写入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为模版id,value为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;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 计算运费
|
* 计算运费
|
||||||
*
|
*
|
||||||
|
Loading…
x
Reference in New Issue
Block a user